4 #include <utk/cubertk.h>
8 static void cleanup(void);
9 static void display(void);
10 static void reshape(int x, int y);
11 static void keyb(unsigned char key, int x, int y);
12 static void keyup(unsigned char key, int x, int y);
13 static void mouse(int bn, int st, int x, int y);
14 static void motion(int x, int y);
16 static void ucolor(int r, int g, int b, int a);
17 static void uclip(int x1, int y1, int x2, int y2);
18 static void uimage(int x, int y, const void *pix, int xsz, int ysz);
19 static void urect(int x1, int y1, int x2, int y2);
20 static void uline(int x1, int y1, int x2, int y2, int width);
21 static void utext(int x, int y, const char *txt, int sz);
22 static int utextspacing(void);
23 static int utextwidth(const char *txt, int sz);
25 static int win_width, win_height;
26 static float uiscale = 1.0f;
29 static struct dtx_font *uifont;
30 static utk_widget *uiroot;
33 int main(int argc, char **argv)
35 glutInit(&argc, argv);
36 glutInitWindowSize(1280, 800);
37 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
38 glutCreateWindow("dunger");
40 win_width = glutGet(GLUT_WINDOW_WIDTH);
41 win_height = glutGet(GLUT_WINDOW_HEIGHT);
43 glutDisplayFunc(display);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutKeyboardUpFunc(keyup);
48 glutMotionFunc(motion);
49 glutPassiveMotionFunc(motion);
65 glClearColor(0.15, 0.15, 0.15, 1);
67 if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
68 fprintf(stderr, "failed to open uifont.ttf\n");
71 dtx_prepare_range(uifont, FONTSZ, ' ', 'z');
72 dtx_use_font(uifont, FONTSZ);
74 if(!(uiroot = utk_init(win_width / uiscale, win_height / uiscale))) {
75 fprintf(stderr, "failed to initialized ubertk\n");
78 utk_set_color_func(ucolor);
79 utk_set_clip_func(uclip);
80 utk_set_image_func(uimage);
81 utk_set_rect_func(urect);
82 utk_set_line_func(uline);
83 utk_set_text_func(utext);
84 utk_set_text_spacing_func(utextspacing);
85 utk_set_text_width_func(utextwidth);
87 win = utk_window(uiroot, 20, 20, 100, 100, "window");
93 static void cleanup(void)
95 dtx_close_font(uifont);
99 static void display(void)
101 glClear(GL_COLOR_BUFFER_BIT);
103 glMatrixMode(GL_MODELVIEW);
111 static void reshape(int x, int y)
116 glViewport(0, 0, x, y);
117 glMatrixMode(GL_PROJECTION);
119 glOrtho(0, x, y, 0, -1, 1);
122 utk_set_size(uiroot, x / uiscale, y / uiscale);
126 static void keyb(unsigned char key, int x, int y)
128 if(key == 27) exit(0);
129 utk_keyboard_event(key, 1);
133 static void keyup(unsigned char key, int x, int y)
135 utk_keyboard_event(key, 0);
139 static void mouse(int bn, int st, int x, int y)
141 int bidx = bn - GLUT_LEFT_BUTTON;
142 int press = st == GLUT_DOWN;
144 utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
148 static void motion(int x, int y)
150 utk_mmotion_event(x / uiscale, y / uiscale);
154 static void ucolor(int r, int g, int b, int a)
156 glColor4ub(r, g, b, a);
159 static void uclip(int x1, int y1, int x2, int y2)
161 if(!(x1 | y1 | x2 | y2)) {
162 glDisable(GL_SCISSOR_TEST);
164 glEnable(GL_SCISSOR_TEST);
172 glScissor(x1, win_height - y2, x2 - x1, y2 - y1);
175 static void uimage(int x, int y, const void *pix, int xsz, int ysz)
178 glRasterPos2f(x * uiscale, y * uiscale);
179 glDrawPixels(xsz, ysz, GL_BGRA, GL_UNSIGNED_BYTE, pix);
182 static void urect(int x1, int y1, int x2, int y2)
184 glRectf(x1 * uiscale, y1 * uiscale, x2 * uiscale, y2 * uiscale);
187 static void uline(int x1, int y1, int x2, int y2, int width)
191 glVertex2f(x1 * uiscale, y1 * uiscale);
192 glVertex2f(x2 * uiscale, y2 * uiscale);
196 static void utext(int x, int y, const char *txt, int sz)
198 glMatrixMode(GL_PROJECTION);
200 glTranslatef(x * uiscale, (y - dtx_baseline()) * uiscale, 0);
201 glScalef(uiscale, -uiscale, 1);
209 static int utextspacing(void)
211 return dtx_line_height();
214 static int utextwidth(const char *txt, int sz)
216 return dtx_string_width(txt);