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;
29 static float uiscale = 1.0f;
33 static struct dtx_font *uifont;
34 static utk_widget *uiroot;
36 static struct level *lvl;
39 int main(int argc, char **argv)
41 glutInit(&argc, argv);
42 glutInitWindowSize(1280, 800);
43 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
44 glutCreateWindow("dunger");
46 win_width = glutGet(GLUT_WINDOW_WIDTH);
47 win_height = glutGet(GLUT_WINDOW_HEIGHT);
49 glutDisplayFunc(display);
50 glutReshapeFunc(reshape);
51 glutKeyboardFunc(keyb);
52 glutKeyboardUpFunc(keyup);
54 glutMotionFunc(motion);
55 glutPassiveMotionFunc(motion);
71 glClearColor(0.15, 0.15, 0.15, 1);
73 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
74 fprintf(stderr, "failed to open uifont.ttf\n");
77 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
78 dtx_use_font(uifont, FONTSZ);
80 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
81 fprintf(stderr, "failed to initialized ubertk\n");
84 utk_set_color_func(ucolor);
85 utk_set_clip_func(uclip);
86 utk_set_image_func(uimage);
87 utk_set_rect_func(urect);
88 utk_set_line_func(uline);
89 utk_set_text_func(utext);
90 utk_set_text_spacing_func(utextspacing);
91 utk_set_text_width_func(utextwidth);
93 win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
94 utk_set_pos(win, 15, 15);
95 utk_button(win, "hello", 0, 0, 0, 0);
96 utk_button(win, "button 2", 0, 0, 0, 0);
97 utk_button(win, "button 3", 0, 0, 0, 0);
99 if(!(lvl = create_level(32, 32))) {
100 fprintf(stderr, "failed to create level\n");
107 static void cleanup(void)
110 dtx_close_font(uifont);
114 static void display(void)
116 int splitx = UISPLIT * uiscale;
118 view_width = win_width - splitx;
119 view_height = win_height;
121 glClear(GL_COLOR_BUFFER_BIT);
123 glMatrixMode(GL_MODELVIEW);
128 glMatrixMode(GL_PROJECTION);
130 glOrtho(0, splitx, win_height, 0, -1, 1);
131 glViewport(0, 0, splitx, win_height);
134 glColor3f(0.25, 0.25, 0.25);
136 glVertex2f(splitx, 0);
137 glVertex2f(splitx, win_height);
138 glVertex2f(0, win_height);
144 glMatrixMode(GL_PROJECTION);
146 glOrtho(0, view_width, 0, view_height, -1, 1);
147 glViewport(splitx, 0, view_width, view_height);
150 glColor3f(0.1, 0.1, 0.1);
152 glVertex2f(view_width, 0);
153 glVertex2f(view_width, view_height);
154 glVertex2f(0, view_height);
162 static void reshape(int x, int y)
168 utk_set_size(uiroot, x / uiscale, y / uiscale);
172 static void keyb(unsigned char key, int x, int y)
174 if(key == 27) exit(0);
175 utk_keyboard_event(key, 1);
179 static void keyup(unsigned char key, int x, int y)
181 utk_keyboard_event(key, 0);
185 static void mouse(int bn, int st, int x, int y)
187 int bidx = bn - GLUT_LEFT_BUTTON;
188 int press = st == GLUT_DOWN;
190 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
194 static void motion(int x, int y)
196 utk_mmotion_event(x / uiscale, y / uiscale);
200 static void ucolor(int r, int g, int b, int a)
202 glColor4ub(r, g, b, a);
205 static void uclip(int x1, int y1, int x2, int y2)
207 if(!(x1 | y1 | x2 | y2)) {
208 glDisable(GL_SCISSOR_TEST);
210 glEnable(GL_SCISSOR_TEST);
218 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
221 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
224 glRasterPos2f(x * uiscale, y * uiscale);
225 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
228 static void urect(int x1, int y1, int x2, int y2)
230 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
233 static void uline(int x1, int y1, int x2, int y2, int width)
237 glVertex2f(x1 * uiscale, y1 * uiscale);
238 glVertex2f(x2 * uiscale, y2 * uiscale);
242 static void utext(int x, int y, const char *txt, int sz)
244 glMatrixMode(GL_PROJECTION);
246 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
247 glScalef(uiscale, -uiscale, 1);
255 static int utextspacing(void)
257 return dtx_line_height();
260 static int utextwidth(const char *txt, int sz)
262 return dtx_string_width(txt);