Poor performance using alpha blending

Hi everybody,

I am using SDL for about two month now, and it didn’t cause me any troubles
so far. Until now… :wink:

As soon as I am using alpha blending in my game, everything gets really
slow. Of course I read through all the articles of this mailinglist.

I am using a 50% transpareny (value 128) because this should be the fastest,
and I converted the surface to the display format. I tried SDL_SetAlpha both
with and without SDL_RLEACCEL.

So I guess the point is to use software surfaces for alpha blending. But my
question is: when I use SDL_SetVideoMode with the flag SDL_SWSURFACE, does
it mean that every other surface I create is a software surface? If not,
how can I tell a newly created surface, for example an image I load with
IMG_Load, to be a software surface?

It would be great if you could help me, for I don’t really know what else to
do.

Bye,

Christian

These might help, you probably want the 2nd one:

http://sdldoc.csn.ul.ie/sdldisplayformat.php
http://sdldoc.csn.ul.ie/sdldisplayformatalpha.phpOn Sat, 2003-01-25 at 16:03, Christian Topnik wrote:

Hi everybody,

I am using SDL for about two month now, and it didn’t cause me any troubles
so far. Until now… :wink:

As soon as I am using alpha blending in my game, everything gets really
slow. Of course I read through all the articles of this mailinglist.

I am using a 50% transpareny (value 128) because this should be the fastest,
and I converted the surface to the display format. I tried SDL_SetAlpha both
with and without SDL_RLEACCEL.

These might help, you probably want the 2nd one:

http://sdldoc.csn.ul.ie/sdldisplayformat.php
http://sdldoc.csn.ul.ie/sdldisplayformatalpha.php

Thanks, but as I said, I already converted the surfaces to the display
format. This does not seem to be the error… I still think it’s about
software surface vs hardware surface, but I couldn’t find out how to handle
this.

Bye,

Christian

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
check out how it’s done. This example uses alpha surfaces to fade out a
picture. It’s simple and if it runs correctly on your system you can
eliminate SDL as the issue, and then examine the code to see what needs
to be done. If it doesn’t help then you may very well have a problem.

Hope this helps,
RobertOn Sat, 2003-01-25 at 16:03, Christian Topnik wrote:

Hi everybody,

I am using SDL for about two month now, and it didn’t cause me any troubles
so far. Until now… :wink:

As soon as I am using alpha blending in my game, everything gets really
slow. Of course I read through all the articles of this mailinglist.

I am using a 50% transpareny (value 128) because this should be the fastest,
and I converted the surface to the display format. I tried SDL_SetAlpha both
with and without SDL_RLEACCEL.

So I guess the point is to use software surfaces for alpha blending. But my
question is: when I use SDL_SetVideoMode with the flag SDL_SWSURFACE, does
it mean that every other surface I create is a software surface? If not,
how can I tell a newly created surface, for example an image I load with
IMG_Load, to be a software surface?

It would be great if you could help me, for I don’t really know what else to
do.

Bye,

Christian


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

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! :slight_smile:

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);
}

yeah, i’ve found the same problems… really really slow (even at low
resolutions) in fullscreen…

any workarounds or anything? shouldnt SDL_HWSURFACE work better than
SDL_SWSURFACE ??

m@_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

I’ve only been playing with SDL since the weekend, so forgive me if this doesn’t apply, but when playing around with randomly blitting a bitmap I had two extreme situations due to a single line of code. Mainly, doing SDL_UpdateRect(pSurface,0,0,0,0) after each image blt. I was doing a random transparency as well as a color key knocking out the background on a 24bit bmp. The thing chugged. Just getting into it, I was a little afraid at the performance and was thinking perhaps to just code in straight DirectX. However, by updating only the rect that was newly blt (64x64), the speed was amazing… roughly 10,000 blts in 6 secs. When setting the color key and transparency you can also try the SDL_RLEACCEL flag to help optimize and accelerate.

  • Joby> ----- Original Message -----

From: madprog@hotmail.com (Matt Monson)
Reply-To: sdl at libsdl.org
Date: Mon, 27 Jan 2003 17:30:47 +0000

yeah, i’ve found the same problems… really really slow (even at low
resolutions) in fullscreen…

any workarounds or anything? shouldnt SDL_HWSURFACE work better than
SDL_SWSURFACE ??

m@


Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Matt Monson wrote:

any workarounds or anything? shouldnt SDL_HWSURFACE work better than
SDL_SWSURFACE ??

No. This has been covered on the list before, so I suggest a Google
search (with site:libsdl.org as one of the parameters) might answer this
question far better than I could. But the short answer is that firstly,
hardware surfaces are slow when you’re reading data from them back to
the CPU, and secondly, ‘hardware surface’ rarely means ‘fully
accelerated’ because most hardware acceleration is reserved for 3D
operations.–
Kylotan
http://pages.eidosnet.co.uk/kylotan

Matt Monson wrote:

any workarounds or anything? shouldnt SDL_HWSURFACE work better than
SDL_SWSURFACE ??

This is in the FAQ:
http://www.libsdl.org/faq.php?action=listentries&category=2

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

setenv __GL_SYNC_TO_VBLANK=1, any chance you could add this to the FAQ.
It took a while to find that this was the environmental variable that
needed to be set, for NV to sync on VBLANK.

Thanks,
RobertOn Mon, 2003-01-27 at 15:15, Sam Lantinga wrote:

Matt Monson wrote:

any workarounds or anything? shouldnt SDL_HWSURFACE work better than
SDL_SWSURFACE ??

This is in the FAQ:
http://www.libsdl.org/faq.php?action=listentries&category=2

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl