Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
txt.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#include <stdio.h>
20#include <math.h>
21#include "pot.h"
22#include "txt.h"
23#include "bsurface.h"
24#include "texture.h"
25#include "font.h"
26#include "nstrdup.h"
27
29 TextureString(NULL),
30 Texture(NULL),
31 TextureSize(0),
32 ForegroundColor(0),
33 BackgroundColor(0),
34 Font(0),
35 Convert(0)
36{
37}
38
40{
41 REF_PTR_RELEASE(Texture);
42
43 if (TextureString) {
44 delete[] TextureString;
45 }
46}
47
48bool TextTextureClass::Is_Texture_Valid(FontClass &font, const char *str, int fore, int back, ConvertClass &conv)
49{
50 // if there is no texture at all then obviously it is not valid
51 if (!Texture) return false;
52
53 // if all the parameters the texture was created with the last time are the same as the parameters the texture
54 // was created with this time then the texture is fine!
55 if (TextureString == str && Font == &font && Convert == &conv && ForegroundColor == fore && BackgroundColor == back)
56 return true;
57
58 return false;
59}
60
61bool TextTextureClass::Build_Texture(FontClass &font, const char *str, int fore, int back, ConvertClass &conv)
62{
63 static char default_font_palette[] = {
64 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 };
66
67 // right off determine if we need to rebuild the texture or the current texture
68 // is just fine. If we are rebuilding store of the properties of the texture
69 // we are building for next time.
70 if (Is_Texture_Valid(font, str, fore, back, conv)) {
71 return false;
72 } else {
73 if (TextureString) delete[] TextureString;
74 Font = &font;
75 TextureString = nstrdup(str);
76 Convert = &conv;
77 BackgroundColor = back;
78 ForegroundColor = fore;
79 }
80
81
82 default_font_palette[0] = back;
83 default_font_palette[1] = fore;
84
85 // find the width and height of the text we want to display to the screen
86 // note that the width could be more than the max width of a texture
87 // so we must be prepared to break it down into blocks. Theoretically the
88 // height could be greater than the texture max but lets not be ridiculous
89 int fw = font.String_Pixel_Width(str)+1;
90 int fh = font.Get_Height()+1;
91
92 // Note: we are currently printing the text into
93 // a rectangular buffer. We will later blit the text into a square buffer
94 // since some cards require square textures.
95 Rect rect(0,0,fw,fh);
96 BSurface bsurf(fw, fh, 1);
97 bsurf.Fill(0);
98 font.Print(str, bsurf, rect, TPoint2D<int>(0,0), conv, (unsigned char *)default_font_palette);
99
100 // figure out the size of the best texture which can hold the
101 // text we wrote. Since textures need to be assumed to be square
102 // the best size should be found by finding the rectangular surface
103 // area of the text, and creating a Power of Two sized square which
104 // can hold the data in the ractangular surface area.
105 float fsize = sqrt(fw * fh);
106 TextureSize = Find_POT(ceil(fsize));
107 // If this is still not enough, quadruple area:
108 if ((TextureSize / fh) * TextureSize < fw) {
109 TextureSize *= 2;
110 }
111
112 // we now need to create a westwood style surface out of the surrender surface
113 // so we can use all the westwood drawing primitives. Clear this surface to
114 // black (shouldn't need to do this I dont think)
115 BSurface bsurf2(TextureSize, TextureSize, 1);
116 bsurf2.Fill(0);
117
118 // we need to calculate how many texture pot widths it takes
119 // to hold the width of our font.
120 int mw = (fw & (TextureSize - 1)) ? (fw / TextureSize)+1 : (fw /TextureSize);
121
122 // now we need to blit the old surface into the new surface (effectively
123 // making the text in a square surface).
124 for (int lp = 0; lp < mw; lp ++) {
125 int blitw = MIN(fw - (TextureSize *lp), TextureSize);
126 int lp_tsize = lp * TextureSize;
127 Rect destrect(0,fh*lp,blitw,fh*lp+fh);
128 Rect srcrect(lp_tsize, 0, lp_tsize + blitw, fh);
129 /* original code
130 d->blit(0, fh * lp, *s, lp_tsize, 0, lp_tsize + blitw, fh);
131 */
132 bsurf2.Blit_From(destrect,bsurf,srcrect);
133 }
134
135 // Create texture without mip mapping
136 REF_PTR_RELEASE(Texture);
137
138 // TODO: copy bsurf2 into texture
139
140 return true;
141}
142
#define NULL
Definition BaseType.h:92
#define MIN(a, b)
Definition always.h:189
int Find_POT(int val)
Definition pot.cpp:53
virtual int String_Pixel_Width(char const *string) const =0
virtual Point2D Print(char const *string, Surface &surface, Rect const &cliprect, Point2D const &point, ConvertClass const &converter, unsigned char const *remap=NULL) const =0
virtual int Get_Height(void) const =0
TextTextureClass(void)
Definition txt.cpp:28
~TextTextureClass(void)
Definition txt.cpp:39
bool Build_Texture(FontClass &font, const char *str, int fore, int back, ConvertClass &conv)
Definition txt.cpp:61
virtual bool Blit_From(Rect const &dcliprect, Rect const &destrect, Surface const &source, Rect const &scliprect, Rect const &sourcerect, bool trans=false)
Definition xsurface.cpp:525
virtual bool Fill(int color)
Definition xsurface.cpp:434
char * nstrdup(const char *str)
Definition nstrdup.cpp:56
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
TRect< int > Rect
Definition trect.h:221