File: Images.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 (559 lines) | stat: -rw-r--r-- 12,629 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Images.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 "Images.h"

#include "MiscWidgets.h"

#include "../gui/CGuiHandler.h"
#include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../render/CAnimation.h"
#include "../render/Canvas.h"
#include "../render/ColorFilter.h"
#include "../render/Colors.h"

#include "../battle/BattleInterface.h"
#include "../battle/BattleInterfaceClasses.h"

#include "../CGameInfo.h"
#include "../CPlayerInterface.h"

#include "../../CCallback.h"

#include "../../lib/CConfigHandler.h"
#include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
#include "../../lib/CRandomGenerator.h"

CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
	: bg(image)
	, needRefresh(false)
{
	pos += position;
	pos.w = bg->width();
	pos.h = bg->height();

	addUsedEvents(SHOW_POPUP);
}

CPicture::CPicture( const ImagePath &bmpname, int x, int y )
	: CPicture(bmpname, Point(x,y))
{}

CPicture::CPicture( const ImagePath & bmpname )
	: CPicture(bmpname, Point(0,0))
{}

CPicture::CPicture( const ImagePath & bmpname, const Point & position, EImageBlitMode mode )
	: bg(GH.renderHandler().loadImage(bmpname, mode))
	, needRefresh(false)
{
	pos.x += position.x;
	pos.y += position.y;

	assert(bg);
	if(bg)
	{
		pos.w = bg->width();
		pos.h = bg->height();
	}
	else
	{
		pos.w = pos.h = 0;
	}

	addUsedEvents(SHOW_POPUP);
}

CPicture::CPicture( const ImagePath & bmpname, const Point & position )
	:CPicture(bmpname, position, EImageBlitMode::COLORKEY)
{}

CPicture::CPicture(const ImagePath & bmpname, const Rect &SrcRect, int x, int y)
	: CPicture(bmpname, Point(x,y))
{
	srcRect = SrcRect;
	pos.w = srcRect->w;
	pos.h = srcRect->h;

	addUsedEvents(SHOW_POPUP);
}

CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
	: CPicture(image, Point(x,y))
{
	srcRect = SrcRect;
	pos.w = srcRect->w;
	pos.h = srcRect->h;

	addUsedEvents(SHOW_POPUP);
}

void CPicture::show(Canvas & to)
{
	if (needRefresh)
		showAll(to);
}

void CPicture::showAll(Canvas & to)
{
	if(bg)
	{
		if (srcRect.has_value())
			to.draw(bg, pos.topLeft(), *srcRect);
		else
			to.draw(bg, pos.topLeft());
	}
}

void CPicture::setAlpha(uint8_t value)
{
	bg->setAlpha(value);
}

void CPicture::scaleTo(Point size)
{
	bg->scaleTo(size, EScalingAlgorithm::BILINEAR);

	pos.w = bg->width();
	pos.h = bg->height();
}

void CPicture::setPlayerColor(PlayerColor player)
{
	bg->playerColored(player);
}

void CPicture::addRClickCallback(const std::function<void()> & callback)
{
	rCallback = callback;
}

void CPicture::showPopupWindow(const Point & cursorPosition)
{
	if(rCallback)
		rCallback();
}

CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position)
	: CIntObject(0, position.topLeft())
	, texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
{
	pos.w = position.w;
	pos.h = position.h;
	imageArea = Rect(Point(), texture->dimensions());
}

CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea)
	: CIntObject(0, position.topLeft())
	, texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
	, imageArea(imageArea)
{
	pos.w = position.w;
	pos.h = position.h;
}

void CFilledTexture::showAll(Canvas & to)
{
	CanvasClipRectGuard guard(to, pos);

	for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
	{
		for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
			to.draw(texture, Point(x,y), imageArea);
	}
}

void FilledTexturePlayerIndexed::setPlayerColor(PlayerColor player)
{
	texture->playerColored(player);
}

FilledTexturePlayerColored::FilledTexturePlayerColored(Rect position)
	:CFilledTexture(ImagePath::builtin("DiBoxBck"), position)
{
}

