File: Animation.h

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 (96 lines) | stat: -rw-r--r-- 2,796 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
/*
 * Animation.h, 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
 *
 */
#pragma once
//code is copied from vcmiclient/CAnimation.h with minimal changes

#include "../lib/JsonNode.h"
#include "../lib/GameConstants.h"
#include <QRgb>
#include <QImage>

/*
 * Base class for images, can be used for non-animation pictures as well
 */

class DefFile;
/// Class for handling animation
class Animation
{
private:
	//source[group][position] - file with this frame, if string is empty - image located in def file
	std::map<size_t, std::vector<JsonNode>> source;

	//bitmap[group][position], store objects with loaded bitmaps
	std::map<size_t, std::map<size_t, std::shared_ptr<QImage> > > images;

	//animation file name
	std::string name;

	bool preloaded;

	std::shared_ptr<DefFile> defFile;

	//loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
	bool loadFrame(size_t frame, size_t group);

	//unloadFrame, returns true if image has been unloaded ( either deleted or decreased refCount)
	bool unloadFrame(size_t frame, size_t group);

	//initialize animation from file
	//void initFromJson(const JsonNode & input);
	void init();

	//to get rid of copy-pasting error message :]
	void printError(size_t frame, size_t group, std::string type) const;

	//not a very nice method to get image from another def file
	//TODO: remove after implementing resource manager
	std::shared_ptr<QImage> getFromExtraDef(std::string filename);

public:
	Animation(std::string Name);
	Animation();
	~Animation();

	//duplicates frame at [sourceGroup, sourceFrame] as last frame in targetGroup
	//and loads it if animation is preloaded
	void duplicateImage(size_t sourceGroup, size_t sourceFrame, size_t targetGroup);

	// adjust the color of the animation, used in battle spell effects, e.g. Cloned objects

	//add custom surface to the selected position.
	void setCustom(std::string filename, size_t frame, size_t group = 0);

	std::shared_ptr<QImage> getImage(size_t frame, size_t group = 0, bool verbose = true) const;

	//all available frames
	void load();
	void unload();
	void preload();

	//all frames from group
	void loadGroup  (size_t group);
	void unloadGroup(size_t group);

	//single image
	void load  (size_t frame, size_t group = 0);
	void unload(size_t frame, size_t group = 0);

	void exportBitmaps(const QDir & path) const;

	//total count of frames in group (including not loaded)
	size_t size(size_t group = 0) const;

	void horizontalFlip();
	void verticalFlip();
	void playerColored(PlayerColor player);

	void createFlippedGroup(const size_t sourceGroup, const size_t targetGroup);
};