无法使用SDL 2.0获得8位调色板输出

时间:2016-02-15 19:53:52

标签: sdl sdl-2

这是我的代码,在OSX 10.11.4上使用SDL 2.0.4:

SDL_Surface *output_surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);
SDL_Texture *output_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, width, height);

SDL_Color c[256];

// Setting each color to red as a test.
for(u8 i = 255; i--;) {
    c[i].r = 255;
    c[i].g = 0;
    c[i].b = 0;
}

SDL_SetPaletteColors(output_surface->format->palette, c, 0, 256);

然后......

SDL_Rect r = {
    .x = 0,
    .y = 0,
    .w = width,
    .h = height
};

// Doesn't fill with red.
SDL_FillRect(output_surface, &r, 4);

SDL_UpdateTexture(output_texture, NULL, output_surface->pixels, output_surface->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, output_texture, NULL, NULL);
SDL_RenderPresent(renderer);

我期望看到的是整个窗口都是红色的,但我得到了完全不同的东西。更改传递给SDL_FillRect的颜色编号表明我得到了灰度调色板(0是黑色,255是白色),即使SDL_SetPaletteColors没有返回错误,我已经通过{{循环1}}验证调色板已被更改。

我在这里缺少什么?

编辑:我被要求发布整个程序。这是:

output_surface->format->palette->colors

int main(int argc, const char *argv[]) { SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; SDL_Surface *output_surface = NULL; SDL_Texture *output_texture = NULL; int width = 640; int height = 480; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0) return 0; window = SDL_CreateWindow("Sample", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); output_surface = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0); output_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, width, height); SDL_Color c[256]; for(u8 i = 255; i--;) { c[i].r = 255; c[i].g = 0; c[i].b = 0; c[i].a = 255; } SDL_SetPaletteColors(output_surface->format->palette, c, 0, 255); SDL_Rect r = { .x = 0, .y = 0, .w = width, .h = height }; bool running = true; while(running) { SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type){ case SDL_KEYDOWN: running = false; break; } } SDL_FillRect(output_surface, &r, 124); SDL_UpdateTexture(output_texture, NULL, output_surface->pixels, output_surface->pitch); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, output_texture, NULL, NULL); SDL_RenderPresent(renderer); } SDL_FreeSurface(output_surface); SDL_DestroyTexture(output_texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } 传递给0为黑色,SDL_FillRect为白色,中间任意数字为灰色阴影。

1 个答案:

答案 0 :(得分:1)

好的,找到了解决方案。

删除此行:

output_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, width, height);

而是在调用SDL_SetPaletteColors之后或更改曲面之后的某处添加此行。像素(如游戏循环中):

output_texture = SDL_CreateTextureFromSurface(renderer, output_surface);