Any idea why colorkey isn't working here?

I wrote up this simple C program to test basic SDL functionality.
Everything works, with one minor problem. The colorkey
doesn’t get converted. I’m loading an 8-bit PNG sprite file
where palette index #0 is the background color. I’d expect to
see only the sprites displayed, but what I get is the entire
image, including the background.

This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.
//

#include “stdafx.h”
#include “sdl.h”
#include “sdl_image.h”
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int windowID;
int textureID;
SDL_Surface* surface;
char* dummy = “”;
SDL_Color color;

SDL_Init(SDL_INIT_VIDEO);

windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (windowID == 0)
{
    printf("Unable to create window: %s\n", SDL_GetError());
}
else printf("Window created: %d\n", windowID);

if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
{
    printf("Unable to create renderer: %s\n", SDL_GetError());
}
else printf("Renderer created successfully.\n");

if (SDL_SelectRenderer(windowID) != 0)
{
    printf("Unable to select renderer: %s\n", SDL_GetError());
}
else printf("Renderer selected successfully\n");

SDL_RenderPresent();

surface = IMG_Load("<INSERT FILENAME HERE>");
if (!surface)
{
    printf("Unable to load image!\n");
}
else printf("Image Loaded\n");

color = surface->format->palette->colors[0];
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

textureID = SDL_CreateTextureFromSurface(0, surface);
if (textureID == 0)
{
    printf("Unable to create texture: %s\n", SDL_GetError());
}
else printf("Texture created: %d\n", textureID);

SDL_FreeSurface(surface);

if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
{
    printf("Unable to render texture: %s", SDL_GetError());
}

SDL_RenderPresent();

scanf_s("%s", dummy);
return 0;

}

Anyone know anything about why this isn’t working? This is proving to be
a critical problem, since I can’t draw sprites without a colorkey to make
the mask work. Is there an API I’m missing, or a workaround for it?

I wrote up this simple C program to test basic SDL functionality.
Everything works, with one minor problem. The colorkey
doesn’t get converted. I’m loading an 8-bit PNG sprite file
where palette index #0 is the background color. I’d expect to
see only the sprites displayed, but what I get is the entire
image, including the background.

This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.
//

#include “stdafx.h”
#include “sdl.h”
#include “sdl_image.h”
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int windowID;
int textureID;
SDL_Surface* surface;
char* dummy = “”;
SDL_Color color;

SDL_Init(SDL_INIT_VIDEO);

windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (windowID == 0)
{
    printf("Unable to create window: %s\n", SDL_GetError());
}
else printf("Window created: %d\n", windowID);

if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
{
    printf("Unable to create renderer: %s\n", SDL_GetError());
}
else printf("Renderer created successfully.\n");

if (SDL_SelectRenderer(windowID) != 0)
{
    printf("Unable to select renderer: %s\n", SDL_GetError());
}
else printf("Renderer selected successfully\n");

SDL_RenderPresent();

surface = IMG_Load("<INSERT FILENAME HERE>");
if (!surface)
{
    printf("Unable to load image!\n");
}
else printf("Image Loaded\n");

color = surface->format->palette->colors[0];
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

textureID = SDL_CreateTextureFromSurface(0, surface);
if (textureID == 0)
{
    printf("Unable to create texture: %s\n", SDL_GetError());
}
else printf("Texture created: %d\n", textureID);

SDL_FreeSurface(surface);

if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
{
    printf("Unable to render texture: %s", SDL_GetError());
}

SDL_RenderPresent();

scanf_s("%s", dummy);
return 0;

}From: @Mason_Wheeler (Mason Wheeler)
Subject: [SDL] Any idea why colorkey isn’t working here?

i have had a few problems with different depth pngs, i think the png format
does strange things to the palette below full color ones… i find its best
if you want to use 8 bit to use gif or xpm or some other image format that
is designed for low colors.

2009/7/8 Mason Wheeler > Anyone know anything about why this isn’t working? This is proving to be

a critical problem, since I can’t draw sprites without a colorkey to make
the mask work. Is there an API I’m missing, or a workaround for it?

From: Mason Wheeler
**Subject: [SDL] Any idea why colorkey isn’t working here?

I wrote up this simple C program to test basic SDL functionality.
Everything works, with one minor problem. The colorkey
doesn’t get converted. I’m loading an 8-bit PNG sprite file
where palette index #0 is the background color. I’d expect to
see only the sprites displayed, but what I get is the entire
image, including the background.

This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.
//

#include “stdafx.h”
#include “sdl.h”
#include “sdl_image.h”
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int windowID;
int textureID;
SDL_Surface* surface;
char* dummy = “”;
SDL_Color color;

SDL_Init(SDL_INIT_VIDEO);

windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240,

SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (windowID == 0)
{
printf(“Unable to create window: %s\n”, SDL_GetError());
}
else printf(“Window created: %d\n”, windowID);

if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
{
    printf("Unable to create renderer: %s\n", SDL_GetError());
}
else printf("Renderer created successfully.\n");

if (SDL_SelectRenderer(windowID) != 0)
{
    printf("Unable to select renderer: %s\n", SDL_GetError());
}
else printf("Renderer selected successfully\n");

SDL_RenderPresent();

surface = IMG_Load("<INSERT FILENAME HERE>");
if (!surface)
{
    printf("Unable to load image!\n");
}
else printf("Image Loaded\n");

color = surface->format->palette->colors[0];
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r,

color.g, color.b));

textureID = SDL_CreateTextureFromSurface(0, surface);
if (textureID == 0)
{
    printf("Unable to create texture: %s\n", SDL_GetError());
}
else printf("Texture created: %d\n", textureID);

SDL_FreeSurface(surface);

if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
{
    printf("Unable to render texture: %s", SDL_GetError());
}

