aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/animationresource.cpp
blob: f492daa93c7491ae027016112ca026ad6ddec86a (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
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "sword25/gfx/animationresource.h"

#include "sword25/kernel/kernel.h"
#include "sword25/kernel/string.h"
#include "sword25/package/packagemanager.h"
#include "sword25/util/tinyxml/tinyxml.h"
#include "sword25/gfx/bitmapresource.h"

namespace Sword25 {

// -----------------------------------------------------------------------------

#define BS_LOG_PREFIX "ANIMATIONRESOURCE"

// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------

namespace {
const int   DEFAULT_FPS = 10;
const int   MIN_FPS     = 1;
const int   MAX_FPS     = 200;
}

// -----------------------------------------------------------------------------
// Construction / Destruction
// -----------------------------------------------------------------------------

BS_AnimationResource::BS_AnimationResource(const Common::String &FileName) :
	BS_Resource(FileName, BS_Resource::TYPE_ANIMATION),
	m_Valid(false) {
	// Pointer auf den Package-Manager bekommen
	BS_PackageManager *PackagePtr = BS_Kernel::GetInstance()->GetPackage();
	BS_ASSERT(PackagePtr);

	// Animations-XML laden
	TiXmlDocument Doc;
	{
		// Die Daten werden zun�chst �ber den Package-Manager gelesen und dann in einen um ein Byte gr��eren Buffer kopiert und
		// NULL-Terminiert, da TinyXML NULL-Terminierte Daten ben�tigt.
		unsigned int FileSize;
		char *LoadBuffer = (char *) PackagePtr->GetFile(GetFileName(), &FileSize);
		if (!LoadBuffer) {
			BS_LOG_ERRORLN("Could not read \"%s\".", GetFileName().c_str());
			return;
		}
		char *WorkBuffer;
		WorkBuffer = (char *)malloc(FileSize + 1);
		memcpy(&WorkBuffer[0], LoadBuffer, FileSize);
		delete LoadBuffer;
		WorkBuffer[FileSize] = '\0';

		// Datei parsen
		Doc.Parse(&WorkBuffer[0]);
		free(WorkBuffer);
		if (Doc.Error()) {
			BS_LOG_ERRORLN("The following TinyXML-Error occured while parsing \"%s\": %s", GetFileName().c_str(), Doc.ErrorDesc());
			return;
		}
	}

	// Wurzelknoten des Animations-Tags finden, pr�fen und Attribute auslesen.
	TiXmlElement *pElement;
	{
		TiXmlNode *pNode = Doc.FirstChild("animation");
		if (!pNode || pNode->Type() != TiXmlNode::ELEMENT) {
			BS_LOG_ERRORLN("No <animation> tag found in \"%s\".", GetFileName().c_str());
			return;
		}
		pElement = pNode->ToElement();

		// Animation-Tag parsen
		if (!ParseAnimationTag(*pElement, m_FPS, m_AnimationType)) {
			BS_LOG_ERRORLN("An error occurred while parsing <animation> tag in \"%s\".", GetFileName().c_str());
			return;
		}
	}

	// Zeit (in Millisekunden) bestimmen f�r die ein einzelner Frame angezeigt wird
	m_MillisPerFrame = 1000000 / m_FPS;

	// In das Verzeichnis der Eingabedatei wechseln, da die Dateiverweise innerhalb der XML-Datei relativ zu diesem Verzeichnis sind.
	Common::String OldDirectory = PackagePtr->GetCurrentDirectory();
	if (GetFileName().contains('/')) {
		Common::String Dir = Common::String(GetFileName().c_str(), strrchr(GetFileName().c_str(), '/'));
		PackagePtr->ChangeDirectory(Dir);
	}

	// Nacheinander alle Frames-Informationen erstellen.
	TiXmlElement *pFrameElement = pElement->FirstChild("frame")->ToElement();
	while (pFrameElement) {
		Frame CurFrame;

		if (!ParseFrameTag(*pFrameElement, CurFrame, *PackagePtr)) {
			BS_LOG_ERRORLN("An error occurred in \"%s\" while parsing <frame> tag.", GetFileName().c_str());
			return;
		}

		m_Frames.push_back(CurFrame);
		pFrameElement = pFrameElement->NextSiblingElement("frame");
	}

	// Ursprungsverzeichnis wieder herstellen
	PackagePtr->ChangeDirectory(OldDirectory);

	// Sicherstellen, dass die Animation mindestens einen Frame besitzt
	if (m_Frames.empty()) {
		BS_LOG_ERRORLN("\"%s\" does not have any frames.", GetFileName().c_str());
		return;
	}

	// Alle Frame-Dateien werden vorgecached
	if (!PrecacheAllFrames()) {
		BS_LOG_ERRORLN("Could not precache all frames of \"%s\".", GetFileName().c_str());
		return;
	}

	// Feststellen, ob die Animation skalierbar ist
	if (!ComputeFeatures()) {
		BS_LOG_ERRORLN("Could not determine the features of \"%s\".", GetFileName().c_str());
		return;
	}

	m_Valid = true;
}

// -----------------------------------------------------------------------------
// Dokument-Parsermethoden
// -----------------------------------------------------------------------------

bool BS_AnimationResource::ParseAnimationTag(TiXmlElement &AnimationTag, int &FPS, BS_Animation::ANIMATION_TYPES &AnimationType) {
	// FPS einlesen
	const char *FPSString;
	if ((FPSString = AnimationTag.Attribute("fps"))) {
		int TempFPS;
		if (!BS_String::ToInt(Common::String(FPSString), TempFPS) || TempFPS < MIN_FPS || TempFPS > MAX_FPS) {
			BS_LOG_WARNINGLN("Illegal fps value (\"%s\") in <animation> tag in \"%s\". Assuming default (\"%d\"). "
			                 "The fps value has to be between %d and %d.",
			                 FPSString, GetFileName().c_str(), DEFAULT_FPS, MIN_FPS, MAX_FPS);
		} else
			FPS = TempFPS;
	}

	// Loop-Typ einlesen
	const char *LoopTypeString;
	if ((LoopTypeString = AnimationTag.Attribute("type"))) {
		if (strcmp(LoopTypeString, "oneshot") == 0)
			AnimationType = BS_Animation::AT_ONESHOT;
		else if (strcmp(LoopTypeString, "loop") == 0)
			AnimationType = BS_Animation::AT_LOOP;
		else if (strcmp(LoopTypeString, "jojo") == 0)
			AnimationType = BS_Animation::AT_JOJO;
		else
			BS_LOG_WARNINGLN("Illegal type value (\"%s\") in <animation> tag in \"%s\". Assuming default (\"loop\").",
			                 LoopTypeString, GetFileName().c_str());
	}

	return true;
}

// -----------------------------------------------------------------------------

bool BS_AnimationResource::ParseFrameTag(TiXmlElement &FrameTag, Frame &Frame_, BS_PackageManager &PackageManager) {
	const char *FileString = FrameTag.Attribute("file");
	if (!FileString) {
		BS_LOG_ERRORLN("<frame> tag without file attribute occurred in \"%s\".", GetFileName().c_str());
		return false;
	}
	Frame_.FileName = PackageManager.GetAbsolutePath(FileString);
	if (Frame_.FileName == "") {
		BS_LOG_ERRORLN("Could not create absolute path for file specified in <frame> tag in \"%s\": \"%s\".", GetFileName().c_str(), FileString);
		return false;
	}

	const char *ActionString = FrameTag.Attribute("action");
	if (ActionString)
		Frame_.Action = ActionString;

	const char *HotspotxString = FrameTag.Attribute("hotspotx");
	const char *HotspotyString = FrameTag.Attribute("hotspoty");
	if ((!HotspotxString && HotspotyString) ||
	        (HotspotxString && !HotspotyString))
		BS_LOG_WARNINGLN("%s attribute occurred without %s attribute in <frame> tag in \"%s\". Assuming default (\"0\").",
		                 HotspotxString ? "hotspotx" : "hotspoty",
		                 !HotspotyString ? "hotspoty" : "hotspotx",
		                 GetFileName().c_str());

	Frame_.HotspotX = 0;
	if (HotspotxString && !BS_String::ToInt(Common::String(HotspotxString), Frame_.HotspotX))
		BS_LOG_WARNINGLN("Illegal hotspotx value (\"%s\") in frame tag in \"%s\". Assuming default (\"%s\").",
		                 HotspotxString, GetFileName().c_str(), Frame_.HotspotX);

	Frame_.HotspotY = 0;
	if (HotspotyString && !BS_String::ToInt(Common::String(HotspotyString), Frame_.HotspotY))
		BS_LOG_WARNINGLN("Illegal hotspoty value (\"%s\") in frame tag in \"%s\". Assuming default (\"%s\").",
		                 HotspotyString, GetFileName().c_str(), Frame_.HotspotY);

	const char *FlipVString = FrameTag.Attribute("flipv");
	if (FlipVString) {
		if (!BS_String::ToBool(Common::String(FlipVString), Frame_.FlipV)) {
			BS_LOG_WARNINGLN("Illegal flipv value (\"%s\") in <frame> tag in \"%s\". Assuming default (\"false\").",
			                 FlipVString, GetFileName().c_str());
			Frame_.FlipV = false;
		}
	} else
		Frame_.FlipV = false;

	const char *FlipHString = FrameTag.Attribute("fliph");
	if (FlipHString) {
		if (!BS_String::ToBool(FlipHString, Frame_.FlipH)) {
			BS_LOG_WARNINGLN("Illegal fliph value (\"%s\") in <frame> tag in \"%s\". Assuming default (\"false\").",
			                 FlipHString, GetFileName().c_str());
			Frame_.FlipH = false;
		}
	} else
		Frame_.FlipH = false;

	return true;
}

// -----------------------------------------------------------------------------

BS_AnimationResource::~BS_AnimationResource() {
}

// -----------------------------------------------------------------------------

bool BS_AnimationResource::PrecacheAllFrames() const {
	Common::Array<Frame>::const_iterator Iter = m_Frames.begin();
	for (; Iter != m_Frames.end(); ++Iter) {
		if (!BS_Kernel::GetInstance()->GetResourceManager()->PrecacheResource((*Iter).FileName)) {
			BS_LOG_ERRORLN("Could not precache \"%s\".", (*Iter).FileName.c_str());
			return false;
		}
	}

	return true;
}

// -----------------------------------------------------------------------------

bool BS_AnimationResource::ComputeFeatures() {
	BS_ASSERT(m_Frames.size());

	// Alle Features werden als vorhanden angenommen
	m_ScalingAllowed = true;
	m_AlphaAllowed = true;
	m_ColorModulationAllowed = true;

	// Alle Frame durchgehen und alle Features deaktivieren, die auch nur von einem Frame nicht unterst�tzt werden.
	Common::Array<Frame>::const_iterator Iter = m_Frames.begin();
	for (; Iter != m_Frames.end(); ++Iter) {
		BS_BitmapResource *pBitmap;
		if (!(pBitmap = static_cast<BS_BitmapResource *>(BS_Kernel::GetInstance()->GetResourceManager()->RequestResource((*Iter).FileName)))) {
			BS_LOG_ERRORLN("Could not request \"%s\".", (*Iter).FileName.c_str());
			return false;
		}

		if (!pBitmap->IsScalingAllowed())
			m_ScalingAllowed = false;
		if (!pBitmap->IsAlphaAllowed())
			m_AlphaAllowed = false;
		if (!pBitmap->IsColorModulationAllowed())
			m_ColorModulationAllowed = false;

		pBitmap->Release();
	}

	return true;
}

} // End of namespace Sword25