summaryrefslogtreecommitdiff
path: root/lib/glfont.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/glfont.cc')
-rw-r--r--lib/glfont.cc82
1 files changed, 59 insertions, 23 deletions
diff --git a/lib/glfont.cc b/lib/glfont.cc
index e58feaf..d935d95 100644
--- a/lib/glfont.cc
+++ b/lib/glfont.cc
@@ -4,6 +4,8 @@
Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 };
+#define DEBUG 1
+
/*
font file format
@@ -42,7 +44,7 @@ off|siz|description
1 | Z | Datas
Z = maxX * maxY
-
+Datas are stored in order X first, then Y.
Char map:
========
@@ -54,8 +56,8 @@ off|siz|description
0 | 2 | Unicode (?)
2 | 2 | Corresponding char entry
-I'm not sure about Unicode. I write this only to say it's an attempt to
-make the fonts "internationals". If the "unicode" is < 255, then it should
+I'm not sure about my word 'Unicode'. I write this only to say it's an attempt
+to make the fonts "internationals". If the "unicode" is < 255, then it should
match only one byte in the string. Otherwise, it should match two bytes.
@@ -69,17 +71,16 @@ nbT = number of textures
*/
-mogltk::font::font(const String & file) {
- Input ffont(file);
+mogltk::font::font(Handle * ffont) {
int i;
- ffont.SetZ();
-
- ffont.read(&nbentries, 2);
- ffont.read(&flags, 1);
- ffont.read(&maxX, 1);
- ffont.read(&maxY, 1);
+ ffont->SetZ();
+ ffont->read(&nbentries, 2);
+ ffont->read(&flags, 1);
+ ffont->read(&maxX, 1);
+ ffont->read(&maxY, 1);
+
nbcU = 256 / maxX;
nbcV = 256 / maxY;
@@ -91,6 +92,11 @@ mogltk::font::font(const String & file) {
nbT++;
}
+#ifdef DEBUG
+ printm(M_INFO, "Creating font texture: %i entries, flags = 0x%02x, maxX = %i, maxY = %i\n", nbentries, flags, maxX, maxY);
+ printm(M_INFO, "Which makes %i texture(s), with %i char by texture, %i on X, and %i on Y\n", nbT, nbcT, nbcU, nbcV);
+#endif
+
fonttex = (texture **) malloc(nbT * sizeof(texture *));
for (i = 0; i < nbT; i++) {
@@ -100,13 +106,13 @@ mogltk::font::font(const String & file) {
sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8));
Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels;
- Uint8 curU = 0, curV = 0, curT = 0;
+ Uint32 curU = 0, curV = 0, curT = 0;
for (int i = 0; i < nbentries; i++) {
- ffont.read(&sizes[i], 1);
+ 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);
+ ffont->read(&f, 1);
if (flags & 1) {
Uint8 r, g, b, a;
r = f & 3;
@@ -129,18 +135,15 @@ mogltk::font::font(const String & file) {
curU = 0;
if ((curV += maxY) >= 256) {
curV = 0;
- curtex = (Uint8 *) fonttex[++curT]->GetSurface()->pixels;
+ if ((curT + 1) != nbT)
+ 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);
+ ffont->read(corresp, 2 * sizeof(Uint16) * nbentries);
}
mogltk::font::~font() {
@@ -153,8 +156,9 @@ mogltk::font::~font() {
free(sizes);
}
-void mogltk::font::drawentry(Uint16 entry, Color c, int x, int y) {
+void mogltk::font::drawentry(Uint16 entry, int x, int y, Color c) {
bool was2D;
+ int trueentry, cx, cy, px, py;
was2D = mogltk::glbase::is2D();
@@ -162,9 +166,41 @@ void mogltk::font::drawentry(Uint16 entry, Color c, int x, int y) {
mogltk::glbase::Enter2DMode();
}
-
+ Bind(entry / nbcT);
+ c.Bind();
+ trueentry = entry % nbcT;
+ cx = trueentry % nbcU;
+ cy = trueentry / nbcU;
+ px = cx * maxX;
+ py = cy * maxY;
+
+ glBegin(GL_TRIANGLE_STRIP);
+ glTexCoord2i(px , py ); glVertex2i(x , y );
+ glTexCoord2i(px + maxX - 1, py ); glVertex2i(x + maxX - 1, y );
+ glTexCoord2i(px , py + maxY - 1); glVertex2i(x , y + maxY - 1);
+ glTexCoord2i(px + maxX - 1, py + maxY - 1); glVertex2i(x + maxX - 1, y + maxY - 1);
+ glEnd();
if (!was2D) {
mogltk::glbase::Leave2DMode();
}
}
+
+void mogltk::font::Bind(int index) {
+ fonttex[index]->Bind();
+}
+
+void mogltk::font::putcursor(int x, int y) {
+ cx = ox = x;
+ cy = y;
+}
+
+void mogltk::font::putentry(Uint16 entry, Color c) {
+ drawentry(entry, cx, cy, c);
+ cx += sizes[entry];
+}
+
+void mogltk::font::newline(void) {
+ cx = ox;
+ cy += maxY;
+}