Sign in to follow this  
Side Winder

Need help with program structure

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this