-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.cpp
More file actions
75 lines (66 loc) · 2.02 KB
/
Copy pathassets.cpp
File metadata and controls
75 lines (66 loc) · 2.02 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
66
67
68
69
70
71
72
73
74
75
#include "assets.h"
AssetsManager::AssetsManager() : nAssets(0)
{
cfg = SingletonConfig::config();
}
int AssetsManager::Initialize(SDL_Renderer * r)
{
aLib = new Json::Value;
std::ifstream file((*cfg)["Assets"]["List"].asCString(), std::ifstream::in);
file >> *aLib;
file.close();
ren = r;
return 0;
}
void AssetsManager::__Load(AssetData *a)
{
if (a->IsLoaded) return;
if ((*a->json)["Surface"].asBool()) {
a->surf = IMG_Load((*a->json)["File"].asCString());
if (a->surf == NULL) { LogSDLError(std::cout, "IMG_Load"); }
else { a->IsLoaded = true; }
}
if ((*a->json)["Texture"].asBool()) {
a->tex = IMG_LoadTexture(ren,(*a->json)["File"].asCString());
if (a->tex == NULL) { LogSDLError(std::cout, "IMG_LoadTexture"); }
else { a->IsLoaded = true; }
}
}
void AssetsManager::PreLoadAssets()
{
for (Json::ValueIterator it1 = aLib->begin(); it1 != aLib->end(); it1++) {
for (Json::ValueIterator it2 = it1->begin(); it2 != it1->end(); it2++) {
nAssets++;
}
}
assets = new AssetsMap(nAssets);
AssetData * load;
for (Json::ValueIterator it1 = aLib->begin(); it1 != aLib->end(); it1++) {
for (Json::ValueIterator it2 = it1->begin(); it2 != it1->end(); it2++) {
load = new AssetData;
load->Initialize(it2->asCString());
assets->emplace((*load->json)["Name"].asCString(), load);
if ((*load->json)["LoadOnStartup"].asBool()) { __Load(load); }
}
}
SDL_SetCursor(SDL_CreateColorCursor(assets->at("DefaultCursor")->surf,0,0));
std::cout << "Loaded " << nAssets << " assets.\n";
}
AssetData * AssetsManager::LoadAsset(std::string name)
{
try {
if (!assets->at(name)->IsLoaded) { __Load(assets->at(name)); }
return assets->at(name);
} catch (const std::out_of_range &e) {
LogError(std::cout, e.what());
}
return NULL;
}
void AssetsManager::UnloadAssets()
{
for (AssetsMap::iterator it = assets->begin(); it != assets->end(); it++) {
if ((*it->second->json)["Unload"].asBool() && it->second->IsLoaded) {
it->second->Unload();
}
}
}