How to make a screen scroll on the x and y axis (allegro)

So I'm working on a game, and I can't seem to figure out how to make the screen scroll on either the x or y axis. If anyone can help me out that would be great. Please be as detailed as possible. Thank you


  • slook26slook26 Utah
    edited January 2014

    How do you mean scroll, like a continuous scroll or scrolling like the user moves and a camera follows him around the map? If it's the second one here's how I did it:

    I took the map drawing function and added it to take in my camera's X and Y coordinates:


    void Map::OnDraw(SDL_Renderer* ren, int cameraX, int cameraY)

    //omitted code for length reasons

    for(int x = 0; x <= map_tiles_x; x++)
        for(int y = 0; y <= map_tiles_y; y++)

    //omitted good portion of code for length reasons

            SDL_Rect dest;
            dest.x = (x * TILE_SIZE) - cameraX;
            dest.y = (y * TILE_SIZE) - cameraY;
            dest.w = TILE_SIZE;
            dest.h = TILE_SIZE;

    //omitted rest of code for length reasons


    So when the character moves, the tiles move opposite the player. E.g. player moves down, the tiles get shifted up, player moves up, tiles get shifted down. That way it looks like they're scrolling around a map. You can even setup statements where if the camera is near the edge of the map to not move the map, but move the player.

  • SalamanderSalamander Stockholm

    Yup, slook's answer a good one.

    If you want to develop that even more, one can always add camera velocity and acceleration, so that it doesn't seem "glued" to the player but more like it was following him reluctantly, as if pulled with an elastic band...

    But make sure you get this right first. Develop later. And good luck!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!