libdrawtext

annotate 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
rev   line source
nuclear@1 1 /* Simple libdrawtext example.
nuclear@1 2 *
nuclear@1 3 * Important parts are marked with XXX comments.
nuclear@1 4 */
nuclear@1 5 #include <stdio.h>
nuclear@1 6 #include <stdlib.h>
nuclear@1 7
nuclear@1 8 #ifndef __APPLE__
nuclear@1 9 #include <GL/glut.h>
nuclear@1 10 #else
nuclear@1 11 #include <GLUT/glut.h>
nuclear@1 12 #endif
nuclear@1 13
nuclear@1 14 #include "drawtext.h"
nuclear@1 15
nuclear@1 16 void disp(void);
nuclear@1 17 void reshape(int x, int y);
nuclear@1 18 void keyb(unsigned char key, int x, int y);
nuclear@1 19
nuclear@1 20 /* XXX fonts are represented by the opaque struct dtx_font type, so you
nuclear@1 21 * need to create at least one with dtx_open_font (see main).
nuclear@1 22 */
nuclear@1 23 struct dtx_font *font;
nuclear@1 24
nuclear@1 25 int main(int argc, char **argv)
nuclear@1 26 {
nuclear@1 27 glutInit(&argc, argv);
nuclear@1 28 glutInitWindowSize(512, 384);
nuclear@1 29 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
nuclear@1 30 glutCreateWindow("libdrawtext example: simple");
nuclear@1 31
nuclear@1 32 glutDisplayFunc(disp);
nuclear@1 33 glutReshapeFunc(reshape);
nuclear@1 34 glutKeyboardFunc(keyb);
nuclear@1 35
nuclear@1 36 /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
nuclear@1 37 if(!(font = dtx_open_font("serif.ttf", 24))) {
nuclear@1 38 fprintf(stderr, "failed to open font\n");
nuclear@1 39 return 1;
nuclear@1 40 }
nuclear@1 41 /* XXX select the font and size to render with by calling dtx_use_font
nuclear@1 42 * if you want to use a different font size, you must first call:
nuclear@1 43 * dtx_prepare(font, size) once.
nuclear@1 44 */
nuclear@1 45 dtx_use_font(font, 24);
nuclear@1 46
nuclear@1 47 glutMainLoop();
nuclear@1 48 return 0;
nuclear@1 49 }
nuclear@1 50
nuclear@1 51 const char *text = "Some sample text goes here.\n"
nuclear@1 52 "Yada yada yada, more text...\n"
nuclear@1 53 "foobar xyzzy\n";
nuclear@1 54
nuclear@1 55 void disp(void)
nuclear@1 56 {
nuclear@1 57 glClear(GL_COLOR_BUFFER_BIT);
nuclear@1 58
nuclear@1 59 glMatrixMode(GL_MODELVIEW);
nuclear@1 60 glLoadIdentity();
nuclear@1 61
nuclear@1 62 glPushMatrix();
nuclear@1 63 glTranslatef(-200, 150, 0);
nuclear@1 64 glColor3f(1, 1, 1);
nuclear@1 65 /* XXX call dtx_string to draw utf-8 text.
nuclear@1 66 * any transformations and the current color apply
nuclear@1 67 */
nuclear@1 68 dtx_string(text);
nuclear@1 69 glPopMatrix();
nuclear@1 70
nuclear@1 71 glPushMatrix();
nuclear@1 72 glTranslatef(-200, 50, 0);
nuclear@1 73 glScalef(2, 0.7, 1);
nuclear@1 74 glColor3f(0.6, 0.7, 1.0);
nuclear@1 75 dtx_string(text);
nuclear@1 76 glPopMatrix();
nuclear@1 77
nuclear@1 78 glPushMatrix();
nuclear@1 79 glTranslatef(-80, -90, 0);
nuclear@1 80 glRotatef(20, 0, 0, 1);
nuclear@1 81 glColor3f(1.0, 0.7, 0.6);
nuclear@1 82 dtx_string(text);
nuclear@1 83 glPopMatrix();
nuclear@1 84
nuclear@1 85 glutSwapBuffers();
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 void reshape(int x, int y)
nuclear@1 89 {
nuclear@1 90 glViewport(0, 0, x, y);
nuclear@1 91
nuclear@1 92 glMatrixMode(GL_PROJECTION);
nuclear@1 93 glLoadIdentity();
nuclear@1 94 glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
nuclear@1 95 }
nuclear@1 96
nuclear@1 97 void keyb(unsigned char key, int x, int y)
nuclear@1 98 {
nuclear@1 99 if(key == 27) {
nuclear@1 100 exit(0);
nuclear@1 101 }
nuclear@1 102 }