我的代码在没有这些行的情况下运行得很好:
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;
}
答案 0 :(得分: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;
但建议您使用内置功能更改调色板颜色
还有一些用于在左上角像素@
处获取颜色的示例代码