-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaManagerUnit.cpp
More file actions
100 lines (84 loc) · 2.99 KB
/
Copy pathMediaManagerUnit.cpp
File metadata and controls
100 lines (84 loc) · 2.99 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 "MediaManagerUnit.hpp"
#include "Logger.hpp"
TGenericMediaManager::TGenericMediaManager() {}
TGenericMediaManager::~TGenericMediaManager() {}
TFileMediaManager::TFileMediaManager() {}
TTexture* TFileMediaManager::LoadTexture(const std::string& name, const std::string& path) {
auto tex = std::make_unique<TTexture>();
if (tex->LoadFrom(path)) {
tex->name = name;
textures[name] = std::move(tex);
return textures[name].get();
}
return nullptr;
}
TMesh* TFileMediaManager::LoadMesh(const std::string& name, const std::string& path) {
auto mesh = std::make_unique<TMesh>();
if (mesh->LoadFromFile(path)) {
meshes[name] = std::move(mesh);
return meshes[name].get();
}
return nullptr;
}
TSound* TFileMediaManager::LoadSound(const std::string& name, const std::string& path) {
auto sound = std::make_unique<TSound>(path);
sound->SetName(name);
sounds[name] = std::move(sound);
return sounds[name].get();
}
TModel* TFileMediaManager::LoadModel(const std::string& name, const std::string& path) {
auto model = std::make_unique<TModel>();
if (model->load(path, this)) {
models[name] = std::move(model);
return models[name].get();
}
return nullptr;
}
TMesh* TFileMediaManager::getMeshByName(const std::string& name) {
auto it = meshes.find(name);
if (it != meshes.end()) return it->second.get();
TMesh* m = LoadMesh(name, "media/" + name + ".mesh");
if (m) return m;
// Fallback: create empty mesh to prevent crashes and repeat loading attempts
auto empty = std::make_unique<TMesh>();
TMesh* ptr = empty.get();
meshes[name] = std::move(empty);
return ptr;
}
TTexture* TFileMediaManager::getTextureByName(const std::string& name) {
auto it = textures.find(name);
if (it != textures.end()) return it->second.get();
return LoadTexture(name, "media/" + name + ".png");
}
TSound* TFileMediaManager::getSoundByName(const std::string& name) {
auto it = sounds.find(name);
if (it != sounds.end()) return it->second.get();
return LoadSound(name, "media/" + name + ".wav");
}
TModel* TFileMediaManager::getModelByName(const std::string& name) {
auto it = models.find(name);
if (it != models.end()) return it->second.get();
TModel* m = LoadModel(name, "media/" + name + ".model");
if (m) return m;
// Fallback: create empty model to prevent crashes
auto empty = std::make_unique<TModel>();
TModel* ptr = empty.get();
models[name] = std::move(empty);
return ptr;
}
void TFileMediaManager::LoadGameAssets() {
// Core UI textures should be pre-loaded
getTextureByName("font");
getTextureByName("numbers");
getTextureByName("heart");
getTextureByName("point");
getTextureByName("tile1");
getTextureByName("loading");
getTextureByName("title");
getTextureByName("gameover");
getTextureByName("intro");
// Common sounds
getSoundByName("x2");
getSoundByName("hit0");
getSoundByName("swhoosh0");
}