libdrawtext

annotate examples/unicode/unicode.c @ 3:fe0c54e574ae

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