I think I may have encountered a compatibilty problem with SDL versions 1.2 and 2.0: When using SDL_MapRGB and SDL_FillRect to draw to a Surface, SDL 2.0 apparently swaps the RGB red and blue channels, while SDL 1.2 does not. The following C code is a minimal working example which demonstrates the problem:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int main(void)
{
const unsigned height = 16;
const unsigned widthpercolour = 16;
SDL_Surface *surface;
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = widthpercolour;
rect.h = height;
if (SDL_Init(0) != 0) {
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
surface = SDL_CreateRGBSurface(0, 3 * widthpercolour, height, 24, 0x0000ff, 0x00ff00, 0xff0000, 0);
if (surface == NULL) {
fprintf(stderr, "Could not create SDL Surface: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_FillRect(surface, NULL, 0);
SDL_FillRect(surface, &rect, SDL_MapRGB(surface->format, 255, 0, 0));
rect.x += widthpercolour;
SDL_FillRect(surface, &rect, SDL_MapRGB(surface->format, 0, 255, 0));
rect.x += widthpercolour;
SDL_FillRect(surface, &rect, SDL_MapRGB(surface->format, 0, 0, 255));
if (SDL_SaveBMP(surface, "colourtest.bmp") != 0) {
SDL_FreeSurface(surface);
SDL_Quit();
fprintf(stderr, "Could not save SDL Surface: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_FreeSurface(surface);
SDL_Quit();
return EXIT_SUCCESS;
}
When compiled with
gcc $(sdl-config --cflags --libs) colourtest.c -o colourtest
(which uses SDL 1.2 headers and libraries), the code produces (as I expected) the following bitmap file:
However, when compiled with
gcc $(sdl2-config --cflags --libs) colourtest.c -o colourtest
(which uses SDL 2.0), the code produces (unexpectedly) the following bitmap file:
I tried changing the (r,g,b) masks, but that changes nothing.
As far as I can tell, the documentation, including the migration guide, mentions none of this and I was unable to find anything else on the matter. This leads me to assume that either this is a bug or I am not using the functions correctly.