www.pudn.com > ICE_1.4.zip > game_engine.cpp


// Class automatically generated by Dev-C++ New Class wizard 
 
#include "game_engine.h" // class's header file 
 
// class constructor 
game_Engine::game_Engine()  
{ 
    //always set pointers to NULL and initialize values 
    destroyMe = false; 
    m_time = 0; 
    m_pCurrState = NULL; 
     
    m_pEventHandler = NULL; 
} 
 
// class destructor 
game_Engine::~game_Engine() 
{ 
    m_device->drop(); 
} 
 
 
#include  
// called to start up the irrlicht engine and set up game variables 
bool game_Engine::Init(irr::video::EDriverType device_type /*EDT_OPENGL*/, int res_x, int res_y, bool fullscreen /*false*/,  
					   int colordepth /*16*/, bool stencilbuffer /*false*/, const wchar_t *text) 
{ 
    //TODO: add error checking/handling here 
     
    //TODO: remove this 
printf("game_Engine::Init()\n"); 
     
    //(init can be called while in game, in which case EH was already created) 
    if(!m_pEventHandler) m_pEventHandler = new game_EventHandler(this); 
 
    //TODO: add error checking for this: 
    //if(m_device) error!! should not have a valid m_device before calling init() 
 
	//set up IrrLicht 
	//if(!m_device)  
 m_device = irr::createDevice(device_type, irr::core::dimension2d(res_x, res_y), 
                   colordepth, fullscreen, stencilbuffer, m_pEventHandler); 
 
    if(!m_device) return false; 
 
	m_device->setWindowCaption(text); 
 
	m_driver = m_device->getVideoDriver(); 
	m_smgr = m_device->getSceneManager(); 
    
//TODO  !!COPY AND UNCOMMENT THIS IN YOUR "xxx_Engine"s INIT() FUNCTION!! 
/* 
    //if we havent been configured yet, go into config state 
    //else start up directly into the first game state 
    std::string configured = ICE_Options::getConfig("configured"); 
    if(configured == "F") m_pCurrState = new gs_Configure(this);  
    else m_pCurrState = new gs_Intro(this); 
*/ 
 
	return true; 
} 
 
//called in main loop to see if engine is still running 
// returns false when the game is shutting down 
bool game_Engine::Update() 
{ 
    //ask the IrrLicht engine for another game loop 
    if( !m_device->run() ) { return false; } 
     
    //if RequestDestroy() was called, we'll call Destroy() now 
    if( this->destroyMe ) { Destroy(); return false; } 
     
    //update timer (though we dont use it right now)  
    m_time = m_device->getTimer()->getTime(); 
     
    //update our state 
	if(m_pCurrState) m_pCurrState->Update(); 
 
	return true; 
} 
 
// renders the scene (according to the appropriate state) 
void game_Engine::Render() 
{ 
    using namespace irr; 
     
    //dont waste time redrawing 
    if( !m_device->isWindowActive() ) return;  
 
    //we should alway have a state, so call it's render function 
    if(m_pCurrState) m_pCurrState->Render(); 
 
} 
 
// event handler 
bool game_Engine::OnEvent(irr::SEvent event) 
{ 
    using namespace irr; 
 
	//quit if the user presses ESC in Intro, otherwise move back to Intro -PDZ 01/04/03 0.9.4a  
	if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown ) { 
		if(m_GameState == GS_INTRO)  
			RequestDestroy(); 
		else 
			RequestStateChange(GS_INTRO); 
		return true;       
    } 
 
    // otherwise, let others try to handle the event: 
    if(m_pCurrState) 
            return m_pCurrState->onEvent(event); 
 
	return false; 
} 
 
void game_Engine::Destroy() { 
 //make sure we exit our current state 
 if(m_pCurrState) delete m_pCurrState;  
 
 //required to properly shut down IrrLicht 
 m_device->closeDevice(); 
} 
 
/*************************************************** 
    SAMPLE implementation of RequestStateChange 
**************************************************** 
//this is where states are changed 
void game_tetris::RequestStateChange(tGAME_STATE newState) { 
 
    //exit the current state 
    // each state's destructor should clean up any dynamic memory allocated by it 
    if(m_pCurrState) delete m_pCurrState;   
    m_GameState = newState; 
    switch(newState) { 
    case GS_INTRO: 
        m_pCurrState = new gs_Intro(this); //show menu buttons 
        break; 
    case GS_OPTS: 
        m_pCurrState = new gs_OptionMenu(this); //when we have options, they'll be set here 
        break; 
         
    case GS_MAIN: 
        m_pCurrState = new gs_Main(this); //the main game 
        break; 
         
    default: 
        //ERROR!! undefined state request 
        //should add some error handling here 
        break; 
    } 
     
} 
*****************************************************/ 
 
game_EventHandler::game_EventHandler(game_Engine* boss) { 
    m_pBoss = boss; 
} 
bool game_EventHandler::OnEvent(irr::SEvent event) { 
    return m_pBoss->OnEvent(event); 
}