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 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 | GLUT_MULTISAMPLE);
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 glEnable(GL_MULTISAMPLE);
78 glClearColor(0.15, 0.15, 0.15, 1);
80 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
81 fprintf(stderr, "failed to open uifont.ttf\n");
84 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
85 dtx_use_font(uifont, FONTSZ);
87 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
88 fprintf(stderr, "failed to initialized ubertk\n");
91 utk_set_color_func(ucolor);
92 utk_set_clip_func(uclip);
93 utk_set_image_func(uimage);
94 utk_set_rect_func(urect);
95 utk_set_line_func(uline);
96 utk_set_text_func(utext);
97 utk_set_text_spacing_func(utextspacing);
98 utk_set_text_width_func(utextwidth);
100 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
101 utk_set_pos(win, 15, 15);
102 utk_button(win, "hello", 0, 0, 0, 0);
103 utk_button(win, "button 2", 0, 0, 0, 0);
104 utk_button(win, "button 3", 0, 0, 0, 0);
106 if(!(lvl = create_level(32, 32))) {
107 fprintf(stderr, "failed to create level\n");
111 splitx = UISPLIT * uiscale;
112 view_width = win_width - splitx;
113 view_height = win_height;
118 static void cleanup(void)
121 dtx_close_font(uifont);
125 static void display(void)
127 glClear(GL_COLOR_BUFFER_BIT);
129 glMatrixMode(GL_MODELVIEW);
134 glMatrixMode(GL_PROJECTION);
136 glOrtho(0, splitx, win_height, 0, -1, 1);
137 glViewport(0, 0, splitx, win_height);
140 glColor3f(0.25, 0.25, 0.25);
142 glVertex2f(splitx, 0);
143 glVertex2f(splitx, win_height);
144 glVertex2f(0, win_height);
150 glMatrixMode(GL_PROJECTION);
152 glOrtho(0, view_width, 0, view_height, -1, 1);
153 glViewport(splitx, 0, view_width, view_height);
156 glColor3f(0.1, 0.1, 0.1);
158 glVertex2f(view_width, 0);
159 glVertex2f(view_width, view_height);
160 glVertex2f(0, view_height);
168 static void reshape(int x, int y)
174 utk_set_size(uiroot, x / uiscale, y / uiscale);
178 static void keyb(unsigned char key, int x, int y)
180 if(key == 27) exit(0);
181 utk_keyboard_event(key, 1);
185 static void keyup(unsigned char key, int x, int y)
187 utk_keyboard_event(key, 0);
191 static void mouse(int bn, int st, int x, int y)
193 int bidx = bn - GLUT_LEFT_BUTTON;
194 int press = st == GLUT_DOWN;
196 bnstate[bidx] = press;
205 clickx = clicky = -1;
208 if(press) view_zoom += 0.1;
210 if(press) view_zoom -= 0.1;
213 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
217 static void motion(int x, int y)
225 if(clickx >= splitx) {
232 utk_mmotion_event(x / uiscale, y / uiscale);
236 static void ucolor(int r, int g, int b, int a)
238 glColor4ub(r, g, b, a);
241 static void uclip(int x1, int y1, int x2, int y2)
243 if(!(x1 | y1 | x2 | y2)) {
244 glDisable(GL_SCISSOR_TEST);
246 glEnable(GL_SCISSOR_TEST);
254 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
257 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
260 glRasterPos2f(x * uiscale, y * uiscale);
261 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
264 static void urect(int x1, int y1, int x2, int y2)
266 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
269 static void uline(int x1, int y1, int x2, int y2, int width)
273 glVertex2f(x1 * uiscale, y1 * uiscale);
274 glVertex2f(x2 * uiscale, y2 * uiscale);
278 static void utext(int x, int y, const char *txt, int sz)
280 glMatrixMode(GL_PROJECTION);
282 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
283 glScalef(uiscale, -uiscale, 1);
291 static int utextspacing(void)
293 return dtx_line_height();
296 static int utextwidth(const char *txt, int sz)
298 return dtx_string_width(txt);