Below, wherever you see // PUSH-BUTTON is an attempt at making a simple button. It's a little crude. A simple red box, where clicking it within a particular area (as determined by the X and Y co-ordinates of the mouse) will show a print out that the button has been "clicked".
My question is whether someone knows a better way of doing the same thing? Often, I see in games all sorts of input components, some are even partially transparent. How about other input components? Drop-down boxes? Lists? Tabs? Is there a (3rd-party?) open-source OpenGL library that can help me? Are these things that I'll need to implement from scratch?
C++:
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <pthread.h>
#include <iostream>
#include <cstdlib>
#include "theMachine.h"
int main(int argc, char ** argv)
{
glutInit( & argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
glutInitWindowSize(1200, 600);
glutInitWindowPosition(0, 0);
window = glutCreateWindow("The Machine");
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(specialKeyPressed);
glutMouseFunc(mouse);
//glutPassiveMotionFunc(constantPassiveMoveMouse);
//glutMotionFunc(constantMoveMouse);
InitGL(1200, 600);
glutMainLoop();
return 0;
}
void InitGL(GLsizei Width, GLsizei Height)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void ReSizeGLScene(int Width, int Height)
{
if(Height == 0)
{
Height = 1;
}
width = Width;
height = Height;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0, 1., 0.0);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-0.5, 0.0, 0.0);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5, 0.0, 0.0);
glEnd();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// PUSH-BUTTON
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0, 0.0);
glVertex2f(80.0, 0.0);
glVertex2f(80.0, 20.0);
glVertex2f(0.0, 20.0);
glEnd();
glutSwapBuffers();
}
void keyPressed(unsigned char key, int x, int y)
{
nap();
switch(key)
{
case ESCAPE:
case 'Q':
case 'q':
glutDestroyWindow(window);
exit(1);
break;
default:
std::cout << "Key " << key << " pressed. No action there yet."
<< std::endl;
break;
}
}
void specialKeyPressed(int key, int x, int y)
{
nap();
switch(key)
{
case GLUT_KEY_PAGE_UP:
break;
case GLUT_KEY_PAGE_DOWN:
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
nap();
std::cout << "X: " << x << " Y: " << y << std::endl;
switch(button)
{
case GLUT_LEFT_BUTTON:
if(GLUT_DOWN == state)
{
std::cout << "Left mouse button down." << std::endl;
// PUSH-BUTTON
if((x < 81) && (y < 21))
{
std::cout << "The \"button\" was pressed." << std::endl;
}
}
else if(GLUT_UP == state)
{
std::cout << "Left mouse button up." << std::endl;
}
break;
case GLUT_MIDDLE_BUTTON:
if(GLUT_DOWN == state)
{
std::cout << "Middle mouse button down." << std::endl;
}
else if(GLUT_UP == state)
{
std::cout << "Middle mouse button up." << std::endl;
}
break;
case GLUT_RIGHT_BUTTON:
if(GLUT_DOWN == state)
{
std::cout << "Right mouse button down." << std::endl;
}
else if(GLUT_UP == state)
{
std::cout << "Right mouse button up." << std::endl;
}
break;
default:
break;
}
}
void constantPassiveMoveMouse(int x, int y)
{
std::cout << "X: " << x << " Y: " << y << std::endl;
}
void constantMoveMouse(int x, int y)
{
std::cout << "* X: " << x << " Y: " << y << std::endl;
}
void nap()
{
const struct timespec ts = {0, 800};
struct timespec remainder;
nanosleep( & ts, & remainder);
}
Header:
#ifndef THEMACHINE_H
#define THEMACHINE_H
#define ESCAPE 27
int window;
float angle = 0.0;
int width;
int height;
void InitGL(int Width, int Height);
void keyPressed(unsigned char key, int x, int y);
void specialKeyPressed(int key, int x, int y);
void DrawGLScene();
void ReSizeGLScene(GLsizei Width, GLsizei Height);
void mouse(int button, int state, int x, int y);
void constantPassiveMoveMouse(int x, int y);
void constantMoveMouse(int x, int y);
void nap();
#endif
Unless specified otherwise, I am developing in a Linux (Ubuntu 7.10) operating system.