iDevGames Forums
can display float numbers but not integers - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: can display float numbers but not integers (/thread-5999.html)



can display float numbers but not integers - WhatMeWorry - Jan 2, 2005 11:57 PM

This should be trivial, but with my modest intellect, it is not.
I came across code snippet:


sprintf (outString, "test float: %9.3f", testFloat);
drawGLString (gRasterX, gRasterY, outString);

which calls the ever popular:

void drawGLString(double x, double y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len-1; i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
}
}

However, when I try something like:

sprintf (outString, "test int: %3d", testInteger);
rawGLString (gRasterX, gRasterY, outString);

I get nothing. I've tried many, many other integer/decimal C formats
but to no avail. They compile OK, but nothing ever displays.
glutBitmapCharacter's help only talks about the Font and glosses over
the string[i].

Anybody want to shame me for my ignornace?
thanks.


can display float numbers but not integers - NCarter - Jan 3, 2005 04:52 AM

Are you saying that it does work when you use "test float: %9.3f" but not when you use "test int: %3d", or does it not work at all?

If it isn't working at all, try drawing a quad or something in the same place as the text. If you can't see that either, you're probably missing the viewport. Check your transformations and so on.

Have you initialised OpenGL properly prior to calling your font code?

If it works for floats but not for ints... that's a little odd. Is your outString buffer large enough?

This thread covers the same problem - maybe there are some answers in there.


can display float numbers but not integers - TomorrowPlusX - Jan 3, 2005 05:05 AM

You may also want to try outputting the string via printf to stdout to see if the string itself is being formatted. This seems very peculiar to me.


can display float numbers but not integers - WhatMeWorry - Jan 3, 2005 10:07 AM

Ok, I feel like Homer Simpson.

The problem was in

void drawGLString(double x, double y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len-1; i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
}
}

the len-1 was stopping the last char of the integer display string.
So just doing for (i = 0; i < len; i++) fixed the problem.

However, now when I send just a help string like:

sprintf (outString, "H or h = toggle display of help menu\n");
drawGLString (gRasterX, gRasterY, outString);

The display NOW puts in a garbage character at the end of the display!

So I'd say this is a bug with drawGLString(). Btw, I got this from
the "bunny" program.


can display float numbers but not integers - NCarter - Jan 3, 2005 10:25 AM

WhatMeWorry Wrote:However, now when I send just a help string like:

sprintf (outString, "H or h = toggle display of help menu\n");
drawGLString (gRasterX, gRasterY, outString);

The display NOW puts in a garbage character at the end of the display!

So I'd say this is a bug with drawGLString().
I reckon it's actually caused by the '\n' at the end of your string. I doubt if drawGLString()/glutBitmapCharacter() will treat carriage returns as anything other than a printable garbage character unless you program it to do otherwise.


can display float numbers but not integers - ThemsAllTook - Jan 3, 2005 10:27 AM

WhatMeWorry Wrote:However, now when I send just a help string like:

sprintf (outString, "H or h = toggle display of help menu\n");
drawGLString (gRasterX, gRasterY, outString);

The display NOW puts in a garbage character at the end of the display!

So I'd say this is a bug with drawGLString(). Btw, I got this from
the "bunny" program.

It's not too surprising for a newline to generate an unprintable character glyph. You'll probably have to do wrapping manually; although in this case, since there isn't anything in the string after the newline, you can just strip it out.

[Edit: Rats! You beat me to it, Neil. Your reply came in as I was typing this one... Smile ]

- Alex Diener