aboutsummaryrefslogtreecommitdiff
path: root/engines/teenagent/animation.cpp
blob: 7958df35c9865716453591c54917a26818c3256f (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "teenagent/teenagent.h"
#include "teenagent/animation.h"

#include "common/endian.h"
#include "common/textconsole.h"

namespace TeenAgent {

Animation::Animation() : id(0), x(0), y(0), loop(true), paused(false), ignore(false), data(0), data_size(0), frames_count(0), frames(0), index(0) {
}

Surface *Animation::firstFrame() {
	if (frames == NULL || frames_count == 0)
		return NULL;

	Surface *r = frames;
	uint16 pos = READ_LE_UINT16(data + 1);
	if (pos != 0) {
		r->x = pos % 320;
		r->y = pos / 320;
	}
	return r;
}

Surface *Animation::currentFrame(int dt) {
	if (paused)
		return firstFrame();

	if (frames == NULL || frames_count == 0)
		return NULL;

	Surface *r;

	if (data != NULL) {
		uint32 frame = 3 * index;
		debugC(2, kDebugAnimation, "%u/%u", index, data_size / 3);
		index += dt;

		if (!loop && index >= data_size / 3) {
			return NULL;
		}

		if (data[frame] - 1 >= frames_count) {
			warning("invalid frame %u(0x%x) (max %u) index %u, mod %u", frame, frame, frames_count, index - 1, data_size / 3);
			return NULL;
		}

		r = frames + data[frame] - 1;
		uint16 pos = READ_LE_UINT16(data + frame + 1);
		index %= (data_size / 3);

		if (pos != 0) {
			x = r->x = pos % 320;
			y = r->y = pos / 320;
		}
	} else {
		debugC(2, kDebugAnimation, "index %u", index);
		r = frames + index;
		index += dt;
		index %= frames_count;
	}

	return r;
}

void Animation::restart() {
	paused = false;
	ignore = false;
	index = 0;
}

void Animation::free() {
	id = 0;
	x = y = 0;
	loop = true;
	paused = false;
	ignore = false;

	delete[] data;
	data = NULL;
	data_size = 0;

	frames_count = 0;
	delete[] frames;
	frames = NULL;

	index = 0;
}

void Animation::load(Common::SeekableReadStream &s, Type type) {
	//FIXME: do not reload the same animation each time
	free();

	if (s.size() <= 1) {
		debugC(1, kDebugAnimation, "empty animation");
		return;
	}

	uint16 pos = 0;
	int off = 0;
	switch (type) {
	case kTypeLan:
		data_size = s.readUint16LE();
		if (s.eos()) {
			debugC(1, kDebugAnimation, "empty animation");
			return;
		}

		data_size -= 2;
		data = new byte[data_size];
		data_size = s.read(data, data_size);
		for (int i = 0; i < data_size; ++i)
			debugC(2, kDebugAnimation, "%02x ", data[i]);
		debugC(2, kDebugAnimation, ", %u frames", data_size / 3);
		frames_count = s.readByte();
		debugC(1, kDebugAnimation, "%u physical frames", frames_count);
		if (frames_count == 0)
			return;

		frames = new Surface[frames_count];

		s.skip(frames_count * 2 - 2); //sizes
		pos = s.readUint16LE();
		debugC(3, kDebugAnimation, "pos?: 0x%04x", pos);

		for (uint16 i = 0; i < frames_count; ++i) {
			frames[i].load(s, Surface::kTypeLan);
			frames[i].x = 0;
			frames[i].y = 0;
		}
		break;

	case kTypeInventory: {
		data_size = 3 * s.readByte();
		data = new byte[data_size];

		frames_count = 0;
		for (byte i = 0; i < data_size / 3; ++i) {
			int idx = i * 3;
			byte unk = s.readByte();
			debugC(3, kDebugAnimation, "unk?: 0x%02x", unk);
			data[idx] = s.readByte();
			if (data[idx] == 0)
				data[idx] = 1; //fixme: investigate
			if (data[idx] > frames_count)
				frames_count = data[idx];
			data[idx + 1] = 0;
			data[idx + 2] = 0;
			debugC(2, kDebugAnimation, "frame #%u", data[idx]);
		}

		frames = new Surface[frames_count];

		for (uint16 i = 0; i < frames_count; ++i) {
			frames[i].load(s, Surface::kTypeOns);
		}
	}
	break;

	case kTypeVaria:
		frames_count = s.readByte();
		debugC(1, kDebugAnimation, "loading varia resource, %u physical frames", frames_count);
		uint16 offset[255];
		for (byte i = 0; i < frames_count; ++i) {
			offset[i] = s.readUint16LE();
			debugC(0, kDebugAnimation, "%u: %04x", i, offset[i]);
		}
		frames = new Surface[frames_count];
		for (uint16 i = 0; i < frames_count; ++i) {
			debugC(0, kDebugAnimation, "%04x", offset[i]);
			s.seek(offset[i] + off);
			frames[i].load(s, Surface::kTypeOns);
		}

		break;
	}

	debugC(2, kDebugAnimation, "%u frames", data_size / 3);
}

Animation::~Animation() {
	free();
}

} // End of namespace TeenAgent