【问题标题】:C++ OpenGL Game Menu Linking objectsC++ OpenGL 游戏菜单链接对象
【发布时间】:2013-03-11 08:06:05
【问题描述】:

嘿,我目前正在尝试使用 C++ 和 OpenGL 开发游戏。我从菜单开始,有两个单独的文件。一个用 C++ 编写的顶级状态机,并在正交视图中呈现 5 个框,以充当 openGL 中的菜单。

如何将每个案例链接到一个矩形/“按钮”?我不知道该怎么做。 (虽然我的游戏能够缩放到任何大小的窗口,但我已经阅读了一些关于屏幕上可点击位置的信息,这使得以这种方式实现它很棘手。)

这是状态机文件代码

#include <windows.h>
#include <gl\GL.h>
#include <GL\freeglut.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int startMenu(){

        int choice;

    cout << "1 new game" << endl;
    cout << "2 options" << endl;
    cout << "3 exit" << endl;
    cin >> choice;
    return choice;
}


void stateNewGame(){

    cout << "loads new game" << endl;
}

void stateOptionMenu(){

    cout << "loads options" << endl;
}


int main(int argc, char * argv []){

    bool exit = false;
    for (;;)
    {
        int choice = startMenu();
        switch(choice)
        {
        case(1):
            stateNewGame();
            break;

        case(2):
            stateOptionMenu();
            break;

        case(3):
            exit=true;
            break;
        default:
            cout<< "select again" << endl;
            break;
        }

    if (exit==true)
            break;
    }

    return 0;
}

这是 OPENGL 文件代码

//including windows, gl and glut for graphics
//stdio for buffering
#include <windows.h>
#include <gl\GL.h>
//#include <glut.h>
#include <GL\freeglut.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

//hold display list
GLuint displist; 

//two variables for window size
int width = 500;
int height = 500;


//orthogonal projection
void orthogonalStart (void) {
    //switch to projection matrix
    glMatrixMode(GL_PROJECTION);

    //start projections
    glPushMatrix();

    //clear previous instructions
    glLoadIdentity();

    //change window size to variables defined
    gluOrtho2D(0, width, 0, height);

    //switch back to model matrix
    glMatrixMode(GL_MODELVIEW);
}

void orthogonalEnd (void) {
    //Switch back to projection mode
    glMatrixMode(GL_PROJECTION);

    //Finish the calls above
    glPopMatrix();

    //Switch back to our model matrix
    glMatrixMode(GL_MODELVIEW);
}

void display (void) {

    //clearing draw buffer painting bkg red
    glClearColor (1.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

     //calling function
         orthogonalStart();

     //declare displist as display list 
     displist = glGenLists(1);

     //compile the new list
     glNewList(displist,GL_COMPILE); 

     //start projections
     glPushMatrix();


     for(int i=0; i<=4; i++){

        //draw a square
        //QUADSTRIP Improvement?
        glBegin(GL_QUADS);
        glColor3f(0,0,1);
        glVertex2f(100, 150);
        glColor3f(0,1,1);
        glVertex2f(100, 100);
        glColor3f(1,0,1);
        glVertex2f(400, 100);
        glColor3f(1,1,1);
        glVertex2f(400, 150);



        //end drawing
        glEnd();

        //end projections
        glPopMatrix();

        //translate start point
        glTranslatef(0, 75, 0);

       glFlush();

    }

   //end the list
       glEndList(); 


//call displist
glCallList(displist);

//calling function
orthogonalEnd();

//move contents of back buffer to front buffer
glutSwapBuffers();
glLoadIdentity ();
glFlush();
}

int main(int argc, char * argv []){

glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Basic Window");
glutDisplayFunc (display);
glutMainLoop ();
return 0;
}

【问题讨论】:

  • 顺便说一句,这是 C++,所以包括“cstdio”,而不是“stdio.h”。而且你真的应该正确缩进你的代码!

标签: c++ opengl button game-engine


【解决方案1】:

This tutorial 解释了如何处理鼠标点击事件。通过遍历一组矩形来处理单击,测试每个矩形是否包含单击的点。可变大小的窗口可以通过存储规范化的矩形(例如,将屏幕的宽度和高度映射到 1024x1024 框)来处理,同样在测试之前对点进行规范化。

【讨论】:

    【解决方案2】:

    顺便说一句,您可能想了解一下即时模式 GUI (imgui) 概念,它可以帮助您简单地处理菜单图形和鼠标事件之间的交互。

    你可以看看this tutorial,或source code from nvidia

    您也可以在AntTweakBar 中找到灵感。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 2016-01-07
      • 2017-02-13
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多