curvedraw

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