File: tileanim.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (366 lines) | stat: -rw-r--r-- 11,328 bytes parent folder | download | duplicates (2)
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
/* 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 "ultima/ultima4/core/config.h"
#include "ultima/ultima4/map/direction.h"
#include "ultima/ultima4/gfx/image.h"
#include "ultima/ultima4/gfx/screen.h"
#include "ultima/ultima4/map/tileanim.h"
#include "ultima/ultima4/ultima4.h"
#include "ultima/ultima4/core/utils.h"
#include "ultima/ultima4/map/tile.h"

namespace Ultima {
namespace Ultima4 {

TileAnimTransform *TileAnimTransform::create(const ConfigElement &conf) {
	TileAnimTransform *transform;
	static const char *const transformTypeEnumStrings[] = { "invert", "pixel", "scroll", "frame", "pixel_color", nullptr };

	int type = conf.getEnum("type", transformTypeEnumStrings);

	switch (type) {
	case 0:
		transform = new TileAnimInvertTransform(
			conf.getInt("x"), conf.getInt("y"),
			conf.getInt("width"), conf.getInt("height"));
		break;

	case 1: {
		transform = new TileAnimPixelTransform(
			conf.getInt("x"), conf.getInt("y"));

		Std::vector<ConfigElement> children = conf.getChildren();
		for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
			if (i->getName() == "color") {
				RGBA *rgba = loadColorFromConf(*i);
				((TileAnimPixelTransform *)transform)->_colors.push_back(rgba);
			}
		}
		break;
	}

	case 2:
		transform = new TileAnimScrollTransform(conf.getInt("increment"));
		break;

	case 3:
		transform = new TileAnimFrameTransform();
		break;

	case 4: {
		transform = new TileAnimPixelColorTransform(
			conf.getInt("x"), conf.getInt("y"),
			conf.getInt("width"), conf.getInt("height"));

		Std::vector<ConfigElement> children = conf.getChildren();
		for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
			if (i->getName() == "color") {
				RGBA *rgba = loadColorFromConf(*i);
				if (i == children.begin())
					((TileAnimPixelColorTransform *)transform)->_start = rgba;
				else
					((TileAnimPixelColorTransform *)transform)->_end = rgba;
			}
		}
		break;
	}

	default:
		error("Unknown type");
	}

	// See if the transform is performed randomly
	if (conf.exists("random"))
		transform->_random = conf.getInt("random");
	else
		transform->_random = 0;

	return transform;
}

RGBA *TileAnimTransform::loadColorFromConf(const ConfigElement &conf) {
	RGBA *rgba;

	rgba = new RGBA();
	rgba->r = conf.getInt("red");
	rgba->g = conf.getInt("green");
	rgba->b = conf.getInt("blue");
	rgba->a = IM_OPAQUE;

	return rgba;
}

TileAnimInvertTransform::TileAnimInvertTransform(int xp, int yp, int width, int height) {
	this->x = xp;
	this->y = yp;
	this->w = width;
	this->h = height;
}

bool TileAnimInvertTransform::drawsTile() const {
	return false;
}

void TileAnimInvertTransform::draw(Image *dest, Tile *tile, MapTile &mapTile) {
	int scale = tile->getScale();
	tile->getImage()->drawSubRectInvertedOn(dest, x * scale, y * scale, x * scale,
	                                        (tile->getHeight() * mapTile._frame) + (y * scale), w * scale, h * scale);
}

TileAnimPixelTransform::TileAnimPixelTransform(int xp, int yp) {
	this->x = xp;
	this->y = yp;
}

bool TileAnimPixelTransform::drawsTile() const {
	return false;
}

void TileAnimPixelTransform::draw(Image *dest, Tile *tile, MapTile &mapTile) {
	RGBA *color = _colors[xu4_random(_colors.size())];
	int scale = tile->getScale();
	dest->fillRect(x * scale, y * scale, scale, scale, color->r, color->g, color->b, color->a);
}

bool TileAnimScrollTransform::drawsTile() const {
	return true;
}

TileAnimScrollTransform::TileAnimScrollTransform(int i) : _increment(i), _current(0), _lastOffset(0) {}

void TileAnimScrollTransform::draw(Image *dest, Tile *tile, MapTile &mapTile) {
	if (_increment == 0)
		_increment = tile->getScale();

	int offset = g_screen->_currentCycle * 4 / SCR_CYCLE_PER_SECOND * tile->getScale();
	if (_lastOffset != offset) {
		_lastOffset = offset;
		_current += _increment;
		if (_current >= tile->getHeight())
			_current = 0;
	}

	tile->getImage()->drawSubRectOn(dest, 0, _current, 0, tile->getHeight() * mapTile._frame, tile->getWidth(), tile->getHeight() - _current);
	if (_current != 0)
		tile->getImage()->drawSubRectOn(dest, 0, 0, 0, (tile->getHeight() * mapTile._frame) + tile->getHeight() - _current, tile->getWidth(), _current);

}

bool TileAnimFrameTransform::drawsTile() const {
	return true;
}

void TileAnimFrameTransform::draw(Image *dest, Tile *tile, MapTile &mapTile) {
	if (++_currentFrame >= tile->getFrames())
		_currentFrame = 0;
	tile->getImage()->drawSubRectOn(dest, 0, 0, 0, _currentFrame * tile->getHeight(), tile->getWidth(), tile->getHeight());


}

TileAnimPixelColorTransform::TileAnimPixelColorTransform(int xp, int yp, int width, int height) {
	this->x = xp;
	this->y = yp;
	this->w = width;
	this->h = height;
	_start = _end = nullptr;
}

TileAnimPixelColorTransform::~TileAnimPixelColorTransform() {
	delete _start;
	delete _end;
}

bool TileAnimPixelColorTransform::drawsTile() const {
	return false;
}

void TileAnimPixelColorTransform::draw(Image *dest, Tile *tile, MapTile &mapTile) {
	RGBA diff = *_end;
	int scale = tile->getScale();
	diff.r -= _start->r;
	diff.g -= _start->g;
	diff.b -= _start->b;

	Image *tileImage = tile->getImage();

	for (int j = y * scale; j < (y * scale) + (h * scale); j++) {
		for (int i = x * scale; i < (x * scale) + (w * scale); i++) {
			RGBA pixelAt;

			tileImage->getPixel(i, j + (mapTile._frame * tile->getHeight()), pixelAt.r, pixelAt.g, pixelAt.b, pixelAt.a);
			if (pixelAt.r >= _start->r && pixelAt.r <= _end->r &&
			        pixelAt.g >= _start->g && pixelAt.g <= _end->g &&
			        pixelAt.b >= _start->b && pixelAt.b <= _end->b) {
				dest->putPixel(i, j, _start->r + xu4_random(diff.r), _start->g + xu4_random(diff.g), _start->b + xu4_random(diff.b), pixelAt.a);
			}
		}
	}
}

TileAnimContext *TileAnimContext::create(const ConfigElement &conf) {
	TileAnimContext *context;
	static const char *const contextTypeEnumStrings[] = { "frame", "dir", nullptr };
	static const char *const dirEnumStrings[] = { "none", "west", "north", "east", "south", nullptr };

	TileAnimContext::Type type = (TileAnimContext::Type)conf.getEnum("type", contextTypeEnumStrings);

	switch (type) {
	case FRAME:
		context = new TileAnimFrameContext(conf.getInt("frame"));
		break;
	case DIR:
		context = new TileAnimPlayerDirContext(Direction(conf.getEnum("dir", dirEnumStrings)));
		break;
	default:
		context = nullptr;
		break;
	}

	// Add the transforms to the context
	if (context) {
		Std::vector<ConfigElement> children = conf.getChildren();

		for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
			if (i->getName() == "transform") {
				TileAnimTransform *transform = TileAnimTransform::create(*i);
				context->add(transform);
			}
		}
	}

	return context;
}

void TileAnimContext::add(TileAnimTransform *transform) {
	_animTransforms.push_back(transform);
}

TileAnimFrameContext::TileAnimFrameContext(int f) : _frame(f) {}

bool TileAnimFrameContext::isInContext(Tile *t, MapTile &mapTile, Direction dir) {
	return (mapTile._frame == _frame);
}

TileAnimPlayerDirContext::TileAnimPlayerDirContext(Direction d) : _dir(d) {}

bool TileAnimPlayerDirContext::isInContext(Tile *t, MapTile &mapTile, Direction d) {
	return (d == _dir);
}

/*-------------------------------------------------------------------*/

