libdrawtext

changeset 53:8e93efcd23ae

added simple example
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 15 Sep 2011 13:21:35 +0300
parents 34130f58141a
children 4b5bdd7b2cb8
files .hgignore examples/Makefile examples/simple/Makefile examples/simple/simple.c
diffstat 4 files changed, 149 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Thu Sep 15 10:47:38 2011 +0300
     1.2 +++ b/.hgignore	Thu Sep 15 13:21:35 2011 +0300
     1.3 @@ -1,6 +1,11 @@
     1.4 -^libdrawtext\.a$
     1.5 -^libdrawtext\.so.*
     1.6 +libdrawtext\.a
     1.7 +libdrawtext\.so
     1.8  drawtext\.dylib$
     1.9  ^Makefile$
    1.10  \.d$
    1.11  \.o$
    1.12 +\.swp$
    1.13 +\.tar.gz$
    1.14 +^examples/fonts
    1.15 +\.ttf$
    1.16 +^examples/simple/simple$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/examples/Makefile	Thu Sep 15 13:21:35 2011 +0300
     2.3 @@ -0,0 +1,10 @@
     2.4 +fonts = fonts/FreeSerif.ttf
     2.5 +
     2.6 +.PHONY: all
     2.7 +all: $(fonts)
     2.8 +	cd simple; $(MAKE)
     2.9 +
    2.10 +fonts/FreeSerif.ttf:
    2.11 +	wget http://ftp.gnu.org/gnu/freefont/freefont-ttf-20100919.tar.gz
    2.12 +	tar xzvf freefont-ttf-20100919.tar.gz
    2.13 +	mv freefont-20100919 fonts
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/examples/simple/Makefile	Thu Sep 15 13:21:35 2011 +0300
     3.3 @@ -0,0 +1,30 @@
     3.4 +obj = simple.o
     3.5 +bin = simple
     3.6 +
     3.7 +CC = gcc
     3.8 +CFLAGS = -pedantic -Wall -g -I../../src
     3.9 +LDFLAGS = -L. -ldrawtext $(libgl)
    3.10 +
    3.11 +lib_so = libdrawtext.so
    3.12 +font = serif.ttf
    3.13 +
    3.14 +ifeq ($(shell uname -s), Darwin)
    3.15 +	libgl = -framework OpenGL -framework GLUT
    3.16 +else
    3.17 +	libgl = -lGL -lGLU -lglut
    3.18 +endif
    3.19 +
    3.20 +$(bin): $(obj) $(lib_so) $(font)
    3.21 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    3.22 +
    3.23 +$(lib_so): ../../libdrawtext.so.0.0
    3.24 +	rm -f $@
    3.25 +	ln -s $< $@
    3.26 +
    3.27 +$(font): ../fonts/FreeSerif.ttf
    3.28 +	rm -f $@
    3.29 +	ln -s $< $@
    3.30 +
    3.31 +.PHONY: clean
    3.32 +clean:
    3.33 +	rm -f $(obj) $(bin) $(lib_so)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/examples/simple/simple.c	Thu Sep 15 13:21:35 2011 +0300
     4.3 @@ -0,0 +1,102 @@
     4.4 +/* Simple libdrawtext example.
     4.5 + *
     4.6 + * Important parts are marked with XXX comments.
     4.7 + */
     4.8 +#include <stdio.h>
     4.9 +#include <stdlib.h>
    4.10 +
    4.11 +#ifndef __APPLE__
    4.12 +#include <GL/glut.h>
    4.13 +#else
    4.14 +#include <GLUT/glut.h>
    4.15 +#endif
    4.16 +
    4.17 +#include "drawtext.h"
    4.18 +
    4.19 +void disp(void);
    4.20 +void reshape(int x, int y);
    4.21 +void keyb(unsigned char key, int x, int y);
    4.22 +
    4.23 +/* XXX fonts are represented by the opaque struct dtx_font type, so you
    4.24 + * need to create at least one with dtx_open_font (see main).
    4.25 + */
    4.26 +struct dtx_font *font;
    4.27 +
    4.28 +int main(int argc, char **argv)
    4.29 +{
    4.30 +	glutInit(&argc, argv);
    4.31 +	glutInitWindowSize(512, 384);
    4.32 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    4.33 +	glutCreateWindow("libdrawtext example: simple");
    4.34 +
    4.35 +	glutDisplayFunc(disp);
    4.36 +	glutReshapeFunc(reshape);
    4.37 +	glutKeyboardFunc(keyb);
    4.38 +
    4.39 +	/* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
    4.40 +	if(!(font = dtx_open_font("serif.ttf", 24))) {
    4.41 +		fprintf(stderr, "failed to open font\n");
    4.42 +		return 1;
    4.43 +	}
    4.44 +	/* XXX select the font and size to render with by calling dtx_use_font
    4.45 +	 * if you want to use a different font size, you must first call:
    4.46 +	 * dtx_prepare(font, size) once.
    4.47 +	 */
    4.48 +	dtx_use_font(font, 24);
    4.49 +
    4.50 +	glutMainLoop();
    4.51 +	return 0;
    4.52 +}
    4.53 +
    4.54 +const char *text = "Some sample text goes here.\n"
    4.55 +	"Yada yada yada, more text...\n"
    4.56 +	"foobar xyzzy\n";
    4.57 +
    4.58 +void disp(void)
    4.59 +{
    4.60 +	glClear(GL_COLOR_BUFFER_BIT);
    4.61 +
    4.62 +	glMatrixMode(GL_MODELVIEW);
    4.63 +	glLoadIdentity();
    4.64 +
    4.65 +	glPushMatrix();
    4.66 +	glTranslatef(-200, 150, 0);
    4.67 +	glColor3f(1, 1, 1);
    4.68 +	/* XXX call dtx_string to draw utf-8 text.
    4.69 +	 * any transformations and the current color apply
    4.70 +	 */
    4.71 +	dtx_string(text);
    4.72 +	glPopMatrix();
    4.73 +
    4.74 +	glPushMatrix();
    4.75 +	glTranslatef(-200, 50, 0);
    4.76 +	glScalef(2, 0.7, 1);
    4.77 +	glColor3f(0.6, 0.7, 1.0);
    4.78 +	dtx_string(text);
    4.79 +	glPopMatrix();
    4.80 +
    4.81 +	glPushMatrix();
    4.82 +	glTranslatef(-80, -90, 0);
    4.83 +	glRotatef(20, 0, 0, 1);
    4.84 +	glColor3f(1.0, 0.7, 0.6);
    4.85 +	dtx_string(text);
    4.86 +	glPopMatrix();
    4.87 +
    4.88 +	glutSwapBuffers();
    4.89 +}
    4.90 +
    4.91 +void reshape(int x, int y)
    4.92 +{
    4.93 +	glViewport(0, 0, x, y);
    4.94 +
    4.95 +	glMatrixMode(GL_PROJECTION);
    4.96 +	glLoadIdentity();
    4.97 +	glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
    4.98 +}
    4.99 +
   4.100 +void keyb(unsigned char key, int x, int y)
   4.101 +{
   4.102 +	if(key == 27) {
   4.103 +		exit(0);
   4.104 +	}
   4.105 +}