X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fui.cc;h=78c7ece0274a9442b011fd38dd6074a577b0c990;hp=33fbccf60cc838aa507c775095a4c2ed7e75fdc7;hb=6ef619c6d92c698728576a4ec1c798a0f716d9a4;hpb=c64bd959ffb4034cb288780f13a351b00fb22ca0 diff --git a/src/ui.cc b/src/ui.cc index 33fbccf..78c7ece 100644 --- a/src/ui.cc +++ b/src/ui.cc @@ -14,13 +14,23 @@ static bool init(); struct Message { long start_time, show_until; char *str; + Vec3 color; Message *next; }; static Message *msglist; +struct Text { + char *str; + Vec2 pos; + Vec3 color; + Text *next; +}; +static Text *txlist; + static long timeout = 2000; static long trans_time = 250; -static dtx_font *font; +dtx_font *ui_font; +int ui_font_size = FONTSZ; void set_message_timeout(long tm) { @@ -30,13 +40,26 @@ void set_message_timeout(long tm) void show_message(const char *fmt, ...) { va_list ap; + va_start(ap, fmt); + show_messagev(timeout, Vec3(1, 1, 1), fmt, ap); + va_end(ap); +} + +void show_message(long timeout, const Vec3 &color, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + show_messagev(timeout, color, fmt, ap); + va_end(ap); +} + +void show_messagev(long timeout, const Vec3 &color, const char *fmt, va_list ap) +{ char buf[512]; init(); - va_start(ap, fmt); vsnprintf(buf, sizeof buf, fmt, ap); - va_end(ap); Message *msg = new Message; int len = strlen(buf); @@ -44,6 +67,7 @@ void show_message(const char *fmt, ...) memcpy(msg->str, buf, len + 1); msg->start_time = time_msec; msg->show_until = time_msec + timeout; + msg->color = color; Message dummy; dummy.next = msglist; @@ -56,9 +80,36 @@ void show_message(const char *fmt, ...) msglist = dummy.next; } +void print_text(const Vec2 &pos, const Vec3 &color, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + print_textv(pos, color, fmt, ap); + va_end(ap); +} + +void print_textv(const Vec2 &pos, const Vec3 &color, const char *fmt, va_list ap) +{ + char buf[512]; + + init(); + + vsnprintf(buf, sizeof buf, fmt, ap); + + Text *tx = new Text; + int len = strlen(buf); + tx->str = new char[len + 1]; + memcpy(tx->str, buf, len + 1); + tx->color = color; + tx->pos = Vec2(pos.x, -pos.y); + + tx->next = txlist; + txlist = tx; +} + void draw_ui() { - if(!font) return; + if(!ui_font) return; while(msglist && msglist->show_until <= time_msec) { Message *msg = msglist; @@ -67,7 +118,7 @@ void draw_ui() delete msg; } - dtx_use_font(font, FONTSZ); + dtx_use_font(ui_font, ui_font_size); glMatrixMode(GL_PROJECTION); glPushMatrix(); @@ -90,12 +141,27 @@ void draw_ui() long dur = msg->show_until - msg->start_time; float alpha = smoothstep(0, trans_time, t) * (1.0 - smoothstep(dur - trans_time, dur, t)); - glColor4f(1.0, 0.5, 0.1, alpha); + glColor4f(msg->color.x, msg->color.y, msg->color.z, alpha); glTranslatef(0, -dtx_line_height(), 0); dtx_string(msg->str); msg = msg->next; } + while(txlist) { + Text *tx = txlist; + txlist = txlist->next; + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(tx->pos.x, tx->pos.y, 0); + + glColor3f(tx->color.x, tx->color.y, tx->color.z); + dtx_string(tx->str); + + delete [] tx->str; + delete tx; + } + glPopAttrib(); glMatrixMode(GL_PROJECTION); @@ -111,10 +177,10 @@ static bool init() done_init = true; - if(!(font = dtx_open_font("data/ui.font", 0))) { + if(!(ui_font = dtx_open_font("data/ui.font", 0))) { fprintf(stderr, "failed to open font: data/ui.font\n"); return false; } - dtx_prepare_range(font, FONTSZ, 32, 127); + dtx_prepare_range(ui_font, ui_font_size, 32, 127); return true; }