CGame.h
Code: Select all
#ifndef CGame_H_
#define CGame_H_
//Include Irrlicht header file and use its namespaces
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
//Include title screen header, which includes the CGameState header.
#include "CTitleScreen.h"
//forward declaration of the event receiver, so the CGame class knows what
//that the event receiver class exists
class CMyEventRec;
class CGame
{
public:
CGame();
~CGame();//drops the irrlicht device
/*This will initialize both the CGame class, and the event receiver class.
It will dynamically create a new event receiver class to the pointer in the private
section of the CGame class. e.g. CMyEventRec* myEventRec; a "this" pointer will be
passed into its constructor. It will then initialize all the usual Irrlicht pointers*/
void CGame_Init();
//Will call on the current gamestate's Render function
void Render();
//Will call on the current gamestate's Update function
bool Update();
//usual Irrlicht engine pointers
IrrlichtDevice* device;
ISceneManager* sceneMgr;
IVideoDriver* driver;
IGUIEnvironment* guiEnv;
//The pointer to the current gamestate
CGameState* currentGameState;
//The pointer which the event receiver class is allocated to
CMyEventRec* eventRec;
bool OnEvent( SEvent event );
/////////////////////////////////////////////////
/////////////GAME SPECIFIC FUNCTIONS/////////////
/////////////////////////////////////////////////
//Nothing as of yet.
};
////////////////////////
//EVENT RECEIVER CLASS//
////////////////////////
class CMyEventRec : public IEventReceiver
{
//Pointer to the CGame class
CGame* ptrCGame;
//Declare the CGame class a friend of this class
//friend class CGame;
public:
//Constructor takes a pointer to the CGame class as a parameter, and assigns
//it to CGame* ptrCGame;
CMyEventRec( CGame* gam ){ ptrCGame = gam; }
//Destructor does nothing so far
~CMyEventRec();
//The virtual OnEvent function
virtual bool OnEvent( SEvent event );
//Engine pointers inside the event receiver class
IrrlichtDevice* ptrDevice;
ISceneManager* ptrMgr;
IVideoDriver* ptrDriver;
IGUIEnvironment* ptrGUI;
///////////////////////////////////////////////////////
//////////////////GAME SPECIFIC STUFF//////////////////
///////////////////////////////////////////////////////
//Nothing as of yet
private:
};
#endif
Code: Select all
#include "CGame.h"
//Does nothing.
CGame::CGame()
{
}
//The destructor drops the irrlicht device
CGame::~CGame()
{
device->drop();
}
//Initialization function
void CGame::CGame_Init()
{
//allocate a new event receiver class
eventRec = new CMyEventRec( this );
//Create device
device = createDevice( EDT_DIRECTX8,
dimension2d<s32>( 640, 480 ),
32,
false,
false,
eventRec );
//Set a window caption
device->setWindowCaption(L"POLISH PONG - Peter Szlachetka");
//Initialize the engine pointers
sceneMgr = device->getSceneManager();
driver = device->getVideoDriver();
guiEnv = device->getGUIEnvironment();
//Create the title screen upon execution
currentGameState = new CTitleScreen( device, sceneMgr, driver, guiEnv );
}
//Main render function. It calls on the current game state's render fnction
void CGame::Render()
{
currentGameState->Render();
}
//Main update function. It calls on the current game state's update function
bool CGame::Update()
{
currentGameState->Update();
}
//CGame OnEvent function calls on the current game state's OnEvent function
bool CGame::OnEvent( SEvent event )
{
if( currentGameState )
{
currentGameState->OnEvent( event );
}
}
//////CMyEventRec class functions///////
//Main event function, calls the CGame's OnEvent function
bool CMyEventRec::OnEvent( SEvent event )
{
return ptrCGame->OnEvent( event );
}
/////////////////////////////////////////////////////////////
////////////////////GAME SPECIFIC STUFF//////////////////////
/////////////////////////////////////////////////////////////
//nothing as of yet
Code: Select all
#include "CGame.h"
int main()
{
CGame game;
game.CGame_Init();
while( game.Update() )
{
game.Render();
}
return 0;
}