Hello everyone,
I'm currently experimenting with SDL for some basic scrolling shooter. Since the moving background reeeally slowed things down, I went online and found "SDL_Layer". The documentation is sparse, but I thought I'd figured it out - until I tried it. With just the background in one layer and a single object in another, it's just as slow as vanilla SDL. Because of the moving background I can't use dirty rects. Unfortunately, Google was not helpful on this topic, so I presume there is something wrong with my code.
Here is what I tried:
SDL_Surface *screen;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
// Initialize screen
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF );
if( screen == NULL ) {
fprintf(stderr, "Unable to init video: %s\n", SDL_GetError());
exit(1);
}
rmask = 0x00ff0000;
gmask = 0x0000ff00;
bmask = 0x000000ff;
amask = 0xff000000;
display = SDLayer_CreateRGBLayeredDisplay(SDL_HWSURFACE | SDL_FULLSCREEN, SDLAYER_FLIP, LAYERS, layer_widths, layer_heights,
32, rmask, gmask, bmask, amask);
SDLayer_SetColorKey(display, SDL_SRCCOLORKEY, 0); // without this, only the topmost layer will be visible
bmpBackground = SDL_LoadBMP("Background.bmp");
if(bmpBackgroundRewind == NULL) {
fprintf(stderr, "Couldn't find a graphic: %s\n", SDL_GetError());
exit(1);
}
bmpPlayer = SDL_LoadBMP("Player.bmp");
if(bmpPlayer == NULL) {
fprintf(stderr, "Couldn't find a graphic: %s\n", SDL_GetError());
exit(1);
}
SDL_SetColorKey(bmpPlayer, SDL_SRCCOLORKEY, SDL_MapRGB(bmpPlayer->format, 255, 0, 255));
I then enter the main loop, which repeatedly triggers the draw() function:
SDL_Rect background;
SDL_Rect dest;
static int background_y = 0;
if(++background_y >= HEIGHT) background_y=0;
// draw a moving background
background.x = 0;
background.y = background_y;
background.w = WIDTH;
background.h = HEIGHT;
SDLayer_Blit(bmpBackground, NULL, display, &background, 0);
background.x = 0;
background.y = background_y-HEIGHT;
background.w = WIDTH;
background.h = HEIGHT;
SDLayer_Blit(bmpBackground, NULL, display, &background, 0);
// draw the player
dest.x = player1.x; dest.y = player1.y;
dest.w = player1.w; dest.h = player1.h;
SDLayer_Blit(bmpPlayer, NULL, display, &dest, 1);
// draw the buffer to the screen
SDLayer_Update(display);
I currently disabled everything else, so it's definitely this bit which is causing the slowness. My laptop isn't the fastest one out there, but the speed I'm currently getting can't be right.
Also, I have a hunch the moving background could somehow be realized with viewports and scrolling factors, but should I even bother? When the program's finished, there will be way more objects to update, and it doesn't even work smoothly for one. Or is there some alternative to blitting that I missed?
Anyways, thanks in advance for any help!
Sincerely
Arivor