4 #include <utk/cubertk.h>
9 static void cleanup(void);
10 static void display(void);
11 static void reshape(int x, int y);
12 static void keyb(unsigned char key, int x, int y);
13 static void keyup(unsigned char key, int x, int y);
14 static void mouse(int bn, int st, int x, int y);
15 static void motion(int x, int y);
17 static void ucolor(int r, int g, int b, int a);
18 static void uclip(int x1, int y1, int x2, int y2);
19 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
20 static void urect(int x1, int y1, int x2, int y2);
21 static void uline(int x1, int y1, int x2, int y2, int width);
22 static void utext(int x, int y, const char *txt, int sz);
23 static int utextspacing(void);
24 static int utextwidth(const char *txt, int sz);
26 int win_width, win_height;
27 int view_width, view_height;
28 float view_panx, view_pany, view_zoom = 1.0f;
30 static int bnstate[8];
31 static int mousex, mousey, clickx, clicky;
33 static float uiscale = 1.0f;
38 static struct dtx_font *uifont;
39 static utk_widget *uiroot;
41 static struct level *lvl;
44 int main(int argc, char **argv)
46 glutInit(&argc, argv);
47 glutInitWindowSize(1280, 800);
48 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
49 glutCreateWindow("dunger");
51 win_width = glutGet(GLUT_WINDOW_WIDTH);
52 win_height = glutGet(GLUT_WINDOW_HEIGHT);
54 glutDisplayFunc(display);
55 glutReshapeFunc(reshape);
56 glutKeyboardFunc(keyb);
57 glutKeyboardUpFunc(keyup);
59 glutMotionFunc(motion);
60 glutPassiveMotionFunc(motion);
76 glClearColor(0.15, 0.15, 0.15, 1);
78 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
79 fprintf(stderr, "failed to open uifont.ttf\n");
82 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
83 dtx_use_font(uifont, FONTSZ);
85 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
86 fprintf(stderr, "failed to initialized ubertk\n");
89 utk_set_color_func(ucolor);
90 utk_set_clip_func(uclip);
91 utk_set_image_func(uimage);
92 utk_set_rect_func(urect);
93 utk_set_line_func(uline);
94 utk_set_text_func(utext);
95 utk_set_text_spacing_func(utextspacing);
96 utk_set_text_width_func(utextwidth);
98 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
99 utk_set_pos(win, 15, 15);
100 utk_button(win, "hello", 0, 0, 0, 0);
101 utk_button(win, "button 2", 0, 0, 0, 0);
102 utk_button(win, "button 3", 0, 0, 0, 0);
104 if(!(lvl = create_level(32, 32))) {
105 fprintf(stderr, "failed to create level\n");
109 splitx = UISPLIT * uiscale;
110 view_width = win_width - splitx;
111 view_height = win_height;
116 static void cleanup(void)
119 dtx_close_font(uifont);
123 static void display(void)
125 glClear(GL_COLOR_BUFFER_BIT);
127 glMatrixMode(GL_MODELVIEW);
132 glMatrixMode(GL_PROJECTION);
134 glOrtho(0, splitx, win_height, 0, -1, 1);
135 glViewport(0, 0, splitx, win_height);
138 glColor3f(0.25, 0.25, 0.25);
140 glVertex2f(splitx, 0);
141 glVertex2f(splitx, win_height);
142 glVertex2f(0, win_height);
148 glMatrixMode(GL_PROJECTION);
150 glOrtho(0, view_width, 0, view_height, -1, 1);
151 glViewport(splitx, 0, view_width, view_height);
154 glColor3f(0.1, 0.1, 0.1);
156 glVertex2f(view_width, 0);
157 glVertex2f(view_width, view_height);
158 glVertex2f(0, view_height);
166 static void reshape(int x, int y)
172 utk_set_size(uiroot, x / uiscale, y / uiscale);
176 static void keyb(unsigned char key, int x, int y)
178 if(key == 27) exit(0);
179 utk_keyboard_event(key, 1);
183 static void keyup(unsigned char key, int x, int y)
185 utk_keyboard_event(key, 0);
189 static void mouse(int bn, int st, int x, int y)
191 int bidx = bn - GLUT_LEFT_BUTTON;
192 int press = st == GLUT_DOWN;
194 bnstate[bidx] = press;
203 clickx = clicky = -1;
206 if(press) view_zoom += 0.1;
208 if(press) view_zoom -= 0.1;
211 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
215 static void motion(int x, int y)
223 if(clickx >= splitx) {
230 utk_mmotion_event(x / uiscale, y / uiscale);
234 static void ucolor(int r, int g, int b, int a)
236 glColor4ub(r, g, b, a);
239 static void uclip(int x1, int y1, int x2, int y2)
241 if(!(x1 | y1 | x2 | y2)) {
242 glDisable(GL_SCISSOR_TEST);
244 glEnable(GL_SCISSOR_TEST);
252 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
255 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
258 glRasterPos2f(x * uiscale, y * uiscale);
259 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
262 static void urect(int x1, int y1, int x2, int y2)
264 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
267 static void uline(int x1, int y1, int x2, int y2, int width)
271 glVertex2f(x1 * uiscale, y1 * uiscale);
272 glVertex2f(x2 * uiscale, y2 * uiscale);
276 static void utext(int x, int y, const char *txt, int sz)
278 glMatrixMode(GL_PROJECTION);
280 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
281 glScalef(uiscale, -uiscale, 1);
289 static int utextspacing(void)
291 return dtx_line_height();
294 static int utextwidth(const char *txt, int sz)
296 return dtx_string_width(txt);