curvedraw
annotate src/widgets.cc @ 0:8e524989c904
getting there
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 15 Dec 2015 07:15:53 +0200 |
parents | |
children | 7dcd0f6113e5 |
rev | line source |
---|---|
nuclear@0 | 1 #include <stdio.h> |
nuclear@0 | 2 #include <stdlib.h> |
nuclear@0 | 3 #include <string.h> |
nuclear@0 | 4 #include <stdarg.h> |
nuclear@0 | 5 #include <drawtext.h> |
nuclear@0 | 6 #include "opengl.h" |
nuclear@0 | 7 #include "widgets.h" |
nuclear@0 | 8 |
nuclear@0 | 9 static dtx_font *font; |
nuclear@0 | 10 |
nuclear@0 | 11 static void init_font() |
nuclear@0 | 12 { |
nuclear@0 | 13 if(font) return; |
nuclear@0 | 14 |
nuclear@0 | 15 if(!(font = dtx_open_font("data/droid_sans.ttf", 16))) { |
nuclear@0 | 16 fprintf(stderr, "failed to load font\n"); |
nuclear@0 | 17 abort(); |
nuclear@0 | 18 } |
nuclear@0 | 19 dtx_use_font(font, 16); |
nuclear@0 | 20 } |
nuclear@0 | 21 |
nuclear@0 | 22 Widget::Widget() |
nuclear@0 | 23 { |
nuclear@0 | 24 text = 0; |
nuclear@0 | 25 } |
nuclear@0 | 26 |
nuclear@0 | 27 Widget::~Widget() |
nuclear@0 | 28 { |
nuclear@0 | 29 delete [] text; |
nuclear@0 | 30 } |
nuclear@0 | 31 |
nuclear@0 | 32 void Widget::set_position(const Vector2 &p) |
nuclear@0 | 33 { |
nuclear@0 | 34 pos = p; |
nuclear@0 | 35 } |
nuclear@0 | 36 |
nuclear@0 | 37 const Vector2 &Widget::get_position() const |
nuclear@0 | 38 { |
nuclear@0 | 39 return pos; |
nuclear@0 | 40 } |
nuclear@0 | 41 |
nuclear@0 | 42 void Widget::set_text(const char *str) |
nuclear@0 | 43 { |
nuclear@0 | 44 text = new char[strlen(str) + 1]; |
nuclear@0 | 45 strcpy(text, str); |
nuclear@0 | 46 } |
nuclear@0 | 47 |
nuclear@0 | 48 void Widget::set_textf(const char *str, ...) |
nuclear@0 | 49 { |
nuclear@0 | 50 va_list ap; |
nuclear@0 | 51 int sz = strlen(str) * 4; |
nuclear@0 | 52 char *buf = (char*)alloca(sz); |
nuclear@0 | 53 |
nuclear@0 | 54 va_start(ap, str); |
nuclear@0 | 55 vsnprintf(buf, sz - 1, text, ap); |
nuclear@0 | 56 va_end(ap); |
nuclear@0 | 57 |
nuclear@0 | 58 set_text(buf); |
nuclear@0 | 59 } |
nuclear@0 | 60 |
nuclear@0 | 61 const char *Widget::get_text() const |
nuclear@0 | 62 { |
nuclear@0 | 63 return text; |
nuclear@0 | 64 } |
nuclear@0 | 65 |
nuclear@0 | 66 // ---- label ---- |
nuclear@0 | 67 |
nuclear@0 | 68 void Label::draw() const |
nuclear@0 | 69 { |
nuclear@0 | 70 init_font(); |
nuclear@0 | 71 |
nuclear@0 | 72 glMatrixMode(GL_MODELVIEW); |
nuclear@0 | 73 glPushMatrix(); |
nuclear@0 | 74 glTranslatef(pos.x, pos.y, 0); |
nuclear@0 | 75 glScalef(0.1, 0.1, 1); |
nuclear@0 | 76 |
nuclear@0 | 77 dtx_string(text); |
nuclear@0 | 78 dtx_flush(); |
nuclear@0 | 79 |
nuclear@0 | 80 glPopMatrix(); |
nuclear@0 | 81 } |