-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetmanager.cpp
More file actions
100 lines (87 loc) · 3.22 KB
/
Copy pathassetmanager.cpp
File metadata and controls
100 lines (87 loc) · 3.22 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
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
#include "assetmanager.h"
#include <iostream>
#include <string>
#include "raylib.h"
#include "json.hpp"
#include "jsonparser.h"
#ifndef MAX_MATERIAL_MAPS
#define MAX_MATERIAL_MAPS 12
#endif
extern std::unordered_map<std::string, Texture> textures_buffer;
extern std::unordered_map<std::string, Sound> sounds_buffer;
extern std::unordered_map<std::string, Model> models_buffer;
extern std::unordered_map<std::string, Shader> shader_buffer;
extern std::unordered_map<std::string, Font> font_buffer;
void LoadAssets(nlohmann::json assets){
for(auto& a : assets){
if(a["asset_type"] == "sprite"){
textures_buffer[a["name"].get<std::string>()] = LoadTexture(a["path"].get<std::string>().c_str());
}else if(a["asset_type"] == "font"){
font_buffer[a["name"].get<std::string>()] = LoadFont(a["path"].get<std::string>().c_str());
} else if(a["asset_type"] == "model"){
models_buffer[a["name"].get<std::string>()] = LoadModel(a["path"].get<std::string>().c_str());
} else if(a["asset_type"] == "shader"){
shader_buffer[a["name"].get<std::string>()] = LoadShader(a["vspath"].get<std::string>().c_str(),a["fspath"].get<std::string>().c_str());
}else if(a["asset_type"] == "sound"){
sounds_buffer[a["name"].get<std::string>()] = LoadSound(a["path"].get<std::string>().c_str());
}
}
}
void UnloadAssets(){
for(auto& [name,model] : models_buffer){
for(int i = 0; i < model.materialCount; i++) {
for(int j = 0; j < MAX_MATERIAL_MAPS; j++) {
Texture2D tex = model.materials[i].maps[j].texture;
if(tex.id > 1) {
UnloadTexture(tex);
model.materials[i].maps[j].texture.id = 0;
}
}
}
UnloadModel(model);
}
for(auto& [name, tex] : textures_buffer) UnloadTexture(tex);
for(auto& [name, font] : font_buffer) UnloadFont(font);
for(auto& [name, shader] : shader_buffer) UnloadShader(shader);
for(auto& [name, sound] : sounds_buffer) UnloadSound(sound);
models_buffer.clear();
textures_buffer.clear();
font_buffer.clear();
shader_buffer.clear();
sounds_buffer.clear();
}
Texture& get_texture(std::string tex){
auto it = textures_buffer.find(tex);
if(it != textures_buffer.end()){
return it->second;
}
throw std::runtime_error("[ART] Texture was not found");
}
Font& get_font(std::string font){
auto it = font_buffer.find(font);
if(it != font_buffer.end()){
return it->second;
}
throw std::runtime_error("[ART] Font was not found");
}
Shader& get_shader(std::string shd){
auto it = shader_buffer.find(shd);
if(it != shader_buffer.end()){
return it->second;
}
throw std::runtime_error("[ART] Shader was not found");
}
Sound& get_sound(std::string snd){
auto it = sounds_buffer.find(snd);
if(it != sounds_buffer.end()){
return it->second;
}
throw std::runtime_error("[ART] Sound was not found");
}
Model& get_model(std::string mdl){
auto it = models_buffer.find(mdl);
if(it != models_buffer.end()){
return it->second;
}
throw std::runtime_error("[ART] Model was not found");
}