c++ - 如何更改表面调色板颜色?

标签 c++ sdl

我的代码在没有这些行的情况下运行得很好:

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_SetPaletteColors

还有一些用于获取左上角像素颜色的示例代码@

SDL_PixelFormat (See example code)

关于c++ - 如何更改表面调色板颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25586368/

相关文章:

c++ - 不循环检测连接请求

c++ - SDL 窗口无响应

c++ - 具有包含另一个类的函数指针的映射的类

c++ - 从字符串中打印出 HTML 标签

c++ - 线程控制台窗口的高效实现

c++ - SDL 应用程序使用 DirectFB 错误 : No available video device

c++ - 编译cpp文件时出错

c - SDL_GetKeyboardState 不起作用

c++ - 隐式类型转换看不到基类匹配。错误或功能?

c++ - 字符数组的初始值设定项字符串对于 char[] 而言太长