[SDL] Poor performance using alpha blending

Christian Topnik christian_sdl at jusos-waltrop.de
Sun Jan 26 08:46:00 PST 2003


> Best place I can point you is to examples.  I personally used the fading
> demo, http://www.2dgame-tutorial.com/sdl/fading.htm, as an example to

Thanks alot! :)

I downloaded and tried it. It runs very smooth, but only as long as I don't
run it in fullscreen, then it gets _extremely_ slow. The only way to speed
it up was to use SDL_SWSURFACE, but _without_ SDL_DOUBLEBUF.

I just wrote a little test program (code at the end of this message). It
needs two pictures "background.bmp" (640x480) and "overlay.bmp" (somewhat
smaller). The first picture is scrolled horizontally while the second is
drawn half-visible in the center of the screen. This program runs very
slowly in fullscreen (even with the SWSURFACE-trick mentioned above), at
least at my PC.

It would be great if anyone had an idea how to improve this...

Bye,

Christian
-------------------------------------------------
#include <stdlib.h>

#include "SDL.h"

int main(int argc, char *argv[]) {

 // Surfaces
 SDL_Surface *screen     = NULL ;
 SDL_Surface *background = NULL ;
 SDL_Surface *overlay    = NULL ;

 SDL_Surface *temp       = NULL;

 // Keyboard input
 Uint8 *keys;

 // Looping and moving
 bool loop = true;
 int movingspeed = 2;

 // Rects
 SDL_Rect backPos; // Where to put the background
 backPos.x = 0;
 backPos.y = 0;

 SDL_Rect backPos2;  // Where to put the 2nd background
 backPos2.x = 0;
 backPos2.y = 0;

 SDL_Rect blitPos;   // Where to put the overlay


 // Screen properties
 int screenWidth  = 640;
 int screenHeight = 480;
 int bpp          = 32;

 // SDL initializing
 if(SDL_Init(SDL_INIT_EVERYTHING) <0) {
        printf("Can't init SDL:  %s\n", SDL_GetError());
        exit(1);
    }
    atexit(SDL_Quit);

 // Setting up the screen
 screen = SDL_SetVideoMode(screenWidth, screenHeight, bpp,
SDL_HWSURFACE|SDL_FULLSCREEN|SDL_DOUBLEBUF);

    if(screen == NULL) {
  printf("\nError: Can't set desired video mode: %s\n", SDL_GetError());
        exit(1);
    }

 // Preparing the other surfaces
 temp = SDL_LoadBMP("background.bmp");
 if(temp == NULL) {
  printf("\nError: %s\n", SDL_GetError());
        exit(1);
    }
 background = SDL_DisplayFormat(temp);
 SDL_FreeSurface(temp);

 temp = SDL_LoadBMP("overlay.bmp");
 if(temp == NULL) {
  printf("\nError: %s\n", SDL_GetError());
        exit(1);
    }
 overlay = SDL_DisplayFormat(temp);
 SDL_FreeSurface(temp);

 // Calculating the position of the overlay
 blitPos.x = (screenWidth-overlay->w)/2;
 blitPos.y = (screenHeight-overlay->h)/2;

 // Setting the alpha value of the overlay
 SDL_SetAlpha(overlay,SDL_SRCALPHA,128);


 // The main loop (exited with ESC)
 while(loop) {

  SDL_BlitSurface(background,NULL,screen,&backPos);
  if(backPos.x!=0) {
   backPos2.x = backPos.x - screenWidth;
   SDL_BlitSurface(background,NULL,screen,&backPos2);
  }
  SDL_BlitSurface(overlay,NULL,screen,&blitPos);

  SDL_Flip(screen);

  backPos.x += movingspeed;
  if(backPos.x>screenWidth)
   backPos.x-=screenWidth;

  SDL_PumpEvents();
  keys = SDL_GetKeyState(NULL);
  loop = !keys[SDLK_ESCAPE];
 }


 // Cleaning up
 SDL_FreeSurface(background);
 SDL_FreeSurface(overlay);

 SDL_FreeSurface(screen);

 exit(0);
}






More information about the SDL mailing list