curvedraw

view src/widgets.cc @ 16:7f795f7fecd6

readme and COPYING
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 10:55:57 +0200
parents 2b7ae76c173f
children
line source
1 /*
2 curvedraw - a simple program to draw curves
3 Copyright (C) 2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdarg.h>
22 #ifdef _MSC_VER
23 #include <malloc.h>
24 #else
25 #include <alloca.h>
26 #endif
27 #include <drawtext.h>
28 #include "opengl.h"
29 #include "widgets.h"
31 #define FONT_FILE "data/droid_sans.ttf"
32 #define FONT_SIZE 24
34 static dtx_font *font;
36 static bool init_font()
37 {
38 if(font) return true;
40 if(!(font = dtx_open_font(FONT_FILE, FONT_SIZE))) {
41 static bool msg_printed;
42 if(!msg_printed) {
43 fprintf(stderr, "failed to load font %s\n", FONT_FILE);
44 msg_printed = true;
45 }
46 return false;
47 }
48 return true;
49 }
51 Widget::Widget()
52 {
53 text = 0;
54 }
56 Widget::~Widget()
57 {
58 delete [] text;
59 }
61 void Widget::set_position(const Vector2 &p)
62 {
63 pos = p;
64 }
66 const Vector2 &Widget::get_position() const
67 {
68 return pos;
69 }
71 void Widget::set_text(const char *str)
72 {
73 char *newtext = new char[strlen(str) + 1];
74 strcpy(newtext, str);
76 delete [] text;
77 text = newtext;
78 }
80 void Widget::set_textf(const char *str, ...)
81 {
82 va_list ap;
83 int sz = strlen(str) * 4;
84 char *buf = (char*)alloca(sz + 1);
86 va_start(ap, str);
87 vsnprintf(buf, sz, str, ap);
88 va_end(ap);
90 set_text(buf);
91 }
93 const char *Widget::get_text() const
94 {
95 return text;
96 }
98 // ---- label ----
100 void Label::draw() const
101 {
102 if(!init_font()) return;
104 glMatrixMode(GL_MODELVIEW);
105 glPushMatrix();
106 glTranslatef(pos.x, pos.y, 0);
107 glScalef(0.003, 0.003, 1);
109 glColor4f(1, 1, 1, 1);
110 dtx_string(text);
111 dtx_flush();
113 glPopMatrix();
114 }