summaryrefslogtreecommitdiffstats
path: root/src/core/AssetManager.cpp
blob: f4c080162552dea700e9aade73f3dfccec077e44 (plain) (blame)
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
#include <AssetManager.hpp>

namespace fs = std::experimental::filesystem;

//const fs::path pathToAssets = "./assets/";
//const fs::path pathToAssetsList = "./items.json";
//const fs::path pathToTextureIndex = "./textures.json";
const std::string pathToAssetsList = "./items.json";
const std::string pathToTextureIndex = "./textures.json";

AssetManager::AssetManager() {
	LoadIds();
	LoadTextureResources();
}

void AssetManager::LoadIds() {
	std::ifstream in(pathToAssetsList);
	nlohmann::json index;
	in >> index;
	for (auto &it:index) {
		int id = it["type"].get<int>();
		int state = it["meta"].get<int>();
		std::string blockName = it["text_type"].get<std::string>();
		assetIds[blockName] = Block(id, state);
	}
	LOG(INFO) << "Loaded " << assetIds.size() << " ids";
}

AssetManager::~AssetManager() {
	delete textureAtlas;
}

//TODO: This function must be replaced with runtime texture atlas generating
void AssetManager::LoadTextureResources() {
	std::ifstream in(pathToTextureIndex);
	nlohmann::json index;
	in >> index;
	std::string filename = index["meta"]["image"].get<std::string>();
	float textureWidth = index["meta"]["size"]["w"].get<int>();
	float textureHeight = index["meta"]["size"]["h"].get<int>();
	for (auto &it:index["frames"]) {
		auto frame = it["frame"];
		TextureCoordinates coord;
		coord.x = frame["x"].get<int>() / textureWidth;
		coord.y = frame["y"].get<int>() / textureHeight;
		coord.w = frame["w"].get<int>() / textureWidth;
		coord.h = frame["h"].get<int>() / textureHeight;
		std::string assetName = it["filename"].get<std::string>();
		assetName.insert(0, "minecraft/textures/");
		assetName.erase(assetName.length() - 4);
		assetTextures[assetName] = coord;
	}

	textureAtlas = new Texture(filename);
	LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
}

TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) {
	if (assetTextures.find(AssetName) != assetTextures.end())
		return assetTextures[AssetName];
	else
		return TextureCoordinates{-1, -1, -1, -1};
}

