问题
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