Colorkey in SDL 1.3

Hi there.
I am trying to port a project from SDL 1.2 to SDL 1.3. Everything is going well except when i try to SDL_CreateTextureFromSurface with a colorkey applied to the surface. The texture is created well but no transparency, the background color is also displayed.
I am using SDL_Image to load the surfaces.
How can i make the textures transparent, and is it possible to use per pixel alpha loading a .png file?

thanks!

please, i need help on this… nobody knows how to solve it?

This should work. Can you post a link to a sample program and images?On Fri, Dec 17, 2010 at 6:01 AM, T-1000 wrote:

Hi there.
I am trying to port a project from SDL 1.2 to SDL 1.3. Everything is going
well except when i try to SDL_CreateTextureFromSurface with a colorkey
applied to the surface. The texture is created well but no transparency, the
background color is also displayed.
I am using SDL_Image to load the surfaces.
How can i make the textures transparent, and is it possible to use per
pixel alpha loading a .png file?

thanks!


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


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

An embedded and charset-unspecified text was scrubbed…
Name: compile
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110104/0d36f7f5/attachment.txt
-------------- next part --------------
A non-text attachment was scrubbed…
Name: dragon.cpp
Type: text/x-c++src
Size: 1703 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110104/0d36f7f5/attachment.cpp
-------------- next part --------------
A non-text attachment was scrubbed…
Name: sprite.png
Type: image/png
Size: 3584 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110104/0d36f7f5/attachment.png

mmm the atachments are in te email to the mailing list… here’s the code:

Code:

#include<stdio.h>
#include<stdlib.h>
#include<SDL/SDL.h>
#include<SDL/SDL_image.h>
#include<SDL/SDL_mixer.h>

#define screen_w 800
#define screen_h 480
#define screen_bpp 16
SDL_Window *window;

int main (int argc, char * argv[])
{
/* INIT STARTS*/
if((SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0))
{
fprintf(stderr,“Could not Initialize SDL.\nError: %s\n”, SDL_GetError());
exit (-1);
}
if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)
fprintf(stderr,“Warning: Couldn’t set 44100 Hz 16-bit audio\n: %s\n”, SDL_GetError());

window = SDL_CreateWindow("Dragon Memory", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_w, screen_h, (SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL));
    fprintf(stderr,"Warning: Couldn't set 44100 Hz 16-bit audio\n: %s\n", SDL_GetError());

if(!window) exit(2);

if (SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
printf("Unable to create renderer: %s\n", SDL_GetError());
SDL_SelectRenderer(window);
SDL_ShowCursor(1);

/* INIT ENDS*/

SDL_Surface *Ssprite;
SDL_Texture *Tsprite;

Ssprite = IMG_Load("sprite.png");
SDL_Rect RspriteSrc;
RspriteSrc.x = 0;
RspriteSrc.y = 0;
RspriteSrc.w = Ssprite->w;
RspriteSrc.h = Ssprite->h;

SDL_Color color;
SDL_GetRGB(*(Uint32 *)Ssprite->pixels, Ssprite->format, &color.r, &color.g, &color.b);
SDL_SetColorKey(Ssprite, SDL_SRCCOLORKEY, SDL_MapRGB(Ssprite->format, color.r, color.g, color.b));
Tsprite = SDL_CreateTextureFromSurface(0, Ssprite);


SDL_RenderCopy(Tsprite, &RspriteSrc, NULL);
SDL_RenderPresent();
SDL_Delay(2000);

SDL_Quit();
fprintf(stderr,"\nBye bye.\n");
return 0;

}

i compile it with

Code:

g++ -c -g dragon.cpp
g++ -o dragon $(sdl-config --libs --cflags) -lSDL_image -lSDL_mixer dragon.o

and the sprite uses the firts pixel color as a colorkey

I’ve also experienced this issue.

My workaround was to blit to a temporary RGBA surface with the same dimensions and all pixels set to 0 alpha, then create the texture from that. This works, but is not ideal.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

The flaw appears to be in how SDL_ConvertSurface is called by SDL_CreateTextureFromSurface.

To convert with the color key, SDL_COPY_COLORKEY needs to be passed for flags. Not a tested bugfix, just something apparent from the code.------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

Hahah, I ran into this bug today and fixed it independently.

Try the latest snapshot:
http://www.libsdl.org/tmp/SDL-1.3.zip

See ya!On Fri, Dec 17, 2010 at 6:01 AM, T-1000 wrote:

Hi there.
I am trying to port a project from SDL 1.2 to SDL 1.3. Everything is going
well except when i try to SDL_CreateTextureFromSurface with a colorkey
applied to the surface. The texture is created well but no transparency, the
background color is also displayed.
I am using SDL_Image to load the surfaces.
How can i make the textures transparent, and is it possible to use per
pixel alpha loading a .png file?

thanks!


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


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC