Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
txt2d.cpp
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 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***************************************************************************
22 * *
23 * Project Name : Commando/G *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/txt2d.cpp $*
26 * *
27 * $Author:: Patrick $*
28 * *
29 * $Modtime:: 3/27/01 5:08p $*
30 * *
31 * $Revision:: 3 $*
32 * *
33 *-------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#include "txt2d.h"
38#include "font.h"
39#include "assetmgr.h"
40#include "ww3d.h"
41#include "pot.h"
42#include "bsurface.h"
43#include "texture.h"
44#include <stdio.h>
45#include <stdarg.h>
46
47#ifdef WW3D_DX8
48#include <srTextureMap.hpp>
49
50float Text2DObjClass::_LastWidth = 0.0f;
51float Text2DObjClass::_LastHeight = 0.0f;
52
53/**************************************************************************
54 * T2DOC::Text2DObjClass -- Constructor for 2D text objects *
55 * *
56 * INPUT: *
57 * *
58 * OUTPUT: *
59 * *
60 * WARNINGS: *
61 * *
62 * HISTORY: *
63 * 09/08/1997 PWG : Created. *
64 *========================================================================*/
65Text2DObjClass::Text2DObjClass(FontClass &font, const char *str, float screen_x, float screen_y, int fore, int back, ConvertClass &conv, bool center, bool clamp, ...):
67{
68 char text[255];
69 // print the variable argument string into an argument list
70 va_list args;
71 va_start(args, clamp);
72 vsprintf(text, str, args);
73 va_end(args);
74
75 Set_Text(font, text, screen_x, screen_y, fore, back, conv, center, clamp);
76}
77
78
79/**************************************************************************
80 * T2DOC::Text2DObjClass -- Constructor for 2D text objects *
81 * *
82 * INPUT: *
83 * *
84 * OUTPUT: *
85 * *
86 * WARNINGS: *
87 * *
88 * HISTORY: *
89 * 09/08/1997 PWG : Created. *
90 * 01/28/2000 SKB : Dont' Set font width *
91 *========================================================================*/
92void Text2DObjClass::Set_Text(FontClass &font, const char *str, float screen_x, float screen_y, int fore, int back, ConvertClass &conv, bool center, bool clamp, ...)
93{
94 int resw, resh, resbits;
95 bool windowed;
96
97 // find the resolution (for centering and pixel to pixel translation)
98 WW3D::Get_Device_Resolution(resw, resh, resbits, windowed);
99
100 // print the variable argument string into an argument list
101 char text[255];
102 va_list args;
103
104 va_start(args, clamp);
105 vsprintf(text, str, args);
106 va_end(args);
107
108
109 // if we didn't build a new texture than all the polygons will be just
110 // fine so we don't need to do anything.
111 if (!TextTexture.Build_Texture(font, text, fore, back, conv))
112 return;
113
114 // cache the TextTexture elements we think are important on the local
115 // stack.
116 srTextureIFace *texture_map = TextTexture.Get_Texture();
117 int tsize = TextTexture.Get_Texture_Size();
118
119 // find the width and height of the text we want to display to the screen
120 // note that the width could be more than the max width of a texture
121 // so we must be prepared to break it down into blocks. Theoretically the
122 // height could be greater than the texture max but lets not be ridiculous
123
124 // SKB: 1/25/99 This should not be determined by this function, it
125 // is up to the caller how he/she wants the font to look.
126 //font.Set_XSpacing(-1);
127 int fw = font.String_Pixel_Width(text)+1;
128 int fh = font.Get_Height()+1;
129 int mw = (fw & (tsize - 1)) ? (fw / tsize)+1 : (fw /tsize);
130
131 // if we requested the text to be centered around a point adjust the
132 // coordinates accordingly.
133 float x_delta = 0.0f;
134 float y_delta = 0.0f;
135 if (center) {
136 x_delta = -((float)fw / (float)resw) / 2.0f;
137 y_delta = -((float)fh / (float)resh) / 2.0f;
138 }
139
140 // Set so user can know where end of string is printed.
141 _LastWidth = ((float) fw / (float)resw);
142 _LastHeight = ((float) fh / (float)resh);
143
144 // if we requested that the coordinates be checked to ensure they are onscreen,
145 // adjust the coordinates as neccessary.
146 if (clamp) {
147
148 if (screen_x < 0) screen_x = 0;
149 if (screen_x + _LastWidth > 1.0f) {
150 screen_x = 1.0f - _LastWidth;
151 assert(screen_x >= 0.0f);
152 }
153 if (screen_y < 0) screen_y = 0;
154 if (screen_y + _LastHeight > 1.0f) {
155 screen_y = 1.0f - _LastHeight;
156 assert(screen_y >= 0.0f);
157 }
158 }
159
160 // for every square material it takes two triangles to represent it.
161 Resize(mw * 2, mw * 4);
162
163 // Set texture.
164 Set_Texture(texture_map);
165
166 // Set shader to 2d opaque preset.
168
169 // Sorting is always set on 2D objects so that sortbias can be used to
170 // determine occlusion between them.
171 Enable_Sort();
172
173 // loop through the rows blocks of our text and make polygons mapped
174 // with the correct section of our partial texture.
175 for (int lp = 0; lp < mw; lp ++) {
176 int pw = MIN(fw - (tsize *lp), tsize);
177 int ph = fh;
178
179 // calculate our actual texture coordinates based on the difference between
180 // the width and height of the texture and the width and height the font
181 // occupys.
182 float tw = (float)pw / (float)tsize;
183 float th = (float)ph / (float)tsize;
184 float ty = ((float)fh * (float)lp) / (float)tsize;
185
186 // figure out the screen space x and y positions of the object in question.
187 float x = ((float)lp * (float)tsize) / (float)resw;
188// x += (0.5f /resw);
189
190 float y = 0;
191// y += (0.5f /resh);
192
193 // Adjust for centering if needed
194 x += x_delta;
195 y += y_delta;
196
197 // convert image width and image height to normalized values
198 float vw = (float)pw / (float)resw;
199 float vh = (float)ph / (float)resh;
200
201 Begin_Tri_Strip();
202 Vertex( x, y, 0, 0, ty);
203 Vertex( x + vw, y, 0, tw, ty);
204 Vertex( x, y + vh, 0, 0, ty + th);
205 Vertex( x + vw, y + vh, 0, tw, ty + th);
206 End_Tri_Strip();
207 }
208
209 // fixup screen_x (align it with a pixel edge)
210 float pixel_x, pixel_y;
211 WW3D::Get_Pixel_Center(pixel_x, pixel_y);
212
213 screen_x *= (float)resw;
214 screen_x = floor(screen_x) + pixel_x;
215 screen_x /= (float)resw;
216
217 // fixup screen_y (align it with a pixel edge)
218 screen_y *= (float)resh;
219 screen_y = floor(screen_y) + pixel_y;
220 screen_y /= (float)resh;
221
222 Set_Position(Vector3(screen_x, screen_y, 0));
223
224 Set_Dirty();
225}
226
227#endif //WW3D_DX8
NUM clamp(NUM lo, NUM val, NUM hi)
Definition BaseType.h:150
#define MIN(a, b)
Definition always.h:189
virtual int String_Pixel_Width(char const *string) const =0
virtual int Get_Height(void) const =0
static ShaderClass _PresetOpaque2DShader
Definition shader.h:381
static void Get_Device_Resolution(int &set_w, int &set_h, int &get_bits, bool &get_windowed)
Definition ww3d.cpp:670
static void Get_Pixel_Center(float &x, float &y)
Definition ww3d.cpp:1947