Info on scrolling tile maps.
Is there a place on the net that shows how to do a scrolling map using SDL? My plan is as follows
//Size of map
00000000000000000
00000000000000000
00000000000000000
//Size of screen
000
000
000
//Now when I reach the end of the screen with my character the map would shift towards the end of the screen. Is there a tutorial that can show me how this is done?
The way I'm doing it is to give the player free movement towards the edge of the window when he's at the end of the map. I accomplished that with a scroll function, which returns a boolean value if the map was able to be scrolled.
Player moving right:
Scroll the map (if possible):
It was pretty much just hacked together (notice the empty if block =)) It may give you a couple of ideas though.
Player moving right:
else if(key[KEY_RIGHT]) { Sprite::AnimateEntity(player, buffer, Sprite::RIGHT, 0); player.draw_direction = Sprite::RIGHT; player.collision_direction = Sprite::RIGHT; if(test->Collision(player, player.x_vel, 0) == false) { if(player.x > SCREEN_W / 2) { if(test->ScrollMap(Level::RIGHT, player)) { } else { player.x += player.x_vel; } } else { player.x += player.x_vel; } } }
Scroll the map (if possible):
bool Level::ScrollMap(int direction, Entity& ent){ switch(direction) { case UP: if(camera_y != 0) { camera_y -= ent.y_vel; return true; } break; case DOWN: if(camera_y + ent.y_vel < ((map_height * tile_height) - (v_tiles_on_screen*tile_height)) + (tile_height*2)) { camera_y += ent.y_vel; return true; } break; case LEFT: if(camera_x != 0) { camera_x -= ent.x_vel; return true; } break; case RIGHT: if(camera_x + ent.x_vel < ((map_width * tile_width) - (h_tiles_on_screen*tile_width)) + (tile_width*2)) { camera_x += ent.x_vel; return true; } break; default: break; } return false;}
It was pretty much just hacked together (notice the empty if block =)) It may give you a couple of ideas though.
position_on_screen = position_in_world_space - screen_offset
It's that simple. You have to keep track of the screen_offset as you move around though.
It's that simple. You have to keep track of the screen_offset as you move around though.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement