问题
I'm pretty new into this and I'd like to display fixed location 2D texts (like level, life, score etc.) on my game's screen which is basically a 3D game. I have found a few similar questions but still could not figure out the solution for me. I mainly use GLFW for everything but I'd like to use glut for displaying texts. I have a funcion (based on a few other questions I've found here) to try out how it works but I'm not sure how exactly can I make it to actually display because now everything starts but no text is displayed, only my 3D world.. Here is my code so far:
//includes...
//and other stuff...
void display()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, WindowWidth, 0.0, WindowHeight);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0.0, 1.0, 0.0); // Green
glRasterPos2i(20, 20);
std::string s = "Some text";
void* font = GLUT_BITMAP_9_BY_15;
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
glColor3d(1.0, 0.0, 0.0);
glutBitmapCharacter(font, c);
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
int main(void)
{
// init glfw and set some options...
glfwInit();
//also, it seems I need to init glut to use my function too which requires arguments...
{
int argc = 1;
char* argv[1] = { (char*)"Something" };
glutInit(&argc, argv);
}
//creating window...
GLFWwindow* window = glfwCreateWindow(WindowWidth, WindowHeight, "The Maze Game", nullptr, nullptr);
// QUESTION: should I call the function here or where the game loop is? It doesn't work either way.. :(
display();
while (!glfwWindowShouldClose(window)) {
//doing stuff...
glfwPollEvents();
// I have to display text later HERE... where the std::couts are... would be better on screen instead of console..
if(moved == true) timer = timer - delta;
std::cout << timer << std::endl;
if (win == true || timer <= 0.f)
{
if (win == true)
{
score++;
}
if (timer <= 0.f && life > 0)
{
life--;
if (life == 0)
{
std::cout << "You LOST" << std::endl;
}
}
//...stuff
timer = mazeTime;
win = false;
std::cout << "Score: " << score << std::endl;
std::cout << "Life: " << life << std::endl;
}
// Clear the colorbuffer
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// etc....
来源:https://stackoverflow.com/questions/61341652/display-2d-text-with-freeglut-using-opengl