JoePerkins
JoePerkins

Reputation: 1005

C++ SDL2 how to color individual tiles

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:

tiles

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! :)

Upvotes: 4

Views: 3537

Answers (1)

user3811082
user3811082

Reputation: 251

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

Upvotes: 5

Related Questions