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 cb_new(utk_event *ev, void *data);
20 static void cb_open(utk_event *ev, void *data);
21 static void cb_save(utk_event *ev, void *data);
23 static void cb_cancel(utk_event *ev, void *data);
24 static void cb_new_ok(utk_event *ev, void *data);
26 static int parse_args(int argc, char **argv);
29 static void ucolor(int r, int g, int b, int a);
30 static void uclip(int x1, int y1, int x2, int y2);
31 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
32 static void urect(int x1, int y1, int x2, int y2);
33 static void uline(int x1, int y1, int x2, int y2, int width);
34 static void utext(int x, int y, const char *txt, int sz);
35 static int utextspacing(void);
36 static int utextwidth(const char *txt, int sz);
39 int win_width, win_height;
40 int view_width, view_height;
41 float view_panx, view_pany, view_zoom = 1.0f;
43 static int bnstate[8];
44 int mousex, mousey, clickx, clicky;
46 static float uiscale = 1.0f;
51 static struct dtx_font *uifont;
52 static utk_widget *uiroot, *uiwin_new;
53 static utk_widget *uigrab;
54 static utk_widget *cbox_newsz;
56 static struct level *lvl;
59 int main(int argc, char **argv)
61 glutInit(&argc, argv);
63 if(parse_args(argc, argv) == -1) {
67 glutInitWindowSize(1280, 800);
68 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
69 glutCreateWindow("dunger");
71 win_width = glutGet(GLUT_WINDOW_WIDTH);
72 win_height = glutGet(GLUT_WINDOW_HEIGHT);
74 glutDisplayFunc(display);
75 glutReshapeFunc(reshape);
76 glutKeyboardFunc(keyb);
77 glutKeyboardUpFunc(keyup);
79 glutMotionFunc(motion);
80 glutPassiveMotionFunc(motion);
95 utk_widget *win, *hbox, *vbox;
97 glEnable(GL_MULTISAMPLE);
99 glClearColor(0.15, 0.15, 0.15, 1);
101 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
102 fprintf(stderr, "failed to open uifont.ttf\n");
105 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
106 dtx_use_font(uifont, FONTSZ);
108 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
109 fprintf(stderr, "failed to initialized ubertk\n");
112 utk_set_color_func(ucolor);
113 utk_set_clip_func(uclip);
114 utk_set_image_func(uimage);
115 utk_set_rect_func(urect);
116 utk_set_line_func(uline);
117 utk_set_text_func(utext);
118 utk_set_text_spacing_func(utextspacing);
119 utk_set_text_width_func(utextwidth);
121 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
122 utk_set_pos(win, 15, 15);
123 utk_button(win, "New", 0, 0, cb_new, 0);
124 utk_button(win, "Open ...", 0, 0, cb_open, 0);
125 utk_button(win, "Save ...", 0, 0, cb_save, 0);
127 uiwin_new = utk_window(uiroot, (win_width - 220) / 2, (win_height - 150) / 2,
128 220, 150, "New level");
129 vbox = utk_vbox(uiwin_new, UTK_DEF_PADDING, UTK_DEF_SPACING);
131 const char *items[] = {"small (16x16)", "medium (24x24)", "large (32x32)"};
132 cbox_newsz = utk_combobox_items(vbox, items, sizeof items / sizeof *items, 0, 0);
133 utk_select(cbox_newsz, 2);
135 hbox = utk_hbox(vbox, UTK_DEF_PADDING, UTK_DEF_SPACING);
136 utk_button(hbox, "OK", 0, 0, cb_new_ok, 0);
137 utk_button(hbox, "Cancel", 0, 0, cb_cancel, uiwin_new);
138 pad = utk_get_padding(uiwin_new);
139 utk_set_size(uiwin_new, utk_get_width(vbox) + pad * 2.0f, utk_get_height(vbox) + pad * 2.0f);
142 if(!(lvl = create_level(32, 32))) {
143 fprintf(stderr, "failed to create level\n");
146 if(init_lview(lvl) == -1) {
150 splitx = UISPLIT * uiscale;
151 view_width = win_width - splitx;
152 view_height = win_height;
157 static void cleanup(void)
161 dtx_close_font(uifont);
165 static void display(void)
167 glClear(GL_COLOR_BUFFER_BIT);
169 glMatrixMode(GL_MODELVIEW);
174 glMatrixMode(GL_PROJECTION);
176 glOrtho(0, view_width, 0, view_height, -1, 1);
177 glViewport(splitx, 0, view_width, view_height);
180 glColor3f(0.1, 0.1, 0.1);
182 glVertex2f(view_width, 0);
183 glVertex2f(view_width, view_height);
184 glVertex2f(0, view_height);
191 glMatrixMode(GL_PROJECTION);
193 glOrtho(0, win_width, win_height, 0, -1, 1);
194 glViewport(0, 0, win_width, win_height);
197 glColor3f(0.25, 0.25, 0.25);
199 glVertex2f(splitx, 0);
200 glVertex2f(splitx, win_height);
201 glVertex2f(0, win_height);
209 static void reshape(int x, int y)
215 utk_set_size(uiroot, x / uiscale, y / uiscale);
218 lview_viewport(splitx, 0, x - splitx, y);
221 static void keyb(unsigned char key, int x, int y)
237 utk_keyboard_event(key, 1);
241 static void keyup(unsigned char key, int x, int y)
243 utk_keyboard_event(key, 0);
247 static void mouse(int bn, int st, int x, int y)
249 int bidx = bn - GLUT_LEFT_BUTTON;
250 int press = st == GLUT_DOWN;
252 bnstate[bidx] = press;
261 clickx = clicky = -1;
264 if(press) zoom_lview(0.1);
266 if(press) zoom_lview(-0.1);
270 lview_mbutton(bidx, press, x, y);
273 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
277 static void motion(int x, int y)
285 if(clickx >= splitx) {
295 utk_mmotion_event(x / uiscale, y / uiscale);
299 static void cb_new(utk_event *ev, void *data)
305 static void cb_new_ok(utk_event *ev, void *data)
307 static int levsz[] = {16, 24, 32};
309 struct level *newlvl;
311 sz = levsz[utk_get_selected(cbox_newsz)];
313 if(!(newlvl = create_level(sz, sz))) {
314 fprintf(stderr, "failed to create new %dx%d level\n", sz, sz);
315 /* TODO: messagebox */
329 static void cb_open(utk_event *ev, void *data)
333 static void cb_save(utk_event *ev, void *data)
337 static void cb_cancel(utk_event *ev, void *data)
343 static int parse_args(int argc, char **argv)
347 for(i=1; i<argc; i++) {
348 if(argv[i][0] == '-') {
349 if(strcmp(argv[i], "-uiscale") == 0) {
350 if(!argv[++i] || !(uiscale = atoi(argv[i]))) {
351 fprintf(stderr, "-uiscale should be followed by a positive number\n");
354 } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
355 printf("Usage: %s [options]\n", argv[0]);
356 printf("Options:\n");
357 printf(" -uiscale <scale>: UI scale factor (default: 1)\n");
358 printf(" -h,-help: print usage and exit\n");
361 fprintf(stderr, "unknown option: %s\n", argv[i]);
365 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
372 /* --- ubertk callbacks --- */
374 static void ucolor(int r, int g, int b, int a)
376 glColor4ub(r, g, b, a);
379 static void uclip(int x1, int y1, int x2, int y2)
381 if(!(x1 | y1 | x2 | y2)) {
382 glDisable(GL_SCISSOR_TEST);
384 glEnable(GL_SCISSOR_TEST);
392 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
395 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
398 glRasterPos2f(x * uiscale, y * uiscale);
399 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
402 static void urect(int x1, int y1, int x2, int y2)
404 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
407 static void uline(int x1, int y1, int x2, int y2, int width)
411 glVertex2f(x1 * uiscale, y1 * uiscale);
412 glVertex2f(x2 * uiscale, y2 * uiscale);
416 static void utext(int x, int y, const char *txt, int sz)
418 glMatrixMode(GL_PROJECTION);
420 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
421 glScalef(uiscale, -uiscale, 1);
429 static int utextspacing(void)
431 return dtx_line_height();
434 static int utextwidth(const char *txt, int sz)
436 return dtx_string_width(txt);