On-screen debug text

On the day 32 Q&A, there was a question about printing debug text on screen, and though that's something which won't be addressed until later, I thought I would share this for those who wants a placeholder in the meantime.

http://www.mattiasgustavsson.com/temp/libs/sysfont.hpp

It is a simple single-header implementation of a generic debug font renderer, which draws text to any 8, 16 or 32 bit bitmap target. You call it like this:

1
sysfont::draw9x16( ptrToPixelBuffer, bitmapWidth, bufferHeight, xpos, ypos, "Your text here." );


It includes the actual font definition as an inline data array (as one bit per pixel), so there's no loading or assets needed (but instead you are limited to two fonts, one 8x8 and one 9x16 - the old DOS fonts).
Here is different single-header library for debug text: http://nothings.org/stb/font/ (public domain) From Sean Barrett, author of stb_image.h.

It includes four fonts - Arial, Times New Roman, Courier and Consolas with different sizes (from 6 up to 50). Each font with each size is available as single header, so you don't need to include all of them, just one you actually use. Header files don't have any dependencies - font data is inside header file, so no file loading, and no memory allocation.

Edited by Mārtiņš Možeiko on
When I make and create games in C and I don't have a renderer sorted yet, I use the AllocConsole() function MSDN Ref http://msdn.microsoft.com/en-us/l...ws/desktop/ms681944(v=vs.85).aspx to create a new console window if one does not exist.

I then have a function that I give a string to print out to that console window.

I'm not sure how graceful my code is for production as I usually remove all references to it and remove the code, but here it is.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Only run from write debug function
void 
_DebugConsole()
{
#ifdef _DEBUG
	if(AllocConsole())
	{
		FILE * file = NULL;
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
		freopen_s(&file, "CONOUT$", "wt", stdout);
		SetConsoleTitle("Debug Console");
		SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
		HWND hWnd = GetConsoleWindow();
		MoveWindow(hWnd,800,0,480,800, true);
	}
#endif
}

void 
WriteDebug(const char* debugText, ...)
{
#ifdef _DEBUG
    //Make sure the console window exists
    _DebugConsole();
 
    //Print out text 
    va_list args;
    //Start the ouput line
    fprintf( stdout, "Debug: " );
    

    va_start( args, debugText );
    vfprintf( stdout, debugText, args );
    va_end( args );

    //Make a new line
    fprintf( stdout, "\n" );
#endif
}


Typical usage.
1
WriteDebug("x: %f > %f y: %f > %f", 0.0f, 0.0f, 0.0f, 0.0f);


I believe this code will work as above I removed it out of my singleton class structure from one of my games i'm working on.

Edited by Dazaster on Reason: Like I said I took it out from a singleton class I usually use