Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
lookuptable.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 : WWPhys *
24 * *
25 * $Archive:: /Commando/Code/wwmath/lookuptable.cpp $*
26 * *
27 * Original Author:: Greg Hjelstrom *
28 * *
29 * $Author:: Byon_g $*
30 * *
31 * $Modtime:: 7/23/01 6:18p $*
32 * *
33 * $Revision:: 5 $*
34 * *
35 *---------------------------------------------------------------------------------------------*
36 * Functions: *
37 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38
39#include "lookuptable.h"
40#include "curve.h"
41#include "wwfile.h"
42#include "ffactory.h"
43#include "chunkio.h"
44#include "persistfactory.h"
45#include "vector2.h"
46
47
48/*
49** Static members
50*/
52
53/*
54** Save-Load stuff. Lookup tables are basically saved 1D curve classes. They are turned
55** into tables at load time. For future expansion, the curve is wrapped in a chunk.
56*/
57enum
58{
61};
62
63
64/***********************************************************************************************
65**
66** LookupTableClass Implementation
67**
68***********************************************************************************************/
70 MinInputValue(0.0f),
71 MaxInputValue(0.0f),
72 OutputSamples(sample_count)
73{
74}
75
79
80void LookupTableClass::Init(const char * name,Curve1DClass * curve)
81{
82 // copy the name
83 Name = name;
84
85 // Store the min and max input values for the table
86 curve->Get_Key(0,NULL,&MinInputValue,NULL);
87 curve->Get_Key(curve->Key_Count()-1,NULL,&MaxInputValue,NULL);
89
90 // Sample the curve and store the output values
91 for (int i=0; i<OutputSamples.Length(); i++) {
92 float x = MinInputValue + (MaxInputValue - MinInputValue) * (float)i / (float)(OutputSamples.Length() - 1);
93 float y;
94 curve->Evaluate(x,&y);
95 OutputSamples[i] = y;
96 }
97}
98
99
100
101/***********************************************************************************************
102**
103** LookupTableManager Implementation
104**
105***********************************************************************************************/
107{
108 // create a default table that the user can use in an emergency
109 LookupTableClass * default_table = NEW_REF(LookupTableClass,(2));
111
112 default_curve->Add_Key(0.5f,0.0f);
113 default_curve->Add_Key(0.5f,1.0f);
114 default_table->Init("DefaultTable",default_curve);
115 Add_Table(default_table);
116
117 delete default_curve;
118 default_table->Release_Ref();
119}
120
122{
123 Reset();
124}
125
127{
128 while (Tables.Peek_Head() != NULL) {
129 Tables.Release_Head();
130 }
131}
132
134{
135 return Tables.Add(table);
136}
137
139{
140 return Tables.Remove(table);
141}
142
143LookupTableClass * LookupTableMgrClass::Get_Table(const char * name,bool try_to_load)
144{
145 // check if we already have this table loaded...
147 for (it.First(); !it.Is_Done(); it.Next()) {
148 if (stricmp(it.Peek_Obj()->Get_Name(),name) == 0) {
149 return it.Get_Obj(); // add a reference for the user...
150 }
151 }
152
153 // otherwise we can try to load it.
154 LookupTableClass * new_table = NULL;
155 if (try_to_load) {
156
157 FileClass * file = _TheFileFactory->Get_File(name);
158 if (file && file->Open()) {
159
160 ChunkLoadClass cload(file);
161
162 Curve1DClass * curve = NULL;
163 Load_Table_Desc(cload,&curve);
164 if (curve != NULL) {
165 new_table = NEW_REF(LookupTableClass,());
166 new_table->Init(name,curve);
167 Add_Table(new_table);
168 delete curve;
169 }
170 }
171 _TheFileFactory->Return_File(file);
172 }
173
174 return new_table; // constructor ref is returned to user.
175}
176
178(
179 ChunkSaveClass & csave,
180 Curve1DClass * curve,
181 const Vector2 & min_corner,
182 const Vector2 & max_corner
183)
184{
185 // save the curve
187 csave.Begin_Chunk(curve->Get_Factory().Chunk_ID());
188 curve->Get_Factory().Save(csave,curve);
189 csave.End_Chunk();
190 csave.End_Chunk();
191
192 // save the extents
194 csave.Write(&(min_corner.X),sizeof(float));
195 csave.Write(&(min_corner.Y),sizeof(float));
196 csave.Write(&(max_corner.X),sizeof(float));
197 csave.Write(&(max_corner.Y),sizeof(float));
198 csave.End_Chunk();
199}
200
202(
203 ChunkLoadClass & cload,
204 Curve1DClass ** curve_ptr,
205 Vector2 * set_min_corner,
206 Vector2 * set_max_corner
207)
208{
209 *curve_ptr = NULL;
210 PersistFactoryClass * factory;
211
212 float xmin,xmax;
213 float ymin,ymax;
214 xmin = xmax = ymin = ymax = 0.0f;
215
216 while (cload.Open_Chunk()) {
217 switch(cload.Cur_Chunk_ID()) {
219 cload.Open_Chunk();
221 WWASSERT(factory != NULL);
222 if (factory != NULL) {
223 *curve_ptr = (Curve1DClass *)factory->Load(cload);
224 }
225 cload.Close_Chunk();
226 break;
228 cload.Read(&xmin,sizeof(xmin));
229 cload.Read(&ymin,sizeof(ymin));
230 cload.Read(&xmax,sizeof(xmax));
231 cload.Read(&ymax,sizeof(ymax));
232 break;
233
234 default:
235 WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",__FILE__,__LINE__));
236 break;
237 }
238 cload.Close_Chunk();
239 }
240
241 if (set_min_corner != NULL) {
242 set_min_corner->Set(xmin,ymin);
243 }
244 if (set_max_corner != NULL) {
245 set_max_corner->Set(xmax,ymax);
246 }
247}
248
249
250
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define W3DNEW
Definition always.h:109
bool Close_Chunk()
Definition chunkio.cpp:448
uint32 Cur_Chunk_ID()
Definition chunkio.cpp:484
uint32 Read(void *buf, uint32 nbytes)
Definition chunkio.cpp:692
bool Open_Chunk()
Definition chunkio.cpp:412
uint32 Write(const void *buf, uint32 nbytes)
Definition chunkio.cpp:264
bool Begin_Chunk(uint32 id)
Definition chunkio.cpp:108
bool End_Chunk()
Definition chunkio.cpp:148
virtual void Evaluate(float time, float *set_val)=0
virtual int Key_Count(void)
Definition curve.cpp:379
virtual void Get_Key(int i, float *set_point, float *set_t, unsigned int *extra=NULL)
Definition curve.cpp:384
virtual int Add_Key(float point, float t, unsigned int extra=0)
Definition curve.cpp:408
virtual int Open(char const *filename, int rights=READ)=0
void First(GenericMultiListClass *list)
Definition multilist.h:182
StringClass Name
Definition lookuptable.h:74
void Init(const char *name, Curve1DClass *curve)
SimpleVecClass< float > OutputSamples
Definition lookuptable.h:78
virtual ~LookupTableClass(void)
LookupTableClass(int sample_count=256)
static void Init(void)
static void Save_Table_Desc(ChunkSaveClass &csave, Curve1DClass *curve, const Vector2 &min, const Vector2 &max)
static void Load_Table_Desc(ChunkLoadClass &cload, Curve1DClass **curve_ptr, Vector2 *set_min=NULL, Vector2 *set_max=NULL)
static RefMultiListClass< LookupTableClass > Tables
static bool Add_Table(LookupTableClass *table)
static bool Remove_Table(LookupTableClass *table)
static void Reset(void)
static void Shutdown(void)
static LookupTableClass * Get_Table(const char *name, bool try_to_load=true)
virtual const PersistFactoryClass & Get_Factory(void) const =0
virtual uint32 Chunk_ID(void) const =0
virtual PersistClass * Load(ChunkLoadClass &cload) const =0
virtual void Save(ChunkSaveClass &csave, PersistClass *obj) const =0
WWINLINE void Release_Ref(void) const
Definition refcount.h:146
ObjectType * Get_Obj(void)
Definition multilist.h:452
ObjectType * Peek_Obj(void)
Definition multilist.h:461
static PersistFactoryClass * Find_Persist_Factory(uint32 chunk_id)
Definition saveload.cpp:173
float Y
Definition vector2.h:79
WWINLINE void Set(float x, float y)
Definition vector2.h:92
float X
Definition vector2.h:74
FileFactoryClass * _TheFileFactory
Definition ffactory.cpp:51
@ LOOKUPTABLE_CHUNK_EXTENTS
@ LOOKUPTABLE_CHUNK_CURVE
#define NEW_REF(C, P)
Definition refcount.h:62
#define WWDEBUG_SAY(x)
Definition wwdebug.h:114