7c022c11862312b50648bdfa989cf01b1db5f06b
[vrlugburz] / tools / dunger / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h>
4 #include <utk/cubertk.h>
5 #include <drawtext.h>
6 #include "level.h"
7
8 static int init(void);
9 static void cleanup(void);
10 static void display(void);
11 static void reshape(int x, int y);
12 static void keyb(unsigned char key, int x, int y);
13 static void keyup(unsigned char key, int x, int y);
14 static void mouse(int bn, int st, int x, int y);
15 static void motion(int x, int y);
16
17 static void ucolor(int r, int g, int b, int a);
18 static void uclip(int x1, int y1, int x2, int y2);
19 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
20 static void urect(int x1, int y1, int x2, int y2);
21 static void uline(int x1, int y1, int x2, int y2, int width);
22 static void utext(int x, int y, const char *txt, int sz);
23 static int utextspacing(void);
24 static int utextwidth(const char *txt, int sz);
25
26 int win_width, win_height;
27 int view_width, view_height;
28
29 static float uiscale = 1.0f;
30 #define UISPLIT 150
31
32 #define FONTSZ  16
33 static struct dtx_font *uifont;
34 static utk_widget *uiroot;
35
36 static struct level *lvl;
37
38
39 int main(int argc, char **argv)
40 {
41         glutInit(&argc, argv);
42         glutInitWindowSize(1280, 800);
43         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
44         glutCreateWindow("dunger");
45
46         win_width = glutGet(GLUT_WINDOW_WIDTH);
47         win_height = glutGet(GLUT_WINDOW_HEIGHT);
48
49         glutDisplayFunc(display);
50         glutReshapeFunc(reshape);
51         glutKeyboardFunc(keyb);
52         glutKeyboardUpFunc(keyup);
53         glutMouseFunc(mouse);
54         glutMotionFunc(motion);
55         glutPassiveMotionFunc(motion);
56
57         if(init() == -1) {
58                 return 1;
59         }
60         atexit(cleanup);
61
62         glutMainLoop();
63         return 0;
64 }
65
66
67 static int init(void)
68 {
69         utk_widget *win;
70
71         glClearColor(0.15, 0.15, 0.15, 1);
72
73         if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
74                 fprintf(stderr, "failed to open uifont.ttf\n");
75                 return -1;
76         }
77         dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
78         dtx_use_font(uifont, FONTSZ);
79
80         if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
81                 fprintf(stderr, "failed to initialized ubertk\n");
82                 return -1;
83         }
84         utk_set_color_func(ucolor);
85         utk_set_clip_func(uclip);
86         utk_set_image_func(uimage);
87         utk_set_rect_func(urect);
88         utk_set_line_func(uline);
89         utk_set_text_func(utext);
90         utk_set_text_spacing_func(utextspacing);
91         utk_set_text_width_func(utextwidth);
92
93         win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
94         utk_set_pos(win, 15, 15);
95         utk_button(win, "hello", 0, 0, 0, 0);
96         utk_button(win, "button 2", 0, 0, 0, 0);
97         utk_button(win, "button 3", 0, 0, 0, 0);
98
99         if(!(lvl = create_level(32, 32))) {
100                 fprintf(stderr, "failed to create level\n");
101                 return -1;
102         }
103
104         return 0;
105 }
106
107 static void cleanup(void)
108 {
109         free_level(lvl);
110         dtx_close_font(uifont);
111         utk_close(uiroot);
112 }
113
114 static void display(void)
115 {
116         int splitx = UISPLIT * uiscale;
117
118         view_width = win_width - splitx;
119         view_height = win_height;
120
121         glClear(GL_COLOR_BUFFER_BIT);
122
123         glMatrixMode(GL_MODELVIEW);
124         glLoadIdentity();
125
126         /* draw UI */
127
128         glMatrixMode(GL_PROJECTION);
129         glLoadIdentity();
130         glOrtho(0, splitx, win_height, 0, -1, 1);
131         glViewport(0, 0, splitx, win_height);
132
133         glBegin(GL_QUADS);
134         glColor3f(0.25, 0.25, 0.25);
135         glVertex2f(0, 0);
136         glVertex2f(splitx, 0);
137         glVertex2f(splitx, win_height);
138         glVertex2f(0, win_height);
139         glEnd();
140         utk_draw(uiroot);
141
142         /* draw view */
143
144         glMatrixMode(GL_PROJECTION);
145         glLoadIdentity();
146         glOrtho(0, view_width, 0, view_height, -1, 1);
147         glViewport(splitx, 0, view_width, view_height);
148
149         glBegin(GL_QUADS);
150         glColor3f(0.1, 0.1, 0.1);
151         glVertex2f(0, 0);
152         glVertex2f(view_width, 0);
153         glVertex2f(view_width, view_height);
154         glVertex2f(0, view_height);
155         glEnd();
156
157         draw_level(lvl);
158
159         glutSwapBuffers();
160 }
161
162 static void reshape(int x, int y)
163 {
164         win_width = x;
165         win_height = y;
166
167         if(uiroot) {
168                 utk_set_size(uiroot, x / uiscale, y / uiscale);
169         }
170 }
171
172 static void keyb(unsigned char key, int x, int y)
173 {
174         if(key == 27) exit(0);
175         utk_keyboard_event(key, 1);
176         glutPostRedisplay();
177 }
178
179 static void keyup(unsigned char key, int x, int y)
180 {
181         utk_keyboard_event(key, 0);
182         glutPostRedisplay();
183 }
184
185 static void mouse(int bn, int st, int x, int y)
186 {
187         int bidx = bn - GLUT_LEFT_BUTTON;
188         int press = st == GLUT_DOWN;
189
190         utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
191         glutPostRedisplay();
192 }
193
194 static void motion(int x, int y)
195 {
196         utk_mmotion_event(x / uiscale, y / uiscale);
197         glutPostRedisplay();
198 }
199
200 static void ucolor(int r, int g, int b, int a)
201 {
202         glColor4ub(r, g, b, a);
203 }
204
205 static void uclip(int x1, int y1, int x2, int y2)
206 {
207         if(!(x1 | y1 | x2 | y2)) {
208                 glDisable(GL_SCISSOR_TEST);
209         } else {
210                 glEnable(GL_SCISSOR_TEST);
211         }
212
213         x1 *= uiscale;
214         y1 *= uiscale;
215         x2 *= uiscale;
216         y2 *= uiscale;
217
218         glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
219 }
220
221 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
222 {
223         glPixelZoom(1, -1);
224         glRasterPos2f(x * uiscale, y * uiscale);
225         glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
226 }
227
228 static void urect(int x1, int y1, int x2, int y2)
229 {
230         glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
231 }
232
233 static void uline(int x1, int y1, int x2, int y2, int width)
234 {
235         glLineWidth(width);
236         glBegin(GL_LINES);
237         glVertex2f(x1 * uiscale, y1 * uiscale);
238         glVertex2f(x2 * uiscale, y2 * uiscale);
239         glEnd();
240 }
241
242 static void utext(int x, int y, const char *txt, int sz)
243 {
244         glMatrixMode(GL_PROJECTION);
245         glPushMatrix();
246         glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
247         glScalef(uiscale, -uiscale, 1);
248
249         dtx_string(txt);
250         dtx_flush();
251
252         glPopMatrix();
253 }
254
255 static int utextspacing(void)
256 {
257         return dtx_line_height();
258 }
259
260 static int utextwidth(const char *txt, int sz)
261 {
262         return dtx_string_width(txt);
263 }