Отображение 2D текста с помощью freeglut, используя OpenGL - PullRequest
0 голосов
/ 21 апреля 2020

Я довольно новичок в этом, и я хотел бы отображать 2D-тексты с фиксированным местоположением (например, уровень, жизнь, счет и т. Д. c.) На экране моей игры, который в основном является 3D-игрой. Я нашел несколько похожих вопросов, но так и не смог найти решение для меня. Я в основном использую GLFW для всего, но я хотел бы использовать перенасыщение для отображения текстов. У меня есть функция (на основе нескольких других вопросов, которые я нашел здесь), чтобы попробовать, как это работает, но я не уверен, как именно я могу сделать это для фактического отображения, потому что теперь все начинается, но текст не отображается , только мой трехмерный мир. Вот мой код:

//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....
...