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;
54 static struct level *lvl;
57 int main(int argc, char **argv)
59 glutInit(&argc, argv);
61 if(parse_args(argc, argv) == -1) {
65 glutInitWindowSize(1280, 800);
66 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
67 glutCreateWindow("dunger");
69 win_width = glutGet(GLUT_WINDOW_WIDTH);
70 win_height = glutGet(GLUT_WINDOW_HEIGHT);
72 glutDisplayFunc(display);
73 glutReshapeFunc(reshape);
74 glutKeyboardFunc(keyb);
75 glutKeyboardUpFunc(keyup);
77 glutMotionFunc(motion);
78 glutPassiveMotionFunc(motion);
92 utk_widget *win, *box;
94 glEnable(GL_MULTISAMPLE);
96 glClearColor(0.15, 0.15, 0.15, 1);
98 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
99 fprintf(stderr, "failed to open uifont.ttf\n");
102 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
103 dtx_use_font(uifont, FONTSZ);
105 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
106 fprintf(stderr, "failed to initialized ubertk\n");
109 utk_set_color_func(ucolor);
110 utk_set_clip_func(uclip);
111 utk_set_image_func(uimage);
112 utk_set_rect_func(urect);
113 utk_set_line_func(uline);
114 utk_set_text_func(utext);
115 utk_set_text_spacing_func(utextspacing);
116 utk_set_text_width_func(utextwidth);
118 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
119 utk_set_pos(win, 15, 15);
120 utk_button(win, "New", 0, 0, cb_new, 0);
121 utk_button(win, "Open ...", 0, 0, cb_open, 0);
122 utk_button(win, "Save ...", 0, 0, cb_save, 0);
124 uiwin_new = utk_window(uiroot, 10, 10, 300, 200, "New level");
126 const char *items[] = {"small (8x8)", "medium (16x16)", "large (32x32)"};
127 utk_combobox_items(uiwin_new, items, sizeof items / sizeof *items, 0, 0);
129 box = utk_hbox(uiwin_new, UTK_DEF_PADDING, UTK_DEF_SPACING);
130 utk_button(box, "OK", 0, 0, cb_new_ok, 0);
131 utk_button(box, "Cancel", 0, 0, cb_cancel, uiwin_new);
133 if(!(lvl = create_level(32, 32))) {
134 fprintf(stderr, "failed to create level\n");
137 if(init_lview(lvl) == -1) {
141 splitx = UISPLIT * uiscale;
142 view_width = win_width - splitx;
143 view_height = win_height;
148 static void cleanup(void)
152 dtx_close_font(uifont);
156 static void display(void)
158 glClear(GL_COLOR_BUFFER_BIT);
160 glMatrixMode(GL_MODELVIEW);
165 glMatrixMode(GL_PROJECTION);
167 glOrtho(0, splitx, win_height, 0, -1, 1);
168 glViewport(0, 0, splitx, win_height);
171 glColor3f(0.25, 0.25, 0.25);
173 glVertex2f(splitx, 0);
174 glVertex2f(splitx, win_height);
175 glVertex2f(0, win_height);
181 glMatrixMode(GL_PROJECTION);
183 glOrtho(0, view_width, 0, view_height, -1, 1);
184 glViewport(splitx, 0, view_width, view_height);
187 glColor3f(0.1, 0.1, 0.1);
189 glVertex2f(view_width, 0);
190 glVertex2f(view_width, view_height);
191 glVertex2f(0, view_height);
199 static void reshape(int x, int y)
205 utk_set_size(uiroot, x / uiscale, y / uiscale);
208 lview_viewport(splitx, 0, x - splitx, y);
211 static void keyb(unsigned char key, int x, int y)
213 if(key == 27) exit(0);
214 utk_keyboard_event(key, 1);
218 static void keyup(unsigned char key, int x, int y)
220 utk_keyboard_event(key, 0);
224 static void mouse(int bn, int st, int x, int y)
226 int bidx = bn - GLUT_LEFT_BUTTON;
227 int press = st == GLUT_DOWN;
229 bnstate[bidx] = press;
238 clickx = clicky = -1;
241 if(press) view_zoom += 0.1;
243 if(press) view_zoom -= 0.1;
246 lview_mbutton(bidx, press, x, y);
248 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
252 static void motion(int x, int y)
260 if(clickx >= splitx) {
269 utk_mmotion_event(x / uiscale, y / uiscale);
273 static void cb_new(utk_event *ev, void *data)
278 static void cb_open(utk_event *ev, void *data)
282 static void cb_save(utk_event *ev, void *data)
286 static void cb_cancel(utk_event *ev, void *data)
291 static void cb_new_ok(utk_event *ev, void *data)
295 static int parse_args(int argc, char **argv)
299 for(i=1; i<argc; i++) {
300 if(argv[i][0] == '-') {
301 if(strcmp(argv[i], "-uiscale") == 0) {
302 if(!argv[++i] || !(uiscale = atoi(argv[i]))) {
303 fprintf(stderr, "-uiscale should be followed by a positive number\n");
306 } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
307 printf("Usage: %s [options]\n", argv[0]);
308 printf("Options:\n");
309 printf(" -uiscale <scale>: UI scale factor (default: 1)\n");
310 printf(" -h,-help: print usage and exit\n");
313 fprintf(stderr, "unknown option: %s\n", argv[i]);
317 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
324 /* --- ubertk callbacks --- */
326 static void ucolor(int r, int g, int b, int a)
328 glColor4ub(r, g, b, a);
331 static void uclip(int x1, int y1, int x2, int y2)
333 if(!(x1 | y1 | x2 | y2)) {
334 glDisable(GL_SCISSOR_TEST);
336 glEnable(GL_SCISSOR_TEST);
344 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
347 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
350 glRasterPos2f(x * uiscale, y * uiscale);
351 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
354 static void urect(int x1, int y1, int x2, int y2)
356 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
359 static void uline(int x1, int y1, int x2, int y2, int width)
363 glVertex2f(x1 * uiscale, y1 * uiscale);
364 glVertex2f(x2 * uiscale, y2 * uiscale);
368 static void utext(int x, int y, const char *txt, int sz)
370 glMatrixMode(GL_PROJECTION);
372 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
373 glScalef(uiscale, -uiscale, 1);
381 static int utextspacing(void)
383 return dtx_line_height();
386 static int utextwidth(const char *txt, int sz)
388 return dtx_string_width(txt);