libdrawtext

view 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 source
1 /* Unicode libdrawtext example.
2 *
3 * Important parts are marked with XXX comments.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
8 #ifndef __APPLE__
9 #include <GL/glut.h>
10 #else
11 #include <GLUT/glut.h>
12 #endif
14 #include "drawtext.h"
16 void disp(void);
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
20 /* XXX fonts are represented by the opaque struct dtx_font type, so you
21 * need to create at least one with dtx_open_font (see main).
22 */
23 struct dtx_font *freeserif, *klingon;
25 #define FONT_SIZE 24
27 int main(int argc, char **argv)
28 {
29 glutInit(&argc, argv);
30 glutInitWindowSize(512, 384);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
32 glutCreateWindow("libdrawtext example: simple");
34 glutDisplayFunc(disp);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyb);
38 /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
39 if(!(freeserif = dtx_open_font("serif.ttf", 0))) {
40 return 1;
41 }
42 dtx_prepare_range(freeserif, FONT_SIZE, 0, 256); /* prepare ASCII */
43 dtx_prepare_range(freeserif, FONT_SIZE, 0x0370, 0x0400); /* greek */
44 dtx_prepare_range(freeserif, FONT_SIZE, 0x4e00, 0x9fc0); /* kanji */
46 if(!(klingon = dtx_open_font("klingon.ttf", 0))) {
47 return 1;
48 }
49 dtx_prepare_range(klingon, FONT_SIZE, 0xf8d0, 0xf900);
51 glutMainLoop();
52 return 0;
53 }
55 const char *ascii_text = "Hello world!";
56 const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7";
57 const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac";
58 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";
61 void disp(void)
62 {
63 glClear(GL_COLOR_BUFFER_BIT);
65 glMatrixMode(GL_MODELVIEW);
66 glLoadIdentity();
68 dtx_use_font(freeserif, FONT_SIZE);
70 glTranslatef(-200, 150, 0);
71 /* XXX call dtx_string to draw utf-8 text.
72 * any transformations and the current color apply
73 */
74 dtx_string(ascii_text);
76 glTranslatef(0, -40, 0);
77 dtx_string(greek_text);
79 glTranslatef(0, -40, 0);
80 dtx_string(kanji_text);
82 dtx_use_font(klingon, FONT_SIZE);
84 glTranslatef(0, -40, 0);
85 dtx_string(klingon_text);
87 glutSwapBuffers();
88 }
90 void reshape(int x, int y)
91 {
92 glViewport(0, 0, x, y);
94 glMatrixMode(GL_PROJECTION);
95 glLoadIdentity();
96 glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
97 }
99 void keyb(unsigned char key, int x, int y)
100 {
101 if(key == 27) {
102 exit(0);
103 }
104 }