5 #include <utk/cubertk.h>
10 static int init(void);
11 static void cleanup(void);
12 static void display(void);
13 static void reshape(int x, int y);
14 static void keyb(unsigned char key, int x, int y);
15 static void keyup(unsigned char key, int x, int y);
16 static void mouse(int bn, int st, int x, int y);
17 static void motion(int x, int y);
19 static void ucolor(int r, int g, int b, int a);
20 static void uclip(int x1, int y1, int x2, int y2);
21 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
22 static void urect(int x1, int y1, int x2, int y2);
23 static void uline(int x1, int y1, int x2, int y2, int width);
24 static void utext(int x, int y, const char *txt, int sz);
25 static int utextspacing(void);
26 static int utextwidth(const char *txt, int sz);
28 static int parse_args(int argc, char **argv);
31 int win_width, win_height;
32 int view_width, view_height;
33 float view_panx, view_pany, view_zoom = 1.0f;
35 static int bnstate[8];
36 int mousex, mousey, clickx, clicky;
38 static float uiscale = 1.0f;
43 static struct dtx_font *uifont;
44 static utk_widget *uiroot;
46 static struct level *lvl;
49 int main(int argc, char **argv)
51 glutInit(&argc, argv);
53 if(parse_args(argc, argv) == -1) {
57 glutInitWindowSize(1280, 800);
58 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
59 glutCreateWindow("dunger");
61 win_width = glutGet(GLUT_WINDOW_WIDTH);
62 win_height = glutGet(GLUT_WINDOW_HEIGHT);
64 glutDisplayFunc(display);
65 glutReshapeFunc(reshape);
66 glutKeyboardFunc(keyb);
67 glutKeyboardUpFunc(keyup);
69 glutMotionFunc(motion);
70 glutPassiveMotionFunc(motion);
86 glEnable(GL_MULTISAMPLE);
88 glClearColor(0.15, 0.15, 0.15, 1);
90 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
91 fprintf(stderr, "failed to open uifont.ttf\n");
94 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
95 dtx_use_font(uifont, FONTSZ);
97 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
98 fprintf(stderr, "failed to initialized ubertk\n");
101 utk_set_color_func(ucolor);
102 utk_set_clip_func(uclip);
103 utk_set_image_func(uimage);
104 utk_set_rect_func(urect);
105 utk_set_line_func(uline);
106 utk_set_text_func(utext);
107 utk_set_text_spacing_func(utextspacing);
108 utk_set_text_width_func(utextwidth);
110 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
111 utk_set_pos(win, 15, 15);
112 utk_button(win, "hello", 0, 0, 0, 0);
113 utk_button(win, "button 2", 0, 0, 0, 0);
114 utk_button(win, "button 3", 0, 0, 0, 0);
116 if(!(lvl = create_level(32, 32))) {
117 fprintf(stderr, "failed to create level\n");
120 if(init_lview(lvl) == -1) {
124 splitx = UISPLIT * uiscale;
125 view_width = win_width - splitx;
126 view_height = win_height;
131 static void cleanup(void)
135 dtx_close_font(uifont);
139 static void display(void)
141 glClear(GL_COLOR_BUFFER_BIT);
143 glMatrixMode(GL_MODELVIEW);
148 glMatrixMode(GL_PROJECTION);
150 glOrtho(0, splitx, win_height, 0, -1, 1);
151 glViewport(0, 0, splitx, win_height);
154 glColor3f(0.25, 0.25, 0.25);
156 glVertex2f(splitx, 0);
157 glVertex2f(splitx, win_height);
158 glVertex2f(0, win_height);
164 glMatrixMode(GL_PROJECTION);
166 glOrtho(0, view_width, 0, view_height, -1, 1);
167 glViewport(splitx, 0, view_width, view_height);
170 glColor3f(0.1, 0.1, 0.1);
172 glVertex2f(view_width, 0);
173 glVertex2f(view_width, view_height);
174 glVertex2f(0, view_height);
182 static void reshape(int x, int y)
188 utk_set_size(uiroot, x / uiscale, y / uiscale);
191 lview_viewport(splitx, 0, x - splitx, y);
194 static void keyb(unsigned char key, int x, int y)
196 if(key == 27) exit(0);
197 utk_keyboard_event(key, 1);
201 static void keyup(unsigned char key, int x, int y)
203 utk_keyboard_event(key, 0);
207 static void mouse(int bn, int st, int x, int y)
209 int bidx = bn - GLUT_LEFT_BUTTON;
210 int press = st == GLUT_DOWN;
212 bnstate[bidx] = press;
221 clickx = clicky = -1;
224 if(press) view_zoom += 0.1;
226 if(press) view_zoom -= 0.1;
229 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
233 static void motion(int x, int y)
241 if(clickx >= splitx) {
250 utk_mmotion_event(x / uiscale, y / uiscale);
254 static int parse_args(int argc, char **argv)
258 for(i=1; i<argc; i++) {
259 if(argv[i][0] == '-') {
260 if(strcmp(argv[i], "-uiscale") == 0) {
261 if(!argv[++i] || !(uiscale = atoi(argv[i]))) {
262 fprintf(stderr, "-uiscale should be followed by a positive number\n");
265 } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
266 printf("Usage: %s [options]\n", argv[0]);
267 printf("Options:\n");
268 printf(" -uiscale <scale>: UI scale factor (default: 1)\n");
269 printf(" -h,-help: print usage and exit\n");
272 fprintf(stderr, "unknown option: %s\n", argv[i]);
276 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
283 /* --- ubertk callbacks --- */
285 static void ucolor(int r, int g, int b, int a)
287 glColor4ub(r, g, b, a);
290 static void uclip(int x1, int y1, int x2, int y2)
292 if(!(x1 | y1 | x2 | y2)) {
293 glDisable(GL_SCISSOR_TEST);
295 glEnable(GL_SCISSOR_TEST);
303 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
306 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
309 glRasterPos2f(x * uiscale, y * uiscale);
310 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
313 static void urect(int x1, int y1, int x2, int y2)
315 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
318 static void uline(int x1, int y1, int x2, int y2, int width)
322 glVertex2f(x1 * uiscale, y1 * uiscale);
323 glVertex2f(x2 * uiscale, y2 * uiscale);
327 static void utext(int x, int y, const char *txt, int sz)
329 glMatrixMode(GL_PROJECTION);
331 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
332 glScalef(uiscale, -uiscale, 1);
340 static int utextspacing(void)
342 return dtx_line_height();
345 static int utextwidth(const char *txt, int sz)
347 return dtx_string_width(txt);