C++ SDL2 how to color individual tiles

亡梦爱人 提交于 2021-02-10 16:28:59

问题


I'm learning C++ and SDL2 by programming a classic roguelike. Right now I build the map by rendering part of a tiles.png image like this one:

I followed 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, but what if I want, say, a brown door somewhere and a gray one somewhere else?

What's the best approach here? Obviously I can't have hundreds of color combinations stored in pngs. Should I create a texture for each tile, or there is a better way?

Thanks! :)


回答1:


SDL-2 means that you use opengl to render tiles. Use blending and color materials. Use SDL_SetRenderDrawColor, SDL_SetRenderDrawBlendMode, SDL_SetTextureBlendMode SDL_SetTextureColorMod, SDL_SetTextureAlphaMod. For example to draw yellow letters:

SDL_SetTextureColorMod(renderer, 255, 255, 0);

to draw different background you need to use letters with alpha-channel. First you need to draw background where text appears, then draw text itself. For example:

//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

Or cheeting version (if you dont want to use alpha-blending):

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


来源:https://stackoverflow.com/questions/43781607/c-sdl2-how-to-color-individual-tiles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!