The problem I am having is I am getting FPS rates as low as 20.
Here is my game loop
Code: Select all
while(device->run())
if (device->isWindowActive())
{
//Work out where we are and what needs to be drawn
v = camera->getAbsolutePosition();
int curXTerrain = (int) v.X/9600;
int curZTerrain = abs((int) v.Z/9600);
curZTerrain = (v.Z < 0?curZTerrain+1:curZTerrain);
int XStart = 0;
int XFinish = NUMBER_OF_ROWS-1;
if (v.X <= 0){
v.X = 0;
XStart = 0;
XFinish = 1;
}else if(curXTerrain > (NUMBER_OF_ROWS-1)){
v.X = NUMBER_OF_ROWS * LENGTH_OF_TERRAIN ;
XStart = curXTerrain-1;
XFinish = (NUMBER_OF_ROWS-1);
}
XStart = curXTerrain-2;
XFinish = curXTerrain + 2;
XStart = (XStart <= 0?0:XStart);
XFinish = (XFinish >= (NUMBER_OF_ROWS-1)?(NUMBER_OF_ROWS-1):XFinish);
int ZStart = 0;
int ZFinish = NUMBER_OF_COLS-1;
if (v.Z > LENGTH_OF_TERRAIN){
v.Z = LENGTH_OF_TERRAIN;
ZStart = 0;
}else if(curZTerrain > (NUMBER_OF_COLS-1)){
v.Z = (NUMBER_OF_COLS-1) * -LENGTH_OF_TERRAIN ;
ZFinish = (NUMBER_OF_COLS-1);
}
ZStart = curZTerrain-2;
ZFinish = curZTerrain + 2;
ZStart = (ZStart <= 0?0:ZStart);
ZFinish = (ZFinish >= (NUMBER_OF_COLS-1)?(NUMBER_OF_COLS-1):XFinish);
camera->setPosition(v);
for(int row= XStart; row <= XFinish; row++){
for(int col= ZStart; col <= ZFinish; col++){
terrain[col][row]->setVisible(true);
}
}
driver->beginScene(true, true, 0 );
smgr->drawAll();
env->drawAll();
driver->endScene();
for(int row= XStart; row <= XFinish; row++){
for(int col= ZStart; col <= ZFinish; col++){
terrain[col][row]->setVisible(false);
terrain[col][row]->setMaterialType(video::EMT_DETAIL_MAP);
}
}
bool useXStart = false;
int Diffrence = XStart - curXTerrain;
if (Diffrence > 1 || Diffrence < -1){
useXStart=true;
}
bool useXFinish = false;
Diffrence = XFinish - curXTerrain;
if (Diffrence > 1 || Diffrence < -1){
useXFinish=true;
}
//Remove detail on the edges
for(int col= ZStart; col <= ZFinish; col++){
if(useXStart)
terrain[col][XStart]->setMaterialType(video::EMT_SOLID);
if (useXFinish)
terrain[col][XFinish]->setMaterialType(video::EMT_SOLID);
}
// display frames per second in window title
int fps = driver->getFPS();
core::stringw str = L"Terrain Renderer - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
str += " ";
str += curXTerrain;
str += " ";
str += (int)(v.X);
str += ",";
str += curZTerrain;
str += " ";
str += (int)(v.Z);
str += " ";
str += (XFinish-curZTerrain);
device->setWindowCaption(str.c_str());
lastFPS = fps;
}