SDL_BlitSurface on a different format surface

Hello all,

I have find a problem in a code I did and even I have solved it, I would
like to know what do you think about it.

Ok, I let a file reproducing the same problem attached so you can follow
me (the latter code doesn’t work until
the changed suggested in this e-mail are done).

In line 58 we have:
SDL_Surface *temp =
SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCALPHA,
w, h, 24, rmask, gmask, bmask, amask );

If we change it for the next:
SDL_Surface *temp =
SDL_DisplayFormatAlpha( SDL_HWSURFACE | SDL_SRCALPHA,
w, h, 24, rmask, gmask, bmask, amask );

it stills doesn’t work, but if you use SDL_DisplayFormat instead of
SDL_DisplayFormatAlpha it works perfectly.

It seams rare to me, that if the source surface has been formatted with
SDL_DisplayFormatAlpha, the destiny
surface needs to be formatted with SDL_DisplayFormat.

BTW, SDL_BlitSurface doesn’t complain about the fact that you are
blitting surfaces with different format.

I use Ubuntu 10.10 and GCC version 4.4.3.

I hope you understood my perfect English.

Thanks.

Zaka.

-------------- next part --------------
A non-text attachment was scrubbed…
Name: pru_sdl.cpp
Type: text/x-c++src
Size: 2278 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110208/ff04e371/attachment.cpp

It’s not totally clear what you’re asking, but there are a couple of
things…

You need to declare main() as:
int main(int argc, char* argv[])

You should set loadedSurf to NULL after you free it, just for safety.

And perhaps the most important: your masks are 32-bit, but you’re creating
24-bit surfaces.

Jonny DOn Tue, Feb 8, 2011 at 1:53 PM, Zakariae El-Abdelouarti Alouaret < shanatorio at gmail.com> wrote:

Hello all,

I have find a problem in a code I did and even I have solved it, I would
like to know what do you think about it.

Ok, I let a file reproducing the same problem attached so you can follow me
(the latter code doesn’t work until
the changed suggested in this e-mail are done).

In line 58 we have:
SDL_Surface *temp =
SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCALPHA,
w, h, 24, rmask, gmask, bmask, amask );

If we change it for the next:
SDL_Surface *temp =
SDL_DisplayFormatAlpha( SDL_HWSURFACE | SDL_SRCALPHA,
w, h, 24, rmask, gmask, bmask, amask );

it stills doesn’t work, but if you use SDL_DisplayFormat instead of
SDL_DisplayFormatAlpha it works perfectly.

It seams rare to me, that if the source surface has been formatted with
SDL_DisplayFormatAlpha, the destiny
surface needs to be formatted with SDL_DisplayFormat.

BTW, SDL_BlitSurface doesn’t complain about the fact that you are blitting
surfaces with different format.

I use Ubuntu 10.10 and GCC version 4.4.3.

I hope you understood my perfect English.

Thanks.

Zaka.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL_DisplayFormatAlpha() creates a copy of an existing surface in the
current display format. You would typically use it to speed up blits
of a surface loaded from a file.

Like this:

SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE |
SDL_ANYFORMAT | SDL_FULLSCREEN);

SDL_Surface* tmp = SDL_LoadBMP(“mybitmap.bmp”);
SDL_Surface* bitmap = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);

SDL_Rect rect = { (800 - bitmap->w) / 2, (600 - bitmap->h) / 2,
bitmap->w, bitmap->h };

SDL_BlitSurface(bitmap, 0, screen, &rect);

For more information on these functions, look here (SDL 1.2 wiki):
http://www.libsdl.org/cgi/docwiki.cgi/SDL_API_by_nameOn 8 February 2011 13:53, Zakariae El-Abdelouarti Alouaret wrote:

? ?SDL_Surface *temp =
? ? ? ?SDL_DisplayFormatAlpha( SDL_HWSURFACE | SDL_SRCALPHA,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?w, h, 24, rmask, gmask, bmask, amask );

Hello Jonny D,

I have followed all your suggestions but the problem persists:

I have let the code that doesn’t draw the image in the screen and
commented out the working solution.

Basically, when I use SDL_DisplayFormat instead of
SDL_DisplayFormatAlpha it works, why is this that way?

Thanks for your help.

Zaka.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: pru_sdl.cpp
Type: text/x-c++src
Size: 2483 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110209/06d0fdc0/attachment.cpp

SDL_DisplayFormat() removes the alpha channel. What you’re probably
experiencing is that you’re blitting to a transparent surface. The
transparent surface will stay transparent. Then when you blit that to the
screen, you get nothing.

Try using this before you blit optimizedSurf to temp:
SDL_SetAlpha(optimizedSurf, 0, 0);

That will disable alpha blending on the optimizedSurf to temp blit. Instead
of blending and staying transparent, it will copy the pixels over. Then you
should be able to blit to the screen from temp.

Here’s something I wrote about this some time ago:
http://code.bluedinosaurs.com/tutorials/blending.html

Jonny DOn Tue, Feb 8, 2011 at 6:42 PM, Zakariae El-Abdelouarti Alouaret < shanatorio at gmail.com> wrote:

Hello Jonny D,

I have followed all your suggestions but the problem persists:

I have let the code that doesn’t draw the image in the screen and commented
out the working solution.

Basically, when I use SDL_DisplayFormat instead of SDL_DisplayFormatAlpha
it works, why is this that way?

Thanks for your help.

Zaka.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org