如何更改曲面调色板颜色
How can I change surface palette colors?
我的代码在没有以下行的情况下运行得非常好:
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;
}
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(参见示例代码)
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 列表视图更改选择颜色
- GtkTreeView 交替行颜色
- dx11 渲染到纹理仅显示透明颜色
- 使用 OpenGL 4.5 更改所选顶点的颜色
- 为什么我的 LEGACY OPENGL 颜色反转了?
- 双击更改 mfc 中列表控件中的行的颜色
- 更改 QT 中按钮的颜色
- 当我使用CHAR_INFO结构时,控制台会无缘无故地改变颜色
- 列表视图项的替代颜色
- GLSL 将 uint 转换为浮点颜色
- std::带有颜色和标题的 clog 包装器无法正确打印整数
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- 如何将矢量对象更改为不同的颜色?
- 将中值剪切颜色缩减算法的输出调色板应用于源图像
- 如何更改曲面调色板颜色
- 调色板的连续颜色