#### Archived

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

# Looping a map.

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

## 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 on other sites
rather than calculating j%num_cols each loop, see if j == num_cols, and if so, set j = 0. an if statement is going to be quicker than a % calculation.

- jeremiah

inlovewithGod.com

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

O.K. Thanks.

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

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×