When working on games, I often find it useful to be able to quickly and easily display text strings on the screen for debugging purposes. A long time ago I wrote a simple little piece of code which could draw text onto a bitmap, and without having a dependency on anything else – it doesn’t use any other classes, functions or libraries, and it doesn’t need any external data – the font bitmaps are included in the source file, as a static array. This system, in all its simplicity, have turned out to be very, very useful, and I’ve used it for most of my stuff for the last 10 years or so.
I recently spent a little time cleaning it up a little, and thought I’d release it to anyone who’s interested. As usual, it’s public domain, so use it in any way you like.
To draw text on any bitmap, you simple include "DebugFont.h", and do something like this:
DebugFont font; font.Blit(pixelDataPtr,w,h,20,30,"My text"); // 20,30 is the x,y position of the text
Where pixelDataPtr is a pointer to the pixel data of the bitmap we want to draw onto, andw and h are the width and height (in pixels) of that pixel data area. The pixel data of the bitmap to draw on, can be 8-, 16- or 32-bit data.
As I finish the refactoring of more parts of my Pixie game engine, I’ll try and post more of these little useful, stand-alone code snippets which can just be dropped into any project – let me know if you find any of them useful