【问题标题】:SDL 1.2 and SDL 2.0 compatibiltySDL 1.2 和 SSL 2.0 兼容性
【发布时间】:2014-04-03 12:36:05
【问题描述】:

我想我可能遇到了与 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 头文件和库),代码生成(如我所料)以下位图文件:

但是,当用

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

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

我尝试更改 (r,g,b) 掩码,但没有任何改变。

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

【问题讨论】:

  • 这里有问题吗?如果是“我该怎么办?”我建议一个错误报告。

标签: sdl


【解决方案1】:

嗯....有趣。不,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

我希望这能有所帮助,或者至少能让你有所收获。

【讨论】:

  • 您接受的答案显示了它应该如何正确完成,但 OP 是正确的,这不是 SDL 以前的行为方式。
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 2015-02-06
  • 2016-09-13
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多