C++ SDL2 如何为单个图块着色

C++ SDL2 how to color individual tiles

我正在通过编写经典 roguelike 来学习 C++ 和 SDL2。现在我通过渲染 tiles.png 图像的一部分来构建地图,如下所示:

我关注了 lazyfoo's tiling tutorial and it works, but I would like to be able to change each tile background and foreground colors. I can change the full texture colors doing something like this other tutorial,但是如果我想要,比如说,某处有一扇棕色的门,而另一处是一扇灰色的门呢?

最好的方法是什么?显然我不能将数百种颜色组合存储在 png 中。我应该为每个图块创建一个纹理,还是有更好的方法?

谢谢! :)

SDL-2 表示您使用 opengl 渲染图块。使用混合和着色材料。使用 SDL_SetRenderDrawColorSDL_SetRenderDrawBlendModeSDL_SetTextureBlendMode SDL_SetTextureColorModSDL_SetTextureAlphaMod。 例如绘制黄色字母:

SDL_SetTextureColorMod(renderer, 255, 255, 0);

要绘制不同的背景,您需要使用带 alpha 通道的字母。首先,您需要在出现文本的位置绘制背景,然后绘制文本本身。 例如:

//load surface with alpha-channel here
SDL_SetRenderDrawColor(renderer, 0, 0, 255); //set blue background
//draw rect for background here
SDL_SetTextureColorMod(renderer, 255, 255, 0); //set yellow letters
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
//draw text here

或者 cheeting 版本(如果你不想使用 alpha 混合):

SDL_SetRenderDrawColor(renderer, 0, 0, 255); //set blue background
//draw rect for background here
SDL_SetTextureColorMod(renderer, 255, 255, 0); //set yellow letters
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
//draw text here