File: graphics.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (347 lines) | stat: -rw-r--r-- 9,037 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
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
/*
 * graphics.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
 *
 */

//code is copied from vcmiclient/Graphics.cpp with minimal changes
#include "StdInc.h"
#include "graphics.h"

#include <vcmi/Entity.h>
#include <vcmi/ArtifactService.h>
#include <vcmi/CreatureService.h>
#include <vcmi/FactionService.h>
#include <vcmi/HeroTypeService.h>
#include <vcmi/SkillService.h>
#include <vcmi/spells/Service.h>

#include "../lib/filesystem/Filesystem.h"
#include "../lib/filesystem/CBinaryReader.h"
#include "Animation.h"
#include "../lib/CThreadHelper.h"
#include "../lib/CModHandler.h"
#include "../lib/VCMI_Lib.h"
#include "../CCallback.h"
#include "../lib/CGeneralTextHandler.h"
#include "BitmapHandler.h"
#include "../lib/CGameState.h"
#include "../lib/JsonNode.h"
#include "../lib/CStopWatch.h"
#include "../lib/mapObjects/CObjectClassesHandler.h"
#include "../lib/mapObjects/CObjectHandler.h"
#include "../lib/CHeroHandler.h"

Graphics * graphics = nullptr;

void Graphics::loadPaletteAndColors()
{
	auto textFile = CResourceHandler::get()->load(ResourceID("DATA/PLAYERS.PAL"))->readAll();
	std::string pals((char*)textFile.first.get(), textFile.second);
	
	playerColorPalette.resize(256);
	playerColors.resize(PlayerColor::PLAYER_LIMIT_I);
	int startPoint = 24; //beginning byte; used to read
	for(int i = 0; i < 256; ++i)
	{
		QColor col;
		col.setRed(pals[startPoint++]);
		col.setGreen(pals[startPoint++]);
		col.setBlue(pals[startPoint++]);
		col.setAlpha(255);
		startPoint++;
		playerColorPalette[i] = col.rgba();
	}
	
	neutralColorPalette.resize(32);
	
	auto stream = CResourceHandler::get()->load(ResourceID("config/NEUTRAL.PAL"));
	CBinaryReader reader(stream.get());
	
	for(int i = 0; i < 32; ++i)
	{
		QColor col;
		col.setRed(reader.readUInt8());
		col.setGreen(reader.readUInt8());
		col.setBlue(reader.readUInt8());
		col.setAlpha(255);
		reader.readUInt8(); // this is "flags" entry, not alpha
		neutralColorPalette[i] = col.rgba();
	}
	
	//colors initialization
	QColor colors[]  = {
		{0xff,0,  0,    255},
		{0x31,0x52,0xff,255},
		{0x9c,0x73,0x52,255},
		{0x42,0x94,0x29,255},
		
		{0xff,0x84,0,   255},
		{0x8c,0x29,0xa5,255},
		{0x09,0x9c,0xa5,255},
		{0xc6,0x7b,0x8c,255}};
	
	for(int i=0;i<8;i++)
	{
		playerColors[i] = colors[i].rgba();
	}
	//gray
	neutralColor = qRgba(0x84, 0x84, 0x84, 0xFF);
}

Graphics::Graphics()
{
#if 0
	std::vector<Task> tasks; //preparing list of graphics to load
	tasks += std::bind(&Graphics::loadFonts,this);
	tasks += std::bind(&Graphics::loadPaletteAndColors,this);
	tasks += std::bind(&Graphics::initializeBattleGraphics,this);
	tasks += std::bind(&Graphics::loadErmuToPicture,this);
	tasks += std::bind(&Graphics::initializeImageLists,this);
	
	CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
	th.run();
#else
	loadPaletteAndColors();
	initializeImageLists();
#endif
	
	//(!) do not load any CAnimation here
}

Graphics::~Graphics()
{
}

void Graphics::load()
{
	loadHeroAnimations();
	loadHeroFlagAnimations();
}

void Graphics::loadHeroAnimations()
{
	for(auto & elem : VLC->heroh->classes.objects)
	{
		for(auto templ : VLC->objtypeh->getHandlerFor(Obj::HERO, elem->getIndex())->getTemplates())
		{
			if(!heroAnimations.count(templ->animationFile))
				heroAnimations[templ->animationFile] = loadHeroAnimation(templ->animationFile);
		}
	}
	
	boatAnimations[0] = loadHeroAnimation("AB01_.DEF");
	boatAnimations[1] = loadHeroAnimation("AB02_.DEF");
	boatAnimations[2] = loadHeroAnimation("AB03_.DEF");
	
	
	mapObjectAnimations["AB01_.DEF"] = boatAnimations[0];
	mapObjectAnimations["AB02_.DEF"] = boatAnimations[1];
	mapObjectAnimations["AB03_.DEF"] = boatAnimations[2];
}
void Graphics::loadHeroFlagAnimations()
{
	static const std::vector<std::string> HERO_FLAG_ANIMATIONS =
	{
		"AF00", "AF01","AF02","AF03",
		"AF04", "AF05","AF06","AF07"
	};
	
	static const std::vector< std::vector<std::string> > BOAT_FLAG_ANIMATIONS =
	{
		{
			"ABF01L", "ABF01G", "ABF01R", "ABF01D",
			"ABF01B", "ABF01P", "ABF01W", "ABF01K"
		},
		{
			"ABF02L", "ABF02G", "ABF02R", "ABF02D",
			"ABF02B", "ABF02P", "ABF02W", "ABF02K"
		},
		{
			"ABF03L", "ABF03G", "ABF03R", "ABF03D",
			"ABF03B", "ABF03P", "ABF03W", "ABF03K"
		}
	};
	
	for(const auto & name : HERO_FLAG_ANIMATIONS)
		heroFlagAnimations.push_back(loadHeroFlagAnimation(name));
	
	for(int i = 0; i < BOAT_FLAG_ANIMATIONS.size(); i++)
		for(const auto & name : BOAT_FLAG_ANIMATIONS[i])
			boatFlagAnimations[i].push_back(loadHeroFlagAnimation(name));
}