TileAnimSet::TileAnimSet(const ConfigElement &conf) {
	_name = conf.getString("name");

	Std::vector<ConfigElement> children = conf.getChildren();
	for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
		if (i->getName() == "tileanim") {
			TileAnim *anim = new TileAnim(*i);
			_tileAnims[anim->_name] = anim;
		}
	}
}

TileAnimSet::~TileAnimSet() {
	for (TileAnimMap::iterator it = _tileAnims.begin(); it != _tileAnims.end(); ++it)
		delete it->_value;
}

TileAnim *TileAnimSet::getByName(const Common::String &name) {
	TileAnimMap::iterator i = _tileAnims.find(name);
	if (i == _tileAnims.end())
		return nullptr;
	return i->_value;
}

/*------------------------------------------------------------------------*/

TileAnim::TileAnim(const ConfigElement &conf) : _random(0) {
	_name = conf.getString("name");
	if (conf.exists("random"))
		_random = conf.getInt("random");

	Std::vector<ConfigElement> children = conf.getChildren();
	for (Std::vector<ConfigElement>::iterator i = children.begin(); i != children.end(); i++) {
		if (i->getName() == "transform") {
			TileAnimTransform *transform = TileAnimTransform::create(*i);

			_transforms.push_back(transform);
		} else if (i->getName() == "context") {
			TileAnimContext *context = TileAnimContext::create(*i);

			_contexts.push_back(context);
		}
	}
}

