Sign in to follow this  
Thrust

Info on scrolling tile maps.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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:

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this