5 #include <cgmath/cgmath.h>
12 static int init(void);
15 long start_time, show_until;
20 static struct message *msglist;
28 static struct text *txlist;
30 static long timeout = 2000;
31 static long trans_time = 250;
33 int ui_font_size = FONTSZ;
35 void set_message_timeout(long tm)
40 void osd_printf(const char *fmt, ...)
44 show_messagev(timeout, 1, 1, 1, fmt, ap);
48 void show_message(long timeout, float r, float g, float b, const char *fmt, ...)
52 show_messagev(timeout, r, g, b, fmt, ap);
56 void show_messagev(long timeout, float r, float g, float b, const char *fmt, va_list ap)
65 vsnprintf(buf, sizeof buf, fmt, ap);
67 if(!(msg = malloc(sizeof *msg))) {
68 perror("failed to allocate memory");
72 if(!(msg->str = malloc(len + 1))) {
73 perror("failed to allocate memory");
76 memcpy(msg->str, buf, len + 1);
77 msg->start_time = time_msec;
78 msg->show_until = time_msec + timeout;
84 struct message *prev = &dummy;
85 while(prev->next && prev->next->show_until < msg->show_until) {
88 msg->next = prev->next;
93 void print_text(float x, float y, float r, float g, float b, const char *fmt, ...)
97 print_textv(x, y, r, g, b, fmt, ap);
101 void print_textv(float x, float y, float r, float g, float b, const char *fmt, va_list ap)
109 vsnprintf(buf, sizeof buf, fmt, ap);
111 if(!(tx = malloc(sizeof *tx))) {
112 perror("failed to allocate memory");
116 if(!(tx->str = malloc(len + 1))) {
117 perror("failed to allocate memory");
120 memcpy(tx->str, buf, len + 1);
135 while(msglist && msglist->show_until <= time_msec) {
136 struct message *msg = msglist;
142 dtx_use_font(ui_font, ui_font_size);
144 glMatrixMode(GL_PROJECTION);
147 glOrtho(0, win_width, -win_height, 0, -1, 1);
148 glMatrixMode(GL_MODELVIEW);
152 glPushAttrib(GL_ENABLE_BIT);
153 glDisable(GL_LIGHTING);
154 glDisable(GL_DEPTH_TEST);
156 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
159 struct message *msg = msglist;
161 long t = time_msec - msg->start_time;
162 long dur = msg->show_until - msg->start_time;
163 float alpha = cgm_smoothstep(0, trans_time, t) *
164 (1.0 - cgm_smoothstep(dur - trans_time, dur, t));
165 glColor4f(msg->color.x, msg->color.y, msg->color.z, alpha);
166 glTranslatef(0, -dtx_line_height(), 0);
167 dtx_string(msg->str);
172 struct text *tx = txlist;
173 txlist = txlist->next;
175 glMatrixMode(GL_MODELVIEW);
177 glTranslatef(tx->pos.x, tx->pos.y, 0);
179 glColor3f(tx->color.x, tx->color.y, tx->color.z);
188 glMatrixMode(GL_PROJECTION);
190 glMatrixMode(GL_MODELVIEW);
196 static int done_init;
197 if(done_init) return 0;
201 if(!(ui_font = dtx_open_font("data/ui.font", 0))) {
202 fprintf(stderr, "failed to open font: data/ui.font\n");
205 dtx_prepare_range(ui_font, ui_font_size, 32, 127);