Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
prim_anim.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 *** 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 : WW3D *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/prim_anim.h $*
26 * *
27 * Author:: Patrick Smith *
28 * *
29 * $Modtime:: 1/29/01 5:43p $*
30 * *
31 * $Revision:: 2 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#if defined(_MSC_VER)
38#pragma once
39#endif
40
41#ifndef __PRIM_ANIM_H
42#define __PRIM_ANIM_H
43
44
45#include "simplevec.h"
46#include "chunkio.h"
47
48
49// Forward declarations
50class ChunkSaveClass;
51class ChunkLoadClass;
52
53
55//
56// PrimitiveAnimationChannelClass
57//
58// This template class provides animated 'channels' of data for the
59// RingRenderObjClass and SphereRenderObjClass objects.
60//
62template<class T>
64{
65public:
66
68 // Public constructors/destructors
73
75 // Public data types
78 {
79 public:
80 KeyClass (void)
81 : m_Time (0) {}
82
83 KeyClass (const T &value, float time)
84 : m_Value (value),
85 m_Time (time) {}
86
87 float Get_Time (void) const { return m_Time; }
88 const T & Get_Value (void) const { return m_Value; }
89 T & Get_Value (void) { return m_Value; }
90
91 float Set_Time (float time) { m_Time = time; }
92 void Set_Value (const T &value) { m_Value = value; }
93
94 private:
95 T m_Value;
96 float m_Time;
97 };
98
100 // Public operators
103 const KeyClass & operator[] (int index) { return Get_Key (index); }
104
106 // Public methods
108 virtual T Evaluate (float time) = 0;
109
110 int Get_Key_Count (void) const;
111 const KeyClass & Get_Key (int index) const;
112 void Set_Key (int index, const T &value, float time);
113 void Set_Key_Value (int index, const T &value);
114 void Add_Key (const T &value, float time);
115 void Insert_Key (int index, const T &value, float time);
116 void Delete_Key (int index);
117 void Reset (void);
118
119 virtual void Save (ChunkSaveClass &csave);
120 virtual void Load (ChunkLoadClass &cload);
121
122protected:
123
125 // Protected constants
127 enum
128 {
129 CHUNKID_VARIABLES = 0x03150809,
130 };
131
132 enum
133 {
135 };
136
138 // Protected methods
141
142protected:
143
145 // Protected member data
149};
150
151
153//
154// LERPAnimationChannelClass
155//
156// This template class provides a simple LERP implementation of the
157// Evaluate () method.
158//
160template<class T>
162{
163public:
164
166 // Public methods
168 virtual T Evaluate (float time);
169};
170
171
173// Set_Key
175template<class T>
177{
178 return m_Data.Count ();
179}
180
182// Set_Key_Value
184template<class T>
189
191// Set_Key
193template<class T>
194void PrimitiveAnimationChannelClass<T>::Set_Key (int index, const T &value, float time)
195{
196 m_Data[index].Set_Value (value);
197 m_Data[index].Set_Time (time);
198 return ;
199}
200
202// Set_Key_Value
204template<class T>
206{
207 m_Data[index].Set_Value (value);
208 return ;
209}
210
212// Add_Key
214template<class T>
216{
217 m_Data.Add (KeyClass (value, time));
218 return ;
219}
220
222// Insert_Key
224template<class T>
225void PrimitiveAnimationChannelClass<T>::Insert_Key (int index, const T &value, float time)
226{
227 m_Data.Insert (index, KeyClass (value, time));
228 return ;
229}
230
232// Delete_Key
234template<class T>
236{
237 m_Data.Delete (index);
238 return ;
239}
240
242// Reset
244template<class T>
246{
247 m_Data.Delete_All ();
248 m_LastIndex = 0;
249 return ;
250}
251
253// operator=
255template<class T> const PrimitiveAnimationChannelClass<T> &
257{
258 Reset ();
259
260 //
261 // Copy the data array
262 //
263 for (int index = 0; index < src.Get_Key_Count (); index ++) {
264 m_Data.Add (src.Get_Key (index));
265 }
266
268 return *this;
269}
270
272// Save
274template<class T> void
276{
278
279 //
280 // Save each key
281 //
282 for (int index = 0; index < m_Data.Count (); index ++) {
283 KeyClass &value = m_Data[index];
285 }
286
287 csave.End_Chunk ();
288
289 return ;
290}
291
293// Load
295template<class T> void
297{
298 Reset ();
299
300 while (cload.Open_Chunk ()) {
301 switch (cload.Cur_Chunk_ID ()) {
302
304 Load_Variables (cload);
305 break;
306 }
307
308 cload.Close_Chunk ();
309 }
310
311 return ;
312}
313
315// Load_Variables
317template<class T> void
319{
320 //
321 // Loop through all the microchunks that define the variables
322 //
323 while (cload.Open_Micro_Chunk ()) {
324 switch (cload.Cur_Micro_Chunk_ID ()) {
325
326 case VARID_KEY:
327 {
329 cload.Read (&value, sizeof (value));
330 m_Data.Add (value);
331 }
332 break;
333 }
334
335 cload.Close_Micro_Chunk ();
336 }
337
338 return ;
339}
340
342// Evaluate
344template<class T> T
346{
347 int key_count = m_Data.Count ();
348 T value = m_Data[key_count - 1].Get_Value ();
349
350 //
351 // Don't interpolate past the last keyframe
352 //
353 if (time < m_Data[key_count - 1].Get_Time ()) {
354
355 // Check to see if the last key index is valid
356 if (time < m_Data[m_LastIndex].Get_Time ()) {
357 m_LastIndex = 0;
358 }
359
360 KeyClass *key1 = &m_Data[m_LastIndex];
361 KeyClass *key2 = &m_Data[key_count - 1];
362
363 //
364 // Search, using last_key as our starting point
365 //
366 for (int keyidx = m_LastIndex; keyidx < (key_count - 1); keyidx ++) {
367
368 if (time < m_Data[keyidx+1].Get_Time ()) {
369 key1 = &m_Data[keyidx];
370 key2 = &m_Data[keyidx+1];
371 m_LastIndex = keyidx;
372 break;
373 }
374 }
375
376 // Calculate the linear percent between the two keys
377 float percent = (time - key1->Get_Time ()) / (key2->Get_Time () - key1->Get_Time ());
378
379 // Interpolate the value
380 value = (key1->Get_Value () + (key2->Get_Value () - key1->Get_Value ()) * percent);
381 }
382
383 return value;
384}
385
386#endif //__PRIM_ANIM_H
void const char * value
#define WRITE_MICRO_CHUNK(csave, id, var)
Definition chunkio.h:293
bool Close_Micro_Chunk()
Definition chunkio.cpp:585
bool Close_Chunk()
Definition chunkio.cpp:448
uint32 Cur_Chunk_ID()
Definition chunkio.cpp:484
uint32 Cur_Micro_Chunk_ID()
Definition chunkio.cpp:622
uint32 Read(void *buf, uint32 nbytes)
Definition chunkio.cpp:692
bool Open_Chunk()
Definition chunkio.cpp:412
bool Open_Micro_Chunk()
Definition chunkio.cpp:557
bool Begin_Chunk(uint32 id)
Definition chunkio.cpp:108
bool End_Chunk()
Definition chunkio.cpp:148
virtual T Evaluate(float time)
Definition prim_anim.h:345
KeyClass(const T &value, float time)
Definition prim_anim.h:83
void Set_Key_Value(int index, const T &value)
Definition prim_anim.h:205
virtual void Save(ChunkSaveClass &csave)
Definition prim_anim.h:275
virtual T Evaluate(float time)=0
virtual void Load(ChunkLoadClass &cload)
Definition prim_anim.h:296
const KeyClass & Get_Key(int index) const
Definition prim_anim.h:185
void Add_Key(const T &value, float time)
Definition prim_anim.h:215
virtual ~PrimitiveAnimationChannelClass(void)
Definition prim_anim.h:72
int Get_Key_Count(void) const
Definition prim_anim.h:176
void Insert_Key(int index, const T &value, float time)
Definition prim_anim.h:225
SimpleDynVecClass< KeyClass > m_Data
Definition prim_anim.h:147
const KeyClass & operator[](int index)
Definition prim_anim.h:103
void Load_Variables(ChunkLoadClass &cload)
Definition prim_anim.h:318
const PrimitiveAnimationChannelClass< T > & operator=(const PrimitiveAnimationChannelClass< T > &src)
Definition prim_anim.h:256
void Set_Key(int index, const T &value, float time)
Definition prim_anim.h:194
int percent
Definition patch.cpp:426
else return(RetVal)