libdrawtext

diff examples/unicode/unicode.c @ 55:59e5858de836

fixed a bug in utf-8 decoding
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 15 Sep 2011 23:32:39 +0300
parents
children 17fed026b24b
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/unicode/unicode.c	Thu Sep 15 23:32:39 2011 +0300
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* Unicode 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 *freeserif, *klingon;
    1.27 +
    1.28 +#define FONT_SIZE	24
    1.29 +
    1.30 +int main(int argc, char **argv)
    1.31 +{
    1.32 +	glutInit(&argc, argv);
    1.33 +	glutInitWindowSize(512, 384);
    1.34 +	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    1.35 +	glutCreateWindow("libdrawtext example: simple");
    1.36 +
    1.37 +	glutDisplayFunc(disp);
    1.38 +	glutReshapeFunc(reshape);
    1.39 +	glutKeyboardFunc(keyb);
    1.40 +
    1.41 +	/* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
    1.42 +	if(!(freeserif = dtx_open_font("serif.ttf", 0))) {
    1.43 +		return 1;
    1.44 +	}
    1.45 +	dtx_prepare_range(freeserif, FONT_SIZE, 0, 256);	/* prepare ASCII */
    1.46 +	dtx_prepare_range(freeserif, FONT_SIZE, 0x0370, 0x0400);	/* greek */
    1.47 +	dtx_prepare_range(freeserif, FONT_SIZE, 0x4e00, 0x9fc0);	/* kanji */
    1.48 +
    1.49 +	if(!(klingon = dtx_open_font("klingon.ttf", 0))) {
    1.50 +		return 1;
    1.51 +	}
    1.52 +	dtx_prepare_range(klingon, FONT_SIZE, 0xf8d0, 0xf900);
    1.53 +
    1.54 +	glutMainLoop();
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +const char *ascii_text = "Hello world!";
    1.59 +const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7";
    1.60 +const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac";
    1.61 +const char *klingon_text = "\xef\xa3\xa3\xef\xa3\x9d\xef\xa3\x93\xef\xa3\x98\xef\xa3\x9d\xef\xa3\xa2\xef\xa3\xa1\xef\xa3\x9d\xef\xa3\x99";
    1.62 +
    1.63 +
    1.64 +void disp(void)
    1.65 +{
    1.66 +	glClear(GL_COLOR_BUFFER_BIT);
    1.67 +
    1.68 +	glMatrixMode(GL_MODELVIEW);
    1.69 +	glLoadIdentity();
    1.70 +
    1.71 +	dtx_use_font(freeserif, FONT_SIZE);
    1.72 +
    1.73 +	glTranslatef(-200, 150, 0);
    1.74 +	/* XXX call dtx_string to draw utf-8 text.
    1.75 +	 * any transformations and the current color apply
    1.76 +	 */
    1.77 +	dtx_string(ascii_text);
    1.78 +
    1.79 +	glTranslatef(0, -40, 0);
    1.80 +	dtx_string(greek_text);
    1.81 +
    1.82 +	glTranslatef(0, -40, 0);
    1.83 +	dtx_string(kanji_text);
    1.84 +
    1.85 +	dtx_use_font(klingon, FONT_SIZE);
    1.86 +
    1.87 +	glTranslatef(0, -40, 0);
    1.88 +	dtx_string(klingon_text);
    1.89 +
    1.90 +	glutSwapBuffers();
    1.91 +}
    1.92 +
    1.93 +void reshape(int x, int y)
    1.94 +{
    1.95 +	glViewport(0, 0, x, y);
    1.96 +
    1.97 +	glMatrixMode(GL_PROJECTION);
    1.98 +	glLoadIdentity();
    1.99 +	glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
   1.100 +}
   1.101 +
   1.102 +void keyb(unsigned char key, int x, int y)
   1.103 +{
   1.104 +	if(key == 27) {
   1.105 +		exit(0);
   1.106 +	}
   1.107 +}