C++ / opengl/glut - Text rendering

Started by
4 comments, last by farmdve 10 years, 6 months ago

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..

Advertisement
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++)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

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:

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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

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

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.

This topic is closed to new replies.

Advertisement