forked from vittorioromeo/SSVOpenHexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assets.cpp
155 lines (139 loc) · 4.45 KB
/
Assets.cpp
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
#include <iostream>
#include <string>
#include <json/json.h>
#include <json/reader.h>
#include <fstream>
#include <map>
#include "boost/filesystem.hpp"
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "Assets.h"
#include "MusicData.h"
#include "LevelData.h"
#include "StyleData.h"
#include "Utils.h"
using namespace std;
using namespace sf;
using namespace boost::filesystem;
namespace hg
{
map<string, Font> fontsMap;
map<string, SoundBuffer*> soundBufferPtrsMap;
map<string, Sound*> soundPtrsMap;
map<string, Music*> musicPtrsMap;
map<string, MusicData> musicDataMap;
map<string, StyleData> styleDataMap;
map<string, LevelData> levelDataMap;
Json::Value scoreRoot;
void loadAssets()
{
log("loading fonts"); loadFonts();
log("loading sounds"); loadSounds();
log("loading music"); loadMusic();
log("loading music data"); loadMusicData();
log("loading style data"); loadStyleData();
log("loading level data"); loadLevelData();
log("loading scores"); loadScores();
}
void loadFonts()
{
for(auto filePath : getAllFilePaths("Fonts/", ".ttf"))
{
string fileName = path(filePath).stem().string();
Font font;
font.loadFromFile(filePath);
fontsMap.insert(make_pair(fileName, font));
}
}
void loadSounds()
{
Json::Value soundsRoot = getJsonFileRoot("Sounds/sounds.json");
for(Json::ValueIterator itr{soundsRoot.begin()}; itr != soundsRoot.end(); itr++)
{
SoundBuffer* soundBuffer{new SoundBuffer};
soundBuffer->loadFromFile("Sounds/" + (*itr).asString());
soundBufferPtrsMap.insert(make_pair(itr.key().asString(), soundBuffer));
Sound* soundPtr{new Sound};
soundPtr->setBuffer(*soundBuffer);
soundPtr->setVolume(getSoundVolume());
soundPtrsMap.insert(make_pair(itr.key().asString(), soundPtr));
}
}
void loadMusic()
{
for(auto filePath : getAllFilePaths("Music/", ".ogg"))
{
string fileName = path(filePath).stem().string();
Music* music{new Music};
music->openFromFile(filePath);
music->setVolume(getMusicVolume());
musicPtrsMap.insert(make_pair(fileName, music));
}
}
void loadMusicData()
{
for(auto filePath : getAllFilePaths("Music/", ".json"))
{
MusicData musicData{loadMusicFromJson(getJsonFileRoot(filePath))};
musicDataMap.insert(make_pair(musicData.getId(), musicData));
}
}
void loadStyleData()
{
for(auto filePath : getAllFilePaths("Styles/", ".json"))
{
StyleData styleData{loadStyleFromJson(getJsonFileRoot(filePath))};
styleDataMap.insert(make_pair(styleData.getId(), styleData));
}
}
void loadLevelData()
{
for(auto filePath : getAllFilePaths("Levels/", ".json"))
{
LevelData levelData{loadLevelFromJson(getJsonFileRoot(filePath))};
levelDataMap.insert(make_pair(levelData.getId(), levelData)); // replace with getId
}
}
void loadScores() { scoreRoot = getJsonFileRoot("scores.json"); }
void saveScores()
{
Json::StyledStreamWriter writer;
ofstream test("scores.json", std::ifstream::binary);
writer.write(test, scoreRoot);
}
void stopAllMusic() { for(auto pair : musicPtrsMap) pair.second->stop(); }
void stopAllSounds() { for(auto pair : soundPtrsMap) pair.second->stop(); }
void playSound(string mId) { if(!getNoSound()) getSoundPtr(mId)->play(); }
Font& getFont(string mId) { return fontsMap.find(mId)->second; }
Sound* getSoundPtr(string mId) { return soundPtrsMap.find(mId)->second; }
Music* getMusicPtr(string mId) { return musicPtrsMap.find(mId)->second; }
MusicData getMusicData(string mId) { return musicDataMap.find(mId)->second; }
StyleData getStyleData(string mId) { return styleDataMap.find(mId)->second; }
LevelData getLevelData(string mId) { return levelDataMap.find(mId)->second; }
vector<LevelData> getAllLevelData()
{
vector<LevelData> result;
for(auto pair : levelDataMap) result.push_back(pair.second);
return result;
}
vector<string> getAllLevelDataIds()
{
vector<LevelData> levelDataVector{getAllLevelData()};
sort(begin(levelDataVector), end(levelDataVector),
[](LevelData a, LevelData b) -> bool
{
return a.getMenuPriority() < b.getMenuPriority();
});
vector<string> result;
for(auto levelData : levelDataVector) result.push_back(levelData.getId());
return result;
}
float getScore(string mId)
{
return scoreRoot[mId].asFloat();
}
void setScore(string mId, float mScore)
{
scoreRoot[mId] = mScore;
}
}