summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpixel <pixel>2002-12-17 00:09:50 +0000
committerpixel <pixel>2002-12-17 00:09:50 +0000
commitf0981aa6b09e67ec57b7cb92421513415292e97f (patch)
tree482ea3feccc16763733afaaaef36a9a81b55f6b1
parentdd1e9ef7dd0d490f9397d524e1cfde13d6314623 (diff)
Commit of the day...
-rw-r--r--include/glbase.h2
-rw-r--r--include/glfont.h10
-rw-r--r--include/gltexture.h1
-rw-r--r--lib/glbase.cc4
-rw-r--r--lib/glfont.cc82
-rw-r--r--lib/gltexture.cc12
-rw-r--r--src/test.cc71
7 files changed, 142 insertions, 40 deletions
diff --git a/include/glbase.h b/include/glbase.h
index 39c7493..27c4f75 100644
--- a/include/glbase.h
+++ b/include/glbase.h
@@ -16,7 +16,7 @@ namespace mogltk {
static void Leave2DMode(void);
static void Flip(void);
static bool is2D(void);
- static void glVertex(GLshort, GLshort, GLshort = 0, GLshort = 1);
+ static void glVertex(GLshort, GLshort, GLshort = 0, GLshort = 1);
static void glVertex(GLint, GLint, GLint = 0, GLint = 1);
static void glVertex(GLfloat, GLfloat, GLfloat = 0.0, GLfloat = 1.0);
static void glVertex(GLdouble, GLdouble, GLdouble = 0.0, GLdouble = 1.0);
diff --git a/include/glfont.h b/include/glfont.h
index de4ef63..cbff848 100644
--- a/include/glfont.h
+++ b/include/glfont.h
@@ -4,14 +4,18 @@
#include <SDL.h>
#include <String.h>
#include <Color.h>
+#include <Handle.h>
#include "gltexture.h"
namespace mogltk {
class font : public Base {
public:
- font(const String & = "font.bin");
+ font(Handle *);
virtual ~font();
- void drawentry(Uint16, Color = Color(255, 255, 255), int = -1, int = -1);
+ void drawentry(Uint16, int, int, Color = Color(255, 255, 255, 255));
+ void putcursor(int, int);
+ void putentry(Uint16, Color = Color(255, 255, 255, 255));
+ void newline(void);
private:
Uint8 * sizes;
@@ -19,6 +23,8 @@ namespace mogltk {
Uint8 flags, maxX, maxY, nbcU, nbcV;
texture ** fonttex;
Uint16 * corresp;
+ void Bind(int);
+ int cx, cy, ox;
};
};
diff --git a/include/gltexture.h b/include/gltexture.h
index 1e4c3d2..9cbfe54 100644
--- a/include/gltexture.h
+++ b/include/gltexture.h
@@ -27,6 +27,7 @@ namespace mogltk {
static texture * footer;
texture * next, * prev;
#endif
+ static texture * active;
};
};
diff --git a/lib/glbase.cc b/lib/glbase.cc
index 214f2d9..aa98d30 100644
--- a/lib/glbase.cc
+++ b/lib/glbase.cc
@@ -49,7 +49,7 @@ int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) {
glClearColor(0, 0, 0, 0);
glShadeModel(GL_SMOOTH);
-
+
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
@@ -61,7 +61,7 @@ int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Flip();
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;
+}
diff --git a/lib/gltexture.cc b/lib/gltexture.cc
index f33fe98..564ffad 100644
--- a/lib/gltexture.cc
+++ b/lib/gltexture.cc
@@ -11,6 +11,8 @@ mogltk::texture * mogltk::texture::header = 0;
mogltk::texture * mogltk::texture::footer = 0;
#endif
+mogltk::texture * mogltk::texture::active = 0;
+
mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h),
texture_allocated(false), planar(plane), tainted(true) {
if ((BITCOUNT(w) != 1) || (BITCOUNT(h) != 1))
@@ -116,8 +118,10 @@ void mogltk::texture::Bind(bool expand) {
if ((!texture_allocated) || tainted)
Generate();
glEnable(GL_TEXTURE_2D);
+ if (active == this)
+ return;
#ifdef DEBUG
- printm(M_INFO, "Binding texture index %i\n", tex);
+ printm(M_INFO, "Binding texture index %i.\n", tex);
#endif
glBindTexture(GL_TEXTURE_2D, tex);
if (expand) {
@@ -127,6 +131,8 @@ void mogltk::texture::Bind(bool expand) {
glMatrixMode(GL_MODELVIEW);
}
+ active = this;
+
#ifdef TRACE_TEXTURES
if (header == this)
return;
@@ -158,6 +164,10 @@ GLuint mogltk::texture::GetHeight() {
void mogltk::texture::Unbind(void) {
glDisable(GL_TEXTURE_2D);
+ active = 0;
+#ifdef DEBUG
+ printm(M_INFO, "Unbinding texture.\n");
+#endif
}
void mogltk::texture::Taint(void) {
diff --git a/src/test.cc b/src/test.cc
index f82a6bd..43abd28 100644
--- a/src/test.cc
+++ b/src/test.cc
@@ -1,14 +1,18 @@
#include <SDL.h>
-#include "generic.h"
-#include "Main.h"
+#include <generic.h>
+#include <Main.h>
+#include <Input.h>
#include "glbase.h"
#include "gltexture.h"
+#include "glfont.h"
CODE_BEGINS
virtual int startup() throw (GeneralException) {
Uint8 * texture;
verbosity = M_INFO;
mogltk::glbase::setup();
+ Input ffont("font.bin");
+ mogltk::font font(&ffont);
verbosity = M_INFO;
@@ -32,30 +36,32 @@ virtual int startup() throw (GeneralException) {
throw GeneralException("Error: could not load texture.");
}
SDL_BlitSurface(s, NULL, mytex->GetSurface(), NULL);
-/*
+
+/*
for (int y = 0; y < 256; y += 2) {
for (int x = 0; x < 256; x += 2) {
- texture[(x + y * 256) * 4 + 0] = 255;
- texture[(x + y * 256) * 4 + 1] = 255;
+ texture[(x + y * 256) * 4 + 0] = 0;
+ texture[(x + y * 256) * 4 + 1] = 0;
texture[(x + y * 256) * 4 + 2] = 255;
- texture[(x + y * 256) * 4 + 3] = 255;
+ texture[(x + y * 256) * 4 + 3] = 0;
}
}
-*/
+*/
+
mogltk::glbase::Enter2DMode();
mytex->Bind();
glBegin(GL_TRIANGLE_STRIP);
-// glColor3d(0, 0, 0);
+ glColor3d(0, 0, 0);
glTexCoord2i(0, 0);
glVertex2f(50, 50);
-// glColor3d(1, 0, 0);
+ glColor3d(1, 0, 0);
glTexCoord2i(511, 0);
glVertex2f(561, 50);
-// glColor3d(0, 1, 0);
+ glColor3d(0, 1, 0);
glTexCoord2i(0, 511);
glVertex2f(50, 561);
-// glColor3d(0, 0, 1);
+ glColor3d(0, 0, 1);
glTexCoord2i(511, 511);
glVertex2f(561, 561);
glEnd();
@@ -77,6 +83,49 @@ virtual int startup() throw (GeneralException) {
glEnd();
mogltk::glbase::Leave2DMode();
+ font.putcursor(10, 10);
+ font.putentry('P');
+ font.putentry('i');
+ font.putentry('x');
+ font.putentry('e');
+ font.putentry('l');
+ font.putentry('P');
+ font.putentry('a');
+ font.putentry('w');
+ font.putentry('a');
+ font.putentry('!');
+ font.newline();
+ font.putentry('I');
+ font.putentry('t');
+ font.putentry(' ');
+ font.putentry('w');
+ font.putentry('o');
+ font.putentry('r');
+ font.putentry('k');
+ font.putentry('s');
+ font.putentry('!');
+ font.putentry('!');
+ font.newline();
+ font.putentry('I');
+ font.putentry(' ');
+ font.putentry('c');
+ font.putentry('a');
+ font.putentry('n');
+ font.putentry('\'');
+ font.putentry('t');
+ font.putentry(' ');
+ font.putentry('b');
+ font.putentry('e');
+ font.putentry('l');
+ font.putentry('i');
+ font.putentry('e');
+ font.putentry('v');
+ font.putentry('e');
+ font.putentry(' ');
+ font.putentry('i');
+ font.putentry('t');
+ font.putentry('!');
+
mogltk::glbase::Flip();
// sleep(15);