-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
161 lines (133 loc) · 7.18 KB
/
ResourceManager.cpp
File metadata and controls
161 lines (133 loc) · 7.18 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
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
156
157
158
159
160
161
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2016 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "ResourceManager.h"
#include <iostream>
#include <cstdlib>
// Static member initialization
std::unordered_map<std::string, std::string> ResourceManager::mResourcePaths;
std::string ResourceManager::mResourceBaseDir;
bool ResourceManager::mIsInitialized = false;
// Initialize resource paths
void ResourceManager::initialize() {
if (mIsInitialized) return;
// Check for environment variable override first
const char* envPath = std::getenv("RP3D_RESOURCE_PATH");
if (envPath != nullptr) {
mResourceBaseDir = std::string(envPath);
std::cout << "ResourceManager: Using environment path: " << mResourceBaseDir << std::endl;
} else {
// Dynamic discovery starting from executable directory
std::filesystem::path currentPath = std::filesystem::current_path();
// Search for testbed directory structure
bool found = false;
std::filesystem::path searchPath = currentPath;
for (int depth = 0; depth <= MAX_SEARCH_DEPTH && !found; ++depth) {
// Look for testbed/shaders and testbed/meshes directories
std::filesystem::path testbedPath = searchPath / "testbed";
std::filesystem::path shadersPath = testbedPath / "shaders";
std::filesystem::path meshesPath = testbedPath / "meshes";
if (std::filesystem::exists(shadersPath) && std::filesystem::exists(meshesPath)) {
mResourceBaseDir = testbedPath.string();
found = true;
std::cout << "ResourceManager: Found resources at: " << mResourceBaseDir << std::endl;
} else {
// Try direct shaders/meshes in current search path
shadersPath = searchPath / "shaders";
meshesPath = searchPath / "meshes";
if (std::filesystem::exists(shadersPath) && std::filesystem::exists(meshesPath)) {
mResourceBaseDir = searchPath.string();
found = true;
std::cout << "ResourceManager: Found resources at: " << mResourceBaseDir << std::endl;
}
}
// Move up one directory level
if (!found && searchPath.has_parent_path()) {
searchPath = searchPath.parent_path();
}
}
if (!found) {
// Fallback to current directory (maintains backwards compatibility)
mResourceBaseDir = currentPath.string();
std::cout << "ResourceManager: Warning - Could not locate resource directories. "
<< "Using current directory: " << mResourceBaseDir << std::endl;
std::cout << "ResourceManager: You can set RP3D_RESOURCE_PATH environment variable "
<< "to specify the resource directory manually." << std::endl;
}
}
// Cache common directory paths
std::filesystem::path basePath(mResourceBaseDir);
mResourcePaths["shaders"] = (basePath / "shaders").string();
mResourcePaths["meshes"] = (basePath / "meshes").string();
mIsInitialized = true;
}
// Get shader file path
std::string ResourceManager::getShaderPath(const std::string& shaderFilename) {
initialize();
std::filesystem::path shaderPath = std::filesystem::path(mResourcePaths["shaders"]) / shaderFilename;
std::string fullPath = shaderPath.string();
// Check if file exists, fallback to relative path for backwards compatibility
if (!std::filesystem::exists(fullPath)) {
std::cout << "ResourceManager: Warning - Shader not found at: " << fullPath
<< ". Falling back to relative path." << std::endl;
return "shaders/" + shaderFilename;
}
return fullPath;
}
// Get mesh file path
std::string ResourceManager::getMeshPath(const std::string& meshFilename) {
initialize();
std::filesystem::path meshPath = std::filesystem::path(mResourcePaths["meshes"]) / meshFilename;
std::string fullPath = meshPath.string();
// Check if file exists, fallback to relative path for backwards compatibility
if (!std::filesystem::exists(fullPath)) {
std::cout << "ResourceManager: Warning - Mesh not found at: " << fullPath
<< ". Falling back to relative path." << std::endl;
return "meshes/" + meshFilename;
}
return fullPath;
}
// Get mesh directory path
std::string ResourceManager::getMeshDirectoryPath() {
initialize();
std::string meshDir = mResourcePaths["meshes"] + "/";
// Check if directory exists, fallback for backwards compatibility
if (!std::filesystem::exists(mResourcePaths["meshes"])) {
std::cout << "ResourceManager: Warning - Mesh directory not found at: " << mResourcePaths["meshes"]
<< ". Falling back to relative path." << std::endl;
return "meshes/";
}
return meshDir;
}
// Check if file exists
bool ResourceManager::fileExists(const std::string& filepath) {
return std::filesystem::exists(filepath);
}
// Set resource base directory manually
void ResourceManager::setResourceBaseDirectory(const std::string& baseDir) {
mResourceBaseDir = baseDir;
mIsInitialized = false; // Force re-initialization
initialize();
}