can display float numbers but not integers

Member
Posts: 321
Joined: 2004.10
Post: #1
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.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #2
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
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.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #4
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.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #5
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  float FBO on iPhone (invalid operation error) Fenris 11 8,469 Jan 17, 2013 05:40 AM
Last Post: Fenris
  Best way to readback float pixels? kelvin 8 5,662 Feb 27, 2008 10:23 PM
Last Post: kelvin
  convert NSArray in float[] bonanza 6 7,117 Jun 25, 2007 10:33 AM
Last Post: TomorrowPlusX
  glDrawElements vs. glDrawArrays - The numbers are in! inio 22 20,448 Jul 19, 2003 10:00 AM
Last Post: Josh