summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorleo2002-03-10 03:24:36 +0000
committerleo2002-03-10 03:24:36 +0000
commit881162593c5aff96ec559a3526c7647197ddee81 (patch)
treedeab302a975ba912573c6f8bd591f794c8290052 /common
parent4470ba0ac09a6c4e0ef85b03c29834c542d00564 (diff)
Moved the texture font to a class to allow support for multiple fonts.
git-svn-id: http://svn.leocad.org/trunk@280 c7d43263-9d01-0410-8a33-9dba5d9f93d6
Diffstat (limited to 'common')
-rw-r--r--common/project.cpp120
-rw-r--r--common/project.h4
-rw-r--r--common/texfont.cpp123
-rw-r--r--common/texfont.h32
-rw-r--r--common/texture.h2
5 files changed, 183 insertions, 98 deletions
diff --git a/common/project.cpp b/common/project.cpp
index 497e9ad..c2c8c4f 100644
--- a/common/project.cpp
+++ b/common/project.cpp
@@ -69,14 +69,6 @@ static LC_VIEWPORT viewports[14] = {
{ 4, {{ 0.0f, 0.0f, 0.5f, 0.5f }, { 0.5f, 0.0f, 0.5f, 0.5f },
{ 0.0f, 0.5f, 0.5f, 0.5f }, { 0.5f, 0.5f, 0.5f, 0.5f } }}};// 4
-typedef struct
-{
- unsigned char width;
- float left, right, top, bottom;
-} LC_TXFVERT;
-
-static LC_TXFVERT glyphs[93];
-
/////////////////////////////////////////////////////////////////////////////
// Project construction/destruction
@@ -119,40 +111,6 @@ Project::Project()
for (i = 0; i < 10; i++)
m_pClipboard[i] = NULL;
-
- // Create font table
- float inv = 1.0f/128;
- char *charlines[16] = {
- "abcdefghijklmn", "opqrstuvwxyz0", "123456789ABC", "DEFGHIJKLMN",
- "OPQRSTUVWX", "YZ,.!;:<>/?{}@$%", "&*()-+=_[] #" };
- unsigned char lefts[7][17] = {
- { 1, 11, 21, 30, 40, 50, 56, 66, 76, 80, 84, 93, 97, 111, 121 },
- { 1, 11, 21, 31, 38, 47, 53, 63, 72, 86, 94, 103, 111, 120 },
- { 1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 94, 106, 118, },
- { 1, 13, 24, 34, 47, 59, 64, 73, 84, 94, 108, 120 },
- { 1, 14, 25, 38, 50, 61, 71, 83, 94, 109, 120 },
- { 1, 12, 22, 26, 30, 35, 39, 43, 52, 61, 65, 75, 81, 87, 103, 112, 125 },
- { 3, 14, 23, 28, 33, 38, 47, 56, 65, 70, 75, 79, 88 } };
- // tops = 1 20 39 58 77 96 112 (+16)
- memset(glyphs, 0, sizeof(glyphs));
-
- // ASCII 32-125
- for (i = 32; i < 126; i++)
- for (int x = 0; x < 7; x++)
- for (int y = 0; charlines[x][y]; y++)
- if (charlines[x][y] == i)
- {
- glyphs[i-32].width = lefts[x][y+1] - lefts[x][y];
- glyphs[i-32].left = (float)lefts[x][y]*inv;
- glyphs[i-32].right = (float)(lefts[x][y+1])*inv;
- if (x != 6)
- glyphs[i-32].top = (float)(1 + 19*x);
- else
- glyphs[i-32].top = 112;
- glyphs[i-32].bottom = glyphs[i-32].top + 16;
- glyphs[i-32].top *= inv;
- glyphs[i-32].bottom *= inv;
- }
}
Project::~Project()
@@ -2101,53 +2059,27 @@ void Project::RenderScene(bool bShaded, bool bDrawViewports)
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
- LC_TXFVERT* glyph;
-
- glPushMatrix();
+ glPushMatrix();
glTranslatef(1.4f*ds, 0, 0);
glMultMatrixf(m.m);
- glyph = &glyphs['X'-32];
glBegin(GL_QUADS);
- glTexCoord2f(glyph->left, glyph->top);
- glVertex2f(0, 0.4f*ds);
- glTexCoord2f(glyph->left, glyph->bottom);
- glVertex2f(0, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->bottom);
- glVertex2f(glyph->width*ds/20, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->top);
- glVertex2f(glyph->width*ds/20, 0.4f*ds);
+ m_ScreenFont.PrintCharScaled (0.025f * ds, 'X');
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0, 1.4f*ds, 0);
glMultMatrixf(m.m);
- glyph = &glyphs['Y'-32];
glBegin(GL_QUADS);
- glTexCoord2f(glyph->left, glyph->top);
- glVertex2f(0, 0.4f*ds);
- glTexCoord2f(glyph->left, glyph->bottom);
- glVertex2f(0, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->bottom);
- glVertex2f(glyph->width*ds/20, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->top);
- glVertex2f(glyph->width*ds/20, 0.4f*ds);
+ m_ScreenFont.PrintCharScaled (0.025f * ds, 'Y');
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0, 0, 1.4f*ds);
glMultMatrixf(m.m);
- glyph = &glyphs['Z'-32];
glBegin(GL_QUADS);
- glTexCoord2f(glyph->left, glyph->top);
- glVertex2f(0, 0.4f*ds);
- glTexCoord2f(glyph->left, glyph->bottom);
- glVertex2f(0, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->bottom);
- glVertex2f(glyph->width*ds/20, -0.4f*ds);
- glTexCoord2f(glyph->right, glyph->top);
- glVertex2f(glyph->width*ds/20, 0.4f*ds);
+ m_ScreenFont.PrintCharScaled (0.025f * ds, 'Z');
glEnd();
glPopMatrix();
@@ -2521,24 +2453,7 @@ void Project::RenderViewports(bool bBackground, bool bLines)
w = viewports[m_nViewportMode].dim[vp][2] * (float)(m_nViewX - 1);
h = viewports[m_nViewportMode].dim[vp][3] * (float)(m_nViewY - 1);
- float l = x+3, t = y+h-6;
- for (const char* p = m_pViewCameras[vp]->GetName(); *p; p++)
- {
- if (*p < 32 || *p > 125)
- continue;
- if (glyphs[*p-32].width == 0)
- continue;
-
- glTexCoord2f(glyphs[*p-32].left, glyphs[*p-32].top);
- glVertex2f(l, t);
- glTexCoord2f(glyphs[*p-32].left, glyphs[*p-32].bottom);
- glVertex2f(l, t-16);
- glTexCoord2f(glyphs[*p-32].right, glyphs[*p-32].bottom);
- glVertex2f(l + glyphs[*p-32].width, t-16);
- glTexCoord2f(glyphs[*p-32].right, glyphs[*p-32].top);
- glVertex2f(l + glyphs[*p-32].width, t);
- l += glyphs[*p-32].width;
- }
+ m_ScreenFont.PrintText (x + 3, y + h - 6, m_pViewCameras[vp]->GetName ());
}
glEnd();
@@ -2676,8 +2591,19 @@ void Project::RenderInitialize()
else
glDisable (GL_FOG);
- // Load font (always the first texture)
- m_pTextures[0].Load(false);
+ // Load font
+ if (!m_ScreenFont.IsLoaded ())
+ {
+ char filename[LC_MAXPATH];
+ FileDisk file;
+
+ strcpy (filename, m_LibraryPath);
+ strcat (filename, "sysfont.txf");
+
+ if (file.Open (filename, "rb"))
+ m_ScreenFont.FileLoad (file);
+ }
+
glAlphaFunc(GL_GREATER, 0.0625);
if (m_nScene & LC_SCENE_FLOOR)
@@ -5985,13 +5911,13 @@ void Project::GetFocusPosition(float* pos)
pos[0] = pos[1] = pos[2] = 0.0f;
}
-Texture* Project::FindTexture(char* name)
+Texture* Project::FindTexture (const char* name)
{
- for (int i = 0; i < m_nTextureCount; i++)
- if (!strcmp (name, m_pTextures[i].m_strName))
- return &m_pTextures[i];
+ for (int i = 0; i < m_nTextureCount; i++)
+ if (!strcmp (name, m_pTextures[i].m_strName))
+ return &m_pTextures[i];
- return NULL;
+ return NULL;
}
// Remeber to make 'name' uppercase.
diff --git a/common/project.h b/common/project.h
index 60a3021..a9bb1e8 100644
--- a/common/project.h
+++ b/common/project.h
@@ -5,6 +5,7 @@
#include "defines.h"
#include "typedefs.h"
#include "opengl.h"
+#include "texfont.h"
#include "array.h"
typedef enum
@@ -94,7 +95,7 @@ public:
void Render(bool bToMemory);
void SetViewSize(int cx, int cy);
- Texture* FindTexture(char* name);
+ Texture* FindTexture (const char* name);
PieceInfo* FindPieceInfo (const char* name) const;
void CheckAutoSave();
void GetFocusPosition(float* pos);
@@ -124,6 +125,7 @@ protected:
PieceInfo* m_pPieceIdx; // index
int m_nTextureCount;
Texture* m_pTextures;
+ TexFont m_ScreenFont;
char* m_pMovedReference;
int m_nMovedCount;
diff --git a/common/texfont.cpp b/common/texfont.cpp
new file mode 100644
index 0000000..252b72b
--- /dev/null
+++ b/common/texfont.cpp
@@ -0,0 +1,123 @@
+//
+// Texture Font
+//
+
+#include "globals.h"
+#include "project.h"
+#include "texfont.h"
+#include "texture.h"
+#include "file.h"
+
+#define LC_TEXFONT_FILE_VERSION 1 // LeoCAD 0.74
+#define LC_TEXFONT_FILE_HEADER "LeoCAD Texture Font\0\0\0\0\0\0\0\0\0\0\0\0"
+
+// ============================================================================
+
+TexFont::TexFont ()
+{
+ m_bLoaded = false;
+ m_pTexture = NULL;
+
+ memset (&m_Glyphs, 0, sizeof (m_Glyphs));
+}
+
+TexFont::~TexFont ()
+{
+ if (m_pTexture != NULL)
+ m_pTexture->DeRef ();
+}
+
+bool TexFont::FileLoad (File& file)
+{
+ unsigned char version;
+ char buf[64];
+
+ if (m_bLoaded)
+ {
+ console.PrintError ("Texture font already loaded.\n");
+ return false;
+ }
+
+ file.Read (buf, 32);
+ if (strncmp (buf, LC_TEXFONT_FILE_HEADER, 32) != 0)
+ {
+ console.PrintError ("Texture font file header mismatch.\n");
+ return false;
+ }
+
+ file.ReadByte (&version, 1);
+ if (version > LC_TEXFONT_FILE_VERSION)
+ {
+ console.PrintError ("Wrong version of texture font file.\n");
+ return false;
+ }
+
+ memset (buf, 0, 32);
+ file.Read (buf, 8);
+
+ m_pTexture = project->FindTexture (buf);
+ if (m_pTexture == NULL)
+ {
+ console.PrintError ("Cannot find texture for font %s.\n", buf);
+ return false;
+ }
+ m_pTexture->AddRef (false);
+
+ file.ReadByte (&m_nFontHeight, 1);
+
+ for (;;)
+ {
+ unsigned char glyph;
+
+ file.ReadByte (&glyph, 1);
+
+ if (glyph == 0)
+ break;
+
+ file.ReadByte (&m_Glyphs[glyph].width, 1);
+ file.ReadFloat (&m_Glyphs[glyph].left, 1);
+ file.ReadFloat (&m_Glyphs[glyph].right, 1);
+ file.ReadFloat (&m_Glyphs[glyph].top, 1);
+ file.ReadFloat (&m_Glyphs[glyph].bottom, 1);
+
+ m_Glyphs[glyph].left /= m_pTexture->m_nWidth;
+ m_Glyphs[glyph].right /= m_pTexture->m_nWidth;
+ m_Glyphs[glyph].top /= m_pTexture->m_nHeight;
+ m_Glyphs[glyph].bottom /= m_pTexture->m_nHeight;
+ }
+
+ m_bLoaded = true;
+
+ return true;
+}
+
+void TexFont::PrintText (float left, float top, const char* text) const
+{
+ while (*text != 0)
+ {
+ glTexCoord2f (m_Glyphs[*text].left, m_Glyphs[*text].top);
+ glVertex2f (left, top);
+ glTexCoord2f (m_Glyphs[*text].left, m_Glyphs[*text].bottom);
+ glVertex2f (left, top - m_nFontHeight);
+ glTexCoord2f (m_Glyphs[*text].right, m_Glyphs[*text].bottom);
+ glVertex2f (left + m_Glyphs[*text].width, top - m_nFontHeight);
+ glTexCoord2f (m_Glyphs[*text].right, m_Glyphs[*text].top);
+ glVertex2f (left + m_Glyphs[*text].width, top);
+
+ left += m_Glyphs[*text].width;
+ text++;
+ }
+}
+
+// Temporary function to draw the axis icon text
+void TexFont::PrintCharScaled (float scale, char ch) const
+{
+ glTexCoord2f (m_Glyphs[ch].left, m_Glyphs[ch].top);
+ glVertex2f (-scale * m_Glyphs[ch].width, scale * m_nFontHeight);
+ glTexCoord2f (m_Glyphs[ch].left, m_Glyphs[ch].bottom);
+ glVertex2f (-scale * m_Glyphs[ch].width, -scale * m_nFontHeight);
+ glTexCoord2f (m_Glyphs[ch].right, m_Glyphs[ch].bottom);
+ glVertex2f (scale * m_Glyphs[ch].width, -scale * m_nFontHeight);
+ glTexCoord2f (m_Glyphs[ch].right, m_Glyphs[ch].top);
+ glVertex2f (scale * m_Glyphs[ch].width, scale * m_nFontHeight);
+}
diff --git a/common/texfont.h b/common/texfont.h
new file mode 100644
index 0000000..e3dc185
--- /dev/null
+++ b/common/texfont.h
@@ -0,0 +1,32 @@
+#ifndef _TEXFONT_H_
+#define _TEXFONT_H_
+
+class File;
+class Texture;
+
+class TexFont
+{
+public:
+ TexFont ();
+ ~TexFont ();
+
+ bool IsLoaded () const
+ { return m_bLoaded; }
+
+ bool FileLoad (File& file);
+ void PrintText (float left, float top, const char* text) const;
+ void PrintCharScaled (float scale, char ch) const;
+
+protected:
+ struct
+ {
+ unsigned char width;
+ float left, right, top, bottom;
+ } m_Glyphs[256];
+
+ Texture* m_pTexture;
+ unsigned char m_nFontHeight;
+ bool m_bLoaded;
+};
+
+#endif // _TEXFONT_H_
diff --git a/common/texture.h b/common/texture.h
index 52c223b..82eaf2e 100644
--- a/common/texture.h
+++ b/common/texture.h
@@ -7,6 +7,8 @@
class File;
+#include "opengl.h"
+
typedef enum { LC_INTENSITY, LC_RGB, LC_RGBA } LC_TEXTURE_TYPES;
class Texture