void FilledTexturePlayerColored::setPlayerColor(PlayerColor player)
{
	ImagePath imagePath = ImagePath::builtin("DialogBoxBackground_" + player.toString() + ".bmp");

	texture = GH.renderHandler().loadImage(imagePath, EImageBlitMode::COLORKEY);
}

CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
	frame(Frame),
	group(Group),
	flags(Flags)
{
	pos.x += x;
	pos.y += y;
	anim = GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY);
	init();
}

CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
	anim(GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY)),
	frame(Frame),
	group(Group),
	flags(Flags),
	scaledSize(targetPos.w, targetPos.h)
{
	pos.x += targetPos.x;
	pos.y += targetPos.y;
	init();
}

size_t CAnimImage::size()
{
	return anim->size(group);
}

bool CAnimImage::isScaled() const
{
	return (scaledSize.x != 0);
}

void CAnimImage::setSizeFromImage(const IImage &img)
{
	if (isScaled())
	{
		// At the time of writing this, IImage had no method to scale to different aspect ratio
		// Therefore, have to ignore the target height and preserve original aspect ratio
		pos.w = scaledSize.x;
		pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
	}
	else
	{
		pos.w = img.width();
		pos.h = img.height();
	}
}

void CAnimImage::init()
{
	visible = true;
	auto img = anim->getImage(frame, group);
	if (img)
		setSizeFromImage(*img);
}

CAnimImage::~CAnimImage()
{
}

void CAnimImage::showAll(Canvas & to)
{
	if(!visible)
		return;

	std::vector<size_t> frames = {frame};

	if((flags & CShowableAnim::BASE) && frame != 0)
	{
		frames.insert(frames.begin(), 0);
	}

	for(auto targetFrame : frames)
	{
		if(auto img = anim->getImage(targetFrame, group))
		{
			if(isScaled())
				img->scaleTo(scaledSize, EScalingAlgorithm::BILINEAR);

			to.draw(img, pos.topLeft());
		}
	}
}

void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
{
	this->frame = frame;
	anim = GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
	init();
}

void CAnimImage::setScale(Point scale)
{
	scaledSize = scale;
}

void CAnimImage::setFrame(size_t Frame, size_t Group)
{
	if (frame == Frame && group==Group)
		return;
	if (anim->size(Group) > Frame)
	{
		frame = Frame;
		group = Group;
		if(auto img = anim->getImage(frame, group))
		{
			if (player.has_value())
				img->playerColored(*player);
			setSizeFromImage(*img);
		}
	}
	else
		logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
}

void CAnimImage::setPlayerColor(PlayerColor currPlayer)
{
	player = currPlayer;
	anim->getImage(frame, group)->playerColored(*player);
	if (flags & CShowableAnim::BASE)
			anim->getImage(0, group)->playerColored(*player);
}

bool CAnimImage::isPlayerColored() const
{
	return player.has_value();
}

CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
	anim(GH.renderHandler().loadAnimation(name, (Flags & CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_OVERLAY : EImageBlitMode::COLORKEY)),
	group(Group),
	frame(0),
	first(0),
	frameTimeTotal(frameTime),
	frameTimePassed(0),
	flags(Flags),
	xOffset(0),
	yOffset(0),
	alpha(alpha)
{
	last = anim->size(group);

	auto image = anim->getImage(0, group);
	if (!image)
		throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());

	pos.w = image->width();
	pos.h = image->height();
	pos.x+= x;
	pos.y+= y;

	addUsedEvents(TIME);
}

void CShowableAnim::setAlpha(ui32 alphaValue)
{
	alpha = std::min<ui32>(alphaValue, 255);
}

bool CShowableAnim::set(size_t Group, size_t from, size_t to)
{
	size_t max = anim->size(Group);

	if (to < max)
		max = to;

	if (max < from || max == 0)
		return false;

	group = Group;
	frame = first = from;
	last = max;
	frameTimePassed = 0;
	return true;
}

bool CShowableAnim::set(size_t Group)
{
	if (anim->size(Group)== 0)
		return false;
	if (group != Group)
	{
		first = 0;
		group = Group;
		last = anim->size(Group);
	}
	frame = 0;
	frameTimePassed = 0;
	return true;
}

