aboutsummaryrefslogtreecommitdiff
path: root/src/render-text.c
blob: 8285827b6094eff53bbc2f81f02b7d178ed934e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * render-text.c
 *
 * Simple text rendering
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <png.h>
#include <stdlib.h>
#include <glew.h>

#include "render.h"
#include "texture.h"

void render_text_setup(RenderContext *r) {

	texture_load(r, "font");

}

void render_text_write(GLfloat x, GLfloat y, const char *text, RenderContext *r) {

	Texture *texture;
	int i;
	const GLfloat lwidth = 0.03;
	const GLfloat lheight = lwidth*r->aspect;
	const GLfloat lspacing = lwidth/10.0;
	
	y -= lheight;
	
	texture = texture_lookup(r, "font");
	glBindTexture(GL_TEXTURE_2D, texture->texname);
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	glBegin(GL_QUADS);
	
		for ( i=0; i<strlen(text); i++ ) {
		
			int idx;
			int tx, ty;
			GLfloat txc, tyc;
			const GLfloat unit = 1.0/8.0;
			char c;
			
			c = text[i];
			if ( c == ' ' ) {
				x += lwidth + lspacing;
				continue;
			}
			if ( c == '-' ) {
				c = 'Z' + 1;
			}
			if ( (c == '\r') || (c == '\n') ) {
				continue;
			}
			
			idx = toupper(c) - 'A';
			tx = idx % 8;  ty = idx / 8;
			txc = tx * unit;
			tyc = (1.0-unit) - ty * unit;
			
			glColor3f(0.4, 0.4, 0.4);
			glTexCoord2f(txc, tyc);
			glVertex2f(x, y);
			
			glTexCoord2f(txc+unit, tyc);
			glVertex2f(x+lwidth, y);
			
			glColor3f(0.8, 0.8, 0.8);
			glTexCoord2f(txc+unit, tyc+unit);
			glVertex2f(x+lwidth, y+lheight);
			
			glTexCoord2f(txc, tyc+unit);
			glVertex2f(x, y+lheight);
			
			x += lwidth + lspacing;
			
		}
		
	glEnd();
	
	glDisable(GL_TEXTURE_2D);

}