Include transparency information for icon.png in the image data

This commit is contained in:
Cameron Cawley
2026-08-18 20:03:36 +01:00
committed by Sam Lantinga
parent e4c41d393b
commit 6f3d0c1337
25 changed files with 35 additions and 124 deletions

View File

@@ -188,7 +188,7 @@ static bool LoadSprite(const char *file)
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file, true);
sprites[i] = LoadTexture(state->renderers[i], file);
if (!sprites[i]) {
return false;
}

View File

@@ -1097,11 +1097,6 @@ static SDL_Surface *SDLTest_LoadIcon(const char *file)
return NULL;
}
if (icon->format == SDL_PIXELFORMAT_INDEX8) {
/* Set the colorkey */
SDL_SetSurfaceColorKey(icon, 1, *((Uint8 *)icon->pixels));
}
return icon;
}

View File

@@ -2,18 +2,19 @@ unsigned char icon_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
0x02, 0x03, 0x00, 0x00, 0x00, 0x0e, 0x14, 0x92, 0x67, 0x00, 0x00, 0x00,
0x09, 0x50, 0x4c, 0x54, 0x45, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
0xff, 0x00, 0x53, 0xed, 0x60, 0xc2, 0x00, 0x00, 0x00, 0x69, 0x49, 0x44,
0x41, 0x54, 0x18, 0xd3, 0x63, 0x60, 0xa0, 0x18, 0x84, 0x86, 0x42, 0x68,
0xd6, 0x55, 0xab, 0x02, 0xc0, 0x0c, 0xa9, 0x55, 0xab, 0x96, 0x80, 0x19,
0x59, 0xab, 0x56, 0xad, 0x04, 0xd1, 0x8c, 0xab, 0x80, 0xc0, 0x01, 0xc8,
0x60, 0x03, 0x31, 0x26, 0x80, 0x18, 0x4b, 0xa3, 0x96, 0x46, 0x81, 0x18,
0x52, 0x4b, 0x81, 0x70, 0x09, 0x98, 0x01, 0x14, 0x59, 0x02, 0xd1, 0x0d,
0xd1, 0x8f, 0xc4, 0x58, 0x99, 0x85, 0xce, 0x80, 0x4a, 0x21, 0x0c, 0x9c,
0xba, 0x6a, 0x55, 0x18, 0x88, 0xc1, 0xb8, 0x34, 0x34, 0x34, 0x0a, 0x64,
0x17, 0x43, 0x66, 0x68, 0x68, 0x26, 0xc4, 0x3d, 0x53, 0xc3, 0x96, 0xa0,
0xba, 0x10, 0xee, 0x66, 0x82, 0x00, 0x00, 0x0b, 0x4e, 0x32, 0x71, 0x69,
0x34, 0x3a, 0xd6, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
0x42, 0x60, 0x82
0x0c, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xff, 0x5a, 0x53, 0x1b, 0x29, 0x00, 0x00, 0x00,
0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8, 0x66, 0x00, 0x00,
0x00, 0x6a, 0x49, 0x44, 0x41, 0x54, 0x18, 0xd3, 0x63, 0x60, 0xa0, 0x18,
0xac, 0x5a, 0x05, 0xa1, 0xb9, 0x42, 0x43, 0x17, 0x80, 0x19, 0xaa, 0xa1,
0xa1, 0x11, 0x60, 0xc6, 0xd4, 0xd0, 0xd0, 0x30, 0x10, 0xcd, 0x14, 0x0a,
0x04, 0x0d, 0x40, 0x06, 0x27, 0x88, 0x91, 0x00, 0x62, 0x44, 0x2d, 0x8d,
0x5a, 0x0a, 0x62, 0xa8, 0x46, 0x3d, 0x8d, 0x7a, 0x1a, 0x01, 0x66, 0x00,
0x45, 0x22, 0x20, 0xba, 0x21, 0xfa, 0x91, 0x18, 0x61, 0x53, 0xd1, 0x19,
0x50, 0x29, 0x84, 0x81, 0x59, 0xa1, 0xa1, 0x2b, 0x41, 0x0c, 0xa6, 0xa8,
0x55, 0xab, 0x96, 0x82, 0xec, 0x62, 0x98, 0xb6, 0x6a, 0xd5, 0x34, 0x88,
0x7b, 0xb2, 0x56, 0x46, 0xa0, 0xba, 0x10, 0xee, 0x66, 0x82, 0x00, 0x00,
0xe2, 0x90, 0x27, 0xdc, 0x70, 0xc2, 0x01, 0x8a, 0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
unsigned int icon_png_len = 183;
unsigned int icon_png_len = 200;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 183 B

After

