summaryrefslogtreecommitdiff
path: root/lib/glfont.cc
blob: 833d18de03f6a7e2a5b8d5dd2edc96857e16a51e (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "glbase.h"
#include "glfont.h"
#include "Input.h"

Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 };

/*

font file format
================

off|siz|description
---+---+-------------------------------
 0 | 2 | Number of entries
 2 | 1 | Flags
 3 | 1 | maxX (maximum width)
 4 | 1 | maxY (maximum height)
 5 | X | char entries
5+X| Y | char map

Flags:
-----

0000000R

R = RGBA (=1) or Alpha (=0)

ABGR in 1232 format:
ABBGGGRR

variables comments
==================

nbcU = number of chars on X by texture
nbcV = number of chars on Y by texture
nbcT = number of char by texture
nbT  = number of textures

*/

mogltk::font::font(const String & file) {
    Input ffont(file);
    int i;
    
    ffont.SetZ();
    
    ffont.read(&nbentries, 2);
    ffont.read(&flags, 1);
    ffont.read(&maxX, 1);
    ffont.read(&maxY, 1);
    
    nbcU = 256 / maxX;
    nbcV = 256 / maxY;
    
    nbcT = nbcU * nbcV;
    
    nbT = nbentries / nbcT;
    
    if (nbentries % nbcT) {
	nbT++;
    }
    
    fonttex = (texture **) malloc(nbT * sizeof(texture *));
    
    for (i = 0; i < nbT; i++) {
	fonttex[i] = new texture(256, 256, true);
    }
    
    sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8));

    Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels;
    Uint8 curU = 0, curV = 0, curT = 0;
    for (int i = 0; i < nbentries; i++) {
	ffont.read(&sizes[i], 1);
	for (int v = 0; v < maxY; v++) {
	    for (int u = 0; u < maxX; u++) {
		Uint8 f;
		ffont.read(&f, 1);
		if (flags & 1) {
		    Uint8 r, g, b, a;
		    r = f & 3;
		    g = (f >> 2) & 7;
		    b = (f >> 5) & 3;
		    a = (f >> 7) & 1;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 0] = prescale2[r];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 1] = prescale3[g];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 2] = prescale2[b];
		    curtex[(curU + u + (curV + v) * 256) * 4 + 3] = a ? 255 : 0;
		} else {
		    curtex[(curU + u + (curV + v) * 256) * 4 + 0] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 1] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 2] = 255;
		    curtex[(curU + u + (curV + v) * 256) * 4 + 3] = f;
		}
	    }
	}
	curU += maxX;
	if (curU >= 256) {
	    curU = 0;
	    curV += maxY;
	    if (curV >= 256) {
		curV = 0;
		curT++;
		curtex = (Uint8 *) fonttex[curT]->GetSurface()->pixels;
	    }
	}
    }
    
    for (int i = 0; i < nbT; i++) {
	fonttex[i]->Generate();
    }
    
    corresp = (Uint16 *) malloc(nbentries * 2 * sizeof(Uint16));
    
    ffont.read(corresp, 2 * sizeof(Uint16) * nbentries);
}

mogltk::font::~font() {
    int i;
    for (i = 0; i < nbT; i++) {
	delete fonttex[i];
    }
    
    free((void *) fonttex);
    free(sizes);
}

void mogltk::font::drawentry(Uint16 entry, Color c, int x, int y) {
    bool was2D;
    
    was2D = mogltk::glbase::is2D();
    
    if (!was2D) {
	mogltk::glbase::Enter2DMode();
    }
    
    
    
    if (!was2D) {
	mogltk::glbase::Leave2DMode();
    }
}