File: CAnimation.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (228 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download
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
/*
 * CAnimation.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "CAnimation.h"

#include "../gui/CGuiHandler.h"
#include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../render/IScreenHandler.h"

#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/json/JsonUtils.h"

bool CAnimation::loadFrame(size_t frame, size_t group, bool verbose)
{
	if(size(group) <= frame)
	{
		if(verbose)
			printError(frame, group, "LoadFrame");
		return false;
	}

	if(auto image = getImageImpl(frame, group, false))
		return true;

	std::shared_ptr<IImage> image = GH.renderHandler().loadImage(getImageLocator(frame, group));

	if(image)
	{
		images[group][frame] = image;

		if (player.isValidPlayer())
			image->playerColored(player);
		return true;
	}
	else
	{
		// image is missing
		printError(frame, group, "LoadFrame");
		images[group][frame] = GH.renderHandler().loadImage(ImagePath::builtin("DEFAULT"), EImageBlitMode::OPAQUE);
		return false;
	}
}

bool CAnimation::unloadFrame(size_t frame, size_t group)
{
	auto image = getImage(frame, group, false);
	if(image)
	{
		images[group].erase(frame);

		if(images[group].empty())
			images.erase(group);
		return true;
	}
	return false;
}

void CAnimation::exportBitmaps(const boost::filesystem::path& path) const
{
	if(images.empty())
	{
		logGlobal->error("Nothing to export, animation is empty");
		return;
	}

	boost::filesystem::path actualPath = path / "SPRITES" / name.getName();
	boost::filesystem::create_directories(actualPath);

	size_t counter = 0;

	for(const auto & groupPair : images)
	{
		size_t group = groupPair.first;

		for(const auto & imagePair : groupPair.second)
		{
			size_t frame = imagePair.first;
			const auto img = imagePair.second;

			boost::format fmt("%d_%d.bmp");
			fmt % group % frame;

			img->exportBitmap(actualPath / fmt.str());
			counter++;
		}
	}

	logGlobal->info("Exported %d frames to %s", counter, actualPath.string());
}

void CAnimation::printError(size_t frame, size_t group, std::string type) const
{
	logGlobal->error("%s error: Request for frame not present in CAnimation! File name: %s, Group: %d, Frame: %d", type, name.getOriginalName(), group, frame);
}

CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <ImageLocator> > layout, EImageBlitMode mode):
	name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
	source(layout),
	mode(mode)
{
	if(source.empty())
		logAnim->error("Animation %s failed to load", Name.getOriginalName());
}

CAnimation::~CAnimation() = default;

void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
{
	ImageLocator clone(getImageLocator(sourceFrame, sourceGroup));
	source[targetGroup].push_back(clone);
}

std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
{
	if (!loadFrame(frame, group, verbose))
		return nullptr;
	return getImageImpl(frame, group, verbose);
}

std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
{
	auto groupIter = images.find(group);
	if (groupIter != images.end())
	{
		auto imageIter = groupIter->second.find(frame);
		if (imageIter != groupIter->second.end())
			return imageIter->second;
	}
	if (verbose)
		printError(frame, group, "GetImage");
	return nullptr;
}

size_t CAnimation::size(size_t group) const
{
	auto iter = source.find(group);
	if (iter != source.end())
		return iter->second.size();
	return 0;
}

void CAnimation::horizontalFlip()
{
	for(auto & group : source)
		for(size_t i = 0; i < group.second.size(); ++i)
			horizontalFlip(i, group.first);
}

void CAnimation::verticalFlip()
{
	for(auto & group : source)
		for(size_t i = 0; i < group.second.size(); ++i)
			verticalFlip(i, group.first);
}

void CAnimation::horizontalFlip(size_t frame, size_t group)
{
	auto i1 = images.find(group);
	if(i1 != images.end())
	{
		auto i2 = i1->second.find(frame);

		if(i2 != i1->second.end())
			i2->second = nullptr;
	}

	auto locator = getImageLocator(frame, group);
	locator.horizontalFlip = !locator.horizontalFlip;
	source[group][frame] = locator;
}

void CAnimation::verticalFlip(size_t frame, size_t group)
{
	auto i1 = images.find(group);
	if(i1 != images.end())
	{
		auto i2 = i1->second.find(frame);

		if(i2 != i1->second.end())
			i2->second = nullptr;
	}

	auto locator = getImageLocator(frame, group);
	locator.verticalFlip = !locator.verticalFlip;
	source[group][frame] = locator;
}

void CAnimation::playerColored(PlayerColor targetPlayer)
{
	player = targetPlayer;
	for(auto & group : images)
		for(auto & image : group.second)
			image.second->playerColored(player);
}

void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targetGroup)
{
	for(size_t frame = 0; frame < size(sourceGroup); ++frame)
	{
		duplicateImage(sourceGroup, frame, targetGroup);
		verticalFlip(frame, targetGroup);
	}
}

ImageLocator CAnimation::getImageLocator(size_t frame, size_t group) const
{
	try
	{
		const ImageLocator & locator = source.at(group).at(frame);

		if (!locator.empty())
			return locator;
	}
	catch (std::out_of_range &)
	{
		throw std::runtime_error("Frame " + std::to_string(frame) + " of group " + std::to_string(group) + " is missing from animation " + name.getOriginalName() );
	}

	return ImageLocator(name, frame, group, mode);
}