首页 文章

SDL 1.2和SDL 2.0的兼容性

提问于
浏览
2

我想我可能遇到了SDL版本1.2和2.0的兼容性问题:当使用SDL_MapRGB和SDL_FillRect绘制到Surface时,SDL 2.0显然交换RGB红色和蓝色通道,而SDL 1.2则没有 . 以下C代码是一个演示问题的最小工作示例:

#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;
}

编译时

gcc $(sdl-config --cflags --libs) colourtest.c -o colourtest

(使用SDL 1.2头文件和库),代码生成(如我所料)以下位图文件:

rgb rectangles correct order

但是,编译时

gcc $(sdl2-config --cflags --libs) colourtest.c -o colourtest

(使用SDL 2.0),代码产生(意外)以下位图文件:

bgr, reversed order

我尝试改变(r,g,b)面具,但这没有任何改变 .

据我所知,文档,包括迁移指南,没有提到这一点,我无法在此问题上找到任何其他内容 . 这导致我假设这是一个错误或我没有正确使用这些功能 .

1 回答

  • 2

    嗯....有趣 . 不,SDL 2.0没有交换到bgr,它仍然是旧的RGB .

    这就是我要说的 . 将要发生的唯一原因是字节顺序被交换,因为SDL将rgb映射到您的机器字节顺序 . 也许由于某种原因,一个版本自动解决这个问题而另一个版本让你决定是否要使用你的机器的字节顺序(在这种情况下默认是小端或选择使用大端)?

    尝试使用变量来存储您的rbga值,然后使用此代码确保将颜色值分配给正确的位,无论您的计算机上的字节顺序如何:

    Uint32 red, greeb, blue, alpha
    
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    red = 0xff000000;
    green = 0x00ff0000;
    blue = 0x0000ff00;
    alpha = 0x000000ff;
    
    #else
    red = 0x000000ff;
    green = 0x0000ff00;
    blue = 0x00ff0000;
    alpha = 0xff000000;
    
    #endif
    

    我希望这对你有所帮助,或者至少给你一些东西 .

相关问题