显示计时器GLUT游戏

时间:2015-10-17 11:18:09

标签: c++ opengl timer glut

如何显示每10毫秒递增一次的计时器。我已经有一个函数X调用glutTimerFunc(10, X, 0);并且每次都增加timer的值0.1,但问题在于显示定时器本身的值在窗口。我尝试使用方法打印值并使用Display()中的glutDisplayFunc(Display)方法调用它,但由于某种原因它无法正常工作。该方法的代码如下。

void displayTimer(void * font, int z, float x, float y)
{
    glRasterPos2f(x, y);
    glutBitmapCharacter(font, z);
}

1 个答案:

答案 0 :(得分:1)

根据displayTimer()函数的标题,您似乎试图在屏幕上显示z坐标而不是您想要的实际计时器。

首先,我建议重命名' z'参数' text'并将其数据类型更改为std :: string。然后,您可以将计时器值作为字符串传递给displayTimer()函数。接下来,您将不得不遍历'文本'字符串,以便在屏幕上显示它(逐个字符)。

以下是您的功能如何显示的示例:

void displayTimer(void * font, std::string text, float x, float y)
{
    glRasterPos2f(x, y);
    for (string::iterator i = text.begin(); i != text.end(); ++i)
    {
      char c = *i;
      glutBitmapCharacter(font, c);
    }
}

我不确定您的代码是如何构建的,但有关如何使用X()函数以及Display()函数在屏幕上显示计时器值的示例如下:

#include <string>

std::string timerValue;
void * textFont;

void X(int value)
{
   timerValue = std::to_string(value);
}

void displayTimer(void * font, std::string text, float x, float y)
{
   glColor3f(0.0f, 1.0f, 0.0f); // Set color to green purely for testing.
   glRasterPos2f(x, y);
   for (string::iterator i = text.begin(); i != text.end(); ++i)
   {
     char c = *i;
     glutBitmapCharacter(font, c);
   }
}

void Display()
{  
    displayTimer(textFont,timerValue,10.0f,20.0f);
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    timerValue = 0;
    textFont = = GLUT_BITMAP_HELVETICA_18;

    glutTimerFunc(10,X,0);
    glutDisplayFunc(Display);

    glutMainloop();

    return 0;
 }

注意 char 数据如何传递给displayTimer()函数中的glutBitmapCharacter()函数而不是z整数。

提示:要避免某些编译器警告,请将x和y displayTimer()函数参数的数据类型更改为 GLfloat ,因为这是用于glRasterPos2f()函数的数据类型。