www.pudn.com > ICE_1.4.zip > gs_main.cpp
// Class automatically generated by Dev-C++ New Class wizard
#include "gs_main.h" // class's header file
irr::gui::IGUIButton* gs_Main::m_btn_exit = NULL;
irr::gui::IGUIButton* gs_Main::m_btn_paused = NULL;
irr::gui::IGUIButton* gs_Main::m_btn_end = NULL;
// class constructor
gs_Main::gs_Main(game_StateHandler*handler) : game_State(handler)
{
using namespace irr;
m_mode = GSM_MODE_PLAY;
if(!m_btn_exit) {
int btn_x = 5 ;
int btn_y = 5 ;
#define BTN_ROW_SPACING 40
#define BTN_W 90
#define BTN_H 30
m_btn_exit = m_env->addButton(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0,
GSM_GUI_BTN_EXIT, L"Back");
//TODO: make this percent of screen. Dont hardcode!
btn_x = 240;
btn_y = 200;
m_btn_paused = m_env->addButton(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0,
GSM_GUI_BTN_PAUSED, L"Paused");
m_btn_paused->setVisible(false);
m_btn_end = m_env->addButton(core::rect(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0,
GSM_GUI_BTN_END, L"Start Over");
m_btn_end->setVisible(false);
}else{
m_btn_exit->setVisible(true);
m_btn_paused->setVisible(false);
m_btn_end->setVisible(false);
}
for(int x=0; x< irr::KEY_KEY_CODES_COUNT; x++) keys[x] = false;
m_boardTime = m_device->getTimer()->getTime();
m_boardUpdateSpeed = 30;
m_inputTime = m_boardTime;
m_inputUpdateSpeed = 50;
//set up rendering of the 3 objects
//TODO: make percentage of screen, dont hardcode positions!
/*
if(tetris_Options::isRender2Enabled()) //use experimental PBS2 if option is true
m_pileRenderer = (tetris_PileBlobScene*)
new tetris_PileBlobScene2(m_smgr->getRootSceneNode(), m_smgr, 555, &m_pile);
else
m_pileRenderer =
new tetris_PileBlobScene(m_smgr->getRootSceneNode(), m_smgr, 555, &m_pile);
m_pileRenderer->setPosition(irr::core::vector3df(-240,-420,0));
m_pileRenderer->drop();
m_currBlockRenderer =
new tetris_FallingBlobScene(m_smgr->getRootSceneNode(), m_smgr, 555, &m_currBlob);
m_currBlockRenderer->setPosition(irr::core::vector3df(-240,-420, -10));
m_currBlockRenderer->drop();
m_nextBlockRenderer =
new tetris_FallingBlobScene(m_smgr->getRootSceneNode(), m_smgr, 555, &m_nextBlob);
m_nextBlockRenderer->setPosition(irr::core::vector3df(0,-420,0));
m_nextBlockRenderer->drop();
*/
}
// class destructor
gs_Main::~gs_Main()
{
//hide our static GUI buttons
if(m_btn_exit) {
m_btn_exit->setVisible(false);
m_btn_paused->setVisible(false);
m_btn_end->setVisible(false);
}
//reset score to zero
//tetris_ScoreKeep::setScore(0);
}
void gs_Main::Render() {
m_driver->beginScene(true, true, irr::video::SColor(0,0,0,0));
//draw the block pile and falling block scenes
m_smgr->drawAll();
//draw the gui environment
m_env->drawAll();
/*
wchar_t _Info[1024];
irr::gui::IGUIFont * pFnt = m_env->getBuildInFont();
int Ypos = 50;
int Yshift = 50;
swprintf(_Info, 1024,L"FPS: %d ", (const int) m_driver->getFPS() );
pFnt->draw(_Info, irr::core::rect(10, Ypos, 200, Yshift), irr::video::SColor(255, 255, 255, 255), true, true);
Ypos += Yshift;
swprintf(_Info, 1024,L"Level: %d ", 0 ); //tetris_Options::getCurrLevel() );
pFnt->draw(_Info, irr::core::rect(10, Ypos, 200, Yshift), irr::video::SColor(255, 255, 255, 255), true, true);
Ypos += Yshift;
swprintf(_Info, 1024,L"Score: %d ", 135 ); //tetris_ScoreKeep::getScore());
pFnt->draw(_Info, irr::core::rect(10, Ypos, 200, Yshift), irr::video::SColor(255, 255, 255, 255), true, true);
Ypos += Yshift;
swprintf(_Info, 1024,L"LevelUp: %d ", 200 ); //calcNextLevel() );
pFnt->draw(_Info, irr::core::rect(10, Ypos, 200, Yshift), irr::video::SColor(255, 255, 255, 255), true, true);
*/
m_driver->endScene();
}
#include
bool gs_Main::onEvent(irr::SEvent event){
using namespace irr;
using namespace gui;
if(event.EventType == irr::EET_KEY_INPUT_EVENT){
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
if(event.KeyInput.Key == irr::KEY_KEY_P && event.KeyInput.PressedDown) {
if(m_mode == GSM_MODE_PAUSED) {
m_btn_paused->setVisible(false);
m_mode = GSM_MODE_PLAY;
printf("mode: game resumed\n");
}else if(m_mode == GSM_MODE_PLAY) {
m_btn_paused->setVisible(true);
m_mode = GSM_MODE_PAUSED;
printf("mode: game paused\n");
}
}
return true;
}
if (event.EventType == irr::EET_GUI_EVENT) {
s32 id = event.GUIEvent.Caller->getID();
switch(event.GUIEvent.EventType) {
case EGET_BUTTON_CLICKED:
if(id == GSM_GUI_BTN_EXIT) {
m_handler->RequestStateChange(GS_INTRO);
return true;
}
if(id == GSM_GUI_BTN_PAUSED) {
m_btn_paused->setVisible(false);
m_mode = GSM_MODE_PLAY;
return true;
}
if(id = GSM_GUI_BTN_END) {
m_btn_end->setVisible(false);
//start game play over again:
m_handler->RequestStateChange(GS_MAIN);
}
//return true; //if supported button id
break;
}//end switch: type of GUI event
}//end if: GUI event
return false;
}
void gs_Main::Update(){
irr::u32 curr_time = m_device->getTimer()->getTime();
if(m_mode == GSM_MODE_PLAY) {
//only perform these updates if in PLAY mode
//update user input at a set interval
if(curr_time - m_inputTime > m_inputUpdateSpeed) {
/*
irr::core::position2d *pos = m_currBlob.getTCPos();
if(keys[irr::KEY_LEFT]) { if(m_pile.canShiftLeft(&m_currBlob)) pos->X -= 1; }
if(keys[irr::KEY_RIGHT]) { if(m_pile.canShiftRight(&m_currBlob)) pos->X += 1; }
if(keys[irr::KEY_DOWN]) m_currBlob.Fall(30);
*/
m_inputTime = curr_time;
}//end if: update key input
//update 'board' at a set interval
if(curr_time - m_boardTime > m_boardUpdateSpeed) {
/*
if(m_pile.QueryHeight(TS_PILEBLOB_H - 2) && m_pile.isFallingBlobTouching(&m_currBlob)) {
//if we can add a new blob without moving it
// it must have spawned on top of a block--
// which means game over
m_mode = GSM_MODE_END;
m_btn_end->setVisible(true);
printf("mode: game over, you lose\n");
}
//move currBlob down by speed ratio
m_currBlob.Fall( 1*tetris_Options::getCurrLevel() );
//check for collision with pileblob
if(m_pile.isFallingBlobTouching(&m_currBlob) ) {
//blit the blob
irr::core::position2d *pos = m_currBlob.getTCPos();
m_pile.BlitBlobTo(pos->X, pos->Y, &m_currBlob);
//set curr_blob to next blob
m_currBlob = m_nextBlob;
//fill in new nextBlob
m_nextBlob.createNewShape(m_boardTime);
//remove completed lines
if(m_pile.isLineComplete() ){
int numcomplete = m_pile.removeCompletedLines();
//update the new score
tetris_ScoreKeep::setScore( tetris_ScoreKeep::getScore() +
calcLineScore(numcomplete) );
//if using PBS2, tell it to shake
if(tetris_Options::isRender2Enabled()) {
tetris_PileBlobScene2* scn = (tetris_PileBlobScene2*) m_pileRenderer;
scn->addShake(numcomplete);
}
if(calcNextLevel() < tetris_ScoreKeep::getScore()) {
//level up!
tetris_Options::setCurrLevel( tetris_Options::getCurrLevel()+1);
//if we had special level up graphics, we'd call them here
}
}
}
*/
m_boardTime = curr_time;
}//end if: update board
}//end if: m_mode == gsm_mode_play
}//end funct: gs_Main::Update()
/*****************
file change log:
-----------------
01/05/03 0.9.4a - //level up! -PDZ
******************/