curvedraw

view src/widgets.cc @ 6:6e980fddbf3b

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