SDL_RenderPresent();

scanf_s("%s", dummy);
return 0;

}
checked for viruses and spam by CanIt.
http://www.canit4.3d.net.uk/


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

That doesn’t seem to be the problem here. All the colors are showing up just fine,
including the one that’s supposed to be the background color. The problem is that
the colorkey transparency isn’t getting applied. Or do you have a GIF or XPM
sprite sheet that will make this program work right?

i have had a few problems with different depth pngs, i think the png format
does strange things to the palette below full color ones… i find its best if
you want to use 8 bit to use gif or xpm or some other image format that is
designed for low colors.

2009/7/8 Mason Wheeler <@Mason_Wheeler>

Anyone know anything about why this isn’t working? This is proving to beFrom: neil@cloudsprinter.com (Neil White)
Subject: Re: [SDL] Any idea why colorkey isn’t working here?

a critical problem, since I can’t draw sprites without a colorkey to make

the mask work. Is there an API I’m missing, or a workaround for it?

From: Mason Wheeler <@Mason_Wheeler>
Subject: [SDL] Any idea why colorkey isn’t working here?

I wrote up this simple C program to test basic SDL functionality.
Everything works, with one minor problem. The colorkey
doesn’t get converted. I’m loading an 8-bit PNG sprite file

where palette index #0 is the background color. I’d expect to
see only the sprites displayed, but what I get is the entire
image, including the background.

This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.

//

#include “stdafx.h”
#include “sdl.h”
#include “sdl_image.h”
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int windowID;
int textureID;

SDL_Surface* surface;
char* dummy =
"";
SDL_Color color;

SDL_Init(SDL_INIT_VIDEO);

windowID = SDL_CreateWindow(“SDL Test”, 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (windowID == 0)

{
printf(“Unable to create window: %s\n”, SDL_GetError());
}
else printf(“Window created: %d\n”, windowID);

if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)

{
printf(“Unable to create renderer: %s\n”, SDL_GetError());
}
else printf(“Renderer created successfully.\n”);

if (SDL_SelectRenderer(windowID) != 0)
{

   printf("Unable to select renderer: %s\n",

SDL_GetError());
}
else printf(“Renderer selected successfully\n”);

SDL_RenderPresent();

surface = IMG_Load("");
if (!surface)

{
printf(“Unable to load image!\n”);
}
else printf(“Image Loaded\n”);

color = surface->format->palette->colors[0];
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

textureID = SDL_CreateTextureFromSurface(0, surface);
if (textureID == 0)
{
printf(“Unable to create texture: %s\n”, SDL_GetError());
}
else printf(“Texture created: %d\n”,
textureID);

SDL_FreeSurface(surface);

if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
{
printf(“Unable to render texture: %s”, SDL_GetError());
}

SDL_RenderPresent();

scanf_s("%s", dummy);
return 0;
}

checked for viruses and spam by CanIt.
http://www.canit4.3d.net.uk/


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

I’ve been poking away at this, since no one else seems willing to, and
traced down the root of the problem, at least partially.

When I call SDL_CreateTextureFromSurface(), it calls SDL_ConvertSurface(),
and partway down, SDL_ConvertSurface() contains the following:
/* Save the original copy flags */
copy_flags = surface->map->info.flags;
surface->map->info.flags = 0;

The colorkey isn’t getting converted properly because the SDL_COPY_COLORKEY
flag in surface->map->info->flags is getting zeroed. But commenting that
line out doesn’t fix it. What that does instead is gives me a texture
where the color key is solid black, instead of transparent.

Can anyone a bit more familiar with low-level SDL internals take a look at
things from here? This is a serious, debilitating glitch and I don’t have
the domain knowledge to analyze it beyond this point.

Anyone know anything about why this isn’t working? This is proving to be
a critical problem, since I can’t draw sprites without a colorkey to make
the mask work. Is there an API I’m missing, or a workaround for it?

I wrote up this simple C program to test basic SDL functionality.
Everything works, with one minor problem. The colorkey
doesn’t get converted. I’m loading an 8-bit PNG sprite file
where palette index #0 is the background color. I’d expect to
see only the sprites displayed, but what I get is the entire
image, including the background.

This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.
//

#include “stdafx.h”
#include “sdl.h”
#include “sdl_image.h”
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int windowID;
int textureID;
SDL_Surface* surface;
char* dummy = “”;
SDL_Color color;

SDL_Init(SDL_INIT_VIDEO);

windowID = SDL_CreateWindow(“SDL Test”, 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (windowID == 0)
{
printf(“Unable to create window: %s\n”, SDL_GetError());
}
else printf(“Window created: %d\n”, windowID);

if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
{
printf(“Unable to create renderer: %s\n”, SDL_GetError());
}
else printf(“Renderer created successfully.\n”);

if (SDL_SelectRenderer(windowID) != 0)
{
printf(“Unable to select renderer: %s\n”, SDL_GetError());
}
else printf(“Renderer selected successfully\n”);

SDL_RenderPresent();

surface = IMG_Load("");
if (!surface)
{
printf(“Unable to load image!\n”);
}
else printf(“Image Loaded\n”);

color = surface->format->palette->colors[0];
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

textureID = SDL_CreateTextureFromSurface(0, surface);
if (textureID == 0)
{
printf(“Unable to create texture: %s\n”, SDL_GetError());
}
else printf(“Texture created: %d\n”, textureID);

SDL_FreeSurface(surface);

if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
{
printf(“Unable to render texture: %s”, SDL_GetError());
}

SDL_RenderPresent();

scanf_s("%s", dummy);
return 0;
}From: @Mason_Wheeler (Mason Wheeler)
Subject: [SDL] Any idea why colorkey isn’t working here?