开发者

C++ OpenGL Game Menu Linking objects

Hey I'm currently trying to develop a game using C++ and OpenGL. I have started with the menus and have two separate files. a top level state machine written in c++ and a rendering of 5 boxes in ortho view to act like a menu in openGL.

How do I link each case to a rectangle/"button"? I'm not sure how to do this. (I have read a bit about clickable positions on screen although my game is able to scale to any size window, which makes implementing it this way tricky.)

THIS IS THE STATE MACHINE FILE CODE

#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;
}

THIS IS THE OPENGL FILE CODE

//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;
}


This tutorial explains how to handle mouse click events. Handle the click by walking an array of rectangles, testing each for whether it encloses the clicked point. Variable-size windows can be handled simply by storing normalised rectangles (e.g., map the screen's width and height to a 1024x1024 box), and likewise normalising the point before testing.


As an aside, you may want to have a look at Immediate mode GUI (imgui) concepts, that may help you in handling simply the interaction between your menu graphics and mouse events.

You may have a look at this tutorial, or source code from nvidia.

You could also find inspiration in AntTweakBar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