我正在OpenGL中编写一个游戏,其中有3个大盒子和1个小盒子。小盒子隐藏在其中一个大盒子下面,用户必须按A,B或C才能显示小盒子在哪里。我想跟踪用户做出了多少猜测,玩了几轮等等,但是我不确定如何在OpenGL中输出递增的数字。每当发生与数字有关的事情时,我都想增加数字,但是我无法弄清楚。有人可以指出我正确的方向吗?非常感谢!
main.cpp:
#include "Box.h"
#include "Font.h"
#include "Camera.h"
#include "Time.h"
#include <vector>
using namespace std;
#include "vgl.h"
using namespace glm;
#define SPACEBAR_KEY 32
#define ESCAPE_KEY 033
vector<Box * > boxes;
Camera* camera;
Font* font;
int numGenerator, guesses, totalGuesses, rounds, averageGuesses;
bool win;
void closeApp()
{
delete camera;
delete font;
for (auto it = boxes.begin(); it != boxes.end(); ++it)
delete (*it);
}
void startNewGame()
{
rounds++;
win = false;
guesses = 0;
numGenerator = rand() % 3;
boxes[1]->Visible = true;
boxes[2]->Visible = true;
boxes[3]->Visible = true;
if (numGenerator == 0)
{
boxes[0]->Position = vec3(0.0f, -3.0f, 0.0f);
}
else if (numGenerator == 1)
{
boxes[0]->Position = vec3(0.0f, 0.0f, 0.0f);
}
else
{
boxes[0]->Position = vec3(0.0f, 3.0f, 0.0f);
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case ESCAPE_KEY: // ASCII Escape Key Code
closeApp();
exit(EXIT_SUCCESS);
break;
case 'a':
boxes[1]->Visible = false;
if (boxes[0]->Position != vec3(0.0f, -3.0f, 0.0f))
{
guesses++;
totalGuesses++;
}
else
{
win = true;
}
glutPostRedisplay();
break;
case 'b':
boxes[2]->Visible = false;
if (boxes[0]->Position != vec3(0.0f, 0.0f, 0.0f))
{
guesses++;
totalGuesses++;
}
else
{
win = true;
}
glutPostRedisplay();
break;
case 'c':
boxes[3]->Visible = false;
if (boxes[0]->Position != vec3(0.0f, 3.0f, 0.0f))
{
guesses++;
totalGuesses++;
}
else
{
win = true;
}
glutPostRedisplay();
break;
case 'r':
startNewGame();
glutPostRedisplay();
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
font->printText("Guessing Game!", 260, 500, 20);
font->printText("Press 1, 2, or 3 to make one of the boxes disappear!", 10, 480, 15);
font->printText("Try to find the small cube! Press 'r' to restart!", 30, 460, 15);
if (win)
{
font->printText("You win!", 20, 200, 30);
}
font->printText("Guesses: ", 20, 350, 15);
font->printText("Rounds played: ", 20, 330, 15);
font->printText("Total guesses: ", 20, 310, 15);
font->printText("Average guesses: ", 20, 290, 15);
boxes[0]->Draw(camera->ProjectionMatrix, camera->ViewMatrix);
boxes[1]->Draw(camera->ProjectionMatrix, camera->ViewMatrix);
boxes[2]->Draw(camera->ProjectionMatrix, camera->ViewMatrix);
boxes[3]->Draw(camera->ProjectionMatrix, camera->ViewMatrix);
glutSwapBuffers();
}
void init()
{
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
srand(time(NULL));
font = new Font();
camera = new Camera();
camera->ViewMatrix = glm::lookAt(glm::vec3(0, 0, 20), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
VertexBufferData vertexBufferData = VertexBufferData("Data\\Models\\Objects.xml");
boxes.push_back(new Box(vertexBufferData, glm::vec3(0.0f, 0.0f, 0.0f), "data/images/wood.bmp", "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader", vec3(0.4f, 0.4f, 0.4f), true));
boxes.push_back(new Box(vertexBufferData, glm::vec3(0.0f, -3.0f, 0.0f), "data/images/ground.tga", "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader", vec3(1.0f, 1.0f, 1.0f), true));
boxes.push_back(new Box(vertexBufferData,glm::vec3(0.0f, 0.0f, 0.0f), "data/images/metal.bmp", "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader", vec3(1.0f, 1.0f, 1.0f), true));
boxes.push_back(new Box(vertexBufferData, glm::vec3(0.0f, 3.0f, 0.0f), "data/images/brick.bmp", "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader", vec3(1.0f, 1.0f, 1.0f), true));
startNewGame();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1024, 768);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Satterwhite_Project_5");
if (glewInit())
{
cerr << "Unable to init glew" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
最佳答案
您有一个font->printText
方法,这很重要,因此打印字符串对您来说不是问题!这意味着,您只需要准备带数字的字符串,然后使用printText
打印即可。
在C ++中执行此操作的一种简单方法是使用字符串流。在您的display
函数中,而不是font->printText("Guesses: ", 20, 350, 15);
中,输入:
std::stringstream ss;
ss << "Guesses: " << guesses;
font->printText(ss.str().c_str(), 20, 350, 15);
并对所有其余数字执行相同的操作。
不要忘记在
<sstream>
类中包含stringstream
。关于c++ - 如何在OpenGL中输出变量号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40393136/