【问题标题】:Trouble getting 8-bit palette output working with SDL 2.0无法使用 SDL 2.0 获得 8 位调色板输出
【发布时间】:2016-02-16 22:54:20
【问题描述】:

这是我的代码,在 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 没有返回错误并且我已经循环通过 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 是黑色,255 是白色,中间的任何数字都是灰色阴影。

【问题讨论】:

    标签: sdl sdl-2


    【解决方案1】:

    好的,找到解决办法了。

    删除这一行:

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

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

    output_texture = SDL_CreateTextureFromSurface(renderer, output_surface);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多