Question

I'm using the opengl function "drawstring" to create a HUD for my game. I've managed to successful draw string to the screen but I've ran into a problem when it comes to displaying a value such as the players position or score which are floats.

After some research I found that drawstring will only accept const chars. So i then tried to cast or convert a float into a char value but I have been successful.

This is my drawstring method

void Player::DrawString(const char* text, const Vector3* position, const Color* color)

{

       glPushMatrix();

              glDisable(GL_TEXTURE);

              glDisable(GL_LIGHTING);

              glDisable(GL_DEPTH_TEST);

              glColor3f(1.0f, 1.0f, 1.0f);



              glTranslatef(position->x, position->y, position->z);

              glRasterPos2f(0.0f, 0.0f);

              glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (unsigned char*)text);

              glEnable(GL_LIGHTING);

              glEnable(GL_TEXTURE);

              glEnable(GL_DEPTH_TEST);



       glPopMatrix();



}

and this is my method calls

Color c1 = {0.0f, 0.0f, 0.0f};
       DrawString("Player Pos: ", &_vpos, &c1);

      Vector3 vspeed = {20.0f, 0.0f, 500.0f};
      Color c = {1.0f, 1.0f, 1.0f};
       DrawString(cpp, &vspeed, &c1);

cpp is a float that i have tried to cast to a char.

Was it helpful?

Solution

#include <stdio.h>

// ...

char buf[256];
snprintf(buf, sizeof(buf) - 1, "Pos: { %f, %f, %f }", pos.x, pos.y, pos.z);
DrawString(buf, &_vpos, &c1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top