TileAnim::~TileAnim() {
	for (uint idx = 0; idx < _transforms.size(); ++idx)
		delete _transforms[idx];
	for (uint idx = 0; idx < _contexts.size(); ++idx)
		delete _contexts[idx];
}

void TileAnim::draw(Image *dest, Tile *tile, MapTile &mapTile, Direction dir) {
	Std::vector<TileAnimTransform *>::const_iterator t;
	Std::vector<TileAnimContext *>::const_iterator c;
	bool drawn = false;

	// Nothing to do, draw the tile and return!
	if ((_random && xu4_random(100) > _random) || (!_transforms.size() && !_contexts.size()) || mapTile._freezeAnimation) {
		tile->getImage()->drawSubRectOn(dest, 0, 0, 0, mapTile._frame * tile->getHeight(), tile->getWidth(), tile->getHeight());
		return;
	}

	// Do global transforms
	for (t = _transforms.begin(); t != _transforms.end(); t++) {
		TileAnimTransform *transform = *t;

		if (!transform->_random || xu4_random(100) < transform->_random) {
			if (!transform->drawsTile() && !drawn)
				tile->getImage()->drawSubRectOn(dest, 0, 0, 0, mapTile._frame * tile->getHeight(), tile->getWidth(), tile->getHeight());
			transform->draw(dest, tile, mapTile);
			drawn = true;
		}
	}

	// Do contextual transforms
	for (c = _contexts.begin(); c != _contexts.end(); c++) {
		if ((*c)->isInContext(tile, mapTile, dir)) {
			TileAnimContext::TileAnimTransformList ctx_transforms = (*c)->getTransforms();
			for (t = ctx_transforms.begin(); t != ctx_transforms.end(); t++) {
				TileAnimTransform *transform = *t;

				if (!transform->_random || xu4_random(100) < transform->_random) {
					if (!transform->drawsTile() && !drawn)
						tile->getImage()->drawSubRectOn(dest, 0, 0, 0, mapTile._frame * tile->getHeight(), tile->getWidth(), tile->getHeight());
					transform->draw(dest, tile, mapTile);
					drawn = true;
				}
			}
		}
	}
}

} // End of namespace Ultima4
} // End of namespace Ultima