The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Colorkey in SDL 1.3
T-1000


Joined: 16 Dec 2010
Posts: 12
Location: Spain
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!
T-1000


Joined: 16 Dec 2010
Posts: 12
Location: Spain
please, i need help on this... nobody knows how to solve it?
Colorkey in SDL 1.3
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
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:
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




--
    -Sam Lantinga, Founder and President, Galaxy Gameworks LLC
Colorkey in SDL 1.3
T-1000


Joined: 16 Dec 2010
Posts: 12
Location: Spain
Hi there.
Attached is a mini sample program where i have the problem.
There are 3 files, the code itself, the sprite and the compile script.

regards!

El 04/01/11 08:11, Sam Lantinga escribió:
Quote:
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:
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




--
-Sam Lantinga, Founder and President, Galaxy Gameworks LLC
Quote:


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
T-1000


Joined: 16 Dec 2010
Posts: 12
Location: Spain
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
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
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.
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
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.
Colorkey in SDL 1.3
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
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:
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




--
    -Sam Lantinga, Founder and President, Galaxy Gameworks LLC