Hello,
I am creating a simple tile-engine based on a tutorial and at the moment trying to draw layers of tiles. Engine has simple camera system which keeps track of which tiles must be drawn on the screen. The problem here is related to drawing layers. Here's an example of drawing bottom layer:
for(int y = 0, tileY = tileBounds.Top; y < tileBounds.Height; y++, tileY++)
{
for(int x = 0, tileX = tileBounds.Left; x < tileBounds.Width; x++, tileX++)
{
//let's assume that bottom layer must be filled with tiles --> no null check
tile = currentLevel->GetLayer(0).GetTile(tileX, tileY);
tile->Draw((x* tileSize) - camOffset_x, (y*tileSize) - camOffset_y, *window);
}
}
Now if I want to draw another layer: Consider adding next line under tile->Draw:
tile = currentLevel->GetLayer(1).GetTile(tileX, tileY);
if(tile != 0)
tile->Draw((x* tileSize) - camOffset_x, (y*tileSize) - camOffset_y, *window);
If I now move the camera to an area which is filled with tiles from both layers, cpu usge is around 70-80%. In case I use separate loops for both layers, cpu usage is below 20%.
Example (this is faster!):
for(int y = 0, tileY = tileBounds.Top; y < tileBounds.Height; y++, tileY++)
{
for(int x = 0, tileX = tileBounds.Left; x < tileBounds.Width; x++, tileX++)
{
//let's assume that bottom layer must be filled with tiles --> no null check
tile = currentLevel->GetLayer(0).GetTile(tileX, tileY);
tile->Draw((x* tileSize) - camOffset_x, (y*tileSize) - camOffset_y, *window);
}
}
//Draw layer 1
for(int y = 0, tileY = tileBounds.Top; y < tileBounds.Height; y++, tileY++)
{
for(int x = 0, tileX = tileBounds.Left; x < tileBounds.Width; x++, tileX++)
{
tile = currentLevel->GetLayer(1).GetTile(tileX, tileY);
if(tile != 0)
tile->Draw((x* tileSize) - camOffset_x, (y*tileSize) - camOffset_y, *window);
}
}
I'm confused, am I doing something wrong (being blind to own mistakes) or is this behaviour normal for RenderWindow? Using one loop should naturally be faster in my opinion.
Notes:
- Using references / pointers everywhere except with ints.
- Layer 1 is full of Tiles (100x100), Layer 2 has an area of 26*20 tiles which fills screen.
- Getters are simple methods, Tile->Draw only sets position and draws to window-parameter.
- There's not much else going on in rendering code, few test objects here and there.
My computer is a netbook with Intel graphics & Pentium SU4100, crappy setup but it shouldn't be the issue here. Operating system Ubuntu, SFML version 2.0 latest snapshot. I should try using RenderTexture too, but it seems to give me segfaults, I read that it's a problem with Intel drivers.
Now excuse me if post is lacking some information, it's my first post here. So far I've been able to solve my problems with SFML but this goes beyond my understanding
edit: screenshot to clarify situation:
http://bugi.oulu.fi/~zaih/example.png There are grass tiles (layer 0) under water tiles (layer 1)