void draw_tiles(int world_camerax, int world_cameray)
{
int tile;
int y, x;
RECT tile_src;
int dst_x, dst_y;
short offset_x, offset_y;
// draw the tiles one by one...
for (y = 0; y < SCREEN_SIZEY + 1; y++)
{
for (x = 0; x < SCREEN_SIZEX + 1; x++)
{
tile = map [y + (world_cameray / TILE_SIZE)]
[x + (world_camerax / TILE_SIZE)];
if(tile == 0) continue; // do not draw
dst_x = (x * TILE_SIZE) - (world_camerax % TILE_SIZE);
dst_y = (y * TILE_SIZE) - (world_cameray % TILE_SIZE);
// Calcul de clipping
// bltfast cannot blit offsurface
offset_x = (dst_x < 0 ? -dst_x : 0);
offset_y = (dst_y < 0 ? -dst_y : 0);
tile_src.left = (tile - 1) * TILE_SIZE + offset_x;
tile_src.top = offset_y;
if(dst_x > 607)
tile_src.right = tile * TILE_SIZE - (dst_x - 607);
else
tile_src.right = tile * TILE_SIZE;
if(dst_y > 447)
tile_src.bottom = TILE_SIZE - (dst_y - 447);
else
tile_src.bottom = TILE_SIZE;
G.lpDDSBack->BltFast(dst_x + offset_x, dst_y + offset_y,G.lpDDSTiles, &tile_src, NULL);
}
}
}
Tiles blitting optimisation anyone ?
Hi,
I would love if anyone can help me with
optimisation for my tiles drawing function.
It''s currently running at 60 fps on my
good''ol Pentium 120 mhz.
Let''s discuss...
This is my draw_tiles function :
- Consider unrolling your loop.
- Change % TILE_SIZE to & TILE_SIZE-1 (or a variable that is equal to TILE_SIZE-1)
- Read some of the previous topics on this list which have done this time and time again.
- Change % TILE_SIZE to & TILE_SIZE-1 (or a variable that is equal to TILE_SIZE-1)
- Read some of the previous topics on this list which have done this time and time again.
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
Hi,
I don''t understand what you mean by "unrolling" a loop.
Sorry but I am quit new to game programming and optimization
technique.
I mean... how can you make a loop to not loop (unrolled) !?
Thanks for your help,
Stephane.
I don''t understand what you mean by "unrolling" a loop.
Sorry but I am quit new to game programming and optimization
technique.
I mean... how can you make a loop to not loop (unrolled) !?
Thanks for your help,
Stephane.
My main suggestions would be to remove those divides etc from your loops.
since world_camerax and world_cameray never change, you can have those two divisions performed ONCE, outside the loops.
ie,
int tile_divx=world_camerax/TILE_SIZE;
int tile_divy=world_cameray/TILE_SIZE;
this reduces those to an add in the loop.
the same can be done with the %''s:
int tile_modx=world_camerax%TILE_SIZE;
int tile_mody=world_cameray%TILE_SIZE;
just removing those 4 divides and replaceing them with adds will make a significant difference.
now for a few more petty optimizations, which are hangovers from my dos assembly days:
you repetitively use tile*TILE_SIZE.
you could set a variable right after you work out what the tile is and save yourself a few multiplies.
ie, int tileTILE=tile*TILE_SIZE;
tile_src.right = tileTILE - (dst_x - 607);
and
tile_src.left = tileTILE - TILE_SIZE + offset_x;
(this one is equivalent to (tile-1)*TILESIZE)
one last minor optimisation - remove those last two multiplications from the loop.
ie y*TILESIZE is equal to (y-1)*TILESIZE + TILESIZE
so if you store y*TILESIZE each time the y loop runs through, and then add TILESIZE each time, you use an add rather than a multiply
ie:
int ycumulative=0;
for(y=...)
{
int xcumulative=0;
for(x=...)
{
dst_y=ycumulative-tilemody;
...
// last line
xcumulative+=TILESIZE;
}
ycumulative+=TILESIZE;
}
anyway, hope that helps you
Wyzfen
since world_camerax and world_cameray never change, you can have those two divisions performed ONCE, outside the loops.
ie,
int tile_divx=world_camerax/TILE_SIZE;
int tile_divy=world_cameray/TILE_SIZE;
this reduces those to an add in the loop.
the same can be done with the %''s:
int tile_modx=world_camerax%TILE_SIZE;
int tile_mody=world_cameray%TILE_SIZE;
just removing those 4 divides and replaceing them with adds will make a significant difference.
now for a few more petty optimizations, which are hangovers from my dos assembly days:
you repetitively use tile*TILE_SIZE.
you could set a variable right after you work out what the tile is and save yourself a few multiplies.
ie, int tileTILE=tile*TILE_SIZE;
tile_src.right = tileTILE - (dst_x - 607);
and
tile_src.left = tileTILE - TILE_SIZE + offset_x;
(this one is equivalent to (tile-1)*TILESIZE)
one last minor optimisation - remove those last two multiplications from the loop.
ie y*TILESIZE is equal to (y-1)*TILESIZE + TILESIZE
so if you store y*TILESIZE each time the y loop runs through, and then add TILESIZE each time, you use an add rather than a multiply
ie:
int ycumulative=0;
for(y=...)
{
int xcumulative=0;
for(x=...)
{
dst_y=ycumulative-tilemody;
...
// last line
xcumulative+=TILESIZE;
}
ycumulative+=TILESIZE;
}
anyway, hope that helps you
Wyzfen
Bios: Unrolling a loop basically just means to write it as a series of statements instead of looping over and over. For instance, here''s a loop:
for (x=0; x<5; x++)
array[x] += 10;
and here it is unrolled:
array[0] += 10;
array[1] += 10;
array[2] += 10;
array[3] += 10;
array[4] += 10;
Generally, this will only be worth your time if the inner loop is small enough that you''ll actually profit from eliminating an incrementation and a comparison. In your case, unrolling the loop looks unnecessary to me. There''s too much going on inside of the loop for unrolling to help you at all.
-Ironblayde
Aeon Software
Next thing you know, they''ll take my thoughts away.
for (x=0; x<5; x++)
array[x] += 10;
and here it is unrolled:
array[0] += 10;
array[1] += 10;
array[2] += 10;
array[3] += 10;
array[4] += 10;
Generally, this will only be worth your time if the inner loop is small enough that you''ll actually profit from eliminating an incrementation and a comparison. In your case, unrolling the loop looks unnecessary to me. There''s too much going on inside of the loop for unrolling to help you at all.
-Ironblayde
Aeon Software
Next thing you know, they''ll take my thoughts away.
"Your superior intellect is no match for our puny weapons!"
Hi,
Thank you so much for helping me out.
I''m at school right now so I cannot implement your
suggestions but I will as soon as I can.
What you all said look very good to me and I''m sure
I''ll be able to decrease the computations needed for
drawing.... it will give me more time for AI and/or sprite
handling.
Thanks so much again,
Stephane.
Thank you so much for helping me out.
I''m at school right now so I cannot implement your
suggestions but I will as soon as I can.
What you all said look very good to me and I''m sure
I''ll be able to decrease the computations needed for
drawing.... it will give me more time for AI and/or sprite
handling.
Thanks so much again,
Stephane.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement