curvedraw

view src/widgets.cc @ 1:7dcd0f6113e5

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