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;
32 static dtx_font *font;
34 void set_message_timeout(long tm)
39 void show_message(const char *fmt, ...)
43 show_messagev(timeout, Vec3(1, 1, 1), fmt, ap);
47 void show_message(long timeout, const Vec3 &color, const char *fmt, ...)
51 show_messagev(timeout, color, fmt, ap);
55 void show_messagev(long timeout, const Vec3 &color, const char *fmt, va_list ap)
61 vsnprintf(buf, sizeof buf, fmt, ap);
63 Message *msg = new Message;
64 int len = strlen(buf);
65 msg->str = new char[len + 1];
66 memcpy(msg->str, buf, len + 1);
67 msg->start_time = time_msec;
68 msg->show_until = time_msec + timeout;
73 Message *prev = &dummy;
74 while(prev->next && prev->next->show_until < msg->show_until) {
77 msg->next = prev->next;
82 void print_text(const Vec2 &pos, const Vec3 &color, const char *fmt, ...)
86 print_textv(pos, color, fmt, ap);
90 void print_textv(const Vec2 &pos, const Vec3 &color, const char *fmt, va_list ap)
96 vsnprintf(buf, sizeof buf, fmt, ap);
99 int len = strlen(buf);
100 tx->str = new char[len + 1];
101 memcpy(tx->str, buf, len + 1);
103 tx->pos = Vec2(pos.x, -pos.y);
113 while(msglist && msglist->show_until <= time_msec) {
114 Message *msg = msglist;
120 dtx_use_font(font, FONTSZ);
122 glMatrixMode(GL_PROJECTION);
125 glOrtho(0, win_width, -win_height, 0, -1, 1);
126 glMatrixMode(GL_MODELVIEW);
130 glPushAttrib(GL_ENABLE_BIT);
131 glDisable(GL_LIGHTING);
132 glDisable(GL_DEPTH_TEST);
134 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 Message *msg = msglist;
139 long t = time_msec - msg->start_time;
140 long dur = msg->show_until - msg->start_time;
141 float alpha = smoothstep(0, trans_time, t) *
142 (1.0 - smoothstep(dur - trans_time, dur, t));
143 glColor4f(msg->color.x, msg->color.y, msg->color.z, alpha);
144 glTranslatef(0, -dtx_line_height(), 0);
145 dtx_string(msg->str);
151 txlist = txlist->next;
153 glMatrixMode(GL_MODELVIEW);
155 glTranslatef(tx->pos.x, tx->pos.y, 0);
157 glColor3f(tx->color.x, tx->color.y, tx->color.z);
166 glMatrixMode(GL_PROJECTION);
168 glMatrixMode(GL_MODELVIEW);
174 static bool done_init;
175 if(done_init) return true;
179 if(!(font = dtx_open_font("data/ui.font", 0))) {
180 fprintf(stderr, "failed to open font: data/ui.font\n");
183 dtx_prepare_range(font, FONTSZ, 32, 127);