curvedraw

view 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
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", 16))) {
16 fprintf(stderr, "failed to load font\n");
17 abort();
18 }
19 dtx_use_font(font, 16);
20 }
22 Widget::Widget()
23 {
24 text = 0;
25 }
27 Widget::~Widget()
28 {
29 delete [] text;
30 }
32 void Widget::set_position(const Vector2 &p)
33 {
34 pos = p;
35 }
37 const Vector2 &Widget::get_position() const
38 {
39 return pos;
40 }
42 void Widget::set_text(const char *str)
43 {
44 text = new char[strlen(str) + 1];
45 strcpy(text, str);
46 }
48 void Widget::set_textf(const char *str, ...)
49 {
50 va_list ap;
51 int sz = strlen(str) * 4;
52 char *buf = (char*)alloca(sz);
54 va_start(ap, str);
55 vsnprintf(buf, sz - 1, text, ap);
56 va_end(ap);
58 set_text(buf);
59 }
61 const char *Widget::get_text() const
62 {
63 return text;
64 }
66 // ---- label ----
68 void Label::draw() const
69 {
70 init_font();
72 glMatrixMode(GL_MODELVIEW);
73 glPushMatrix();
74 glTranslatef(pos.x, pos.y, 0);
75 glScalef(0.1, 0.1, 1);
77 dtx_string(text);
78 dtx_flush();
80 glPopMatrix();
81 }