fixed resolution
[faros-demo] / src / ui.cc
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <alloca.h>
4 #include <GL/glut.h>
5 #include "ui.h"
6
7 extern int win_width, win_height;
8
9 extern bool anim_stopped;
10 extern long anim_time;
11
12
13 bool init_ui()
14 {
15         return true;
16 }
17
18 void destroy_ui()
19 {
20 }
21
22 void ui()
23 {
24         glPushAttrib(GL_ENABLE_BIT);
25         glDisable(GL_DEPTH_TEST);
26
27         glMatrixMode(GL_MODELVIEW);
28         glPushMatrix();
29         glLoadIdentity();
30         glMatrixMode(GL_PROJECTION);
31         glPushMatrix();
32         glLoadIdentity();
33         glOrtho(0, win_width, win_height, 0, -1, 1);
34
35         glEnable(GL_BLEND);
36         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
37
38         if(anim_stopped) {
39                 glBegin(GL_QUADS);
40                 glColor3f(1, 1, 1);
41                 glVertex2f(10, 10);
42                 glVertex2f(10, 50);
43                 glVertex2f(22, 50);
44                 glVertex2f(22, 10);
45                 glVertex2f(30, 10);
46                 glVertex2f(30, 50);
47                 glVertex2f(42, 50);
48                 glVertex2f(42, 10);
49                 glEnd();
50         }
51
52         glColor3f(1, 0.9, 0.5);
53         gl_printf(win_width - 100, 20, "%4ld.%03ld", anim_time / 1000, anim_time % 1000);
54
55         glPopMatrix();
56         glMatrixMode(GL_MODELVIEW);
57         glPopMatrix();
58
59         glPopAttrib();
60 }
61
62 void gl_printf(int x, int y, const char *fmt, ...)
63 {
64         va_list ap;
65         int buf_size, curx, cury;
66         char *buf, tmp;
67
68         va_start(ap, fmt);
69         buf_size = vsnprintf(&tmp, 0, fmt, ap);
70         va_end(ap);
71
72         if(buf_size == -1) {
73                 buf_size = 512;
74         }
75
76         buf = (char*)alloca(buf_size + 1);
77         va_start(ap, fmt);
78         vsnprintf(buf, buf_size + 1, fmt, ap);
79         va_end(ap);
80
81         static const float tabstop = 4;
82         static const float line_spacing = 18;
83
84         curx = x;
85         cury = y;
86         glRasterPos2i(x, y);
87
88         while(*buf) {
89                 char c = *buf++;
90
91                 switch(c) {
92                 case '\r':
93                         if(*buf == '\n') ++buf;
94                 case '\n':
95                         cury += line_spacing;
96                         curx = x;
97                         glRasterPos2i(curx, cury);
98                         break;
99
100                 case '\t':
101                         curx = (curx / tabstop) * tabstop + tabstop;
102                         glRasterPos2i(curx, cury);
103                         break;
104
105                 default:
106                         glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
107                 }
108         }
109 }