Arialth
Arialth

Reputation: 157

SDL - Scrolling through an (offscreen) surface

I am having a hard time wrapping my head about the concept of making a scrolling graphics program in SDL in c++... for a sidescrolling game, for instance, or a top down game with a map larger than the resolution of the screen.

Basically, I was under the impression that i need to blit all my resources to a virtual surface and then blit only a portion of that to the screen, but this does not appear to be working. Here is a portion of the code I am using:

    void showMap(){
        for(int y=0;y<MAP_Y;y++){
            for(int x=0;x<MAP_X;x++){
                switch(map[x][y]){
                    case 0:
                        apply_surface(x*STEP,y*STEP,tiles,full,&clips[0][0]);
                        break;
                    case 1:
                        apply_surface(x*STEP,y*STEP,tiles,full,&clips[1][0]);
                        break;
                    case 2:
                        apply_surface(x*STEP,y*STEP,tiles,full,&clips[2][0]);
                        break;
                    case 3:
                        apply_surface(x*STEP,y*STEP,tiles,full,&clips[3][0]);
                        break;
                    default:
                        apply_surface(x*STEP,y*STEP,tiles,full,&clips[4][4]);
                        break;

                }
            }
        }
        apply_surface(0,0,full,master,&vp_xy);
    }

The prototype for apply_surface is as follows:

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *dest, SDL_Rect* clip);

It blits source to dest at the given x,y with the specified clipping. In the example, I have an array of SDL_Rects that hold the location of various tiles. This code works if I blit directly to master (the actual screen), but attempting to blit to 'full' and then blitting a portion of that to master only lands me with an empty screen. What am I missing here?

Upvotes: 0

Views: 1417

Answers (1)

Arialth
Arialth

Reputation: 157

Ok ok I fould what I was doing wrong.

When I created the 'full' surface, it remained null. I just discovered the command SDL_CreateRGBSurface().

Like so:

full = SDL_CreateRGBSurface(SDL_SWSURFACE,STEP*MAP_X,STEP*MAP_Y,S_BPP,0,0,0,0);

Can't blit to a null surface. I wasn't aware of this function. The more you know~

Upvotes: 1

Related Questions