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_new_ok(utk_event *ev, void *data);
21 static void cb_open(utk_event *ev, void *data);
22 static void cb_open_ok(utk_event *ev, void *data);
23 static void cb_save(utk_event *ev, void *data);
24 static void cb_save_ok(utk_event *ev, void *data);
26 static void cb_cancel(utk_event *ev, void *data);
28 static int parse_args(int argc, char **argv);
31 static void ucolor(int r, int g, int b, int a);
32 static void uclip(int x1, int y1, int x2, int y2);
33 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
34 static void urect(int x1, int y1, int x2, int y2);
35 static void uline(int x1, int y1, int x2, int y2, int width);
36 static void utext(int x, int y, const char *txt, int sz);
37 static int utextspacing(void);
38 static int utextwidth(const char *txt, int sz);
41 int win_width, win_height;
42 int view_width, view_height;
43 float view_panx, view_pany, view_zoom = 1.0f;
45 static int bnstate[8];
46 int mousex, mousey, clickx, clicky;
48 static float uiscale = 1.0f;
53 static struct dtx_font *uifont;
54 static utk_widget *uiroot, *uiwin_new;
55 static utk_widget *uigrab;
56 static utk_widget *cbox_newsz;
58 static struct level lvl;
60 static const char *opt_fname;
63 int main(int argc, char **argv)
65 glutInit(&argc, argv);
67 if(parse_args(argc, argv) == -1) {
71 glutInitWindowSize(1280, 800);
72 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
73 glutCreateWindow("dunger");
75 win_width = glutGet(GLUT_WINDOW_WIDTH);
76 win_height = glutGet(GLUT_WINDOW_HEIGHT);
78 glutDisplayFunc(display);
79 glutReshapeFunc(reshape);
80 glutKeyboardFunc(keyb);
81 glutKeyboardUpFunc(keyup);
83 glutMotionFunc(motion);
84 glutPassiveMotionFunc(motion);
99 utk_widget *win, *hbox, *vbox;
101 glEnable(GL_MULTISAMPLE);
103 glClearColor(0.15, 0.15, 0.15, 1);
105 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
106 fprintf(stderr, "failed to open uifont.ttf\n");
109 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
110 dtx_use_font(uifont, FONTSZ);
112 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
113 fprintf(stderr, "failed to initialized ubertk\n");
116 utk_set_color_func(ucolor);
117 utk_set_clip_func(uclip);
118 utk_set_image_func(uimage);
119 utk_set_rect_func(urect);
120 utk_set_line_func(uline);
121 utk_set_text_func(utext);
122 utk_set_text_spacing_func(utextspacing);
123 utk_set_text_width_func(utextwidth);
125 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
126 utk_set_pos(win, 15, 15);
127 utk_button(win, "New", 0, 0, cb_new, 0);
128 utk_button(win, "Open ...", 0, 0, cb_open, 0);
129 utk_button(win, "Save ...", 0, 0, cb_save, 0);
131 uiwin_new = utk_window(uiroot, (win_width - 220) / 2, (win_height - 150) / 2,
132 220, 150, "New level");
133 vbox = utk_vbox(uiwin_new, UTK_DEF_PADDING, UTK_DEF_SPACING);
135 const char *items[] = {"small (16x16)", "medium (24x24)", "large (32x32)"};
136 cbox_newsz = utk_combobox_items(vbox, items, sizeof items / sizeof *items, 0, 0);
137 utk_select(cbox_newsz, 2);
139 hbox = utk_hbox(vbox, UTK_DEF_PADDING, UTK_DEF_SPACING);
140 utk_button(hbox, "OK", 0, 0, cb_new_ok, 0);
141 utk_button(hbox, "Cancel", 0, 0, cb_cancel, uiwin_new);
142 pad = utk_get_padding(uiwin_new);
143 utk_set_size(uiwin_new, utk_get_width(vbox) + pad * 2.0f, utk_get_height(vbox) + pad * 2.0f);
147 if(load_level(&lvl, opt_fname) == -1) {
148 fprintf(stderr, "failed to load level: %s\n", opt_fname);
152 if(init_level(&lvl, 32, 32) == -1) {
153 fprintf(stderr, "failed to create level\n");
157 if(init_lview(&lvl) == -1) {
161 splitx = UISPLIT * uiscale;
162 view_width = win_width - splitx;
163 view_height = win_height;
168 static void cleanup(void)
172 dtx_close_font(uifont);
176 static void display(void)
178 glClear(GL_COLOR_BUFFER_BIT);
180 glMatrixMode(GL_MODELVIEW);
185 glMatrixMode(GL_PROJECTION);
187 glOrtho(0, view_width, 0, view_height, -1, 1);
188 glViewport(splitx, 0, view_width, view_height);
191 glColor3f(0.1, 0.1, 0.1);
193 glVertex2f(view_width, 0);
194 glVertex2f(view_width, view_height);
195 glVertex2f(0, view_height);
202 glMatrixMode(GL_PROJECTION);
204 glOrtho(0, win_width, win_height, 0, -1, 1);
205 glViewport(0, 0, win_width, win_height);
208 glColor3f(0.25, 0.25, 0.25);
210 glVertex2f(splitx, 0);
211 glVertex2f(splitx, win_height);
212 glVertex2f(0, win_height);
216 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
224 static void reshape(int x, int y)
230 utk_set_size(uiroot, x / uiscale, y / uiscale);
233 lview_viewport(splitx, 0, x - splitx, y);
236 static void keyb(unsigned char key, int x, int y)
241 if(utk_is_dialog(uigrab)) {
242 utk_destroy_window(uigrab);
256 utk_keyboard_event(key, 1);
260 static void keyup(unsigned char key, int x, int y)
262 utk_keyboard_event(key, 0);
266 static void mouse(int bn, int st, int x, int y)
268 int bidx = bn - GLUT_LEFT_BUTTON;
269 int press = st == GLUT_DOWN;
271 bnstate[bidx] = press;
280 clickx = clicky = -1;
283 if(press) zoom_lview(0.1);
285 if(press) zoom_lview(-0.1);
289 lview_mbutton(bidx, press, x, y);
292 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
296 static void motion(int x, int y)
304 if(clickx >= splitx) {
314 utk_mmotion_event(x / uiscale, y / uiscale);
318 static void cb_new(utk_event *ev, void *data)
324 static void cb_new_ok(utk_event *ev, void *data)
326 static int levsz[] = {16, 24, 32};
330 sz = levsz[utk_get_selected(cbox_newsz)];
332 if(init_level(&newlvl, sz, sz) == -1) {
333 utk_message_dialog("failed to create new level", UTK_MSG_TYPE_ERROR,
334 UTK_MSG_BN_OK, cb_cancel, 0);
348 static void cb_open(utk_event *ev, void *data)
350 uigrab = utk_file_dialog(UTK_FILE_DIALOG_OPEN, 0, "Level file (*.lvl) [.lvl]", 0, cb_open_ok, 0);
353 static void cb_open_ok(utk_event *ev, void *data)
358 utk_widget *dlg = utk_event_widget(ev);
359 path = utk_file_dialog_path(dlg);
360 printf("selected: %s\n", path);
361 if(load_level(&lvl, path) == -1) {
362 errmsg = alloca(strlen(path) + 32);
363 sprintf(errmsg, "Failed to load level: %s", path);
365 utk_destroy_window(dlg);
366 if(uigrab == dlg) uigrab = 0;
369 uigrab = utk_message_dialog(errmsg, UTK_MSG_TYPE_ERROR, UTK_MSG_BN_OK, cb_cancel, 0);
373 static void cb_save(utk_event *ev, void *data)
375 uigrab = utk_file_dialog(UTK_FILE_DIALOG_SAVE, 0, "Level file (*.lvl) [.lvl]", 0, cb_save_ok, 0);
378 static void cb_save_ok(utk_event *ev, void *data)
383 utk_widget *dlg = utk_event_widget(ev);
384 path = utk_file_dialog_path(dlg);
385 printf("selected: %s\n", path);
386 if(save_level(&lvl, path) == -1) {
387 errmsg = alloca(strlen(path) + 32);
388 sprintf(errmsg, "Failed to save level file: %s", path);
390 utk_destroy_window(dlg);
391 if(uigrab == dlg) uigrab = 0;
394 uigrab = utk_message_dialog(errmsg, UTK_MSG_TYPE_ERROR, UTK_MSG_BN_OK, cb_cancel, 0);
398 static void cb_cancel(utk_event *ev, void *data)
400 if(!data) data = utk_get_window(utk_event_widget(ev));
402 if(utk_is_dialog(data)) {
403 utk_destroy_window(data);
410 static int parse_args(int argc, char **argv)
414 for(i=1; i<argc; i++) {
415 if(argv[i][0] == '-') {
416 if(strcmp(argv[i], "-uiscale") == 0) {
417 if(!argv[++i] || !(uiscale = atoi(argv[i]))) {
418 fprintf(stderr, "-uiscale should be followed by a positive number\n");
421 } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
422 printf("Usage: %s [options]\n", argv[0]);
423 printf("Options:\n");
424 printf(" -uiscale <scale>: UI scale factor (default: 1)\n");
425 printf(" -h,-help: print usage and exit\n");
428 fprintf(stderr, "unknown option: %s\n", argv[i]);
433 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
442 /* --- ubertk callbacks --- */
444 static void ucolor(int r, int g, int b, int a)
446 glColor4ub(r, g, b, a);
449 static void uclip(int x1, int y1, int x2, int y2)
451 if(!(x1 | y1 | x2 | y2)) {
452 glDisable(GL_SCISSOR_TEST);
454 glEnable(GL_SCISSOR_TEST);
462 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
465 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
468 glRasterPos2f(x * uiscale, y * uiscale);
469 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
472 static void urect(int x1, int y1, int x2, int y2)
474 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
477 static void uline(int x1, int y1, int x2, int y2, int width)
481 glVertex2f(x1 * uiscale, y1 * uiscale);
482 glVertex2f(x2 * uiscale, y2 * uiscale);
486 static void utext(int x, int y, const char *txt, int sz)
488 glMatrixMode(GL_PROJECTION);
490 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
491 glScalef(uiscale, -uiscale, 1);
499 static int utextspacing(void)
501 return dtx_line_height();
504 static int utextwidth(const char *txt, int sz)
506 return dtx_string_width(txt);