Jump to content
  • Advertisement
Sign in to follow this  
z42

OpenGL Making a better push button.

This topic is 3763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bovinedragon
ive heard of "glui", never used it tho

http://glui.sourceforge.net/


I take it that most make their own components from scratch?

I should have been clearer in the first post, I don't mind doing it from scratch, just didn't know how to approach it correctly. I've done very little graphics/game programming prior to this, hence the questions...

Share this post


Link to post
Share on other sites
A free opensource is Crazy Eddie http://www.cegui.org.uk/wiki/index.php/Main_Page

to answer your question about list box and other components. Most of them are actually built using primitive components like button and editbox. For example the scrollbar is usually just a button you slide up and down.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!