gameui

view test.cc @ 5:5a84873185ff

rudimentary theme plugin system and other minor fixes
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Mar 2014 01:50:01 +0200
parents f1014234dece
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
5 #include <GL/glut.h>
6 #include "goatkit.h"
8 static bool init();
9 static void cleanup();
10 static void disp();
11 static void idle();
12 static void reshape(int x, int y);
13 static void keypress(unsigned char key, int x, int y);
14 static void keyrelease(unsigned char key, int x, int y);
15 static void skeypress(int key, int x, int y);
16 static void skeyrelease(int key, int x, int y);
17 static void mouse(int bn, int st, int x, int y);
18 static void motion(int x, int y);
20 static std::vector<goatkit::Widget*> widgets;
22 int main(int argc, char **argv)
23 {
24 glutInitWindowSize(800, 600);
25 glutInit(&argc, argv);
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
27 glutCreateWindow("goatkit test");
29 glutDisplayFunc(disp);
30 glutIdleFunc(idle);
31 glutReshapeFunc(reshape);
32 glutKeyboardFunc(keypress);
33 glutKeyboardUpFunc(keyrelease);
34 glutSpecialFunc(skeypress);
35 glutSpecialUpFunc(skeyrelease);
36 glutMouseFunc(mouse);
37 glutMotionFunc(motion);
38 glutPassiveMotionFunc(motion);
40 if(!init()) {
41 return 1;
42 }
43 atexit(cleanup);
45 glutMainLoop();
46 return 0;
47 }
50 static bool init()
51 {
52 goatkit::Button *button = new goatkit::Button;
53 button->set_position(350, 280);
54 button->set_size(100, 40);
55 widgets.push_back(button);
57 return true;
58 }
60 static void cleanup()
61 {
62 for(size_t i=0; i<widgets.size(); i++) {
63 delete widgets[i];
64 }
65 }
67 static void disp()
68 {
69 glClearColor(0.2, 0.2, 0.2, 1.0);
70 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
72 for(size_t i=0; i<widgets.size(); i++) {
73 widgets[i]->draw();
74 }
76 glutSwapBuffers();
77 assert(glGetError() == GL_NO_ERROR);
78 }
80 static void idle()
81 {
82 glutPostRedisplay();
83 }
85 static void reshape(int x, int y)
86 {
87 glViewport(0, 0, x, y);
88 glMatrixMode(GL_PROJECTION);
89 glLoadIdentity();
90 glOrtho(0, x, 0, y, -1, 1);
91 }
93 static void keypress(unsigned char key, int x, int y)
94 {
95 if(key == 27) {
96 exit(0);
97 }
98 }
100 static void keyrelease(unsigned char key, int x, int y)
101 {
102 }
104 static void skeypress(int key, int x, int y)
105 {
106 }
108 static void skeyrelease(int key, int x, int y)
109 {
110 }
112 static void mouse(int bn, int st, int x, int y)
113 {
114 int bidx = bn - GLUT_LEFT_BUTTON;
115 bool down = st == GLUT_DOWN;
117 for(size_t i=0; i<widgets.size(); i++) {
118 goatkit::Widget *w = widgets[i];
120 if(w->hit_test(goatkit::Vec2(x, y))) {
121 goatkit::Event ev;
122 ev.type = goatkit::EV_MOUSE_BUTTON;
123 ev.button.button = bidx;
124 ev.button.press = down;
125 ev.button.pos = goatkit::Vec2(x, y);
126 w->handle_event(ev);
127 }
128 }
129 }
131 static void motion(int x, int y)
132 {
133 static goatkit::Widget *active;
135 if(active && !active->hit_test(goatkit::Vec2(x, y))) {
136 goatkit::Event ev;
137 ev.type = goatkit::EV_MOUSE_FOCUS;
138 ev.focus.enter = false;
139 active->handle_event(ev);
140 active = 0;
141 }
143 for(size_t i=0; i<widgets.size(); i++) {
144 goatkit::Widget *w = widgets[i];
146 if(w->hit_test(goatkit::Vec2(x, y))) {
147 if(active != w) {
148 goatkit::Event ev;
149 ev.type = goatkit::EV_MOUSE_FOCUS;
150 ev.focus.enter = true;
151 w->handle_event(ev);
152 active = w;
153 }
154 }
155 }
156 }