# HG changeset patch # User John Tsiombikas # Date 1316082095 -10800 # Node ID 8e93efcd23aea432df803312667abc1ed5147f9e # Parent 34130f58141acb6883cc29aecb90e6095120075f added simple example diff -r 34130f58141a -r 8e93efcd23ae .hgignore --- a/.hgignore Thu Sep 15 10:47:38 2011 +0300 +++ b/.hgignore Thu Sep 15 13:21:35 2011 +0300 @@ -1,6 +1,11 @@ -^libdrawtext\.a$ -^libdrawtext\.so.* +libdrawtext\.a +libdrawtext\.so drawtext\.dylib$ ^Makefile$ \.d$ \.o$ +\.swp$ +\.tar.gz$ +^examples/fonts +\.ttf$ +^examples/simple/simple$ diff -r 34130f58141a -r 8e93efcd23ae examples/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Makefile Thu Sep 15 13:21:35 2011 +0300 @@ -0,0 +1,10 @@ +fonts = fonts/FreeSerif.ttf + +.PHONY: all +all: $(fonts) + cd simple; $(MAKE) + +fonts/FreeSerif.ttf: + wget http://ftp.gnu.org/gnu/freefont/freefont-ttf-20100919.tar.gz + tar xzvf freefont-ttf-20100919.tar.gz + mv freefont-20100919 fonts diff -r 34130f58141a -r 8e93efcd23ae examples/simple/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple/Makefile Thu Sep 15 13:21:35 2011 +0300 @@ -0,0 +1,30 @@ +obj = simple.o +bin = simple + +CC = gcc +CFLAGS = -pedantic -Wall -g -I../../src +LDFLAGS = -L. -ldrawtext $(libgl) + +lib_so = libdrawtext.so +font = serif.ttf + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT +else + libgl = -lGL -lGLU -lglut +endif + +$(bin): $(obj) $(lib_so) $(font) + $(CC) -o $@ $(obj) $(LDFLAGS) + +$(lib_so): ../../libdrawtext.so.0.0 + rm -f $@ + ln -s $< $@ + +$(font): ../fonts/FreeSerif.ttf + rm -f $@ + ln -s $< $@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) $(lib_so) diff -r 34130f58141a -r 8e93efcd23ae examples/simple/simple.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple/simple.c Thu Sep 15 13:21:35 2011 +0300 @@ -0,0 +1,102 @@ +/* Simple libdrawtext example. + * + * Important parts are marked with XXX comments. + */ +#include +#include + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include "drawtext.h" + +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); + +/* XXX fonts are represented by the opaque struct dtx_font type, so you + * need to create at least one with dtx_open_font (see main). + */ +struct dtx_font *font; + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(512, 384); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutCreateWindow("libdrawtext example: simple"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */ + if(!(font = dtx_open_font("serif.ttf", 24))) { + fprintf(stderr, "failed to open font\n"); + return 1; + } + /* XXX select the font and size to render with by calling dtx_use_font + * if you want to use a different font size, you must first call: + * dtx_prepare(font, size) once. + */ + dtx_use_font(font, 24); + + glutMainLoop(); + return 0; +} + +const char *text = "Some sample text goes here.\n" + "Yada yada yada, more text...\n" + "foobar xyzzy\n"; + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glPushMatrix(); + glTranslatef(-200, 150, 0); + glColor3f(1, 1, 1); + /* XXX call dtx_string to draw utf-8 text. + * any transformations and the current color apply + */ + dtx_string(text); + glPopMatrix(); + + glPushMatrix(); + glTranslatef(-200, 50, 0); + glScalef(2, 0.7, 1); + glColor3f(0.6, 0.7, 1.0); + dtx_string(text); + glPopMatrix(); + + glPushMatrix(); + glTranslatef(-80, -90, 0); + glRotatef(20, 0, 0, 1); + glColor3f(1.0, 0.7, 0.6); + dtx_string(text); + glPopMatrix(); + + glutSwapBuffers(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-x/2, x/2, -y/2, y/2, -1, 1); +} + +void keyb(unsigned char key, int x, int y) +{ + if(key == 27) { + exit(0); + } +}