如何更改曲面调色板颜色

How can I change surface palette colors?

本文关键字:颜色 调色板 曲面 何更改      更新时间:2023-10-16

我的代码在没有以下行的情况下运行得非常好:

SDL_LockSurface(sdlSurface);
sdlSurface->format->palette->colors->r = 255;
sdlSurface->format->palette->colors->b = 0;
sdlSurface->format->palette->colors->g = 0;
sdlSurface->format->palette->colors->a = 255;
SDL_UnlockSurface(sdlSurface);

我正在尝试将表面颜色更改为其他颜色,但不起作用。我想使用调色板结构,我不想使用mapRGBA函数或类似的东西。我只想直接访问颜色组件。

#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char *argv[]) {
    if(SDL_Init(SDL_INIT_VIDEO) != 0) {
        cout << "could not initialized SDL." << endl;
        return 0;
    }

    bool quit = false;
    const char progTitle[] = "SDLProject";
    SDL_Event *sdlEvent = new SDL_Event();
    SDL_Window *sdlWindow;
    SDL_Renderer *sdlRenderer;
    SDL_Texture *sdlTexture;
    int winWidth = 640;
    int winHeight = 480;
    SDL_Surface *sdlSurface;
    sdlWindow = SDL_CreateWindow(progTitle,100,100,winWidth,winHeight,SDL_WINDOW_SHOWN);
    sdlRenderer = SDL_CreateRenderer(sdlWindow,-1,0);
    sdlTexture = SDL_CreateTexture(sdlRenderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STREAMING,winWidth,winHeight);
    sdlSurface = SDL_CreateRGBSurface(0,winWidth,winHeight,8,0,0,0,0);
    SDL_LockSurface(sdlSurface);
    sdlSurface->format->palette->colors->r = 255;
    sdlSurface->format->palette->colors->b = 0;
    sdlSurface->format->palette->colors->g = 0;
    sdlSurface->format->palette->colors->a = 255;
    SDL_UnlockSurface(sdlSurface);
    SDL_UpdateTexture(sdlTexture,NULL,sdlSurface->pixels,sdlSurface->pitch);
    SDL_RenderClear(sdlRenderer);
    SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
    SDL_RenderPresent(sdlRenderer);
    while(!quit) {
        SDL_PollEvent(sdlEvent);

        switch (sdlEvent->type) {
            case SDL_QUIT:
                quit = true;
                break;
        }
    }
    delete sdlSurface;
    SDL_DestroyTexture(sdlTexture);
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit();
    cout << "program finished with zero errors." << endl;
    return 0;
}

所以我把代码改成了这个,但输出仍然是一样的。。。我得到一个黑屏。至于setPaletteColor函数,如果成功,它应该返回0,但我在命令行中没有收到任何错误。

#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char *argv[]) {
    if(SDL_Init(SDL_INIT_VIDEO) != 0) {
        cout << "could not initialized SDL." << endl;
        cout << "tError: " << SDL_GetError() << endl;
        return 0;
    }
    bool quit = false;
    const char progTitle[] = "SDLProject";
    SDL_Event *sdlEvent = new SDL_Event();
    SDL_Window *sdlWindow;
    SDL_Renderer *sdlRenderer;
    SDL_Texture *sdlTexture;
    int winWidth = 640;
    int winHeight = 480;
    SDL_Surface *sdlSurface;
    SDL_Color sdlColor;
    sdlColor.r = 255;
    sdlColor.g = 0;
    sdlColor.b = 255;
    sdlColor.a = 255;
    sdlWindow = SDL_CreateWindow(progTitle,100,100,winWidth,winHeight,SDL_WINDOW_SHOWN);
    sdlRenderer = SDL_CreateRenderer(sdlWindow,-1,0);
    sdlSurface = SDL_CreateRGBSurface(0,winWidth,winHeight,8,0,0,0,0);
    SDL_LockSurface(sdlSurface);
    if(!SDL_SetPaletteColors(sdlSurface->format->palette,&sdlColor,0,sdlSurface->format->palette->ncolors-1)) {
        cout << "palette was not able to be set." << endl;
        cout << "tError: " << SDL_GetError() << endl;
    }
    SDL_UnlockSurface(sdlSurface);
    sdlTexture = SDL_CreateTexture(sdlRenderer,sdlSurface->format->format,SDL_TEXTUREACCESS_STREAMING,winWidth,winHeight);
    SDL_UpdateTexture(sdlTexture,NULL,sdlSurface->pixels,sdlSurface->pitch);
    SDL_RenderClear(sdlRenderer);
    SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
    SDL_RenderPresent(sdlRenderer);
    while(!quit) {
        SDL_PollEvent(sdlEvent);

        switch (sdlEvent->type) {
            case SDL_QUIT:
                quit = true;
                break;
        }
    }
    delete sdlSurface;
    SDL_DestroyTexture(sdlTexture);
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit();
    cout << "program has ended." << endl;
    return 0;
}
SDL_Palette由SDL_Color*的数组组成,因此必须提供要修改的颜色的索引。
int number_of_palette_entries = sdlSurface->format->palette->ncolors;

所以在做这样的事情之前,你应该做一个范围检查:

// change index to pure Red
sdlSurface->format->palette->colors[index].r = 255;
sdlSurface->format->palette->colors[index].g = 0;
sdlSurface->format->palette->colors[index].b = 0;
sdlSurface->format->palette->colors[index].a = 255;

然而,建议您使用内置功能来更改调色板颜色

SDL_SetPanetteColors

还有一些在左上角像素@获得颜色的示例代码

SDL_PixelFormat(参见示例代码)