libdrawtext

annotate examples/unicode/unicode.c @ 59:e0957fc47162

added dtx_char_at_pt (not tested)
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 18 Sep 2011 01:30:30 +0300
parents 17fed026b24b
children 87798d469b81
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@4 23 struct dtx_font *fntmain, *fntcjk, *fntklingon;
nuclear@3 24
nuclear@4 25 #define FONT_SZ 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@4 32 glutCreateWindow("libdrawtext example: unicode");
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@4 39 if(!(fntmain = dtx_open_font("serif.ttf", 0))) {
nuclear@3 40 return 1;
nuclear@3 41 }
nuclear@4 42 dtx_prepare_range(fntmain, FONT_SZ, 0, 256); /* ASCII */
nuclear@4 43 dtx_prepare_range(fntmain, FONT_SZ, 0x370, 0x400); /* greek */
nuclear@4 44 dtx_prepare_range(fntmain, FONT_SZ, 0x400, 0x500); /* cyrilic */
nuclear@3 45
nuclear@4 46 if(!(fntcjk = dtx_open_font("cjk.ttf", 0))) {
nuclear@3 47 return 1;
nuclear@3 48 }
nuclear@4 49 dtx_prepare_range(fntcjk, FONT_SZ, 0x4e00, 0x9fc0); /* kanji */
nuclear@4 50
nuclear@4 51 if(!(fntklingon = dtx_open_font("klingon.ttf", 0))) {
nuclear@4 52 return 1;
nuclear@4 53 }
nuclear@4 54 dtx_prepare_range(fntklingon, FONT_SZ, 0xf8d0, 0xf900); /* klingon */
nuclear@3 55
nuclear@3 56 glutMainLoop();
nuclear@3 57 return 0;
nuclear@3 58 }
nuclear@3 59
nuclear@4 60 /* various UTF-8 strings */
nuclear@7 61 const char *english_text = "Hello g p world!";
nuclear@3 62 const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7";
nuclear@4 63 const char *russian_text = "\xd0\xa0\xd0\xb0\xd1\x81\xd1\x86\xd0\xb2\xd0\xb5\xd1\x82\xd0\xb0\xd0\xbb\xd0\xb8 \xd1\x8f\xd0\xb1\xd0\xbb\xd0\xbe\xd0\xbd\xd0\xb8 \xd0\xb8 \xd0\xb3\xd1\x80\xd1\x83\xd1\x88\xd0\xb8";
nuclear@3 64 const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac";
nuclear@3 65 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 66
nuclear@7 67 void draw_box(struct dtx_box *box)
nuclear@7 68 {
nuclear@7 69 glBegin(GL_LINE_LOOP);
nuclear@7 70 glColor3f(0, 1, 0);
nuclear@7 71 glVertex2f(box->x, box->y);
nuclear@7 72 glVertex2f(box->x + box->width, box->y);
nuclear@7 73 glVertex2f(box->x + box->width, box->y + box->height);
nuclear@7 74 glVertex2f(box->x, box->y + box->height);
nuclear@7 75 glEnd();
nuclear@7 76 glColor3f(1, 1, 1);
nuclear@7 77 }
nuclear@3 78
nuclear@3 79 void disp(void)
nuclear@3 80 {
nuclear@3 81 glClear(GL_COLOR_BUFFER_BIT);
nuclear@3 82
nuclear@3 83 glMatrixMode(GL_MODELVIEW);
nuclear@3 84 glLoadIdentity();
nuclear@3 85
nuclear@4 86 dtx_use_font(fntmain, FONT_SZ);
nuclear@3 87
nuclear@3 88 glTranslatef(-200, 150, 0);
nuclear@3 89 /* XXX call dtx_string to draw utf-8 text.
nuclear@3 90 * any transformations and the current color apply
nuclear@3 91 */
nuclear@4 92 dtx_string(english_text);
nuclear@3 93
nuclear@3 94 glTranslatef(0, -40, 0);
nuclear@3 95 dtx_string(greek_text);
nuclear@3 96
nuclear@3 97 glTranslatef(0, -40, 0);
nuclear@4 98 dtx_string(russian_text);
nuclear@4 99
nuclear@4 100 dtx_use_font(fntcjk, FONT_SZ);
nuclear@4 101
nuclear@4 102 glTranslatef(0, -40, 0);
nuclear@3 103 dtx_string(kanji_text);
nuclear@3 104
nuclear@4 105 dtx_use_font(fntklingon, FONT_SZ);
nuclear@3 106
nuclear@3 107 glTranslatef(0, -40, 0);
nuclear@3 108 dtx_string(klingon_text);
nuclear@3 109
nuclear@3 110 glutSwapBuffers();
nuclear@3 111 }
nuclear@3 112
nuclear@3 113 void reshape(int x, int y)
nuclear@3 114 {
nuclear@3 115 glViewport(0, 0, x, y);
nuclear@3 116
nuclear@3 117 glMatrixMode(GL_PROJECTION);
nuclear@3 118 glLoadIdentity();
nuclear@3 119 glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
nuclear@3 120 }
nuclear@3 121
nuclear@3 122 void keyb(unsigned char key, int x, int y)
nuclear@3 123 {
nuclear@3 124 if(key == 27) {
nuclear@3 125 exit(0);
nuclear@3 126 }
nuclear@3 127 }