-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimThing.cpp
597 lines (512 loc) · 16.1 KB
/
AnimThing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// AnimThing.cpp
// Project: Nostril (aka Postal)
//
// History:
// 02/18/97 JMI Started.
//
// 02/19/97 JMI Added ability to send any message to any CThing when done
// with animation.
//
// 02/20/97 JMI Tweaked positioning; I think it's more correct.
//
// 02/20/97 MJR Added mini-runtime-editor.
//
// 02/21/97 JMI After tweaking the position, I hosed the sprite priority.
// Fixed. Now it's beauteous if you place a dude just a
// bit in front of a looping explosion.
//
// 02/24/97 JMI No longer sets the m_type member of the m_sprite b/c it
// is set by m_sprite's constructor.
//
// 02/24/97 JMI Changed m_pthingSendMsg to m_u16IdSendMsg.
//
// 02/25/97 JMI Forgot to call FreeResources() in GetResources().
//
// 03/13/97 JMI Load now takes a version number.
//
// 03/27/97 JMI Now EditModify() returns failure if there's no resource
// name. GetResources() should've taken care of this via
// rspGetResource() failing, but it seems to crash in Release
// mode, so now EditModify() checks.
//
// 04/10/97 BRH Updated this to work with the new multi layer attribute
// maps.
//
// 05/29/97 JMI Removed ASSERT on m_pRealm->m_pAttribMap which no longer
// exists.
//
// 06/29/97 JMI Converted EditRect(), EditRender(), and/or Render() to
// use Map3Dto2D().
// Also, now determines priority via 2D mapped Y.
//
// 07/27/97 JMI Now determines priority totally based on Z.
// Also, now uses CRealm::Make2dResPath() on res names.
//
//////////////////////////////////////////////////////////////////////////////
//
// This CThing-derived class will play an animation to its finish and then
// destroy itself. It's a mini Postal movie player.
//
//////////////////////////////////////////////////////////////////////////////
#define ANIMTHING_CPP
#include "RSPiX.h"
#include <math.h>
#include "AnimThing.h"
#include "dude.h"
#include "game.h"
////////////////////////////////////////////////////////////////////////////////
// Macros/types/etc.
////////////////////////////////////////////////////////////////////////////////
#define GUI_FILE_NAME "res/editor/EditAnim.gui"
#define GUI_ID_RESOURCE 3
#define GUI_ID_OPTIONS 4
#define GUI_ID_DONTLOOP 5
#define GUI_ID_DOLOOP 6
////////////////////////////////////////////////////////////////////////////////
// Variables/data
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Load object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::Load( // Returns 0 if successfull, non-zero otherwise
RFile* pFile, // In: File to load from
bool bEditMode, // In: True for edit mode, false otherwise
short sFileCount, // In: File count (unique per file, never 0)
ULONG ulFileVersion) // In: Version of file format to load.
{
short sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);
if (sResult == 0)
{
switch (ulFileVersion)
{
default:
case 1:
pFile->Read(&m_dX);
pFile->Read(&m_dY);
pFile->Read(&m_dZ);
pFile->Read(m_szResName);
pFile->Read(&m_sLoop);
break;
}
// Make sure there were no file errors or format errors . . .
if (!pFile->Error() && sResult == 0)
{
// Get resources
sResult = GetResources();
}
else
{
sResult = -1;
TRACE("CAnimThing::Load(): Error reading from file!\n");
}
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Save object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::Save( // Returns 0 if successfull, non-zero otherwise
RFile* pFile, // In: File to save to
short sFileCount) // In: File count (unique per file, never 0)
{
short sResult = CThing::Save(pFile, sFileCount);
if (sResult == 0)
{
pFile->Write(&m_dX);
pFile->Write(&m_dY);
pFile->Write(&m_dZ);
pFile->Write(m_szResName);
pFile->Write(&m_sLoop);
// Make sure there were no file errors
sResult = pFile->Error();
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Startup object
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::Startup(void) // Returns 0 if successfull, non-zero otherwise
{
m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
m_lAnimTime = 0;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Shutdown object
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::Shutdown(void) // Returns 0 if successfull, non-zero otherwise
{
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Suspend object
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::Suspend(void)
{
m_sSuspend++;
}
////////////////////////////////////////////////////////////////////////////////
// Resume object
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::Resume(void)
{
m_sSuspend--;
// If we're actually going to start updating again, we need to reset
// the time so as to ignore any time that passed while we were suspended.
// This method is far from precise, but I'm hoping it's good enough.
if (m_sSuspend == 0)
{
m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
}
}
////////////////////////////////////////////////////////////////////////////////
// Update object
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::Update(void)
{
if (!m_sSuspend)
{
if (m_lAnimTime >= m_paachannel->TotalTime() && m_sLoop == FALSE)
{
// If there's a thing to send a message to . . .
if (m_u16IdSendMsg != CIdBank::IdNil)
{
// Send the message.
SendThingMessage(&m_msg, m_u16IdSendMsg);
}
// Done.
delete this;
return;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Render object
////////////////////////////////////////////////////////////////////////////////
short sEdit;
double dScale = 1.0;
double dScaleInc = 0.025;
void CAnimThing::Render(void)
{
// Get current time diff.
long lCurTime = m_pRealm->m_time.GetGameTime();
m_lAnimTime += lCurTime - m_lAnimPrevTime;
m_lAnimPrevTime = lCurTime;
CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
if (paa != NULL)
{
// No special flags
m_sprite.m_sInFlags = 0;
// Map from 3d to 2d coords
// m_sprite.m_sX2 = m_dX + paa->m_sX;
// m_sprite.m_sY2 = m_dZ - (m_dY - paa->m_sY);
Map3Dto2D(
m_dX,
m_dY,
m_dZ,
&(m_sprite.m_sX2),
&(m_sprite.m_sY2) );
// Offset by hotspot.
m_sprite.m_sX2 += paa->m_sX;
m_sprite.m_sY2 += paa->m_sY;
// Priority is based on our position in 3D realm coords.
m_sprite.m_sPriority = m_dZ;
// Layer should be based on info we get from attribute map.
m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((short) m_dX, (short) m_dZ));
// Copy the color info and the alpha channel to the Alpha Sprite
m_sprite.m_pImage = &(paa->m_imColor);
m_sprite.m_pimAlpha = &(paa->m_pimAlphaArray[0]); // This is an array? What changes my index?!
///////////////////////////////////////////////////////////
// Tiny little built-in editor that gets acctivated via
// the debugger -- just set sEdit to non-zero. It assumes
// the alpha anim has 2 layers, with the second layer at
// 100%. It copies that layer to the first layer, scaling
// it by the specified value. Press LEFT and RIGHT arrows
// (quickly) to modify the scaling value and watch as the
// alpha effect changes.
///////////////////////////////////////////////////////////
if (sEdit && (paa->m_sNumAlphas > 2))
{
unsigned char auc[128];
rspScanKeys(auc);
if (auc[RSP_SK_LEFT])
{
dScale -= dScaleInc;
if (dScale < 0.0)
dScale = 0;
}
if (auc[RSP_SK_RIGHT])
{
dScale += dScaleInc;
}
for (short y = 0; y < paa->m_pimAlphaArray[1].m_sHeight; y++)
{
U8* pSrc = paa->m_pimAlphaArray[1].m_pData + (y * paa->m_pimAlphaArray[1].m_lPitch);
U8* pDst = paa->m_pimAlphaArray[0].m_pData + (y * paa->m_pimAlphaArray[0].m_lPitch);
for (short x = 0; x < paa->m_pimAlphaArray[1].m_sWidth; x++)
{
double dVal = (double)(*pSrc);
dVal *= dScale;
if (dVal < 256.0)
*pDst = (U8)dVal;
else
*pDst = (U8)255;
pSrc++;
pDst++;
}
}
}
///////////////////////////////////////////////////////////
// Update sprite in scene
m_pRealm->m_scene.UpdateSprite(&m_sprite);
}
}
short CAnimThing::Setup( // Returns 0 if successfull, non-zero otherwise
short sX, // In: New x coord
short sY, // In: New y coord
short sZ) // In: New z coord
{
short sResult = 0;
// Use specified position
m_dX = (double)sX;
m_dY = (double)sY;
m_dZ = (double)sZ;
m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
// Load resources
sResult = GetResources();
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to init new object at specified position
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::EditNew( // Returns 0 if successfull, non-zero otherwise
short sX, // In: New x coord
short sY, // In: New y coord
short sZ) // In: New z coord
{
short sResult = 0;
// Use specified position
m_dX = (double)sX;
m_dY = (double)sY;
m_dZ = (double)sZ;
sResult = EditModify();
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to modify object
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::EditModify(void)
{
short sResult = 0;
RGuiItem* pgui = RGuiItem::LoadInstantiate(FullPathVD(GUI_FILE_NAME));
if (pgui != NULL)
{
RListBox* plb = (RListBox*)pgui->GetItemFromId(GUI_ID_OPTIONS);
if (plb != NULL)
{
ASSERT(plb->m_type == RGuiItem::ListBox);
// Select current setting.
plb->SetSel(pgui->GetItemFromId((m_sLoop == FALSE) ? GUI_ID_DONTLOOP : GUI_ID_DOLOOP) );
REdit* pedit = (REdit*)pgui->GetItemFromId(GUI_ID_RESOURCE);
if (pedit != NULL)
{
ASSERT(pedit->m_type == RGuiItem::Edit);
// Set text.
pedit->SetText("%s", m_szResName);
// Realize text.
pedit->Compose();
if (DoGui(pgui) == 1)
{
RGuiItem* pguiSel = plb->GetSel();
if (pguiSel)
{
switch (pguiSel->m_lId)
{
case GUI_ID_DONTLOOP:
m_sLoop = FALSE;
break;
case GUI_ID_DOLOOP:
m_sLoop = TRUE;
break;
}
}
// Get new resource name.
pedit->GetText(m_szResName, sizeof(m_szResName) );
// If no resource name . . .
if (m_szResName[0] == '\0')
{
sResult = 1;
}
}
else
{
sResult = 1;
}
}
else
{
sResult = -3;
}
}
else
{
sResult = -2;
}
// Done with GUI.
delete pgui;
}
else
{
sResult = -1;
}
// If successful so far . . .
if (sResult == 0)
{
// Load resources
sResult = GetResources();
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to move object to specified position
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::EditMove( // Returns 0 if successfull, non-zero otherwise
short sX, // In: New x coord
short sY, // In: New y coord
short sZ) // In: New z coord
{
m_dX = (double)sX;
m_dY = (double)sY;
m_dZ = (double)sZ;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to get the clickable pos/area of an object in 2D.
// (virtual (Overridden here)).
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::EditRect( // Returns nothiing.
RRect* prc) // Out: Clickable pos/area of object.
{
Map3Dto2D(
m_dX,
m_dY,
m_dZ,
&(prc->sX),
&(prc->sY) );
prc->sW = 10; // Safety.
prc->sH = 10; // Safety.
if (m_paachannel != NULL)
{
CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
if (paa != NULL)
{
// Offset by hotspot.
prc->sX += paa->m_sX;
prc->sY += paa->m_sY;
prc->sW = paa->m_imColor.m_sWidth;
prc->sH = paa->m_imColor.m_sHeight;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to get the hotspot of an object in 2D.
// (virtual (Overridden here)).
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::EditHotSpot( // Returns nothiing.
short* psX, // Out: X coord of 2D hotspot relative to
// EditRect() pos.
short* psY) // Out: Y coord of 2D hotspot relative to
// EditRect() pos.
{
*psX = 0; // Safety.
*psY = 0; // Safety.
if (m_paachannel != NULL)
{
CAlphaAnim* paa = (CAlphaAnim*)m_paachannel->GetAtTime(m_lAnimTime);
if (paa != NULL)
{
*psX = -paa->m_sX;
*psY = -paa->m_sY;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to update object
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::EditUpdate(void)
{
}
////////////////////////////////////////////////////////////////////////////////
// Called by editor to render object
////////////////////////////////////////////////////////////////////////////////
void CAnimThing::EditRender(void)
{
// In some cases, object's might need to do a special-case render in edit
// mode because Startup() isn't called. In this case it doesn't matter, so
// we can call the normal Render().
Render();
}
////////////////////////////////////////////////////////////////////////////////
// Get all required resources
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::GetResources(void) // Returns 0 if successfull, non-zero otherwise
{
short sResult = 0;
// Safe to call even if no resource.
FreeResources();
sResult = rspGetResource(
&g_resmgrGame,
m_pRealm->Make2dResPath(m_szResName),
&m_paachannel);
if (sResult == 0)
{
m_lAnimTime = 0;
m_lAnimPrevTime = m_pRealm->m_time.GetGameTime();
if (m_sLoop != FALSE)
{
m_paachannel->SetLooping(RChannel_LoopAtStart | RChannel_LoopAtEnd);
}
else
{
m_paachannel->SetLooping(0);
}
}
else
{
TRACE("GetResources(): Failed to load resource \"%s\".\n",
m_pRealm->Make2dResPath(m_szResName) );
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Free all resources
////////////////////////////////////////////////////////////////////////////////
short CAnimThing::FreeResources(void) // Returns 0 if successfull, non-zero otherwise
{
short sResult = 0;
if (m_paachannel != NULL)
{
rspReleaseResource(&g_resmgrGame, &m_paachannel);
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////