std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) {
	//Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
	const std::map<BlockTextureId, std::string> lookupTable = {
			{BlockTextureId(0, 0),     "minecraft/textures/blocks/air"},
			{BlockTextureId(1, 0),     "minecraft/textures/blocks/stone"},
			{BlockTextureId(1, 1),     "minecraft/textures/blocks/stone_granite"},

			{BlockTextureId(2, 0, 0),  "minecraft/textures/blocks/dirt"},
			{BlockTextureId(2, 0, 1),  "minecraft/textures/blocks/grass_top"},
			{BlockTextureId(2, 0, 2),  "minecraft/textures/blocks/grass_side"},
			{BlockTextureId(2, 0, 3),  "minecraft/textures/blocks/grass_side"},
			{BlockTextureId(2, 0, 4),  "minecraft/textures/blocks/grass_side"},
			{BlockTextureId(2, 0, 5),  "minecraft/textures/blocks/grass_side"},

			{BlockTextureId(3, 0),     "minecraft/textures/blocks/dirt"},
			{BlockTextureId(4, 0),     "minecraft/textures/blocks/cobblestone"},
			{BlockTextureId(5, 0),     "minecraft/textures/blocks/planks"},

			{BlockTextureId(7, 0),     "minecraft/textures/blocks/bedrock"},

			{BlockTextureId(17, 0, 0), "minecraft/textures/blocks/log_oak_top"},
			{BlockTextureId(17, 0, 1), "minecraft/textures/blocks/log_oak_top"},
			{BlockTextureId(17, 0, 2), "minecraft/textures/blocks/log_oak"},
			{BlockTextureId(17, 0, 3), "minecraft/textures/blocks/log_oak"},
			{BlockTextureId(17, 0, 4), "minecraft/textures/blocks/log_oak"},
			{BlockTextureId(17, 0, 5), "minecraft/textures/blocks/log_oak"},

			{BlockTextureId(17, 1, 0), "minecraft/textures/blocks/log_spruce_top"},
			{BlockTextureId(17, 1, 1), "minecraft/textures/blocks/log_spruce_top"},
			{BlockTextureId(17, 1, 2), "minecraft/textures/blocks/log_spruce"},
			{BlockTextureId(17, 1, 3), "minecraft/textures/blocks/log_spruce"},
			{BlockTextureId(17, 1, 4), "minecraft/textures/blocks/log_spruce"},
			{BlockTextureId(17, 1, 5), "minecraft/textures/blocks/log_spruce"},

			{BlockTextureId(17, 2, 0), "minecraft/textures/blocks/log_birch_top"},
			{BlockTextureId(17, 2, 1), "minecraft/textures/blocks/log_birch_top"},
			{BlockTextureId(17, 2, 2), "minecraft/textures/blocks/log_birch"},
			{BlockTextureId(17, 2, 3), "minecraft/textures/blocks/log_birch"},
			{BlockTextureId(17, 2, 4), "minecraft/textures/blocks/log_birch"},
			{BlockTextureId(17, 2, 5), "minecraft/textures/blocks/log_birch"},

			{BlockTextureId(17, 3, 0), "minecraft/textures/blocks/log_jungle_top"},
			{BlockTextureId(17, 3, 1), "minecraft/textures/blocks/log_jungle_top"},
			{BlockTextureId(17, 3, 2), "minecraft/textures/blocks/log_jungle"},
			{BlockTextureId(17, 3, 3), "minecraft/textures/blocks/log_jungle"},
			{BlockTextureId(17, 3, 4), "minecraft/textures/blocks/log_jungle"},
			{BlockTextureId(17, 3, 5), "minecraft/textures/blocks/log_jungle"},

			{BlockTextureId(18, 0),    "minecraft/textures/blocks/leaves_oak"},
			{BlockTextureId(18, 1),    "minecraft/textures/blocks/leaves_spruce"},
			{BlockTextureId(18, 2),    "minecraft/textures/blocks/leaves_birch"},
			{BlockTextureId(18, 3),    "minecraft/textures/blocks/leaves_jungle"},

			{BlockTextureId(61, 0, 0), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(61, 0, 1), "minecraft/textures/blocks/furnace_top"},
			{BlockTextureId(61, 0, 2), "minecraft/textures/blocks/furnace_front_off"},
			{BlockTextureId(61, 0, 3), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(61, 0, 4), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(61, 0, 5), "minecraft/textures/blocks/furnace_side"},

			{BlockTextureId(62, 0, 0), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(62, 0, 1), "minecraft/textures/blocks/furnace_top"},
			{BlockTextureId(62, 0, 2), "minecraft/textures/blocks/furnace_front_on"},
			{BlockTextureId(62, 0, 3), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(62, 0, 4), "minecraft/textures/blocks/furnace_side"},
			{BlockTextureId(62, 0, 5), "minecraft/textures/blocks/furnace_side"},


			{BlockTextureId(31, 0),    "minecraft/textures/blocks/deadbush"},
			{BlockTextureId(31, 1),    "minecraft/textures/blocks/tallgrass"},
			{BlockTextureId(31, 2),    "minecraft/textures/blocks/fern"},
	};
	auto ret = lookupTable.find(block);
	if (ret == lookupTable.end())
		return "";
	else
		return ret->second;
}

GLuint AssetManager::GetTextureAtlas() {
	return textureAtlas->texture;
}

TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) {
	std::string assetName = this->GetTextureAssetNameByBlockId(block);
	return this->GetTextureByAssetName(assetName);
}