Width:  |  Height:  |  Size: 200 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 195 B

After

Width:  |  Height:  |  Size: 213 B

View File

@@ -718,7 +718,7 @@ static Texture *CreateTexture(const char *fname)
if (!tex) {
SDL_Log("Out of memory!");
} else {
tex->texture = LoadTexture(state->renderers[0], fname, true);
tex->texture = LoadTexture(state->renderers[0], fname);
if (!tex->texture) {
SDL_Log("Failed to load '%s': %s", fname, SDL_GetError());
SDL_free(tex);

View File

@@ -118,40 +118,9 @@ static const char *cross[] = {
"16,16"
};
static SDL_Surface *load_image_file(const char *file)
{
SDL_Surface *surface = SDL_LoadSurface(file);
if (surface) {
if (SDL_GetSurfacePalette(surface)) {
const Uint8 bpp = SDL_BITSPERPIXEL(surface->format);
const Uint8 mask = (1 << bpp) - 1;
if (SDL_PIXELORDER(surface->format) == SDL_BITMAPORDER_4321)
SDL_SetSurfaceColorKey(surface, 1, (*(Uint8 *)surface->pixels) & mask);
else
SDL_SetSurfaceColorKey(surface, 1, ((*(Uint8 *)surface->pixels) >> (8 - bpp)) & mask);
} else {
switch (SDL_BITSPERPIXEL(surface->format)) {
case 15:
SDL_SetSurfaceColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
break;
case 16:
SDL_SetSurfaceColorKey(surface, 1, *(Uint16 *)surface->pixels);
break;
case 24:
SDL_SetSurfaceColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
break;
case 32:
SDL_SetSurfaceColorKey(surface, 1, *(Uint32 *)surface->pixels);
break;
}
}
}
return surface;
}
static SDL_Surface *load_image(const char *file)
{
SDL_Surface *surface = load_image_file(file);
SDL_Surface *surface = SDL_LoadSurface(file);
if (surface) {
/* Add a 2x version of this image, if available */
SDL_Surface *surface2x = NULL;
@@ -166,7 +135,7 @@ static SDL_Surface *load_image(const char *file)
} else {
SDL_strlcat(file2x, "2x", len);
}
surface2x = load_image_file(file2x);
surface2x = SDL_LoadSurface(file2x);
SDL_free(file2x);
}
if (surface2x) {

View File

@@ -236,9 +236,6 @@ static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
/* Treat white as transparent */
SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;

View File

@@ -52,7 +52,7 @@ static int LoadSprite(const char *file)
for (i = 0; i < state->num_windows; ++i) {
/* This does the SDL_LoadPNG step repeatedly, but that's OK for test code. */
sprites[i] = LoadTexture(state->renderers[i], file, true);
sprites[i] = LoadTexture(state->renderers[i], file);
if (!sprites[i]) {
return -1;
}

View File

@@ -126,9 +126,6 @@ static SDL_Texture *CreateSpriteTexture(SDL_Renderer *renderer, unsigned char *d
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
/* Treat white as transparent */
SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
}

View File

@@ -263,13 +263,13 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
return SDL_APP_FAILURE;
}
background = LoadTexture(renderer, "sample.png", false);
background = LoadTexture(renderer, "sample.png");
if (!background) {
SDL_Log("Couldn't create background: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
sprite = LoadTexture(renderer, "icon.png", true);
sprite = LoadTexture(renderer, "icon.png");
if (!sprite) {
SDL_Log("Couldn't create sprite: %s", SDL_GetError());
return SDL_APP_FAILURE;

View File

@@ -57,7 +57,7 @@ static GlyphInfo glyphs[128];
static bool LoadFontTexture(void)
{
font_texture = LoadTexture(renderer, "msdf_font.png", false);
font_texture = LoadTexture(renderer, "msdf_font.png");
if (!font_texture) {
SDL_Log("Failed to create font texture: %s", SDL_GetError());
return false;

View File

@@ -1018,7 +1018,7 @@ int main(int argc, char *argv[])
ctx->window = window;
ctx->renderer = renderer;
ctx->rendererID = i;
ctx->settings_icon = LoadTexture(renderer, "icon.png", true);
ctx->settings_icon = LoadTexture(renderer, "icon.png");
if (ctx->settings_icon) {
ctx->settings_rect.w = (float)ctx->settings_icon->w;
ctx->settings_rect.h = (float)ctx->settings_icon->h;

View File

@@ -168,7 +168,7 @@ int main(int argc, char *argv[])
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
sprite = LoadTexture(renderer, "icon.png", true);
sprite = LoadTexture(renderer, "icon.png");
if (!sprite) {
quit(6);
}

View File

@@ -135,8 +135,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png", true);
drawstate->background = LoadTexture(drawstate->renderer, "sample.png", false);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png");
drawstate->background = LoadTexture(drawstate->renderer, "sample.png");
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View File

@@ -252,8 +252,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png", true);
drawstate->background = LoadTexture(drawstate->renderer, "sample.png", false);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png");
drawstate->background = LoadTexture(drawstate->renderer, "sample.png");
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View File

@@ -153,8 +153,8 @@ int main(int argc, char *argv[])
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png", true);
drawstate->background = LoadTexture(drawstate->renderer, "sample.png", false);
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png");
drawstate->background = LoadTexture(drawstate->renderer, "sample.png");
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}

View File

@@ -91,14 +91,6 @@ int main(int argc, char *argv[])
goto quit;
}
if (!SDL_ISPIXELFORMAT_ALPHA(shape->format)) {
/* Set the colorkey to the top-left pixel */
Uint8 r, g, b, a;
SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a);
SDL_SetSurfaceColorKey(shape, 1, SDL_MapSurfaceRGBA(shape, r, g, b, a));
}
if (!resizable) {
/* Set the hit test callback so we can drag the window */
if (!SDL_SetWindowHitTest(window, ShapeHitTest, shape)) {

View File

@@ -60,7 +60,7 @@ static bool LoadSprite(const char *file)
if (sprites[i]) {
SDL_DestroyTexture(sprites[i]);
}
sprites[i] = LoadTexture(state->renderers[i], file, true);
sprites[i] = LoadTexture(state->renderers[i], file);
if (sprites[i]) {
sprite_w = (float)sprites[i]->w;
sprite_h = (float)sprites[i]->h;

View File

@@ -41,9 +41,6 @@ static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
/* Treat white as transparent */
SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;

View File

@@ -40,9 +40,6 @@ static SDL_Surface *CreateSurface(unsigned char *data, unsigned int len, int *w,
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
/* Treat white as transparent */
SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
*w = surface->w;
*h = surface->h;
}

View File

@@ -123,13 +123,11 @@ char *GetResourceFilename(const char *user_specified, const char *def)
* Load the .png file whose name is file, from the SDL_GetBasePath() if
* possible or the current working directory if not.
*
* If transparent is true, set the transparent colour from the top left pixel.
*
* If width_out is non-NULL, set it to the texture width.
*
* If height_out is non-NULL, set it to the texture height.
*/
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent)
SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file)
{
SDL_Surface *temp = NULL;
SDL_Texture *texture = NULL;
@@ -145,35 +143,6 @@ SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transpar
if (!temp) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
} else {
/* Set transparent pixel as the pixel at (0,0) */
if (transparent) {
if (SDL_GetSurfacePalette(temp)) {
const Uint8 bpp = SDL_BITSPERPIXEL(temp->format);
const Uint8 mask = (1 << bpp) - 1;
if (SDL_PIXELORDER(temp->format) == SDL_BITMAPORDER_4321)
SDL_SetSurfaceColorKey(temp, true, (*(Uint8 *)temp->pixels) & mask);
else
SDL_SetSurfaceColorKey(temp, true, ((*(Uint8 *)temp->pixels) >> (8 - bpp)) & mask);
} else {
switch (SDL_BITSPERPIXEL(temp->format)) {
case 15:
SDL_SetSurfaceColorKey(temp, true,
(*(Uint16 *)temp->pixels) & 0x00007FFF);
break;
case 16:
SDL_SetSurfaceColorKey(temp, true, *(Uint16 *)temp->pixels);
break;
case 24:
SDL_SetSurfaceColorKey(temp, true,
(*(Uint32 *)temp->pixels) & 0x00FFFFFF);
break;
case 32:
SDL_SetSurfaceColorKey(temp, true, *(Uint32 *)temp->pixels);
break;
}
}
}
texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s", SDL_GetError());

View File

@@ -20,7 +20,7 @@
extern "C" {
#endif
extern SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, bool transparent);
extern SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file);
extern char *GetNearbyFilename(const char *file);
extern char *GetResourceFilename(const char *user_specified, const char *def);

View File

@@ -183,7 +183,7 @@ int main(int argc, char *argv[])
quit(2);
}
sprite = LoadTexture(state->renderers[0], "icon.png", true);
sprite = LoadTexture(state->renderers[0], "icon.png");
if (!sprite) {
quit(2);
}

View File

@@ -38,9 +38,6 @@ static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned
if (src) {
surface = SDL_LoadPNG_IO(src, true);
if (surface) {
/* Treat white as transparent */
SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
texture = SDL_CreateTextureFromSurface(r, surface);
*w = surface->w;
*h = surface->h;