Sign in to follow this  

C++ / opengl/glut - Text rendering

This topic is 1566 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

Hiii guys (: 

I dont know if this is the right place, but i thought of it as pretty basic, which just makes this more embaresing d: 

i have tried some functions for displaying text, and i made a couple of them work. Cant remember excatly how i wrote it but something like; 

void DrawText(float x, float y, char *string, ...)
{
    int len = (int)strlen(string);
    for (int i = 0; i < len; i++)
    {
         glutBitmapCharacter(Thefont, (int)string[i]);
    }
}


u should get the idea ^^
Anyways i made it work "kinda", it shows the text, but i cant make it show my vars' like;
 

DrawText(200, 200, "MouseX; %d", mouseX);

Then it just types "MouseX %d" on the screen (:
Anyfixes? ^^
Sincerly Wunder..

Share this post


Link to post
Share on other sites
you'll have to do a text replacement yourself in your drawtext function (replace the first %d with the first extra argument passed to the function). Since you are using C++ however you should probably just use stringstream to construct an appropriate string before passing it to the function rather than using old and highly unsafe C practices. (There is usually no need to use functions with a variable number of arguments in C++)

Share this post


Link to post
Share on other sites

Thank you for anwsering, although i'm not sure really sure which approach to take from here. 

Just cant figur out how to cheat it into displaying that freakin variable d; 

 

Btw. sry for my bad english, i'm kinda sleep "depribed"? idk, have been lookin at this freakin code in like 18 - 24 hours now d:   

Share this post


Link to post
Share on other sites
If you absolutely insist on doing it the C way look at the snprintf function. with c++ you should use std::string and std:stringstream rather than char pointers unless you have a very good reason not to.

Share this post


Link to post
Share on other sites

Ohhhh (: 

Actualy started out using "string", but then it didn't work with displaying the variables, so i though that maybe they had to be "char" becos those you loop through, if that makes any sense :D 

Share this post


Link to post
Share on other sites

C way:

char buf[256]; // increase for more data, but this should be enough for a few stuff
snprintf(buf, 256, "MouseX; %d", mouseX);
DrawText(200, 200, buf);

Refactor your function for this to work.

 

As for C++, it probably has better and easier ways to do this.

Edited by farmdve

Share this post


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