aboutsummaryrefslogtreecommitdiff
path: root/source/Sprite.cpp
blob: cb8d32728bbb4930acdd07680ea2b7f22b4fcf84 (plain)
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
#include "Sprite.hpp"

#include "Image.hpp"
#include "RapidJsonHelper.hpp"

#include <rapidjson/document.h>
#include <memory>

using namespace std::literals;

bool Sprite::IsValid() const {
	return mAtlas != nullptr;
}

bool IresSpriteFiles::IsValid() const {
	return !spriteFiles.empty();
}

Sprite* IresSpriteFiles::CreateInstance() const {
	if (IsValid()) {
		return nullptr;
	}

	std::vector<Texture::AtlasSource> sources;
	sources.resize(spriteFiles.size());
	for (auto& file : spriteFiles) {
	}

	Texture::AtlasOutput atlasOut;
	Texture::AtlasInput atlasIn{
		.sources = sources,
		.packingMode = Texture::PM_KeepSquare,
	};
	atlasIn.sources = sources;
	auto atlas = std::make_unique<Texture>();
	if (atlas->InitAtlas(atlasIn, &atlasOut) != Texture::EC_Success) {
		return nullptr;
	}

	auto sprite = std::make_unique<Sprite>();
	sprite->mAtlas.Attach(atlas.release());
	sprite->mBoundingBox = atlasOut.elements[0].subregionSize;
	sprite->mFrames.reserve(atlasOut.elements.size());
	for (auto& elm : atlasOut.elements) {
		// Validate bounding box
		if (sprite->mBoundingBox != elm.subregionSize) {
			return nullptr;
		}

		// Copy frame subregion
		sprite->mFrames.push_back(elm.subregion);
	}
	return sprite.release();
}

Sprite* IresSpriteFiles::GetInstance() {
	if (mInstance == nullptr) {
		mInstance.Attach(CreateInstance());
	}
	return mInstance.Get();
}

void IresSpriteFiles::InvalidateInstance() {
	mInstance.Attach(nullptr);
}

void IresSpriteFiles::Write(rapidjson::Value& value, rapidjson::Document& root) const {
	value.AddMember("Sprites", rapidjson::WriteVectorPrimitives(root, spriteFiles.begin(), spriteFiles.end()), root.GetAllocator());
}

void IresSpriteFiles::Read(const rapidjson::Value& value) {
	auto rvFileList = rapidjson::GetProperty(value, rapidjson::kArrayType, "Sprites"sv);
	if (!rvFileList) return;
	spriteFiles.clear();
	rapidjson::ReadVectorPrimitives<decltype(spriteFiles)>(*rvFileList, spriteFiles);
}

bool IresSpritesheet::IsValid() const {
	return !spritesheetFile.empty() &&
		sheetWSplit != 0 &&
		sheetHSplit != 0;
}

void IresSpritesheet::ResplitSpritesheet(Sprite* sprite, int wSplit, int hSplit) {
	auto atlas = sprite->GetAtlas();
	auto size = atlas->GetInfo().size;
	int frameWidth = size.x / wSplit;
	int frameHeight = size.y / hSplit;

	sprite->mBoundingBox = { frameWidth, frameHeight };
	sprite->mFrames.clear();
	sprite->mFrames.reserve(wSplit * hSplit);

	// Width and height in UV coordinates for each frame
	float deltaU = 1.0f / wSplit;
	float deltaV = 1.0f / hSplit;
	for (int y = 0; y < hSplit; ++y) {
		for (int x = 0; x < wSplit; ++x) {
			auto& subregion = sprite->mFrames.emplace_back();
			// Top left
			subregion.u0 = deltaU * x;
			subregion.v0 = deltaV * y;
			// Bottom right
			subregion.u1 = subregion.u0 + deltaU;
			subregion.u1 = subregion.v0 + deltaV;
		}
	}
}

Sprite* IresSpritesheet::CreateInstance() const {
	if (!IsValid()) {
		return nullptr;
	}

	auto atlas = std::make_unique<Texture>();
	if (!atlas->InitFromFile(spritesheetFile.c_str())) {
		return nullptr;
	}

	auto sprite = std::make_unique<Sprite>();
	sprite->mAtlas.Attach(atlas.release());
	ResplitSpritesheet(sprite.get(), sheetWSplit, sheetHSplit);
	return sprite.release();
}

Sprite* IresSpritesheet::GetInstance() {
	if (mInstance == nullptr) {
		mInstance.Attach(CreateInstance());
	}
	return mInstance.Get();
}

void IresSpritesheet::InvalidateInstance() {
	mInstance.Attach(nullptr);
}

void IresSpritesheet::Write(rapidjson::Value& value, rapidjson::Document& root) const {
	value.AddMember("SpriteSheet", spritesheetFile, root.GetAllocator());
	value.AddMember("WSplit", sheetWSplit, root.GetAllocator());
	value.AddMember("HSplit", sheetHSplit, root.GetAllocator());
}

void IresSpritesheet::Read(const rapidjson::Value& value) {
	BRUSSEL_JSON_GET(value, "SpriteSheet", std::string, spritesheetFile, return );
	BRUSSEL_JSON_GET(value, "WSplit", int, sheetWSplit, return );
	BRUSSEL_JSON_GET(value, "HSplit", int, sheetHSplit, return );
}

SpriteMesh::SpriteMesh(Sprite* sprite)
	: mSprite(sprite) {
}

void SpriteMesh::SetFrame(int frame) {
	// TODO
}

void SpriteMesh::PlayFrame() {
	// TODO
}

void SpriteMesh::SetPlaybackSpeed(int speed) {
	// TODO
}