I don't know what else can I use except guienv, anyway, here is the code.
CGame.cpp
Code: Select all
#include "CGame.h"
//////////////////////////////////////////////////////////////////
// Default constructor, better don't use this //
//////////////////////////////////////////////////////////////////
CGame::CGame() : driverType(EDT_SOFTWARE), dimensions(640,480), bits(16), fullscreen(true), stencilbuffer(false), vsync(false) {}
CGame::CGame(E_DRIVER_TYPE drvType,dimension2d<s32> dim,u32 bts,bool fscr,bool stnc,bool vsnc)
: driverType(drvType),dimensions(dim),bits(bts),fullscreen(fscr),stencilbuffer(stnc),vsync(vsnc) {}
//////////////////////////////////////////////////////////////////
// Runs the Game //
//////////////////////////////////////////////////////////////////
void CGame::Run() {
Initialize();
//IGUIFont *font=guienv->getFont("arial.bmp");
while(device->run()) {
driver->beginScene(true,true,SColor(255,255,0,0));
smgr->drawAll();
guienv->drawAll();
showStats();
driver->endScene();
}
device->drop();
return;
}
//////////////////////////////////////////////////////////////////
// Good 'ol Event Receiver //
//////////////////////////////////////////////////////////////////
bool CGame::OnEvent(const irr::SEvent &event) {
if(!device)
return false;
if(event.EventType==EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) {
switch(event.KeyInput.Key) {
case KEY_ESCAPE:
device->closeDevice();
return true;
case KEY_F9:
takeScreenshot();
listbox->addItem(L"Screenshot taken..");
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////
// Initializations of all variables //
//////////////////////////////////////////////////////////////////
void CGame::Initialize() {
controls[0].Action=EKA_MOVE_FORWARD;
controls[0].KeyCode=KEY_KEY_W;
controls[1].Action=EKA_MOVE_BACKWARD;
controls[1].KeyCode=KEY_KEY_S;
controls[2].Action=EKA_STRAFE_LEFT;
controls[2].KeyCode=KEY_KEY_A;
controls[3].Action=EKA_STRAFE_RIGHT;
controls[3].KeyCode=KEY_KEY_D;
controls[4].Action=EKA_JUMP_UP;
controls[4].KeyCode=KEY_KEY_E;
device=createDevice(driverType,dimensions,bits,fullscreen,stencilbuffer,vsync);
device->setEventReceiver(this);
driver=device->getVideoDriver();
guienv=device->getGUIEnvironment();
smgr=device->getSceneManager();
screen_center=position2d<s32>(driver->getScreenSize().Width/2,driver->getScreenSize().Height/2);
device->getCursorControl()->setVisible(false);
terrain=smgr->addTerrainSceneNode("blank.bmp",0,-1,vector3df(0,0,0),vector3df(0,0,0),vector3df(10,10,10),SColor(100,255,255,255),4,scene::ETPS_9);
terrain->setMaterialFlag(EMF_LIGHTING,false);
terrain->setMaterialTexture(0,driver->getTexture("data/woodflort.tga"));
terrain->scaleTexture(50,50);
skydome=smgr->addSkyDomeSceneNode(driver->getTexture("data/skydome.jpg"),32,32,1,2);
camera=smgr->addCameraSceneNodeFPS(0,100,500,-1,controls,5);
camera->setPosition(vector3df(0,100,0));
camera->setFarValue(12000.0f);
driver->makeColorKeyTexture(driver->getTexture("data/aim.bmp"),SColor(0,0,0,0));
}
void CGame::showStats() {
IGUIFont *font=device->getGUIEnvironment()->getFont("arial.bmp");
guienv->getSkin()->setFont(font);
FPS=driver->getFPS();
screenWidth=driver->getScreenSize().Width;
screenHeight=driver->getScreenSize().Height;
maxprimitive=driver->getMaximalPrimitiveCount();
primitivecount=driver->getPrimitiveCountDrawn();
materialcount=driver->getMaterialRendererCount();
dynamiclightcount=driver->getDynamicLightCount();
referencecount=driver->getReferenceCount();
stringw gameData;
gameData=L"FPS: ";
gameData+=FPS;
gameData+=L"\n";
gameData+=L"Resolution:";
gameData+=L"\nWidth->";
gameData+=screenWidth;
gameData+=L"\nHeight->";
gameData+=screenHeight;
gameData+=L"\nCounts:\n";
gameData+=L"Max Primitive count->";
gameData+=maxprimitive;
gameData+="\nPrimitive count->";
gameData+=primitivecount;
gameData+="\nMaterial count->";
gameData+=materialcount;
gameData+="\nDynamic light count->";
gameData+=dynamiclightcount;
gameData+="\nReference count->";
gameData+=referencecount;
guienv->clear();
info=guienv->addStaticText(gameData.c_str(),rect<s32>(0,0,220,180),true,true,0,-1,true);
listbox = guienv->addListBox(rect<s32>(250, 100, 450, 310),0,-1,true);
}
void CGame::takeScreenshot() {
stringc name;
name="screenshot/screenie-";
name+=device->getTimer()->getTime();
name+=".jpg";
screenshot=driver->createScreenShot();
driver->writeImageToFile(screenshot,name.c_str());
screenshot->drop();
}
void CGame::drawOnRun() {
showStats();
aim=guienv->addImage(driver->getTexture("data/aim.bmp"),screen_center);
aim->setColor(SColor(150,255,255,255));
}