Hey! So I'm writing a game engine and I'm running into problems. :/ Keep in mind it's almost 5:30 AM and I'm probably not thinking logically. So if the solution is obvious please forgive me for asking. Anyways, I have a Graphics, Renderer, ObjectManager, and WorldObject class that all work together to render the graphics(just sprites currently). The Graphics sets up DirectX and has BeginFrame() and EndFrame() functions. The Renderer class uses those two functions before(BeginFrame()) and after(EndFrame()) it's Render() function which iterates over a std::map of elements of type WorldObject. Then finally I have my ObjectManager class that I use to manage the aforementioned std::map.
Now here's where things get not too good. D3DXCreateTextureFromFile() and D3DXCREATESPRITE() both have a LPDIRECT3DDEVICE9 as of its parameters. Renderer is a friend of Graphics. So what I did is in the constructor of Renderer I iterated over the std::map and was able to call those two functions for each texture/sprite. My Renderer shouldn't be creating textures/sprites though. Its job is to render things. Also I have a LPDIRECT3DTEXTURE9 and LPD3DXSPRITE, among others, in my WorldObject class because the derived objects use them when I add an object to the std::map. Why not just make your LPDIRECT3DDEVICE9 accessible to your WorldObject so each derived object can create the texture/sprite itself? That just seems sloppy to me becuase I don't think objects in the game need to know about DirectX related stuff. They should know their position, state, logical data, etc. So what I'm asking is how can I improve on this? I already scrapped my last project because once it got so big everything started falling apart, so I'm trying to do this one properly.
Here's some source code:
#include "ObjectManager.h"
#include "Player.h"
ObjectManager::ObjectManager()
{
AddObject(new Player("Player.png", 100.0f, 100.0f), "Player1");
}
ObjectManager::~ObjectManager()
{
for(auto i : m_WorldObjects)
{
delete i.second;
i.second = nullptr;
}
m_WorldObjects.clear();
}
void ObjectManager::AddObject(WorldObject *Object, std::string objectID)
{
m_WorldObjects.insert(std::pair<std::string, WorldObject*>(objectID, Object));
}
std::map<std::string, WorldObject*> ObjectManager::m_WorldObjects;
#include "Renderer.h"
Renderer::Renderer(Graphics &Graphics)
:
m_Graphics(&Graphics)
{
for(auto i : m_ObjectManager.m_WorldObjects)
{
if(i.second->m_renderable && i.second->m_visible)
{
D3DXCreateTextureFromFile(m_Graphics->m_d3dDevice9, i.second->m_textureFileName.c_str(), &i.second->m_d3dTexture);
D3DXCreateSprite(m_Graphics->m_d3dDevice9, &i.second->m_d3dSprite);
}
}
}
bool Renderer::Update()
{
if(!m_Graphics->BeginFrame())
{
return false;
}
Render();
if(!m_Graphics->EndFrame())
{
return false;
}
return true;
}
void Renderer::Render()
{
for(auto i : m_ObjectManager.m_WorldObjects)
{
if(i.second->m_renderable && i.second->m_visible)
{
i.second->m_d3dSprite->Begin(0);
i.second->m_d3dSprite->Draw(i.second->m_d3dTexture, nullptr, nullptr, &i.second->m_position, D3DCOLOR_XRGB(255, 255, 255));
i.second->m_d3dSprite->End();
}
}
}
#pragma once
#include <d3dx9.h>
class WorldObject
{
public:
bool m_renderable, m_visible;
public:
LPDIRECT3DTEXTURE9 m_d3dTexture;
LPD3DXSPRITE m_d3dSprite;
D3DXVECTOR3 m_position;
std::string m_textureFileName;
};
Thank you in advance for any help.
Love,
Swiss Premium Iced Tea<3
EDIT:
So I think I solved my problem. I haven't tried implimenting it yet, but I think it'll work. I can add two functions to my Graphics class: LoadTextureFromFile(std::string, LPDIRECT3DTEXTURE9 d3dTexture) and CreateSpriteFromTexture(LPD3DXSPRITE d3dSprite, LPDIRECT3DTEXTURE9 d3dTexture). That way I don't need to let any other classes have access to the D3DDEVICE. But then where will I call them from? I don't want to call them from Renderer or WorldObject. -.- Maybe I need another class? I don't know.