Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
font3d.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** Confidential - Westwood Studios ***
21 ***********************************************************************************************
22 * *
23 * Project Name : Commando / G 3D Library *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/font3d.h $*
26 * *
27 * $Author:: Byon_g $*
28 * *
29 * $Modtime:: 4/05/01 2:19p $*
30 * *
31 * $Revision:: 4 $*
32 * *
33 *---------------------------------------------------------------------------------------------*/
34
35
36#if defined(_MSC_VER)
37#pragma once
38#endif
39
40#ifndef FONT3D_H
41#define FONT3D_H
42
43#include "always.h"
44#include "refcount.h"
45#include "vector4.h"
46#include "widestring.h"
47#include "rect.h"
48
49class TextureClass;
50class SurfaceClass;
51
52/******************************************************************
53**
54** Font3DDataClass
55**
56** This class provides an interface to a font texture. Once
57** created and loaded with a font, the object can return texture
58** u v coordinate for any character in the font, as well as the
59** character width for proportional fonts. Fonts are loaded as
60** 16-bit Targa files, then converted to proportional fonts by
61** finding the minimum bounding box for each chacter. The font
62** texture is then minimized to a 256x256 or 128x128 texture
63** material by re-stacking chars by thier minimum bounding box.
64**
65** During use, this class is really no more than a data table accessor
66** Only during creation is any real code run.
67**
68** Since the space char nevers needs to be drawn, do not use
69** the conventional method of acessing and drawing chars (which will
70** still work). Instead, call Get_Space_Width to determine the user-
71** settable width, and skip the drawing.
72**
73*******************************************************************/
75
76public:
77
78 /*
79 ** Constructor, Constructor which loads a targa file,
80 ** and Destructor
81 */
82 Font3DDataClass( const char *filename );
84
85 // the name of the font data (used for name matching and the like.)
86 char *Name;
87
88 /*
89 ** access character width and height in pixels (clamp char to 0.255)
90 */
91
92 unsigned char Char_Width( WCHAR ch = (WCHAR)'H' ) { return CharWidthTable[ch&0xFF]; }// & 0xFF]; } // No need to "& 0xff" with chars!!!
93 unsigned char Char_Height( WCHAR /*ch = 'H'*/ ) { return CharHeight; }
94
95 // u and v are in normalized texture space
96 inline float Char_U_Offset( WCHAR ch = (WCHAR)'H') { return UOffsetTable[ch&0xFF]; }// & 0xFF]; }
97 inline float Char_V_Offset( WCHAR ch = (WCHAR)'H') { return VOffsetTable[ch&0xFF]; }// & 0xFF]; }
98 inline float Char_U_Width( WCHAR ch = (WCHAR)'H' ) { return UWidthTable[ch&0xFF]; }// & 0xFF]; }
99 inline float Char_V_Height( WCHAR /*ch = 'H'*/) { return VHeight; }
100
101 // get all four UV values as one vector4
102 Vector4 Char_UV_Corners( WCHAR ch = (WCHAR)'H' )
103 {
104// ch &= 0xFF;
105 return Vector4( UOffsetTable[ch], VOffsetTable[ch],
106 UOffsetTable[ch] + UWidthTable[ch],
107 VOffsetTable[ch] + VHeight );
108 }
109
110 /*
111 ** access texture material
112 */
113 TextureClass * Peek_Texture( void ) { return Texture; }
114
115private:
116 /*
117 ** The material (texture) which holds the font
118 */
119 TextureClass * Texture;
120
121 /*
122 ** Normalized texture page offsets for each character
123 */
124 float UOffsetTable[ 256 ];
125 float VOffsetTable[ 256 ];
126 float UWidthTable[ 256 ];
127 float VHeight;
128
129 unsigned char CharWidthTable[ 256 ];
130 unsigned char CharHeight;
131
132 /*
133 ** load a targa font image (.TGA)
134 */
135 bool Load_Font_Image( const char *filename );
136
137 /*
138 ** routines to convert a mono-spaced font to a proportional
139 ** font and minimize texture image size as a result
140 */
141 SurfaceClass *Make_Proportional( SurfaceClass *font_image );
142 SurfaceClass *Minimize_Font_Image( SurfaceClass *font_image );
143
144};
145
146/******************************************************************
147**
148** Font3DInstanceClass
149**
150*******************************************************************/
152
153public:
154 /*
155 ** Constructor which creates/gets a Font3DDataClass object,
156 ** and Destructor
157 */
158 Font3DInstanceClass( const char *filename );
160
161 /*
162 ** access texture material
163 */
164 TextureClass *Peek_Texture( void ) { return FontData->Peek_Texture(); }
165
166 /*
167 ** The non-scaled monospace char width in pixels ( set to 0 for proportional spaced font )
168 */
169 void Set_Mono_Spaced( void );
170 void Set_Proportional( void ) { MonoSpacing = 0; Build_Cached_Tables(); }
171
172
173 /*
174 ** Set the font scale (default to 1)
175 ** This amount will be automatically applied to all Char_Screen_Width calls
176 */
177 void Set_Scale( float scale ) { Scale = scale; Build_Cached_Tables(); }
178 // float Get_Scale() const { return Scale; }
179
180 /*
181 ** The scaled character pixel width, height, and spacing data (clamp char to 0.255)
182 */
183 float Char_Width( WCHAR ch ) const { return ScaledWidthTable[ch&0xFF]; }
184 float Char_Spacing( WCHAR ch ) const { return ScaledSpacingTable[ch&0xFF]; }
185 float Char_Height( void ) const { return ScaledHeight; }
186
187
188 /*
189 ** The scaled pixel width of a string; useful before printing to avoid screen overflows.
190 */
191 float String_Width( const WCHAR *test_str );
192 float String_Width( const char *test_str );
193
194 /*
195 ** Char UVs
196 */
197 // u and v are in normalized texture space
198 // inline float Char_U_Offset( WCHAR ch = (WCHAR)'H') { return FontData->Char_U_Offset( ch & 0xFF ); }
199 // inline float Char_V_Offset( WCHAR ch = (WCHAR)'H') { return FontData->Char_V_Offset( ch & 0xFF ); }
200 // inline float Char_U_Width( WCHAR ch = (WCHAR)'H' ) { return FontData->Char_U_Width( ch & 0xFF ); }
201 // inline float Char_V_Height( WCHAR ch = (WCHAR)'H') { return FontData->Char_V_Height( ch & 0xFF ); }
202 // Vector4 Char_UV_Corners( WCHAR ch = (WCHAR)'H' ) { return FontData->Char_UV_Corners( ch & 0xFF ); }
203 RectClass Char_UV( WCHAR ch ) { return RectClass( FontData->Char_U_Offset(ch),
204 FontData->Char_V_Offset(ch),
205 FontData->Char_U_Offset(ch) + FontData->Char_U_Width(ch),
206 FontData->Char_V_Offset(ch) + FontData->Char_V_Height(ch) ); }
207private:
208
209 Font3DDataClass * FontData; // The font data
210 float Scale; // The current scale factor
211 float SpaceSpacing; // non-scaled width of space in pixels ( defaults to 1/2 'H' width )
212 float InterCharSpacing; // non-scaled width between chars in pixels
213 float MonoSpacing; // non-scaled monospace char width in pixels (0 for proportional)
214
215 float ScaledWidthTable[256]; // scaled cache of the chars pixel widths
216 float ScaledSpacingTable[256]; // scaled cache of the chars pixel spacing
217 float ScaledHeight; // scaled cache of the chars pixel height
218
219 void Build_Cached_Tables();
220};
221
222
223#endif
Color scale(const Color &a, const Color &b)
Definition GameMtl.cpp:722
char * Name
Definition font3d.h:86
float Char_U_Width(WCHAR ch=(WCHAR) 'H')
Definition font3d.h:98
unsigned char Char_Width(WCHAR ch=(WCHAR) 'H')
Definition font3d.h:92
unsigned char Char_Height(WCHAR)
Definition font3d.h:93
float Char_V_Height(WCHAR)
Definition font3d.h:99
float Char_U_Offset(WCHAR ch=(WCHAR) 'H')
Definition font3d.h:96
TextureClass * Peek_Texture(void)
Definition font3d.h:113
float Char_V_Offset(WCHAR ch=(WCHAR) 'H')
Definition font3d.h:97
Font3DDataClass(const char *filename)
Definition font3d.cpp:58
Vector4 Char_UV_Corners(WCHAR ch=(WCHAR) 'H')
Definition font3d.h:102
float Char_Spacing(WCHAR ch) const
Definition font3d.h:184
void Set_Proportional(void)
Definition font3d.h:170
RectClass Char_UV(WCHAR ch)
Definition font3d.h:203
TextureClass * Peek_Texture(void)
Definition font3d.h:164
Font3DInstanceClass(const char *filename)
Definition font3d.cpp:355
void Set_Scale(float scale)
Definition font3d.h:177
float Char_Width(WCHAR ch) const
Definition font3d.h:183
void Set_Mono_Spaced(void)
Definition font3d.cpp:378
float Char_Height(void) const
Definition font3d.h:185
float String_Width(const WCHAR *test_str)
Definition font3d.cpp:410
RefCountClass(void)
Definition refcount.h:108