std::shared_ptr<Animation> Graphics::loadHeroFlagAnimation(const std::string & name)
{
	//first - group number to be rotated, second - group number after rotation
	static const std::vector<std::pair<int,int> > rotations =
	{
		{6,10}, {7,11}, {8,12}, {1,13},
		{2,14}, {3,15}
	};
	
	std::shared_ptr<Animation> anim = std::make_shared<Animation>(name);
	anim->preload();
	
	for(const auto & rotation : rotations)
	{
		const int sourceGroup = rotation.first;
		const int targetGroup = rotation.second;
		
		anim->createFlippedGroup(sourceGroup, targetGroup);
	}
	
	return anim;
}

std::shared_ptr<Animation> Graphics::loadHeroAnimation(const std::string &name)
{
	//first - group number to be rotated, second - group number after rotation
	static const std::vector<std::pair<int,int> > rotations =
	{
		{6,10}, {7,11}, {8,12}, {1,13},
		{2,14}, {3,15}
	};
	
	std::shared_ptr<Animation> anim = std::make_shared<Animation>(name);
	anim->preload();
	
	
	for(const auto & rotation : rotations)
	{
		const int sourceGroup = rotation.first;
		const int targetGroup = rotation.second;
		
		anim->createFlippedGroup(sourceGroup, targetGroup);
	}
	
	return anim;
}

void Graphics::blueToPlayersAdv(QImage * sur, PlayerColor player)
{
	if(sur->format() == QImage::Format_Indexed8)
	{
		auto palette = sur->colorTable();
		if(player < PlayerColor::PLAYER_LIMIT)
		{
			for(int i = 0; i < 32; ++i)
				palette[224 + i] = playerColorPalette[player.getNum() * 32 + i];
		}
		else if(player == PlayerColor::NEUTRAL)
		{
			palette = neutralColorPalette;
		}
		else
		{
			logGlobal->error("Wrong player id in blueToPlayersAdv (%s)!", player.getStr());
			return;
		}
		//FIXME: not all player colored images have player palette at last 32 indexes
		//NOTE: following code is much more correct but still not perfect (bugged with status bar)
		sur->setColorTable(palette);
	}
	else
	{
		//TODO: implement. H3 method works only for images with palettes.
		// Add some kind of player-colored overlay?
		// Or keep palette approach here and replace only colors of specific value(s)
		// Or just wait for OpenGL support?
		logGlobal->warn("Image must have palette to be player-colored!");
	}
}

std::shared_ptr<Animation> Graphics::getAnimation(const CGObjectInstance* obj)
{
	if(obj->ID == Obj::HERO)
		return getHeroAnimation(obj->appearance);
	return getAnimation(obj->appearance);
}

std::shared_ptr<Animation> Graphics::getHeroAnimation(const std::shared_ptr<const ObjectTemplate> info)
{
	if(info->animationFile.empty())
	{
		logGlobal->warn("Def name for hero (%d,%d) is empty!", info->id, info->subid);
		return std::shared_ptr<Animation>();
	}
	
	std::shared_ptr<Animation> ret = loadHeroAnimation(info->animationFile);
	
	//already loaded
	if(ret)
	{
		ret->preload();
		return ret;
	}
	
	ret = std::make_shared<Animation>(info->animationFile);
	heroAnimations[info->animationFile] = ret;
	
	ret->preload();
	return ret;
}

std::shared_ptr<Animation> Graphics::getAnimation(const std::shared_ptr<const ObjectTemplate> info)
{	
	if(info->animationFile.empty())
	{
		logGlobal->warn("Def name for obj (%d,%d) is empty!", info->id, info->subid);
		return std::shared_ptr<Animation>();
	}
	
	std::shared_ptr<Animation> ret = mapObjectAnimations[info->animationFile];
	
	//already loaded
	if(ret)
	{
		ret->preload();
		return ret;
	}
	
	ret = std::make_shared<Animation>(info->animationFile);
	mapObjectAnimations[info->animationFile] = ret;
	
	ret->preload();
	return ret;
}

void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
{
	if (!imageName.empty())
	{
		JsonNode entry;
		if(group != 0)
			entry["group"].Integer() = group;
		entry["frame"].Integer() = index;
		entry["file"].String() = imageName;
		
		imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
	}
}

void Graphics::addImageListEntries(const EntityService * service)
{
	auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
	
	auto loopCb = [&](const Entity * entity, bool & stop)
	{
		entity->registerIcons(cb);
	};
	
	service->forEachBase(loopCb);
}

void Graphics::initializeImageLists()
{
	addImageListEntries(VLC->creatures());
	addImageListEntries(VLC->heroTypes());
	addImageListEntries(VLC->artifacts());
	addImageListEntries(VLC->factions());
	addImageListEntries(VLC->spells());
	addImageListEntries(VLC->skills());
}