Archived

This topic is now archived and is closed to further replies.

Another optimisation task for the gurus :)

This topic is 6252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Okay, my SDL 2d tile engine is properly finished. Very nice it is too. This piece of code below worries me however, is there anyway (short of using precalculated globals and the like) that I can use bitshifts or something here? Thats a little over my head to figure out for the time being The code sets an SDL_Rect, ''tile_src'' using the tile index from the map -- Heres the code:
tile_src.x = tile % TILES_PER_ROW * TILE_SIZE;
tile_src.y = tile / TILES_PER_ROW * TILE_SIZE;
tile_src.w = tile % TILES_PER_ROW * TILE_SIZE + TILE_SIZE;
tile_src.h = tile / TILES_PER_ROW * TILE_SIZE + TILE_SIZE;

''TILE_SIZE'' is a macro defined as ''32'' for this example. ''TILES_PER_ROW'' is the number of tile images per row in my tileset, its set to ''4'' in this example. My tileset image is square and a power of two, ready for you guys to work your bitshifting magic Any << >> type ideas? Thanks, Paul.

Share on other sites

tile_src.x = (tile % TILES_PER_ROW) << 5;tile_src.y = tile<<3;tile_src.w = tile_src.x + TILE_SIZE;tile_src.h = tile_src.y + TILE_SIZE;

quote:

''TILE_SIZE'' is a macro defined as ''32'' for this example. ''TILES_PER_ROW'' is the number of tile images per row in my tileset, its set to ''4'' in this example. My tileset image is square and a power of two, ready for you guys to work your bitshifting magic

This is probably a bit faster, but since shifting only works for powers of two just reusing the allready calculated values for x and y for w and h should be a better idea.

Share on other sites
Can't believe I missed the tile_src.w and tile_src.h :p Guess I couldn't see the wood for the trees there... Thanks.

Edited by - Pauly on January 11, 2001 6:33:57 PM

Share on other sites
Try this on for size...
x & 3 is the same as x % 4.
x * 32 is the same as x << 5.

With this information you can do the following:
(need variables i, j)

i = (tile & 3) << 4;
j = (tile / TILES_PER_ROW) << 4;
tile_src.x = i;
tile_src.y = j;
tile_src.w = i + TILE_SIZE;
tile_src.h = j + TILE_SIZE;

This replaces the 4 multiplications, 2 divisions, and 2 mods with 1 division, 2 shifts, and 1 and. This should give you a nice boost. The drawback? The 3 and 4 are hardcoded.

Dino M. Gambone
Good judgement is gained through experience. Experience, however, is gained through bad judgement.

Share on other sites
Opps... the << 4 should be << 5

Dino M. Gambone
Good judgement is gained through experience. Experience, however, is gained through bad judgement.

Share on other sites
Well, for your current example where tiles_per_row is 4

tile_src.x = (tile & 3) << 5;
tile_src.y = (tile >> 2) << 5;
tile_src.w = tile_src.x + TILE_SIZE;
tile_src.h = tile_src.y + TILE_SIZE;

However, later on you will obviously have more tiles than that, so you should use a multiplication instead

tile_src.y = (tile * RECIPROCAL_OF_TPR) << 5;

And in your current case, RECIPROCAL_OF_TPR = 1/4 or 0.25

Understand?

"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 ..."

Share on other sites
Cheers for all the info!! You''ve all been a great help.

Paul.