Jump to content
  • Advertisement
Sign in to follow this  
jinuuchukuu

Someone HELP me please with "GLprint" from Tutorial #14

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It could be simple to use this routine
GLvoid glPrint(const char *fmt, ...)					//Custom GL "Print" Routine
{
	float		length=0;								// Used To Find The Length Of The Text
	char		text[256];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

	if (fmt == NULL)									// If There's No Text
		return;											// Do Nothing

	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

	for (unsigned int loop=0;loop<(strlen(text));loop++)	// Loop To Find Text Length
	{
		length+=gmf[text[loop]].gmfCellIncX;			// Increase Length By Each Characters Width
	}

	glTranslatef(-length/2,0.0f,0.0f);					// Center Our Text On The Screen

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(base);									// Sets The Base Character to 0
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}

if the conversion between char to const char was possible. Apparently, the compiler wont run "vsprintf" with this call : char string[26]; ... for (int i=0; i<26; i++){ ... GLprint (string); } Beside the same compiler accept running "vsprintf" calling it this way: ... GLprint (string); ... Can someone give a method to print only one character from a char type array ? Muchos gracias d' avance ! [Edited by - phantom on September 25, 2005 3:23:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
Arrays are treat like pointers, 'string' would be the first element, and be a char*. 'string' would be any element, and be a char. So what you want to do is get a reference using '&', this will *magically change a char into a char*. Now your code (hope it works haven't tested it) should be more like this:


for (int i=0; i<26; i++){
...
GLprint (&string);
}


That *should* work, but i've only been using pointers and references more recently in C++, so i'm unsure.

* - Not magically, it returns a reference to the variable, which would be its address in memory. It has more really fun uses aswell

Share this post


Link to post
Share on other sites
i wouldnt count on that working all the time, as a string must be null terminated, so unless your lucky enough to have a byte containing 0 show up in memory directly after the location of your character variable, what the program will see is a string containing all values from the character to the first occurance of the null character, which can produce some really weird results, crash your program, etc.

therefor, the only way to really print 1 character would be to change the function to expect 1 character, or make a string containg 2 characters, having the second always be 0 so you are sure only the 1 character you want will show up.

and for future reference, use [ source ] [ /source ] tags.

Share this post


Link to post
Share on other sites
Thats a good point actually...I forgot about NULL character termination. Perhaps just rewriting that glPrint function would be whats needed.

Share this post


Link to post
Share on other sites
Try this:

glPrint("%c",string);

The function is set up to take a printf-style format string and any number of parameters. If you want to print a single character, then you need to specify a format that expects a single character and the character to print.

Share this post


Link to post
Share on other sites
I really thank you all!
First thing i've done,after putting the "&string" and observing that my whole string where printed, I could'nt affort a NULL Terminated char each character,so I tried rewriting glPrint, and falled on many debug Fatal errors...
Fortunatly Dave Hunt! , you had the quickest solution.. I already tried glPrint(" with %i as the tutorial, %b(boolean) that does'nt works, %f with its unusefull %x.xf structure,%d that is quite right, but never thought 'bout %c, GREAT thing ! it worked !
Big Thanks to all !

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!