-
-
Notifications
You must be signed in to change notification settings - Fork 999
/
character_object.cpp
664 lines (534 loc) · 14.9 KB
/
character_object.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/file.h"
#include "bagel/baglib/character_object.h"
#include "bagel/baglib/storage_dev_win.h"
#include "bagel/baglib/master_win.h"
#include "bagel/baglib/pan_window.h"
#include "bagel/bagel.h"
#include "bagel/boflib/file_functions.h"
#include "bagel/boflib/log.h"
namespace Bagel {
CBagCharacterObject *CBagCharacterObject::_pdaWand;
bool CBagCharacterObject::_pdaAnimating;
void CBagCharacterObject::initialize() {
_pdaWand = nullptr;
_pdaAnimating = false;
}
CBagCharacterObject::CBagCharacterObject() : CBagObject() {
_xObjType = CHAR_OBJ;
_bmpBuf = nullptr;
_binBuf = nullptr;
_charTransColor = -1;
// Default is now the nullptr cursor
CBagObject::setOverCursor(0);
setTimeless(true);
_startFrame = -1;
_endFrame = -1;
_playbackSpeed = 1;
_numOfLoops = -1;
_exitAtEnd = false;
_prevFrame = -1;
_firstFrame = true;
_pAnim = false;
_saveState = false;
}
CBagCharacterObject::~CBagCharacterObject() {
CBagCharacterObject::detach();
}
ErrorCode CBagCharacterObject::attach() {
char localBuff[256];
localBuff[0] = '\0';
CBofString filename(localBuff, 256);
CBofPalette *smackPal = CBofApp::getApp()->getPalette();
filename = getFileName();
// Open the smacker file
Video::SmackerDecoder *decoder = new Video::SmackerDecoder();
decoder->setSoundType(Audio::Mixer::kSFXSoundType);
if (!decoder->loadFile(filename.getBuffer())) {
logError(buildString("CHAR SmackOpen failed: %s ", filename.getBuffer()));
delete decoder;
return ERR_FOPEN;
}
_smacker = decoder;
// Create an offscreen bitmap to decompress smacker into
_firstFrame = true;
_bmpBuf = new CBofBitmap(_smacker->getWidth(), _smacker->getHeight(), smackPal);
if ((smackPal != nullptr) && (_bmpBuf != nullptr)) {
_bmpBuf->fillRect(nullptr, smackPal->getNearestIndex(RGB(255, 255, 255)));
}
// Create the text filename
filename.makeUpper();
filename.replaceStr(".SMK", ".BIN");
if (_binBuf != nullptr) {
bofFree(_binBuf);
_binBuf = nullptr;
}
// Does file exist
if (fileExists(filename.getBuffer())) {
CBofFile cInputFile(filename.getBuffer());
_binBufLen = cInputFile.getLength();
if ((_binBuf = (char *)bofAlloc(_binBufLen + 1)) != nullptr) {
cInputFile.read(_binBuf, _binBufLen);
} else {
reportError(ERR_MEMORY);
}
}
// Set the start and stop frames if still default.
if (_playbackSpeed > 0) {
// Forward playback
if (_startFrame == -1)
_startFrame = 0;
if (_endFrame == -1)
_endFrame = _smacker->getFrameCount() - 1;
} else {
// Reverse playback
if (_endFrame == -1)
_endFrame = 1;
if (_startFrame == -1)
_startFrame = _smacker->getFrameCount();
//_smacker->setReverse(true); // TODO: Not supported by SMK
}
if (_saveState) {
// Get the current state for this object
int nState = getState();
// If the state is not the default(0) then move to the correct frame
if (nState != 0)
setFrame(nState + 1);
}
if (_numOfLoops != 0) {
_smacker->start();
} else {
_smacker->stop();
}
setVisible(true);
updatePosition();
refreshCurrentFrame();
// Get chroma from main app now
_charTransColor = CBagel::getBagApp()->getChromaColor();
return CBagObject::attach();
}
ErrorCode CBagCharacterObject::detach() {
if (_saveState) {
// Save off the state/frame information as we detach
// so that we can recreate the scene when we attach again
if (_smacker != nullptr) {
setState(_smacker->getCurFrame());
}
} else {
// Decrement current loop from happening again
if (_numOfLoops > 0)
_numOfLoops--;
}
if (_smacker != nullptr) {
_smacker->close();
delete _smacker;
_smacker = nullptr;
}
if (_bmpBuf != nullptr) {
delete _bmpBuf;
_bmpBuf = nullptr;
}
if (_binBuf != nullptr) {
bofFree(_binBuf);
_binBuf = nullptr;
}
if (this == _pdaWand) {
_pdaWand = nullptr;
}
return CBagObject::detach();
}
bool CBagCharacterObject::refreshCurrentFrame() {
bool newFrameFl = true;
if (_bmpBuf != nullptr) {
if (_smacker != nullptr) {
// Decode the next frame
const Graphics::Surface *surf = _smacker->decodeNextFrame();
if (surf) {
Graphics::ManagedSurface &destSurf = *_bmpBuf;
// Copy the decoded frame into the offscreen bitmap
destSurf.setPalette(_smacker->getPalette(), 0, 256);
destSurf.blitFrom(*surf);
} else {
newFrameFl = false;
}
} else {
// Return false so we don't update our position
newFrameFl = false;
}
}
return newFrameFl;
}
CBofRect CBagCharacterObject::getRect() {
CBofPoint pos = getPosition();
CBofSize size;
if (_bmpBuf)
size = _bmpBuf->getSize();
return CBofRect(pos, size);
}
void CBagCharacterObject::updatePosition() {
// We have an input file
if (_binBuf != nullptr && _smacker != nullptr) {
// Seek to correct place in the file
int frame = _smacker->getCurFrame();
if (frame < 0)
frame = 0;
int32 lSeekPos = frame * 2 * sizeof(int32);
// Read from our memory buffer rather than going to
// disk for the position of the smack dudes.
// Check that we are going to fit
if (lSeekPos + (int32)(2 * sizeof(int32)) <= _binBufLen) {
int xPos = READ_LE_INT32(&_binBuf[lSeekPos]);
lSeekPos += sizeof(int32);
int yPos = READ_LE_INT32(&_binBuf[lSeekPos]);
lSeekPos += sizeof(int32);
// A valid number was read
if ((xPos > -1) && (yPos > -1)) {
CBofPoint newPos(xPos, yPos);
setPosition(newPos);
}
}
}
}
bool CBagCharacterObject::doAdvance() {
// Assume we're not advancing
bool doAdvanceFl = false;
bool pdaWandFl = (this == _pdaWand);
if (pdaWandFl) {
_pdaAnimating = false;
}
// If we are done looping just return with false
if (!_numOfLoops)
return false;
// If we got a background bitmap
if (_bmpBuf != nullptr) {
// If This Panimation is modal, or Panimations are ON, then get next frame.
if (isModal() || !_pAnim || CBagMasterWin::getPanimations()) {
if (_smacker->needsUpdate()) {
doAdvanceFl = true;
// Paint the current frame to the BMP
if (refreshCurrentFrame()) {
// Get the current frame in the correct place
updatePosition();
}
if (_playbackSpeed > 0) {
// We've looped
if (_smacker->getCurFrame() == _endFrame) {
if (_numOfLoops > 0)
_numOfLoops--; // decrement num of loops
// Get next frame, will loop to beginning
_smacker->rewind();
_smacker->start();
}
} else if ((_smacker->getCurFrame() == _endFrame) || (_smacker->getCurFrame() == 1)) {
if (_numOfLoops > 0)
_numOfLoops--; // decrement num of loops
// Get next frame, will loop to beginning
setFrame(_startFrame);
} else {
setFrame(_smacker->getCurFrame() - 2); // HACK: Reverse playback
}
}
} else if (_firstFrame) {
// Only play the first frame
_firstFrame = false;
// Get the current frame in the correct place
updatePosition();
// Paint the current frame to the BMP
refreshCurrentFrame();
}
}
// We got a Bitmap
if (pdaWandFl) {
_pdaAnimating = doAdvanceFl;
}
return doAdvanceFl;
}
bool CBagCharacterObject::isInside(const CBofPoint &point) {
if (getRect().ptInRect(point) && _charTransColor >= 0) {
if (_bmpBuf) {
int x = point.x - getRect().left;
int y = point.y - getRect().top;
int color = _bmpBuf->readPixel(x, y);
return (color != _charTransColor);
}
return true;
}
return false;
}
bool CBagCharacterObject::runObject() {
CBagObject::runObject();
return false;
}
ErrorCode CBagCharacterObject::update(CBofBitmap *bmp, CBofPoint pt, CBofRect * /*srcRect, unused*/, int /*maskColor, unused*/) {
// Get the original position for character
CBofPoint originalPos = getPosition();
bool doAdvanceFl = doAdvance();
// If we have more frames advance this, else exit and detach if needed
if (!doAdvanceFl && _exitAtEnd) {
// Run the ending objects
detach();
}
if (_bmpBuf) {
// Get the new position for the character
CBofPoint newPos = getPosition();
// Get access to the current sDev
// Paint in the new pos
return _bmpBuf->paint(bmp, (pt.x + (newPos.x - originalPos.x)), (pt.y + (newPos.y - originalPos.y)), nullptr, _charTransColor);
}
return ERR_NONE;
}
ParseCodes CBagCharacterObject::setInfo(CBagIfstream &istr) {
bool objectUpdatedFl = false;
while (!istr.eof()) {
char ch = (char)istr.peek();
switch (ch) {
// SAVESTATE - Maintain the state of the character
case 'K': {
char localStr[256];
localStr[0] = 0;
CBofString str(localStr, 256);
getAlphaNumFromStream(istr, str);
if (!str.find("KEEPSTATE")) {
istr.eatWhite();
_saveState = true;
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// LOOP n - n number of times to loop (-1 infinate)
case 'L': {
char localStr[256];
localStr[0] = 0;
CBofString str(localStr, 256);
getAlphaNumFromStream(istr, str);
if (!str.find("LOOP")) {
istr.eatWhite();
getIntFromStream(istr, _numOfLoops);
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// SPEED n - n pace of playback (negative is backward), (0 to hold at current frame)
case 'S': {
char localStr[256];
localStr[0] = 0;
CBofString str(localStr, 256);
getAlphaNumFromStream(istr, str);
if (!str.find("SPEED")) {
istr.eatWhite();
getIntFromStream(istr, _playbackSpeed);
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// EXITATEND - detach at end of looping (call run after objects)
case 'E': {
char localStr[256];
localStr[0] = 0;
CBofString str(localStr, 256);
getAlphaNumFromStream(istr, str);
if (!str.find("EXITATEND")) {
istr.eatWhite();
_exitAtEnd = true;
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// PANIM - Specifies if this object should be affected by the user
// option Panimations On/Off
case 'P': {
CBofString str;
getAlphaNumFromStream(istr, str);
if (!str.find("PANIM")) {
istr.eatWhite();
_pAnim = true;
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// FRAME [start, end]- start and end frames of the move
case 'F': {
char localStr[256];
localStr[0] = 0;
CBofString str(localStr, 256);
getAlphaNumFromStream(istr, str);
if (!str.find("FRAME")) {
CBofRect r;
istr.eatWhite();
getRectFromStream(istr, r);
_startFrame = r.left;
_endFrame = r.top;
if (r.width() && r.height()) {
// error of some type
}
objectUpdatedFl = true;
} else {
putbackStringOnStream(istr, str);
}
break;
}
// No match return from function
default: {
ParseCodes rc = CBagObject::setInfo(istr);
if (rc == PARSING_DONE) {
return PARSING_DONE;
}
if (rc == UPDATED_OBJECT) {
objectUpdatedFl = true;
} else { // rc==UNKNOWN_TOKEN
if (objectUpdatedFl)
return UPDATED_OBJECT;
return UNKNOWN_TOKEN;
}
break;
}
}
}
return PARSING_DONE;
}
void CBagCharacterObject::arrangeFrames() {
int start = getStartFrame();
int end = getEndFrame();
if (_playbackSpeed < 0) {
_startFrame = MAX(start, end);
_endFrame = MIN(start, end);
} else {
_startFrame = MIN(start, end);
_endFrame = MAX(start, end);
}
}
void CBagCharacterObject::setNumOfLoops(int n) {
_numOfLoops = n;
if (_smacker != nullptr) {
if (_numOfLoops != 0) {
_smacker->start();
} else {
_smacker->stop();
}
}
// If this character is modal run until done looping
if (isModal() && isAttached()) {
CBagStorageDevWnd *win = CBagel::getBagApp()->getMasterWnd()->getCurrentStorageDev();
if (win != nullptr) {
win->runModal(this);
}
}
}
void CBagCharacterObject::setPlaybackSpeed(int n) {
if (_playbackSpeed != n) {
if (n < 0) {
_startFrame++;
_endFrame++;
} else {
_startFrame--;
_endFrame--;
}
_playbackSpeed = n;
arrangeFrames();
setCurrentFrame(getStartFrame());
}
}
void CBagCharacterObject::setStartFrame(int n) {
arrangeFrames();
_startFrame = n;
arrangeFrames();
}
void CBagCharacterObject::setEndFrame(int n) {
arrangeFrames();
_endFrame = n;
arrangeFrames();
}
void CBagCharacterObject::setCurrentFrame(int n) {
// Make sure that it is within specified values?
//
// Due to some distinctly bogus code that manipulates the
// start and end frame, the current frame passed in can be negative, which
// will cause slacker-smacker to go toes up, handle that here.
setFrame(n);
// Added updatePosition() because if any movies go backwards, and use
// a .BIN file, then it would not have worked.
updatePosition();
refreshCurrentFrame();
}
void CBagCharacterObject::setFrame(int n) {
// Make sure that it is within specified values?
if (_smacker != nullptr) {
if (n == (int)_smacker->getFrameCount()) {
n -= 3; // HACK: Reverse rewind
}
n = CLIP<int>(n, 0, _smacker->getFrameCount() - 1);
_smacker->forceSeekToFrame(n);
}
}
void CBagCharacterObject::setProperty(const CBofString &prop, int val) {
if (!prop.find("LOOP"))
setNumOfLoops(val);
else if (!prop.find("SPEED"))
setPlaybackSpeed(val);
else if (!prop.find("START_FRAME"))
setStartFrame(val);
else if (!prop.find("END_FRAME"))
setEndFrame(val);
else if (!prop.find("CURR_FRAME")) // This one will not work currently
setCurrentFrame(val);
else
CBagObject::setProperty(prop, val);
}
int CBagCharacterObject::getProperty(const CBofString &prop) {
if (!prop.find("LOOP"))
return getNumberOfLoops();
if (!prop.find("SPEED"))
return getPlaybackSpeed();
if (!prop.find("START_FRAME"))
return getStartFrame();
if (!prop.find("END_FRAME"))
return getEndFrame();
if (!prop.find("CURR_FRAME")) // This one will not work currently
return getCurrentFrame();
return CBagObject::getProperty(prop);
}
void CBagCharacterObject::setPdaWand(CBagCharacterObject *pdaWand) {
_pdaWand = pdaWand;
}
bool CBagCharacterObject::pdaWandAnimating() {
if (_pdaWand == nullptr || !_pdaWand->isAttached()) {
CBagStorageDev *pda = g_SDevManager->getStorageDevice("BPDA_WLD");
if (pda != nullptr) {
CBagCharacterObject *wand = (CBagCharacterObject *)pda->getObject("WANDANIM");
if (wand != nullptr) {
CBagCharacterObject::setPdaWand(wand);
}
}
}
return _pdaAnimating;
}
} // namespace Bagel