🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

show a variable on the screen?

Started by
12 comments, last by da_cobra 22 years, 7 months ago
ok I have this function that prints a text on the screen in my directx program but how can I use this function to also display a variable on the screen, because it would greatly help me if I could do that for debugging purposes void PrintText(char *text, int X, int Y, int bR, int bG, int bB, int tR, int tG, int tB) { if (G.lpDDSBack->GetDC(&G.hdc) == DD_OK) { SetBkColor(G.hdc, RGB(bR, bG, bB)); SetTextColor(G.hdc, RGB(tR, tG, tB)); TextOut(G.hdc, X, Y, text, lstrlen(text)); G.lpDDSBack->ReleaseDC(G.hdc); } } I use the function as follows : PrintText("text", 0, 0, 0, 0, 0, 255, 255, 255) ; this prints "text" on the left top of my screen with a black background and writen in white when I try to use a variable in the function it doesn''t work thanx in advance for any advice...
Advertisement
You need to declare your function with the last parameter as an ellipsis like so:

void PrintText(char *text, int X, int Y, int bR, int bG, int bB, int tR, int tG, int tB,...);

Then you can use the va_start, va_end, and vsprintf macros/functions to get a string containing all the variables you need. vsprintf works just like sprintf to give you a string except instead of all the variables you pass it a va_list which is obtained from va_begin.

For syntax specifics, look up vsprintf in the MSDN.

Good luck.


Edited by - Qoy on November 25, 2001 5:23:22 AM
I don''t understand :/
Don't go brandishing varargs at a newbie!

    intDXPrintf( char* str, int x, int y, ... ){   char buffer[256];   va_list list;   va_start( list, str );   int ret = _vsnprintf( buffer, 256, str, list );   if( ret == 256 ) buffer[255] = '\0';   va_end( list );   if (G.lpDDSBack->GetDC(&G.hdc) == DD_OK)    {       SetBkColor(G.hdc, RGB(bR, bG, bB));       SetTextColor(G.hdc, RGB(tR, tG, tB));       TextOut(G.hdc, x, y, buffer, lstrlen(buffer));       G.lpDDSBack->ReleaseDC(G.hdc);    }    return ret;}    


Usage: just like printf, with coordinates added :
DXPrintf( "This is an int %d.", x, y, an_int );

Edited by - Fruny on November 25, 2001 6:18:37 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
da_cobra:

I''m not quite sure I understand what you mean - could you elaborate?

the rest of you:

I''m not quite sure he means variable parameter list - he says "when I try to use a variable in the function it doesn''t work"... This could mean the function fails when he declares a variable inside the function body....
quote: Original post by Jx
I''m not quite sure he means variable parameter list - he says "when I try to use a variable in the function it doesn''t work"... This could mean the function fails when he declares a variable inside the function body....

Don''t be silly! He clearly states "how can I use this function to also display a variable on the screen, because it would greatly help me if I could do that for debugging purposes."

An alternative to vararg parameter lists is to pass an STL string to the function (if you''re using C++), and preformat the string. Of course, you could do the same with sprintf and C-style strings... The vararg way is more flexible, but the string/sprintf way is easier to understand:
#include <strstream>strstream str;//.// function declaration:void PrintText(string &, int, int, int, int, int, int, int, int);//.// use the functionint some_function(void){  ...  str << "Frame rate: ";  // add a variable (assume m_fFrameRate is a float containing the FPS):  str << m_fFrameRate;  PrintText(str.str(), /* other params */);} 

Works, but it''s kinda messy.

My favorite solution, however, is to display all debug output in a console window. You can get a new console window using AllocConsole, and you can also redirect cout and cin to point to your console (I forget how exactly; something to do with reopen() or so). Then you can just print your data to your console like a Windows console app.

Either way, one of these days you''ll need to learn how to use varargs - no time like the present.
I still can''t get this thing to work

when I try this :

PrintText ("%d", 0, 0, 0, 0, 0, 255, 255, 255, Pos_X) ;

where Pos_X is the variable I want to display +I get the following error =>

error C2660: ''PrintText'' : function does not take 10 parameters

which is normal

When I try this :

PrintText (Pos_X, 0, 0, 0, 0, 0, 255, 255, 255) ;

I get this error =>

error C2664: ''PrintText'' : cannot convert parameter 1 from ''unsigned int'' to ''char *''

Which is also normal so how can I now display my variable
You''re not listening (reading) very closely. Read all the instructions above (especially the source code) and implement them to the letter. Compile, build, execute. If any stage fails, come back with detailed reports of exactly what you did and the exact nature of the error. Give as much information as possible.

"It doesn''t/didn''t work" is such a pathetically generic excuse...
pls don''t flame me I really try

I copied and pasted the first function and changed a bit :

int PrintText(char *text, int x, int y, int bR, int bG, int bB, int tR, int tG, int tB)
{
char buffer[256] ;
va_list list ;
va_start(list, text) ;
int ret = _vsnprintf(buffer, 256, text, list) ;
if (ret==256)
buffer[255] = ''\0'' ;
va_end( list ) ;
if (G.lpDDSBack->GetDC(&G.hdc)==DD_OK)
{
SetBkColor(G.hdc, RGB(bR, bG, bB)) ;
SetTextColor(G.hdc, RGB(tR, tG, tB)) ;
TextOut(G.hdc, x, y, buffer, lstrlen(buffer)) ;
G.lpDDSBack->ReleaseDC(G.hdc) ;
}
return ret ;
}

when I now try the following function :

PrintText ("%d", 0, 0, 0, 0, 0, 255, 255, 255, Pos_X) ;

I get this error and I did all what i said
in my next post I''ll try your solution
error C2660: ''PrintText'' : function does not take 10 parameters
damn your solution works now :/
and I really tried it

hmm well anyways thanx alot for all your help guys!!!

This topic is closed to new replies.

Advertisement