4 #include <gmath/gmath.h>
15 long start_time, show_until;
20 static Message *msglist;
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 show_message(const char *fmt, ...)
44 show_messagev(timeout, Vec3(1, 1, 1), fmt, ap);
48 void show_message(long timeout, const Vec3 &color, const char *fmt, ...)
52 show_messagev(timeout, color, fmt, ap);
56 void show_messagev(long timeout, const Vec3 &color, const char *fmt, va_list ap)
62 vsnprintf(buf, sizeof buf, fmt, ap);
64 Message *msg = new Message;
65 int len = strlen(buf);
66 msg->str = new char[len + 1];
67 memcpy(msg->str, buf, len + 1);
68 msg->start_time = time_msec;
69 msg->show_until = time_msec + timeout;
74 Message *prev = &dummy;
75 while(prev->next && prev->next->show_until < msg->show_until) {
78 msg->next = prev->next;
83 void print_text(const Vec2 &pos, const Vec3 &color, const char *fmt, ...)
87 print_textv(pos, color, fmt, ap);
91 void print_textv(const Vec2 &pos, const Vec3 &color, const char *fmt, va_list ap)
97 vsnprintf(buf, sizeof buf, fmt, ap);
100 int len = strlen(buf);
101 tx->str = new char[len + 1];
102 memcpy(tx->str, buf, len + 1);
104 tx->pos = Vec2(pos.x, -pos.y);
114 while(msglist && msglist->show_until <= time_msec) {
115 Message *msg = msglist;
121 dtx_use_font(ui_font, ui_font_size);
123 glMatrixMode(GL_PROJECTION);
126 glOrtho(0, win_width, -win_height, 0, -1, 1);
127 glMatrixMode(GL_MODELVIEW);
131 glPushAttrib(GL_ENABLE_BIT);
132 glDisable(GL_LIGHTING);
133 glDisable(GL_DEPTH_TEST);
135 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 Message *msg = msglist;
140 long t = time_msec - msg->start_time;
141 long dur = msg->show_until - msg->start_time;
142 float alpha = smoothstep(0, trans_time, t) *
143 (1.0 - smoothstep(dur - trans_time, dur, t));
144 glColor4f(msg->color.x, msg->color.y, msg->color.z, alpha);
145 glTranslatef(0, -dtx_line_height(), 0);
146 dtx_string(msg->str);
152 txlist = txlist->next;
154 glMatrixMode(GL_MODELVIEW);
156 glTranslatef(tx->pos.x, tx->pos.y, 0);
158 glColor3f(tx->color.x, tx->color.y, tx->color.z);
167 glMatrixMode(GL_PROJECTION);
169 glMatrixMode(GL_MODELVIEW);
175 static bool done_init;
176 if(done_init) return true;
180 if(!(ui_font = dtx_open_font("data/ui.font", 0))) {
181 fprintf(stderr, "failed to open font: data/ui.font\n");
184 dtx_prepare_range(ui_font, ui_font_size, 32, 127);