Need help with program structure

Started by
4 comments, last by Dwarf without Axe 16 years, 10 months ago
I'm currently building a 2D sprite-based Asteroids game. I've been building a class for the main Game (initialising, releasing and what-not). I also want to build my own Sprite class to make things easier. So far I've inherited the public elements of the Game class... Now I'm having real problems with the Sprite class, the main rendering loop and generally just fitting everything in in the right spot. Here are the header files containing the Game and Sprite classes:
class Game
{
public:
	
	Game();
	~Game();
	void InitWindow();
	void InitD3D();
	void CleanD3D();
	void CleanWindow();
	void renderFrame(LPD3DXSPRITE);

	HWND GetHWND()					{ return m_hWnd; }
	LPDIRECT3DDEVICE9 GetDevice()	{ return m_d3dDevice; }

	friend class Sprite;
	

	
private:	
	
	HWND					m_hWnd;

	LPDIRECT3D9				m_d3dObject;
	LPDIRECT3DDEVICE9		m_d3dDevice;
	LPD3DXFONT				m_d3dFont;
	D3DPRESENT_PARAMETERS	m_d3dpp;
	
	
	
	
};
and the Sprite class...
class Sprite : public Game
{
private:
	LPDIRECT3DTEXTURE9		m_Sprite;
	LPD3DXSPRITE			m_d3dSprite;

public:
	Sprite();
	~Sprite();
	void Load(LPCTSTR );
	void Draw(float, float, int);
	void Draw(float, float);
	void RotateZ(float);
	void Translation(float, float);

	friend class Game;
	
	
	LPDIRECT3DTEXTURE9 GetSprite() { return m_Sprite; }
	LPD3DXSPRITE	   GetD3DSprite() { return m_d3dSprite; }


};
Cheers.
Advertisement
Is a Sprite also a Game? Should a Sprite have the ability to create windows and initialize graphics? As you can see, your Game and Sprite classes cannot be described in the context of a is-a relationship, and therefore should not inherit from one another. Instead, consider having a pointer to a Game object in your Sprite class. Better yet, isolate the components of your Game class by creating a Window and a Graphics class. You can then assign pointers between modules as required.

Hope this helps.
Very well put DarkLighter, I totally agree. Apart from it being a game engine, you should think about how it works logically and plan ahead.
Don't try and make it perfect, just try and make it work and then refactor later.

I hope this helps.
Take care.
Ok, I've done that.. However; I'm now having a problem. Everything compiles perfectly (no warnings or errors) but when I run the program, my "test" sprite isn't being drawn on the screen.

edit: just found out that if I choose to load a different sprite, when I exit I get an error. I'm assuming this is me not releasing properly?
This is more suitable to the 'Game Programming' forum. Moved [smile]

Having a read around the various design patterns and a brief overview of OOP (if that's your preferred paradigm) methodology will be well worth the time spent.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Without the .cpp files, we will never be able to tell you why the sprite is not being rendered because we cannot see the code that is supposed to do it. You might want to post your rendering code so we can help.
[Development Journal] Formerly "Dwarf with Axe", 2000-2004.

This topic is closed to new replies.

Advertisement