curvedraw

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/widgets.cc	Tue Dec 15 07:15:53 2015 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <stdarg.h>
     1.8 +#include <drawtext.h>
     1.9 +#include "opengl.h"
    1.10 +#include "widgets.h"
    1.11 +
    1.12 +static dtx_font *font;
    1.13 +
    1.14 +static void init_font()
    1.15 +{
    1.16 +	if(font) return;
    1.17 +
    1.18 +	if(!(font = dtx_open_font("data/droid_sans.ttf", 16))) {
    1.19 +		fprintf(stderr, "failed to load font\n");
    1.20 +		abort();
    1.21 +	}
    1.22 +	dtx_use_font(font, 16);
    1.23 +}
    1.24 +
    1.25 +Widget::Widget()
    1.26 +{
    1.27 +	text = 0;
    1.28 +}
    1.29 +
    1.30 +Widget::~Widget()
    1.31 +{
    1.32 +	delete [] text;
    1.33 +}
    1.34 +
    1.35 +void Widget::set_position(const Vector2 &p)
    1.36 +{
    1.37 +	pos = p;
    1.38 +}
    1.39 +
    1.40 +const Vector2 &Widget::get_position() const
    1.41 +{
    1.42 +	return pos;
    1.43 +}
    1.44 +
    1.45 +void Widget::set_text(const char *str)
    1.46 +{
    1.47 +	text = new char[strlen(str) + 1];
    1.48 +	strcpy(text, str);
    1.49 +}
    1.50 +
    1.51 +void Widget::set_textf(const char *str, ...)
    1.52 +{
    1.53 +	va_list ap;
    1.54 +	int sz = strlen(str) * 4;
    1.55 +	char *buf = (char*)alloca(sz);
    1.56 +
    1.57 +	va_start(ap, str);
    1.58 +	vsnprintf(buf, sz - 1, text, ap);
    1.59 +	va_end(ap);
    1.60 +
    1.61 +	set_text(buf);
    1.62 +}
    1.63 +
    1.64 +const char *Widget::get_text() const
    1.65 +{
    1.66 +	return text;
    1.67 +}
    1.68 +
    1.69 +// ---- label ----
    1.70 +
    1.71 +void Label::draw() const
    1.72 +{
    1.73 +	init_font();
    1.74 +
    1.75 +	glMatrixMode(GL_MODELVIEW);
    1.76 +	glPushMatrix();
    1.77 +	glTranslatef(pos.x, pos.y, 0);
    1.78 +	glScalef(0.1, 0.1, 1);
    1.79 +
    1.80 +	dtx_string(text);
    1.81 +	dtx_flush();
    1.82 +
    1.83 +	glPopMatrix();
    1.84 +}