View Full Version : Input/Output Console for OpenGL

09-14-2004, 07:14 AM
I am looking for a sourcecode which can display the buffer of glutKeyboardFunc(keyboard) on the screen.

Or just a code which displays a variable (text). Optimal would be some kind of console.

Thanks for help.

09-14-2004, 11:43 AM
If you are looking for a simple way to debug your code while it is running by displaying text messages within the programs window then this is what I use (this only works with windows and only if you have a handle to the windows device context):

////Begin code
#ifndef __TEXT_H
#define __TEXT_H

#include <String>

class Text

void initialize(HDC hDC)
//Set up a font to use
HFONT hFont;
LOGFONT logfont;

logfont.lfHeight = -10;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = FW_BOLD;
logfont.lfItalic = FALSE;
logfont.lfUnderline = FALSE;
logfont.lfStrikeOut = FALSE;
logfont.lfCharSet = ANSI_CHARSET;
logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logfont.lfQuality = DEFAULT_QUALITY;
logfont.lfPitchAndFamily= DEFAULT_PITCH;
hFont = CreateFontIndirect(&logfont);

SelectObject(hDC, hFont);

nFontBase = glGenLists(128);

wglUseFontOutlines(hDC, 0, 128, nFontBase ,0.0f, 0.0001f, WGL_FONT_POLYGONS, agmf);

output = "";

void drawText()
glScaled(.025, .025, .025) ;

glCallLists(output.length(),GL_UNSIGNED_BYTE,outpu t.c_str());

void setText(string changeTo)
output = changeTo;

void addText(char stringToAdd)
output += stringToAdd;

void addText(string stringToAdd)
output += stringToAdd;

void addText(int stringToAdd)
char buffer [33];
itoa (stringToAdd,buffer,10);
output = output + buffer;

void addText(float stringToAdd)
char buffer[33];
int temp = (int)stringToAdd;
output = output + buffer;

string output;
GLuint nFontBase;



I create a global Text object. I then call the initialize function (this must be done after opengl is set up so that is why I did not put it in the constructor). Then I use the helper functions (addText,setText) to change the message. Then I make sure to call drawText every frame. In the drawText function are some calls to glScale and glTranslate. These you might need to tweek depending on how big you want your text and where you want it positioned. This is a very crude method for displaying text but it serves its purpose. You can add other helper functions to display things like float point integers but I never bothered to. You could also use some operator like << to add successive strings or different data types but I got lazy when I wrote the class. Hope this helps.

But wait, if you are writing a standard c++ console program then you should already have a console. Why can't you just use cout?

The code is mine except for the init function and I forget where I got it. Sorry.