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