Archived

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

Looping a map.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering is there an efficient way to implement a looped tile based map, say for a 2D side scroller:
for (int i = start_map_y; i <=  end_map_y; i++)
{
	for (int j = start_map_x; j <= end_map_x; j++)
	{
		int cellx =  j%num_cols;	
               	cells[cellx].xpos = offset_x;
		cells[i][cellx].ypos = offset_y;
 
what i''ve been doing is "%"ing the column tiles so after say, 40 tiles have been scrolled, it uses the first tile again. That % in the inner loop though, hardly helps the frame rate. Does anyone know a better way of doing it or is this the standard procedure?

Share this post


Link to post
Share on other sites
quote:
Original post by keethrus
an if statement is going to be quicker than a % calculation.



quite the contrary. an if statement is branching, and branching hurts performance.

To the original poster: don''t worry about the % in the inner loop. Unless you''re doing it per-pixel (which you shouldn''t) AND you''re on something like a J2ME phone or Gameboy, the cost is negligible.

Basing optimization strategies on 8-year old TXT files about 386 assembly is not a good idea.

Share this post


Link to post
Share on other sites
Additionally, if ''num_cols'' is a power of 2, you can use
int cellx = j & (num_cols - 1);

instead. Its only a bitwise AND, it''s much faster than a MOD.



Waramp.

"Before you insult a man, walk a mile in his shoes.
That way, when you do insult him, you''ll be a mile away, and you''ll have his shoes."

Share this post


Link to post
Share on other sites