summaryrefslogtreecommitdiff
path: root/common/texfont.cpp
blob: a5c3b5d11afda2843981673ad4edbb6c37ef6854 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Texture Font
//

#include "globals.h"
#include "project.h"
#include "texfont.h"
#include "texture.h"
#include "library.h"
#include "file.h"
#include "lc_application.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 = lcGetPiecesLibrary()->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::GetStringDimensions(int* cx, int* cy, const char* Text) const
{
	*cx = 0;
	*cy = m_nFontHeight;

  while (*Text != 0)
  {
		*cx += m_Glyphs[(int)(*Text)].width;
    Text++;
  }
}

void TexFont::PrintText(float Left, float Top, float ScaleX, float ScaleY, const char* Text) const
{
	float Height = m_nFontHeight * ScaleY;

  while (*Text != 0)
  {
    int ch = *Text;
    glTexCoord2f(m_Glyphs[ch].left, m_Glyphs[ch].top);
    glVertex2f(Left, Top);
    glTexCoord2f(m_Glyphs[ch].left, m_Glyphs[ch].bottom);
    glVertex2f(Left, Top - Height);
    glTexCoord2f(m_Glyphs[ch].right, m_Glyphs[ch].bottom);
    glVertex2f(Left + m_Glyphs[ch].width * ScaleX, Top - Height);
    glTexCoord2f(m_Glyphs[ch].right, m_Glyphs[ch].top);
    glVertex2f(Left + m_Glyphs[ch].width * ScaleX, Top);

    Left += m_Glyphs[ch].width * ScaleX;
    Text++;
  }
}

// Old function, should probably be removed.
void TexFont::PrintText(float Left, float Top, float Z, const char* Text) const
{
	while (*Text != 0)
	{
	  int ch = *Text;
		glTexCoord2f(m_Glyphs[ch].left, m_Glyphs[ch].top);
		glVertex3f(Left, Top, Z);
		glTexCoord2f(m_Glyphs[ch].left, m_Glyphs[ch].bottom);
		glVertex3f(Left, Top - m_nFontHeight, Z);
		glTexCoord2f(m_Glyphs[ch].right, m_Glyphs[ch].bottom);
		glVertex3f(Left + m_Glyphs[ch].width, Top - m_nFontHeight, Z);
		glTexCoord2f(m_Glyphs[ch].right, m_Glyphs[ch].top);
		glVertex3f(Left + m_Glyphs[ch].width, Top, Z);

		Left += m_Glyphs[ch].width;
		Text++;
	}
}

// Temporary function to draw the axis icon text
void TexFont::PrintCharScaled (float scale, int 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);
}