Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Comments

  • slook26slook26 UtahPosts: 4Member
    edited January 27

    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 StockholmPosts: 1Member

    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.