void CShowableAnim::reset()
{
	frame = first;

	if (callback)
		callback();
}

void CShowableAnim::clipRect(int posX, int posY, int width, int height)
{
	xOffset = posX;
	yOffset = posY;
	pos.w = width;
	pos.h = height;
}

void CShowableAnim::show(Canvas & to)
{
	if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
		blitImage(first, group, to);
	blitImage(frame, group, to);
}

void CShowableAnim::tick(uint32_t msPassed)
{
	if ((flags & PLAY_ONCE) && frame + 1 == last)
		return;

	frameTimePassed += msPassed;

	if(frameTimePassed >= frameTimeTotal)
	{
		frameTimePassed -= frameTimeTotal;
		if ( ++frame >= last)
			reset();
	}
}

void CShowableAnim::showAll(Canvas & to)
{
	if ( flags & BASE )// && frame != first)
		blitImage(first, group, to);
	blitImage(frame, group, to);
}

void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
{
	Rect src( xOffset, yOffset, pos.w, pos.h);
	auto img = anim->getImage(frame, group);
	if(img)
	{
		img->setAlpha(alpha);
		img->setOverlayColor(Colors::TRANSPARENCY);
		to.draw(img, pos.topLeft(), src);
	}
}

void CShowableAnim::rotate(bool on, bool vertical)
{
	ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
	if (on)
		flags |= flag;
	else
		flags &= ~flag;
}

void CShowableAnim::setDuration(int durationMs)
{
	frameTimeTotal = durationMs/(last - first);
}

CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
	CShowableAnim(x, y, name, flags | CREATURE_MODE, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
{
	xOffset = 0;
	yOffset = 0;
}

void CCreatureAnim::loopPreview(bool warMachine)
{
	std::vector<ECreatureAnimType> available;

	static const ECreatureAnimType creaPreviewList[] = {
		ECreatureAnimType::HOLDING,
		ECreatureAnimType::HITTED,
		ECreatureAnimType::DEFENCE,
		ECreatureAnimType::ATTACK_FRONT,
		ECreatureAnimType::SPECIAL_FRONT
	};
	static const ECreatureAnimType machPreviewList[] = {
		ECreatureAnimType::HOLDING,
		ECreatureAnimType::MOVING,
		ECreatureAnimType::SHOOT_UP,
		ECreatureAnimType::SHOOT_FRONT,
		ECreatureAnimType::SHOOT_DOWN
	};

	auto & previewList = warMachine ? machPreviewList : creaPreviewList;

	for (auto & elem : previewList)
		if (anim->size(size_t(elem)))
			available.push_back(elem);

	size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);

	if (rnd >= available.size())
	{
		ECreatureAnimType type;
		if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
			type = ECreatureAnimType::HOLDING;
		else
			type = ECreatureAnimType::MOVING;

		//display this anim for ~1 second (time is random, but it looks good)
		for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
			addLast(type);
	}
	else
		addLast(available[rnd]);
}

void CCreatureAnim::addLast(ECreatureAnimType newType)
{
	auto currType = ECreatureAnimType(group);

	if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
	{
		queue.push( ECreatureAnimType::MOVE_START );
	}
	else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
	{
		queue.push( ECreatureAnimType::MOVE_END );
	}
	if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
		queue.push(newType);

	queue.push(newType);
}

void CCreatureAnim::reset()
{
	//if we are in the middle of rotation - set flag
	if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
		rotate(true);
	if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
		rotate(false);

	while (!queue.empty())
	{
		ECreatureAnimType at = queue.front();
		queue.pop();
		if (set(size_t(at)))
			return;
	}
	if  (callback)
		callback();
	while (!queue.empty())
	{
		ECreatureAnimType at = queue.front();
		queue.pop();
		if (set(size_t(at)))
			return;
	}
	set(size_t(ECreatureAnimType::HOLDING));
}

void CCreatureAnim::startPreview(bool warMachine)
{
	callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
}

void CCreatureAnim::clearAndSet(ECreatureAnimType type)
{
	while (!queue.empty())
		queue.pop();
	set(size_t(type));
}