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


// Class automatically generated by Dev-C++ New Class wizard 
 
#include "game_tetris.h" // class's header file 
 
// class constructor 
game_tetris::game_tetris() 
{ 
    //always set pointers to NULL and initialize values 
    destroyMe = false; 
    m_time = 0; 
    m_pCurrState = NULL; 
 
} 
 
// class destructor 
game_tetris::~game_tetris() 
{ 
 m_device->drop(); 
} 
 
// called to start up the irrlicht engine and set up game variables 
bool game_tetris::Init(irr::video::EDriverType device_type /*EDT_OPENGL*/, int res_x, int res_y) 
{ 
    //set up options 
    tetris_Options::Initialize(); 
 
	//set up IrrLicht 
	m_device = irr::createDevice(device_type, irr::core::dimension2d(res_x, res_y), 
                   16, false, false, this); 
 
	m_device->setWindowCaption(WINDOW_TITLE); 
 
	m_driver = m_device->getVideoDriver(); 
	m_smgr = m_device->getSceneManager(); 
	 
	//set up camera     //no parent   //at 0,0,-400           //facing origin (0,0,0) 
	m_smgr->addCameraSceneNode(0, irr::core::vector3df(0,0,-400), irr::core::vector3df(0,0,0)); 
 
	//if you want to float around using an FPS cam, simply uncomment this. The event chain is already set up 
//m_camera = m_smgr->addCameraSceneNodeFPS();  
    
	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_tetris::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_tetris::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_tetris::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;       
    } 
 
    //this is set up so that if you attach an FPS camera to m_camera, the 
    // events will automatically be passed to it. Doesnt get used otherwise. 
    // (the FPS camera is helpful for graphics debugging) 
    if(m_camera) 
            if(m_camera->OnEvent(event) ) return true;  
            // if its handled by camera, we're done 
 
    // otherwise, let others try to handle the event: 
    if(m_pCurrState) 
            return m_pCurrState->onEvent(event); 
 
	return false; 
} 
 
void game_tetris::Destroy() { 
 //make sure we exit our current state 
 if(m_pCurrState) delete m_pCurrState;  
 
 //required to properly shut down IrrLicht 
 m_device->closeDevice(); 
 
} 
 
//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; 
    } 
     
} 
 
 
/***************** 
file change log: 
----------------- 
01/04/03 0.9.3a - //quit if the user presses ESC in Intro, otherwise move back to Intro -PDZ  
 
******************/