File: ImageEntityData.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 (292 lines) | stat: -rw-r--r-- 9,377 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
/* 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/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/graphics/ImageEntityData.h"
#include "hpl1/engine/graphics/Graphics.h"
#include "hpl1/engine/graphics/Material.h"
#include "hpl1/engine/graphics/MaterialHandler.h"
#include "hpl1/engine/graphics/Mesh2d.h"
#include "hpl1/engine/graphics/MeshCreator.h"
#include "hpl1/engine/impl/tinyXML/tinyxml.h"
#include "hpl1/engine/resources/Resources.h"
#include "hpl1/engine/scene/ImageEntity.h"
#include "hpl1/engine/scene/TileSet.h"
#include "hpl1/engine/system/low_level_system.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

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

cImageEntityData::cImageEntityData(tString asName, cGraphics *apGraphics, cResources *apResources)
	: iResourceBase(asName, 0) {
	mpResources = apResources;
	mpGraphics = apGraphics;

	mlFrameNum = 0;

	mbCastShadows = false;
	mbCollidable = false;
	mbLit = true;

	mpMesh = NULL;
	mpCollideMesh = NULL;
}

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

cImageEntityData::~cImageEntityData() {
	for (int i = 0; i < (int)mvImageFrames.size(); i++) {
		hplDelete(mvImageFrames[i].mpMaterial);
	}

	hplDelete(mpMesh);
}

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

//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

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

cImageAnimation *cImageEntityData::GetAnimationByName(const tString &asName) {
	tImageAnimationMapIt it = m_mapAnimations.find(asName);
	if (it == m_mapAnimations.end())
		return NULL;

	return &it->second;
}

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

cImageAnimation *cImageEntityData::GetAnimationByHandle(int alHandle) {
	tImageAnimationMapIt it = m_mapAnimations.begin();

	while (it != m_mapAnimations.end()) {
		if (it->second.mlHandle == alHandle)
			return &it->second;
		it++;
	}

	return NULL;
}

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

bool cImageEntityData::CreateFromFile(const tString &asFile, tIntVec &avImageHandle) {
	bool bGotAnim = false;
	TiXmlDocument *pDoc = hplNew(TiXmlDocument, (asFile.c_str()));
	if (!pDoc->LoadFile()) {
		error("Couldn't load tileset '%s'", asFile.c_str());
		return false;
	}

	TiXmlElement *RootElem = pDoc->RootElement();

	// Temp test::
	Common::fill(avImageHandle.begin(), avImageHandle.end(), -1);

	///////// MAIN ///////////////
	TiXmlElement *MainElem = RootElem->FirstChildElement("MAIN");

	msDataName = cString::ToString(MainElem->Attribute("Name"), "");
	msType = cString::ToString(MainElem->Attribute("Type"), "");
	msSubType = cString::ToString(MainElem->Attribute("Subtype"), "");

	///////// IMAGE ///////////////
	TiXmlElement *ImageElem = RootElem->FirstChildElement("IMAGE");

	tString sImageName = cString::ToString(ImageElem->Attribute("Name"), "");
	tString sDirectory = cString::ToString(ImageElem->Attribute("Dir"), "");
	tString sMaterial = cString::ToString(ImageElem->Attribute("Material"), "");
	tString sMesh = cString::ToString(ImageElem->Attribute("Mesh"), "");

	mvImageSize.x = cString::ToFloat(ImageElem->Attribute("Width"), 512) + 2.0f;
	mvImageSize.y = cString::ToFloat(ImageElem->Attribute("Height"), 512) + 2.0f;

	///////// PROPERTIES ///////////////
	TiXmlElement *PropElem = RootElem->FirstChildElement("PROPERTIES");

	mbCastShadows = cString::ToBool(PropElem->Attribute("CastShadows"), false);
	mbCollidable = cString::ToBool(PropElem->Attribute("Collidable"), false);
	mbCollides = cString::ToBool(PropElem->Attribute("Collides"), false);
	mbLit = cString::ToBool(PropElem->Attribute("Lit"), true);
	tString sCollideMesh = cString::ToString(PropElem->Attribute("CollideMesh"), "square");

	///////// ANIMATIONS ///////////////
	TiXmlElement *AnimationElem = RootElem->FirstChildElement("ANIMATIONS");

	if (AnimationElem != NULL) {
		mlFrameNum = cString::ToInt(AnimationElem->Attribute("Frames"), 1);

		TiXmlElement *AnimChildElem = AnimationElem->FirstChildElement();

		int lCount = 0;
		while (AnimChildElem) {
			cImageAnimation Anim;
			Anim.msName = cString::ToString(AnimChildElem->Attribute("Name"), "");
			Anim.mfSpeed = cString::ToFloat(AnimChildElem->Attribute("Speed"), 1);
			Anim.mbCollidable = cString::ToBool(AnimChildElem->Attribute("Collidable"), false);
			Anim.msSound = cString::ToString(AnimChildElem->Attribute("Sound"), "");
			Anim.mlHandle = lCount;

			tString sData = cString::ToString(AnimChildElem->Attribute("Data"), "");
			cString::GetIntVec(sData, Anim.mvFrameNums);

			m_mapAnimations.insert(tImageAnimationMap::value_type(Anim.msName, Anim));

			AnimChildElem = AnimChildElem->NextSiblingElement();
			lCount++;
		}

		bGotAnim = true;
	} else {
		mlFrameNum = 1;
		bGotAnim = false;
	}

	///////// LOADING /////////////////

	mpResources->AddResourceDir(sDirectory);

	// Create the mesh for drawing
	mpMesh = mpGraphics->GetMeshCreator()->Create2D(sMesh, 2);
	if (mpMesh == NULL) {
		Error("Error creating mesh for '%s'!\n", msName.c_str());
		return false;
	}
	mpMesh->CreateVertexVec();
	mvIdxVec = *mpMesh->GetIndexVec();

	// Create the mesh used for collision
	mpCollideMesh = mpGraphics->GetMeshCreator()->Create2D(sCollideMesh, 2);
	if (mpCollideMesh == NULL) {
		Error("Error creating collide mesh '%s' for '%s'!\n", sCollideMesh.c_str(), msName.c_str());
		return false;
	}
	mpCollideMesh->CreateVertexVec();

	// Determine frame size, there should be some minum size and it should also see to that it
	// can contain all frames for the animations.
	double x = ceil(log((double)((float)mlFrameNum) * mvImageSize.x) / log(2.0f));
	double y = ceil(log((double)mvImageSize.y) / log(2.0f));

	if (x > kMaxImageEntityFrameWidth) {
		y += x - kMaxTileFrameWidth;
		x = kMaxTileFrameWidth;
	}

	mvFrameSize = cVector2l((int)pow(2.0, x), (int)pow(2.0, y));

	// Loop through all animation frames.
	for (int i = 0; i < mlFrameNum; i++) {
		// Get the material
		iMaterial *pMaterial = mpGraphics->GetMaterialHandler()->Create(sMaterial, eMaterialPicture_Image);
		if (pMaterial == NULL) {
			Error("Error creating material '%s' for '%s'!\n", sMaterial.c_str(), msName.c_str());
			return false;
		}

		// Get the textures for the material
		tTextureTypeList lstTypes = pMaterial->GetTextureTypes();
		for (tTextureTypeListIt it = lstTypes.begin(); it != lstTypes.end(); it++) {
			if (avImageHandle[it->mType] == -1) {
				avImageHandle[it->mType] = mpResources->GetImageManager()->CreateFrame(mvFrameSize);
			}

			tString sFile;
			if (bGotAnim) {
				int lNum = i + 1;

				sFile = sImageName;
				if (lNum < 10)
					sFile += "0";
				sFile += Common::String::format("%d", lNum).c_str();
			} else {
				sFile = sImageName;
			}

			cResourceImage *pImage = mpResources->GetImageManager()->CreateImage(
				sFile + it->msSuffix,
				avImageHandle[it->mType]);
			if (pImage == NULL) {
				error("Can't load texture '%s%s'", sFile.c_str(), it->msSuffix.c_str());
				return false;
			}

			pMaterial->SetImage(pImage, it->mType);
		}

		pMaterial->Compile();

		cImageFrame ImageFrame;

		ImageFrame.mpMaterial = pMaterial;
		cRect2f ImageRect = pMaterial->GetTextureOffset(eMaterialTexture_Diffuse);
		ImageFrame.mvVtx = *mpMesh->GetVertexVec(ImageRect, 2, eTileRotation_0);

		mvImageFrames.push_back(ImageFrame);
	}

	///////// CLEAN UP ///////////////

	hplDelete(pDoc);

	mpResources->GetImageManager()->FlushAll();

	return true;
}

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

cImageFrame *cImageEntityData::GetImageFrame(int alFrame) {
	if (alFrame < 0 || alFrame >= (int)mvImageFrames.size())
		return NULL;

	return &mvImageFrames[alFrame];
}

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

//////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////

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

void cImageEntityData::GetFrameNum(TiXmlElement *apElement) {
	mlFrameNum = 1;
}

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

} // namespace hpl