-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameResources.cpp
More file actions
65 lines (52 loc) · 1.93 KB
/
GameResources.cpp
File metadata and controls
65 lines (52 loc) · 1.93 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
55
56
57
58
59
60
61
62
63
64
65
// =================
// GAMERESOURCES.CPP
// implements atlas dictionary parsing and initializes the singleton instance
// =================
#include "header/GameResources.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <unordered_map>
#include <string>
using namespace sf;
using namespace std;
GameResources* GameResources::myInstance = nullptr;
unordered_map<string, IntRect>* GameResources::createDictionary(const string& atlasPath) {
auto* newAtlas = new unordered_map<string, IntRect>();
ifstream file(atlasPath);
if (!file.is_open()) {
cerr << "[GameResources] Could not open atlas file: " << atlasPath << endl;
return newAtlas;
}
string line;
string currentKey = "";
while (getline(file, line)) {
// strip carriage return from windows line endings
if (!line.empty() && line.back() == '\r') line.pop_back();
if (line.empty() || line[0] == '#') continue;
// lines without ':' are treated as sprite names
if (line.find(':') == string::npos) {
currentKey = line;
continue;
}
string prop = line.substr(0, line.find(':'));
string val = line.substr(line.find(':') + 1);
// trim leading whitespace
while (!prop.empty() && prop[0] == ' ') prop.erase(0, 1);
while (!val.empty() && val[0] == ' ') val.erase(0, 1);
if (prop == "bounds" && !currentKey.empty()) {
int x, y, w, h;
char comma;
istringstream ss(val);
if (ss >> x >> comma >> y >> comma >> w >> comma >> h) {
(*newAtlas)[currentKey] = IntRect({ x, y }, { w, h });
}
else {
cerr << "[GameResources] Bad bounds for key: " << currentKey << endl;
}
}
}
file.close();
cerr << "[GameResources] Parsed " << newAtlas->size() << " entries from " << atlasPath << endl;
return newAtlas;
}