libdrawtext

diff examples/simple/simple.c @ 1:daa47eeca303

added simple example
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 15 Sep 2011 13:21:35 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/simple/simple.c	Thu Sep 15 13:21:35 2011 +0300
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* Simple libdrawtext example.
     1.5 + *
     1.6 + * Important parts are marked with XXX comments.
     1.7 + */
     1.8 +#include <stdio.h>
     1.9 +#include <stdlib.h>
    1.10 +
    1.11 +#ifndef __APPLE__
    1.12 +#include <GL/glut.h>
    1.13 +#else
    1.14 +#include <GLUT/glut.h>
    1.15 +#endif
    1.16 +
    1.17 +#include "drawtext.h"
    1.18 +
    1.19 +void disp(void);
    1.20 +void reshape(int x, int y);
    1.21 +void keyb(unsigned char key, int x, int y);
    1.22 +
    1.23 +/* XXX fonts are represented by the opaque struct dtx_font type, so you
    1.24 + * need to create at least one with dtx_open_font (see main).
    1.25 + */
    1.26 +struct dtx_font *font;
    1.27 +
    1.28 +int main(int argc, char **argv)
    1.29 +{
    1.30 +	glutInit(&argc, argv);
    1.31 +	glutInitWindowSize(512, 384);
    1.32 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    1.33 +	glutCreateWindow("libdrawtext example: simple");
    1.34 +
    1.35 +	glutDisplayFunc(disp);
    1.36 +	glutReshapeFunc(reshape);
    1.37 +	glutKeyboardFunc(keyb);
    1.38 +
    1.39 +	/* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
    1.40 +	if(!(font = dtx_open_font("serif.ttf", 24))) {
    1.41 +		fprintf(stderr, "failed to open font\n");
    1.42 +		return 1;
    1.43 +	}
    1.44 +	/* XXX select the font and size to render with by calling dtx_use_font
    1.45 +	 * if you want to use a different font size, you must first call:
    1.46 +	 * dtx_prepare(font, size) once.
    1.47 +	 */
    1.48 +	dtx_use_font(font, 24);
    1.49 +
    1.50 +	glutMainLoop();
    1.51 +	return 0;
    1.52 +}
    1.53 +
    1.54 +const char *text = "Some sample text goes here.\n"
    1.55 +	"Yada yada yada, more text...\n"
    1.56 +	"foobar xyzzy\n";
    1.57 +
    1.58 +void disp(void)
    1.59 +{
    1.60 +	glClear(GL_COLOR_BUFFER_BIT);
    1.61 +
    1.62 +	glMatrixMode(GL_MODELVIEW);
    1.63 +	glLoadIdentity();
    1.64 +
    1.65 +	glPushMatrix();
    1.66 +	glTranslatef(-200, 150, 0);
    1.67 +	glColor3f(1, 1, 1);
    1.68 +	/* XXX call dtx_string to draw utf-8 text.
    1.69 +	 * any transformations and the current color apply
    1.70 +	 */
    1.71 +	dtx_string(text);
    1.72 +	glPopMatrix();
    1.73 +
    1.74 +	glPushMatrix();
    1.75 +	glTranslatef(-200, 50, 0);
    1.76 +	glScalef(2, 0.7, 1);
    1.77 +	glColor3f(0.6, 0.7, 1.0);
    1.78 +	dtx_string(text);
    1.79 +	glPopMatrix();
    1.80 +
    1.81 +	glPushMatrix();
    1.82 +	glTranslatef(-80, -90, 0);
    1.83 +	glRotatef(20, 0, 0, 1);
    1.84 +	glColor3f(1.0, 0.7, 0.6);
    1.85 +	dtx_string(text);
    1.86 +	glPopMatrix();
    1.87 +
    1.88 +	glutSwapBuffers();
    1.89 +}
    1.90 +
    1.91 +void reshape(int x, int y)
    1.92 +{
    1.93 +	glViewport(0, 0, x, y);
    1.94 +
    1.95 +	glMatrixMode(GL_PROJECTION);
    1.96 +	glLoadIdentity();
    1.97 +	glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
    1.98 +}
    1.99 +
   1.100 +void keyb(unsigned char key, int x, int y)
   1.101 +{
   1.102 +	if(key == 27) {
   1.103 +		exit(0);
   1.104 +	}
   1.105 +}