world_of_cow/mmn_17.c

107 lines
2.9 KiB
C
Raw Normal View History

2023-07-03 15:40:28 +00:00
#include <GL/freeglut.h>
#include <GL/freeglut_std.h>
#include <GL/gl.h>
2023-07-05 08:37:27 +00:00
#include <GL/glu.h>
2023-07-03 15:40:28 +00:00
#include <stdio.h>
#include <math.h>
2023-07-05 08:37:27 +00:00
#include "ui.h"
// Ui stuff
ui_button buttons[] = {
{
/* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } },
/* size */ { 100.0, 50.0, 0.0 },
/* onclick */ NULL,
/* text */ "Test :)"
}
};
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
}
};
2023-07-03 15:40:28 +00:00
void display(void) {
printf("Drawing!\n");
2023-07-03 15:40:28 +00:00
// Clear screen
glClearColor(0.0, 0.0, 0.0, 1.0); // Dark theme :)
glClear(GL_COLOR_BUFFER_BIT);
2023-07-05 08:37:27 +00:00
// 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);
}
2023-07-05 08:37:27 +00:00
int err = glGetError();
if(err != 0) {
printf("opengl error %d: %s\n", err, gluErrorString(err));
}
glutSwapBuffers();
2023-07-03 15:40:28 +00:00
glFlush();
}
void mouseEvent(int button, int state, int x, int y) {
if(state == 1) {
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->onClick();
return;
}
}
}
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, button, state);
}
}
}
void mouseWheelEvent(int wheel, int dir, int x, int y) {
printf("wheel event\n");
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();
}
2023-07-03 15:40:28 +00:00
}
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 */
2023-07-05 08:37:27 +00:00
glutCreateWindow("World of Cow");
2023-07-03 15:40:28 +00:00
glutDisplayFunc(display);
glutMouseFunc(mouseEvent);
glutKeyboardFunc(keyboardEvent);
glutMouseWheelFunc(mouseWheelEvent);
2023-07-03 15:40:28 +00:00
/* set projection and camera */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 0.0, 500.0);
glutMainLoop();
return 0;
}