-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
54 lines (46 loc) · 1.63 KB
/
scene.cpp
File metadata and controls
54 lines (46 loc) · 1.63 KB
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
#include "include/json.hpp"
#include "debug.h"
#include "scene.h"
using json = nlohmann::json;
bool Scene::LoadFromResources(std::shared_ptr<ResourcesManager> resourcesManager)
{
std::vector<uint8_t> sceneUnparsed = resourcesManager->GetResource("/res/scene.json");
json data;
try {
data = json::parse(sceneUnparsed.begin(), sceneUnparsed.end());
} catch (const json::parse_error& e) {
Debug::WriteLn(e.what());
return false;
}
try {
for (int i = 0; i < data["materials"].size(); i++) {
materials.push_back(std::shared_ptr<Material> (new Material));
std::vector<uint8_t> binaryTexture = resourcesManager->GetResource(data["materials"][i]["texture"]);
materials[i]->texture.loadFromMemory(binaryTexture.data(), binaryTexture.size());
// materials[i]->texture.copyToImage().saveToFile("sosi.bmp");
// exit(10);
}
for (int i = 0; i < data["cells"].size(); i++) {
cells.push_back(std::shared_ptr<Cell>(new Cell));
cells[i]->wallMaterials = materials[data["cells"][i]["wallMaterials"] - 1];
}
for (int y = 0; y < data["map"].size(); y++) {
map.push_back(std::vector< std::shared_ptr<Cell> >(data["map"][y].size()));
for (int x = 0; x < data["map"][y].size(); x++) {
map[y][x] = (data["map"][y][x] == 0) ? nullptr : cells[data["map"][y][x] - 1];
}
}
} catch (const json::exception& e) {
Debug::WriteLn(e.what());
return false;
}
return true;
}
std::shared_ptr<Cell> Scene::GetCell(sf::Vector2i coord)
{
return map[coord.y][coord.x];
}
sf::Vector2i Scene::GetSize()
{
return sf::Vector2i(map[0].size(), map.size());
}