184 lines
5 KiB
C
184 lines
5 KiB
C
#include <GL/freeglut.h>
|
|
#include <GL/freeglut_std.h>
|
|
#include <GL/gl.h>
|
|
#include <GL/glext.h>
|
|
#include <GL/glu.h>
|
|
#include <stdio.h>
|
|
#include <math.h>
|
|
#include "ui.h"
|
|
|
|
int lastMouseButton = GLUT_LEFT_BUTTON;
|
|
ui_slider* draggedSlider = NULL;
|
|
|
|
// Ui stuff
|
|
void testButtonClick();
|
|
ui_button buttons[] = {
|
|
{
|
|
/* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } },
|
|
/* size */ { 100.0, 50.0, 0.0 },
|
|
/* onclick */ testButtonClick,
|
|
/* text */ "Test :)",
|
|
0
|
|
}
|
|
};
|
|
ui_slider sliders[] = {
|
|
{
|
|
/* pos */ { { 0.5, 0.7, 0.0 }, {-50.0, -5.0, 0.0 } },
|
|
/* size */ { 100.0, 10.0, 10.0 },
|
|
0.1
|
|
}
|
|
};
|
|
|
|
void testButtonClick() {
|
|
printf("Clicked!\n");
|
|
sliders[0].value = 0.5;
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void drawUi(void) {
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
// set ui camera
|
|
int wWidth = glutGet(GLUT_WINDOW_WIDTH);
|
|
int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
gluOrtho2D(0.0, wWidth, 0.0, wHeight);
|
|
|
|
// Draw buttons
|
|
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
|
|
ui_button_draw(b);
|
|
}
|
|
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
|
|
ui_slider_draw(s);
|
|
}
|
|
|
|
int err = glGetError();
|
|
if(err != 0) {
|
|
printf("opengl error %d: %s\n", err, gluErrorString(err));
|
|
}
|
|
}
|
|
|
|
void drawWorld(void) {
|
|
// set perspective camera
|
|
GLdouble wWidth = (GLdouble) glutGet(GLUT_WINDOW_WIDTH);
|
|
GLdouble wHeight = (GLdouble) glutGet(GLUT_WINDOW_HEIGHT);
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
gluLookAt(0.0, 10.0, 20.0, (0.5 - sliders[0].value) * 10.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
gluPerspective(90, wWidth / wHeight , 1.0, 100.0);
|
|
// Lights
|
|
glEnable(GL_LIGHTING);
|
|
glEnable(GL_LIGHT0);
|
|
GLfloat light1[] = { 0.0, 1.0, 0.0, 0.0 };
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light1);
|
|
// World
|
|
glBegin(GL_QUADS);
|
|
glColor3f(1.0, 1.0, 0.0);
|
|
glNormal3f(0.0, 1.0, 0.0);
|
|
glVertex3f(-50.0, 0.0, -50.0);
|
|
glVertex3f(50.0, 0.0, -50.0);
|
|
glVertex3f(50.0, 0.0, 50.0);
|
|
glVertex3f(-50.0, 0.0, 50.0);
|
|
glEnd();
|
|
}
|
|
|
|
void display(void) {
|
|
// Clear screen
|
|
glClearColor(0.0, 0.0, 0.0, 1.0); // Dark theme :)
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
drawWorld();
|
|
drawUi();
|
|
|
|
glutSwapBuffers();
|
|
glFlush();
|
|
}
|
|
|
|
void mouseEvent(int button, int state, int x, int y) {
|
|
if(state == 0) lastMouseButton = button;
|
|
if(state == 0 && button == GLUT_LEFT_BUTTON) {
|
|
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
|
|
if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) {
|
|
b->clicked = 1;
|
|
}
|
|
}
|
|
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
|
|
if(ui_slider_mouse_over(s, x, y)) {
|
|
ui_slider_onclick(s, x, y);
|
|
draggedSlider = s;
|
|
}
|
|
}
|
|
}
|
|
else if(state == 1 && button == GLUT_LEFT_BUTTON) {
|
|
draggedSlider = NULL;
|
|
for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) {
|
|
b->clicked = 0;
|
|
if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) {
|
|
b->onClick();
|
|
}
|
|
}
|
|
}
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void mouseMotionEvent(int x, int y) {
|
|
if(lastMouseButton == GLUT_LEFT_BUTTON) {
|
|
if(draggedSlider != NULL) {
|
|
if(ui_slider_mouse_over(draggedSlider, x, y)) {
|
|
ui_slider_onclick(draggedSlider, x, y);
|
|
}
|
|
}
|
|
}
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void mouseWheelEvent(int wheel, int dir, int x, int y) {
|
|
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
|
|
if(ui_slider_mouse_over(s, x, y)) {
|
|
s->value += dir * 0.05;
|
|
if(s->value < 0.0) s->value = 0.0;
|
|
if(s->value > 1.0) s->value = 1.0;
|
|
printf("Updated %p value to %f\n", s, s->value);
|
|
}
|
|
}
|
|
glutPostRedisplay();
|
|
}
|
|
|
|
void keyboardEvent(unsigned char c, int x, int y) {
|
|
if(c == '\e') {
|
|
glutLeaveMainLoop();
|
|
exit(0);
|
|
}
|
|
if(c == 'a') {
|
|
glutPostRedisplay();
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
/* initialize glut and window */
|
|
glutInit(&argc, argv);
|
|
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
|
|
glutInitWindowSize(800, 500);
|
|
glutInitWindowPosition(450, 450);
|
|
/* create window and set callbacks */
|
|
glutCreateWindow("World of Cow");
|
|
glutDisplayFunc(display);
|
|
glutMouseFunc(mouseEvent);
|
|
glutKeyboardFunc(keyboardEvent);
|
|
glutMouseWheelFunc(mouseWheelEvent);
|
|
glutMotionFunc(mouseMotionEvent);
|
|
|
|
/* set projection and camera */
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
gluOrtho2D(0.0, 800.0, 0.0, 500.0);
|
|
|
|
glutMainLoop();
|
|
|
|
return 0;
|
|
} |