Create a "cout" equivalent for OpenGL in C++

I am trying to make an equivalent of a printf() implementation so that OpenGL will print in the lower, left-hand corner, but for some reason it will only print the last function I send to it.

In a function like this:

1
2
3
	GLOUT("FPS: %4i", FPSPrint);
	GLOUT("this");
	GLOUT("CamPos: (%5.2f, %5.2f, %5.2f)", Camera.Position.X, Camera.Position.Y, Camera.Position.Z);


Only the "CamPos: (0.00, 0.00, 0.00)" is printed, but it is printed 3 times. I have two functions, one is GLOUT, which is supposed to take the text, put the numbers into the string, and save it in TextHolder[], and DrawDebigInfo() which is called once at the end of every frame.

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
char text[256];
char * TextHolder[16];
static int DebugNum = 0;

void GLOUT(const char * fmt, ...){
	va_list		ap;										// Pointer To List Of Arguments
	va_start(ap, fmt);									// Parses The String For Variables
	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

	TextHolder[DebugNum] = text;
	DebugNum++;
}

void DrawDebugInfo(){
	float xLocation = -14;
	float yLocation = -8;

	for (int i = 0; TextHolder[i] != 0; i++){
		glPushMatrix();
		glLoadIdentity();
		glMatrixMode(GL_MODELVIEW);

		glDisable(GL_TEXTURE_2D);
		glLoadIdentity();
		glTranslatef(0,0,-.002);
		glColor3f(1,1,1);
		glRasterPos2f(xLocation/10000, (yLocation+i)/10000);
		glPopMatrix();

		glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
		glListBase(base - 32);								// Sets The Base Character to 32
		glCallLists(strlen(TextHolder[i]), GL_UNSIGNED_BYTE, TextHolder[i]);	// Draws The Display List Text
		glPopAttrib();										// Pops The Display List Bits
	}
	DebugNum = 0;
}

Last edited on
Pointers are not strings.

You are using the same string (text) each time GLOUT is called.
TextHolder[0], TextHolder[1], and TextHolder[2] all point to the same string data: 'text' which was most recently overwritten with the camera position.

If you want to save the string data, use a string.

Also I'm not sure how your drawing code is even working. AFAIK you can't pass a char* to glCallLists and just expect it to print a string.

1
2
3
4
5
6
7
string TextHolder[16];  // <- make it a string

void GLOUT(const char * fmt, ...){

// ...
	TextHolder[DebugNum] = text; // <- now this will actually copy of the string
}


You can then use TextHolder[i].length() instead of strlen... and if you still need a char* for whatever reason you can use TextHolder[i].c_str(), but again I'm really confused as to how you're drawing.
Thank you so much, that worked perfectly! And yes, for some reason glCallLists actually required me to use TextHolder[i].c_str() otherwise it said
cannot convert parameter 3 from 'std::string' to 'const GLvoid *'
Topic archived. No new replies allowed.