Hello!
I'm working on a tilebased game, 480x288px, each tile is 32x32 ==> 15x9 tiles = 135 tiles on screen at the same time.
On my computer (Intel Pentium Core2Duo 3.0 GHz) the process takes around 5-8 % of my CPU. (I'm running Ubuntu, if that makes any sense)
A few days ago I tried to run the application on my laptop (Intel Pentium M 1.3GHz, also Ubuntu) and then the process of the game took around 50-80 % of the CPU!
I came to the conclusion that my draw-code couldn't be very optimized.
I use only one sprite, and changes the image every draw-loop if it is needed.
Here is my Draw method:
void Tilegrid::DrawTiles(sf::RenderWindow & App, const Character & player)
{
// Calculate the tiles the player is within range, plus an extra offset
int extra_offset = 1;
int Xa = int( -0.5f+((float)player.GetPosition().x-(float)GameEngine::WINDOW_WIDTH/2)/(float)GameEngine::TILE_WIDTH ) -extra_offset;
int Xb = int( 0.5f+((float)player.GetPosition().x+(float)GameEngine::WINDOW_WIDTH/2)/(float)GameEngine::TILE_WIDTH ) +extra_offset;
int Ya = int( -0.5f+((float)player.GetPosition().y-(float)GameEngine::WINDOW_HEIGHT/2)/(float)GameEngine::TILE_HEIGHT ) -extra_offset;
int Yb = int( 0.5f+((float)player.GetPosition().y+(float)GameEngine::WINDOW_HEIGHT/2)/(float)GameEngine::TILE_HEIGHT ) +extra_offset;
// If the variables isn't in range, make them so.
(Xa < 0) ? Xa=0 : 0;
(Xb > gridsize.x) ? Xb=gridsize.x : 0;
(Ya < 0) ? Ya=0 : 0;
(Yb > gridsize.y) ? Yb=gridsize.y : 0;
int imgNr = -1;
for(int i=Xa; i<Xb; i++)
{
for(int j=Ya; j<Yb; j++)
{
// Set position
mySprite.SetPosition(i*GameEngine::TILE_WIDTH, j*GameEngine::TILE_HEIGHT);
// If a secondary img is available. Draw it underneath the main one.
if (grid[j][i].GetImage2() != -1)
{
mySprite.SetSubRect( grid[j][i].GetRect2() );
App.Draw(mySprite);
}
// new_imgNr is for optimizing.
// It don't have to re-GetRect if the same img was used last tile.
// This doesn't apply if a secondary image is used.
int new_imgNr = grid[j][i].GetImage();
if (imgNr != new_imgNr || grid[j][i].GetImage2() != -1)
{
imgNr = new_imgNr;
mySprite.SetSubRect( grid[j][i].GetRect() );
}
// Draw main sprite
App.Draw(mySprite);
}
}
}
It must be another way, more CPU friendly. Any ideas?
Thanks
Philip Irri
edit: oh, I just noticed there was another thread discussing this.. oh well