Changing font color on bitmap fonts

I’ve been able to cobble together a basic bit of bitmap font rendering in my SDL2 app. My different sized test bitmap fonts are loading correctly into SDL_Textures and I’m able to render the text I want (yay!)…but only in the color white.

I’ve tried using SDL_SetRenderDrawColor before rendering my text, but there’s no change. I’ve come across the SDL_SetTextureMod stuff, but this (logically) affects every texture in my scene.

I realize this isn’t much info to go on, but I’m hoping that anyone else who’s already gone through this, knows right away what to look for…

(btw. I’m not opposed to OpenGL if that’s the recommended path)

Any ideas? Thanks!

You can use SDL2_gfx to do all the work for you. The function gfxPrimitivesSetFont() takes a pointer to your bitmap font data, after which the function characterRGBA() renders a character in the specified color. There is also stringRGBA() to render an entire string.

If these functions don’t do exactly what you want, or you prefer to code it yourself, you will need to convert each of your bitmap characters into a separate texture, via an intermediate surface. Then you can set the color using SDL_SetTextureColorMod() with the appropriate character’s texture passed as the first parameter.

1 Like

Thanks a lot @rtrussell - I’ll look into that!