Last active
August 29, 2015 14:20
-
-
Save ejrh/b13d483aac59dfb2a721 to your computer and use it in GitHub Desktop.
Test case for a potention SDL2 bug when converting surfaces with colour keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TO COMPILE: gcc -o test colour_key_conversion_test.c -lSDL2 | |
// AND RUN: ./test | |
#include <stdio.h> | |
#include <SDL2/SDL.h> | |
// smiley face image with transparency | |
unsigned short pixel_data[] = { | |
0xB54A, 0xB54A, 0xB54A, 0xB54A, 0xB54A, | |
0xB54A, 0x0000, 0xB54A, 0x0000, 0xB54A, | |
0xB54A, 0xB54A, 0xB54A, 0xB54A, 0xB54A, | |
0x0000, 0xB54A, 0xB54A, 0xB54A, 0x0000, | |
0xB54A, 0x0000, 0x0000, 0x0000, 0xB54A | |
}; | |
int main(int argc, char *argv[]) { | |
int w = 5, h = 5; | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window *window = SDL_CreateWindow("Color key conversion test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 100, 100, 0); | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); | |
SDL_RenderSetScale(renderer, 8, 8); | |
// create green background surface | |
SDL_Surface *background = SDL_CreateRGBSurface(0, 12, 12, 32, 0,0,0,0); | |
SDL_Rect bg_rect = { 0, 0, 12, 12 }; | |
SDL_FillRect(background, &bg_rect, SDL_MapRGB(background->format, 100,200,100)); | |
// create 16-bit surface | |
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(pixel_data, w, h, 16, w*2, 0x0000f800,0x000007e0,0x0000001f,0); | |
SDL_SetColorKey(surface, SDL_TRUE, 0xB54A); | |
// blit original surface to left hand side of background | |
SDL_Rect tgt_rect = { 0, 0, 5, 5 }; | |
SDL_BlitSurface(surface, NULL, background, &tgt_rect); | |
// convert surface to 32-bit (suitable for window) | |
Uint32 pf = SDL_GetWindowPixelFormat(window); | |
SDL_Surface *new_surface = SDL_ConvertSurfaceFormat(surface, pf, 0); | |
// blit converted surface to right hand side | |
SDL_Rect tgt_rect2 = { 6, 0, 5, 5 }; | |
SDL_BlitSurface(new_surface, NULL, background, &tgt_rect2); | |
// show everything | |
SDL_Texture *bg_tex = SDL_CreateTextureFromSurface(renderer, background); | |
SDL_RenderCopy(renderer, bg_tex, NULL, &bg_rect); | |
SDL_RenderPresent(renderer); | |
// print value of first pixel | |
Uint32 first_pixel = ((Uint32 *) new_surface->pixels)[0]; | |
Uint8 r,g,b,a; | |
SDL_GetRGBA(first_pixel, new_surface->format, &r,&g,&b,&a); | |
printf("First pixel = %08x (%d,%d,%d alpha %d)\n", first_pixel, r,g,b,a); | |
// print value of colour key | |
Uint32 colour_key; | |
SDL_GetColorKey(new_surface, &colour_key); | |
SDL_GetRGB(colour_key, new_surface->format, &r,&g,&b); | |
printf("colour key = %08x (%d,%d,%d)\n", colour_key, r,g,b); | |
int waiting = 1; | |
while (waiting) { | |
SDL_Event evt; | |
SDL_WaitEvent(&evt); | |
if (evt.type == SDL_QUIT) | |
waiting = 0; | |
} | |
SDL_FreeSurface(new_surface); | |
SDL_FreeSurface(surface); | |
SDL_DestroyTexture(bg_tex); | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment