File: atlasloader.cpp

package info (click to toggle)
fife 0.4.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,204 kB
  • sloc: cpp: 42,642; xml: 18,881; python: 13,521; makefile: 23
file content (359 lines) | stat: -rw-r--r-- 11,042 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2005-2019 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE 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 Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes

// 3rd party library includes
#include <tinyxml.h>

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "model/model.h"
#include "model/structures/layer.h"
#include "vfs/fife_boost_filesystem.h"
#include "vfs/vfs.h"
#include "vfs/raw/rawdata.h"
#include "util/base/exception.h"
#include "util/log/logger.h"
#include "util/resource/resource.h"
#include "util/resource/resourcemanager.h"
#include "video/animationmanager.h" 
#include "view/visual.h"

#include "atlasloader.h"

namespace FIFE {
	/** Logger to use for this source file.
	 *  @relates Logger
	 */
	static Logger _log(LM_NATIVE_LOADERS);


	size_t Atlas::getImageCount() const {
		return m_subimages.size();
	}

	ImagePtr& Atlas::getPackedImage() {
		return m_image;
	}

	ImagePtr Atlas::getImage(const std::string& id) {
		SubimageMap::iterator iter = m_subimages.find(id);
		if(iter == m_subimages.end())
			return ImagePtr();
		return iter->second.image;
	}

	ImagePtr Atlas::getImage(uint32_t index) {
		if(index > getImageCount())
			return ImagePtr();

		SubimageMap::iterator iter = m_subimages.begin();
		for(uint32_t i = 0; i < index; ++i, ++iter);
		return iter->second.image;
	}

	bool Atlas::addImage(const std::string& imagename, const AtlasData& data) {
		return m_subimages.insert(std::pair<std::string, AtlasData>(imagename, data)).second;
	}

	void Atlas::setPackedImage(const ImagePtr& image) {
		m_image = image;
	}

	const std::string& Atlas::getName() const {
		return m_name;
	}

	AtlasLoader::AtlasLoader(Model* model, VFS* vfs, ImageManager* imageManager, AnimationManager* animationManager)
	: m_model(model), m_vfs(vfs), m_imageManager(imageManager), m_animationManager(animationManager) {
	}

	AtlasLoader::~AtlasLoader() {
	}

	bool AtlasLoader::isLoadable(const std::string& filename) {
		bfs::path atlasPath(filename);
		std::string atlasFilename = atlasPath.string();
		TiXmlDocument atlasFile;

		try {
			RawData* data = m_vfs->open(atlasFilename);

			if (data) {
				if (data->getDataLength() != 0) {
					atlasFile.Parse(data->readString(data->getDataLength()).c_str());

					if (atlasFile.Error()) {
						return false;
					}
				} else {
					return false;
				}

				// done with data delete resource
				delete data;
				data = 0;
			}
		} catch (NotFound&) {
			return false;
		}

		// if we get here then loading the file went well
		TiXmlElement* root = atlasFile.RootElement();

		if (root && root->ValueStr() == "assets") {
			if (root->FirstChildElement("atlas")) {
				return true;
			}
		}

		return false;
	}

	AtlasPtr AtlasLoader::load(const std::string& filename) {
		bfs::path atlasPath(filename);
		bfs::path atlasPathDirectory;
		std::string atlasFilename = atlasPath.string();

		if (HasParentPath(atlasPath)) {
			// save the directory where the atlas file is located
			atlasPathDirectory = GetParentPath(atlasPath);
		}

		TiXmlDocument doc;
		AtlasPtr atlas;

		try {
			RawData* data = m_vfs->open(atlasFilename);

			if (data) {
				if (data->getDataLength() != 0) {
					doc.Parse(data->readString(data->getDataLength()).c_str());

					if (doc.Error()) {
						return atlas;
					}

					// done with data delete resource
					delete data;
					data = 0;
				}
			}
		}
		catch (NotFound& e) {
			FL_ERR(_log, e.what());

			// TODO - should we abort here
			//        or rethrow the exception
			//        or just keep going

			return atlas;
		}

		// if we get here then everything loaded properly
		// so we can just parse out the contents
		TiXmlElement* root = doc.RootElement();

		if (root && root->ValueStr() == "assets") {
			atlas = loadAtlas(filename, root->FirstChildElement("atlas"));
		}

		return atlas;
	}

	std::vector<AtlasPtr> AtlasLoader::loadMultiple(const std::string& filename) {
		bfs::path atlasPath(filename);
		bfs::path atlasPathDirectory;
		std::string atlasFilename = atlasPath.string();

		if (HasParentPath(atlasPath)) {
			// save the directory where the atlas file is located
			atlasPathDirectory = GetParentPath(atlasPath);
		}

		TiXmlDocument doc;
		std::vector<AtlasPtr> atlasVector;

		try {
			RawData* data = m_vfs->open(atlasFilename);

			if (data) {
				if (data->getDataLength() != 0) {
					doc.Parse(data->readString(data->getDataLength()).c_str());

					if (doc.Error()) {
						return atlasVector;
					}

					// done with data delete resource
					delete data;
					data = 0;
				}
			}
		}
		catch (NotFound& e) {
			FL_ERR(_log, e.what());

			// TODO - should we abort here
			//        or rethrow the exception
			//        or just keep going

			return atlasVector;
		}

		// if we get here then everything loaded properly
		// so we can just parse out the contents
		TiXmlElement* root = doc.RootElement();

		if (root && root->ValueStr() == "assets") {
			for (TiXmlElement* atlasElem = root->FirstChildElement("atlas"); atlasElem; atlasElem = atlasElem->NextSiblingElement("atlas")) {
				AtlasPtr atlas = loadAtlas(filename, atlasElem);
				if (atlas) {
					atlasVector.push_back(atlas);
				}
			}
		}

		return atlasVector;
	}

	AtlasPtr AtlasLoader::loadAtlas(const std::string& filename, TiXmlElement* atlasElem) {
		AtlasPtr atlas;
		if (!atlasElem) {
			return atlas;
		}

		const std::string* atlasSource = atlasElem->Attribute(std::string("source"));
		if (atlasSource) {
			const std::string* atlasId = atlasElem->Attribute(std::string("id"));

			bfs::path atlasPath(filename);
			bfs::path atlasPathDirectory;
			if (HasParentPath(atlasPath)) {
				// save the directory where the atlas file is located
				atlasPathDirectory = GetParentPath(atlasPath);
			}

			// Atlas itself doesn't have appended id
			bfs::path atlasImagePath = atlasPathDirectory / *atlasSource;
			atlas.reset(new Atlas(atlasImagePath.string()));

			// End-user could create the same atlas for the second time.
			// Since we don't hold any data for Atlases like ImageManager we need to recreate
			// atlas parameters (to return proper AtlasPtr) but don't reload pixel data (they are held by ImageManager).
			if (!m_imageManager->exists(atlas->getName())) {
				atlas->setPackedImage(m_imageManager->create(atlas->getName()));
			} else {
				atlas->setPackedImage(m_imageManager->getPtr(atlas->getName()));
			}
			// Create subimages with given id and individual position and size
			if (atlasElem->FirstChildElement("subimage")) {
				for (TiXmlElement* imageElem = atlasElem->FirstChildElement("subimage");
					imageElem != 0; imageElem = imageElem->NextSiblingElement("subimage")) {

					const std::string* subimageId = imageElem->Attribute(std::string("id"));
					if (subimageId) {
						Rect region;
						imageElem->QueryValueAttribute("xpos", &region.x);
						imageElem->QueryValueAttribute("ypos", &region.y);
						imageElem->QueryValueAttribute("width", &region.w);
						imageElem->QueryValueAttribute("height", &region.h);

						std::string finalname;
						// atlas id is optional here
						if (atlasId) {
							finalname = *atlasId + ":" + *subimageId;
						} else {
							finalname = *subimageId;
						}
						ImagePtr subImage;

						if (!m_imageManager->exists(finalname)) {
							subImage = m_imageManager->create(finalname);
						} else {
							subImage = m_imageManager->getPtr(finalname);
						}
						subImage->useSharedImage(atlas->getPackedImage(), region);

						AtlasData atlasData = {region, subImage};
						atlas->addImage(finalname, atlasData);
					}
				}
			} else {
				// Create subimages with automatic id and same size
				int frame = 0;
				int atlasWidth = 0;
				int atlasHeight = 0;
				int subimageWidth = 0;
				int subimageHeight = 0;
				atlasElem->QueryValueAttribute("atlas_width", &atlasWidth);
				atlasElem->QueryValueAttribute("atlas_height", &atlasHeight);
				atlasElem->QueryValueAttribute("subimage_width", &subimageWidth);
				atlasElem->QueryValueAttribute("subimage_height", &subimageHeight);
				// file extension of the atlas is also used as subimage extension
				std::string extension = bfs::extension(*atlasSource);
				// we need an atlas id
				if (!atlasId) {
					atlasId = atlasSource;
				}
				
				if (atlasWidth != 0 && atlasHeight != 0 && subimageWidth != 0 && subimageHeight != 0) {
					int x_rows = atlasWidth / subimageWidth;
					int y_rows = atlasHeight / subimageHeight;
					Rect region(0, 0, subimageWidth, subimageHeight);
					for (int y = 0; y < y_rows; ++y) {
						region.y = y * subimageHeight;
						for (int x = 0; x < x_rows; ++x) {
							region.x = x * subimageWidth;

							static char tmp[64];
							snprintf(tmp, 64, "%04d", frame);
							std::ostringstream finalname;
							finalname << *atlasId << ":" << std::string(tmp) << extension;

							ImagePtr subImage;
							if (!m_imageManager->exists(finalname.str())) {
								subImage = m_imageManager->create(finalname.str());
							} else {
								subImage = m_imageManager->getPtr(finalname.str());
							}
							subImage->useSharedImage(atlas->getPackedImage(), region);

							AtlasData atlasData = {region, subImage};
							atlas->addImage(finalname.str(), atlasData);

							++frame;
						}
					}
				}
			}
		}

		return atlas;
	}

	AtlasLoader* createDefaultAtlasLoader(Model* model, VFS* vfs, ImageManager* imageManager, AnimationManager* animationManager) {
		return new AtlasLoader(model, vfs, imageManager, animationManager);
	}
}