forked from fuzzie/unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite_player.cpp
289 lines (241 loc) · 6.67 KB
/
sprite_player.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
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "unity.h"
#include "sprite_player.h"
#include "object.h"
#include "sound.h"
#include "common/system.h"
#include "common/stream.h"
#include "common/textconsole.h"
namespace Unity {
SpritePlayer::SpritePlayer(const char *filename, Object *par, UnityEngine *vm) : _parent(par), _vm(vm) {
_spriteStream = vm->data.openFile(filename);
_sprite = new Sprite(_spriteStream);
_currentEntry = ~0;
_currentSprite = NULL;
_currentSpeechSprite = NULL;
_currentPalette = NULL;
_waitTarget = 0;
_normal.xpos = _normal.ypos = 0;
_normal.xadjust = _normal.yadjust = 0;
_speech.xpos = _speech.ypos = 0;
_speech.xadjust = _speech.yadjust = 0;
_wasSpeech = false;
_wasMarked = false;
}
SpritePlayer::~SpritePlayer() {
delete _sprite;
delete _spriteStream;
}
void SpritePlayer::resetState() {
_normal.xadjust = _normal.yadjust = 0;
_speech.xadjust = _speech.yadjust = 0;
_waitTarget = 0;
}
void SpritePlayer::startAnim(unsigned int a) {
if (a >= _sprite->getNumAnims()) {
error("animation %d is too high (only %d animation(s))", a, _sprite->getNumAnims());
}
_currentEntry = _sprite->getIndexFor(a);
assert(_currentEntry != (unsigned int)~0);
resetState();
}
unsigned int SpritePlayer::getCurrentWidth() {
assert(_currentSprite);
return _currentSprite->width;
}
unsigned int SpritePlayer::getCurrentHeight() {
assert(_currentSprite);
return _currentSprite->height;
}
byte *SpritePlayer::getCurrentData() {
assert(_currentSprite);
return _currentSprite->data;
}
bool SpritePlayer::speaking() {
return _currentSpeechSprite != NULL;
}
unsigned int SpritePlayer::getSpeechWidth() {
assert(_currentSpeechSprite);
return _currentSpeechSprite->width;
}
unsigned int SpritePlayer::getSpeechHeight() {
assert(_currentSpeechSprite);
return _currentSpeechSprite->height;
}
byte *SpritePlayer::getSpeechData() {
assert(_currentSpeechSprite);
return _currentSpeechSprite->data;
}
byte *SpritePlayer::getPalette() {
if (!_currentPalette)
return NULL;
byte *palette = _currentPalette->palette;
_currentPalette = NULL;
return palette;
}
bool SpritePlayer::playing() {
SpriteEntry *e = _sprite->getEntry(_currentEntry);
assert(e);
return !(e->type == se_Pause || e->type == se_Exit);
}
void SpritePlayer::update() {
unsigned int oldEntry = ~0;
while (true) {
SpriteEntry *e = _sprite->getEntry(_currentEntry);
assert(e);
switch (e->type) {
case se_None:
_currentEntry++;
break;
case se_Sprite:
_currentSprite = (SpriteEntrySprite *)e;
// XXX: don't understand how this works either
_wasSpeech = false;
_currentSpeechSprite = NULL;
_currentEntry++;
e = _sprite->getEntry(_currentEntry);
if (e->type == se_Sprite || e->type == se_SpeechSprite)
return;
break;
case se_SpeechSprite:
if (!_wasMarked) {
error("no marked data during sprite playback");
}
_currentSpeechSprite = (SpriteEntrySprite *)e;
// XXX: don't understand how this works :(
if (!_wasSpeech) {
_speech = _marked;
}
_wasSpeech = true;
_currentEntry++;
e = _sprite->getEntry(_currentEntry);
if (e->type == se_Sprite || e->type == se_SpeechSprite)
return;
break;
case se_Palette:
_currentPalette = (SpriteEntryPalette *)e;
_currentEntry++;
break;
case se_Position:
{
SpriteEntryPosition *p = (SpriteEntryPosition *)e;
if (_wasSpeech) {
_speech.xpos = p->newx;
_speech.ypos = p->newy;
} else {
_normal.xpos = p->newx;
_normal.ypos = p->newy;
}
}
_currentEntry++;
break;
case se_RelPos:
{
SpriteEntryRelPos *p = (SpriteEntryRelPos *)e;
if (_wasSpeech) {
_speech.xadjust = p->adjustx;
_speech.yadjust = p->adjusty;
} else {
_normal.xadjust = p->adjustx;
_normal.yadjust = p->adjusty;
}
}
_currentEntry++;
break;
case se_MouthPos:
{
//SpriteEntryMouthPos *p = (SpriteEntryMouthPos *)e;
// TODO: okay, this isn't MouthPos :-) what is it?
// set on characters, drones, cable, welds..
}
_currentEntry++;
break;
case se_Pause:
case se_Exit:
return;
case se_Mark:
// XXX: do something here
_wasMarked = true;
_marked = _normal;
_currentEntry++;
break;
case se_Mask:
warning("sprite mask mode not implemented");
_currentEntry++;
break;
case se_Static:
warning("sprite static mode not implemented");
_currentEntry++;
break;
case se_RandomWait:
if (!_waitTarget) {
unsigned int wait = _vm->_rnd->getRandomNumberRng(0,
((SpriteEntryRandomWait *)e)->rand_amt);
// TODO: is /6 correct? see below
_waitTarget = g_system->getMillis() + ((SpriteEntryRandomWait *)e)->base/6 + wait/6;
return;
}
// fall through
case se_Wait:
if (!_waitTarget) {
// TODO: is /6 correct? just a guess, so almost certainly not
// example values are 388, 777, 4777, 288, 666, 3188, 2188, 700, 200000, 1088, 272..
_waitTarget = g_system->getMillis() + ((SpriteEntryWait *)e)->wait/6;
return;
}
if (_waitTarget < g_system->getMillis()) {
_waitTarget = 0;
_currentEntry++;
continue;
}
return;
case se_Jump:
_currentEntry = _sprite->getIndexFor(((SpriteEntryJump *)e)->target);
assert(_currentEntry != (unsigned int)~0);
if (oldEntry == _currentEntry) {
// XXX: work out why this happens: missing handling?
error("sprite file in infinite loop");
}
oldEntry = _currentEntry;
resetState();
break;
case se_Audio:
_vm->_snd->playAudioBuffer(((SpriteEntryAudio *)e)->length,
((SpriteEntryAudio *)e)->data);
_currentEntry++;
break;
case se_WaitForSound:
warning("sprite waiting for sound not implemented");
_currentEntry++;
break;
case se_Silent:
warning("sprite silencing not implemented");
_currentEntry++;
break;
case se_StateSet:
warning("sprite state setting not implemented");
_currentEntry++;
break;
case se_SetFlag:
warning("flag setting not implemented");
_currentEntry++;
break;
default:
error("internal error: bad sprite player entry");
}
}
}
} // Unity