简体   繁体   English

如何在SDL 2中使用调色板

[英]How to use palettes in SDL 2

I'm updating a program from SDL 1 to SDL 2 and need to use color palettes. 我正在将程序从SDL 1更新为SDL 2并需要使用调色板。 Originally, I used SDL_SetColors(screen, color, 0, intColors); 最初,我使用了SDL_SetColors(screen, color, 0, intColors); but that does't work in SDL 2. I'm trying to use: 但这在SDL 2中不起作用。我正在尝试使用:

SDL_Palette *palette = (SDL_Palette *)malloc(sizeof(color)*intColors);
SDL_SetPaletteColors(palette, color, 0, intColors);
SDL_SetSurfacePalette(surface, palette);

But SDL_SetPaletteColors() returns -1 and fails. 但是SDL_SetPaletteColors()返回-1并失败。 SDL_GetError gives me no information. SDL_GetError没有给我任何信息。

How can I make a palette from a SDL_Color and then set it as my surface's palette? 如何从SDL_Color制作调色板,然后将其设置为我的曲面调色板?

It's hard to tell what your variables are and how you intend to use them without seeing your declarations. 很难说出你的变量是什么以及你打算如何使用它们而不看你的声明。

Here's how I set up a grayscale palette in SDL_gpu: 以下是我在SDL_gpu中设置灰度调色板的方法:

SDL_Color colors[256];
int i;

for(i = 0; i < 256; i++)
{
    colors[i].r = colors[i].g = colors[i].b = (Uint8)i;
}

#ifdef SDL_GPU_USE_SDL2
SDL_SetPaletteColors(result->format->palette, colors, 0, 256);
#else
SDL_SetPalette(result, SDL_LOGPAL, colors, 0, 256);
#endif

The result SDL_Surface already has a palette because it is has an 8-bit pixel depth (see note in https://wiki.libsdl.org/SDL_Palette ). result SDL_Surface已经有一个调色板,因为它具有8位像素深度(请参阅https://wiki.libsdl.org/SDL_Palette中的注释)。

It has been awhile since the OP posted the question and there has been no accepted answer. 自OP发布问题以来,已经有一段时间了,并且没有接受答案。 I ran into the same issue while trying to migrate a SDL 1.2 based game into using 2.0. 在尝试将基于SDL 1.2的游戏迁移到使用2.0时,我遇到了同样的问题。 here is what I did hoping it could help other who may be facing similar issue: 这就是我所做的,希望它可以帮助其他可能面临类似问题的人:
Replace: 更换:
SDL_SetColors(screen, color, 0, intColors);

With: 附:
SDL_ SDL_SetPaletteColors(screen->format->palette, color, 0, intColors);

David 大卫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM