Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::Utility Class Referencefinal

Provides static helper methods for file I/O and path management. More...

#include <utility.hpp>

Static Public Member Functions

static void InitializePaths (int argc, char *argv[])
 Initializes engine paths based on the executable location.
static AppConfig ReadConfigFile ()
 Reads application settings from the JSON config file.
static std::vector< char > ReadShaderBinaryFile (std::string const &filename)
 Reads a binary shader file from disk.
static std::vector< std::string > GetListOfFilesInDirectory (std::string directory)
 Returns a list of full file paths in the given directory.
static boost::filesystem::path GetApplicationPath ()
 Returns the absolute path of the running application.
static boost::filesystem::path GetBuildPath ()
 Returns the build output directory path.
static boost::filesystem::path GetShadersBinaryPath ()
 Returns the directory path containing compiled shader binaries.
static std::vector< std::string > GetListOfFileNamesInDirectory (const char *directory, std::string extToSearch)
 Returns a list of file names in a directory matching the specified extension.
static boost::filesystem::path ResolveProjectRoot ()
 Resolves project root folder (handles Release/Debug/Binaries layouts).
static boost::filesystem::path GetTextureFolderPath ()
 Returns absolute path to Content/Textures.
static boost::filesystem::path GetModelsFolderPath ()
 Returns absolute path to Content/Models.
static boost::filesystem::path GetShadersFolderPath ()
 Returns absolute path to Content/Shaders.
static boost::filesystem::path GetConfigFilePath ()
 Returns absolute path to Config/app_config.json.
static bool IsPackageProvided ()
 Checks whether packed assets (Pack.bin / Pack.json) exist.
static const PackEntriesGetPackEntries ()
 Returns the manifest of packed files.
static std::vector< uint8_t > ReadPackedFile (const std::string &entryPath)
 Reads raw bytes of a file stored inside Pack.bin.

Static Public Attributes

static boost::filesystem::path sApplicationPath
static boost::filesystem::path const sDefaultShadersBinaryRelativePath = {"/Content/Shaders/"}
static boost::filesystem::path sBuildPath
static boost::filesystem::path sShadersBinaryPath
static boost::filesystem::path const sContentRelativePathFolder = path{} / "Content"
static boost::filesystem::path const sContentPackageFilePath = path{} / "Content" / "Pack.bin"
static boost::filesystem::path const sContentPackEntriesFilePath = path{} / "Content" / "Pack.json"
static boost::filesystem::path const sTextureRelativePathFolder = sContentRelativePathFolder / "Textures"
static boost::filesystem::path const sModelsRelativePathFolder = sContentRelativePathFolder / "Models"
static boost::filesystem::path const sShadersRelativePathFolder = sContentRelativePathFolder / "Shaders"
static boost::filesystem::path const sAppConfigFilePath = path{} / "Config" / "app_config.json"

Detailed Description

Provides static helper methods for file I/O and path management.

The Utility class centralizes filesystem operations such as locating shader binaries, listing files, and resolving build or application paths.

All methods are static and thread-safe, designed for use throughout the engine.

Definition at line 59 of file utility.hpp.

Member Function Documentation

◆ GetApplicationPath()

boost::filesystem::path rendering_engine::Utility::GetApplicationPath ( )
static

Returns the absolute path of the running application.

Returns
Application path as boost::filesystem::path.

Definition at line 120 of file utility.cpp.

121{
122 return sApplicationPath;
123}
static boost::filesystem::path sApplicationPath
Definition utility.hpp:156

◆ GetBuildPath()

boost::filesystem::path rendering_engine::Utility::GetBuildPath ( )
static

Returns the build output directory path.

Returns
Build path as boost::filesystem::path.

Definition at line 125 of file utility.cpp.

126{
127 return sBuildPath;
128}
static boost::filesystem::path sBuildPath
Definition utility.hpp:158

◆ GetConfigFilePath()

boost::filesystem::path rendering_engine::Utility::GetConfigFilePath ( )
static

Returns absolute path to Config/app_config.json.

Definition at line 207 of file utility.cpp.

208{
210}
static boost::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
Definition utility.cpp:182
static boost::filesystem::path const sAppConfigFilePath
Definition utility.hpp:166

◆ GetListOfFileNamesInDirectory()

std::vector< std::string > rendering_engine::Utility::GetListOfFileNamesInDirectory ( const char * directory,
std::string extToSearch )
static

Returns a list of file names in a directory matching the specified extension.

Parameters
directoryDirectory path.
extToSearchFile extension to match (e.g., ".spv").
Returns
Vector of matching file names.

Definition at line 149 of file utility.cpp.

150{
151 std::vector<std::string> imageFileNames;
152
153 try
154 {
155 //check if parameter string is directory
156 if( boost::filesystem::exists(boost::filesystem::path(directory)) && boost::filesystem::is_directory(boost::filesystem::path(directory)) )
157 {
158 boost::filesystem::path pathToDirectory = boost::filesystem::path(directory);
159
160 if( boost::filesystem::is_directory(pathToDirectory) )
161 {
162 for( boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(pathToDirectory) )
163 {
164 size_t dot = x.path().string().find_last_of(".");
165
166 if( extToSearch == x.path().string().substr(dot + 1) )
167 {
168 imageFileNames.push_back(x.path().string());
169 }
170 }
171 }
172 }
173 }
174 catch( const boost::filesystem::filesystem_error& ex )
175 {
176 std::cout << ex.what() << '\n';
177 }
178
179 return imageFileNames;
180}

◆ GetListOfFilesInDirectory()

std::vector< std::string > rendering_engine::Utility::GetListOfFilesInDirectory ( std::string directory)
static

Returns a list of full file paths in the given directory.

Parameters
directoryDirectory path to search.
Returns
Vector of file paths.

Definition at line 86 of file utility.cpp.

87{
88 std::vector<std::string> shaderFileNames;
89
90 try
91 {
92 //check if parameter string is directory
93 if( boost::filesystem::exists( boost::filesystem::path( directory ) ) && boost::filesystem::is_directory( boost::filesystem::path( directory ) ) )
94 {
95 boost::filesystem::path pathToDirectory = boost::filesystem::path( directory );
96
97 if( boost::filesystem::is_directory( pathToDirectory ) )
98 {
99 for( boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator( pathToDirectory ) )
100 {
101 size_t dot = x.path().string().find_last_of( "." );
102
103 if( std::string{ "spv" } == x.path().string().substr( dot + 1 ) )
104 {
105 std::cout << "Shader binary file: " << x.path().string() << "\n";
106 shaderFileNames.push_back( x.path().string() );
107 }
108 }
109 }
110 }
111 }
112 catch( const boost::filesystem::filesystem_error& ex )
113 {
114 std::cout << ex.what() << '\n';
115 }
116
117 return shaderFileNames;
118}

◆ GetModelsFolderPath()

boost::filesystem::path rendering_engine::Utility::GetModelsFolderPath ( )
static

Returns absolute path to Content/Models.

Definition at line 197 of file utility.cpp.

198{
200}
static boost::filesystem::path const sModelsRelativePathFolder
Definition utility.hpp:164

◆ GetPackEntries()

const PackEntries & rendering_engine::Utility::GetPackEntries ( )
static

Returns the manifest of packed files.

Definition at line 219 of file utility.cpp.

220{
222 return sPackEntries;
223
224 sPackEntries.clear();
225
226 // Path to Pack.json
227 const boost::filesystem::path jsonPath = ResolveProjectRoot() / sContentPackEntriesFilePath;
228
229 if (!boost::filesystem::exists(jsonPath))
230 {
231 sPackEntriesLoaded = true;
232 return sPackEntries; // empty
233 }
234
235 // Load JSON
236 std::ifstream f(jsonPath.string());
237 if (!f.is_open())
238 {
239 std::cerr << "[ERROR] Failed to open Pack.json\n";
240 sPackEntriesLoaded = true;
241 return sPackEntries;
242 }
243
244 nlohmann::json j;
245 f >> j;
246
247 // Parse entries
248 for (auto it = j.begin(); it != j.end(); ++it)
249 {
250 PackEntry entry;
251 entry.offset = it.value().value("offset", 0);
252 entry.size = it.value().value("size", 0);
253 sPackEntries[it.key()] = entry;
254 }
255
256 sPackEntriesLoaded = true;
257 return sPackEntries;
258}
static boost::filesystem::path const sContentPackEntriesFilePath
Definition utility.hpp:162
static PackEntries sPackEntries
Definition utility.cpp:19
static bool sPackEntriesLoaded
Definition utility.cpp:20

◆ GetShadersBinaryPath()

boost::filesystem::path rendering_engine::Utility::GetShadersBinaryPath ( )
static

Returns the directory path containing compiled shader binaries.

Returns
Shader binary path as boost::filesystem::path.

Definition at line 130 of file utility.cpp.

131{
132 return sShadersBinaryPath;
133}
static boost::filesystem::path sShadersBinaryPath
Definition utility.hpp:159

◆ GetShadersFolderPath()

boost::filesystem::path rendering_engine::Utility::GetShadersFolderPath ( )
static

Returns absolute path to Content/Shaders.

Definition at line 202 of file utility.cpp.

203{
205}
static boost::filesystem::path const sShadersRelativePathFolder
Definition utility.hpp:165

◆ GetTextureFolderPath()

boost::filesystem::path rendering_engine::Utility::GetTextureFolderPath ( )
static

Returns absolute path to Content/Textures.

Definition at line 192 of file utility.cpp.

193{
195}
static boost::filesystem::path const sTextureRelativePathFolder
Definition utility.hpp:163

◆ InitializePaths()

void rendering_engine::Utility::InitializePaths ( int argc,
char * argv[] )
static

Initializes engine paths based on the executable location.

Should be called once during startup to establish base directories for application, build, and shader assets.

Parameters
argcCommand-line argument count.
argvCommand-line argument vector.

Definition at line 24 of file utility.cpp.

25{
26 sApplicationPath = boost::filesystem::path(argv[0]);
27
28 sBuildPath = FindPath( "Build" );
30}
static boost::filesystem::path const sDefaultShadersBinaryRelativePath
Definition utility.hpp:157

◆ IsPackageProvided()

bool rendering_engine::Utility::IsPackageProvided ( )
static

Checks whether packed assets (Pack.bin / Pack.json) exist.

Definition at line 212 of file utility.cpp.

213{
214 const auto root = ResolveProjectRoot();
215 return boost::filesystem::exists(root / sContentPackageFilePath) &&
216 boost::filesystem::exists(root / sContentPackEntriesFilePath);
217}
static boost::filesystem::path const sContentPackageFilePath
Definition utility.hpp:161

◆ ReadConfigFile()

AppConfig rendering_engine::Utility::ReadConfigFile ( )
static

Reads application settings from the JSON config file.

Missing or invalid fields fall back to default AppConfig values.

Returns
Populated AppConfig structure.

Definition at line 32 of file utility.cpp.

33{
34 AppConfig cfg;
35
36 std::ifstream f(GetConfigFilePath().string());
37 if (!f.is_open())
38 {
39 return cfg;
40 }
41
42 try
43 {
44 nlohmann::json appConfigData = nlohmann::json::parse(f);
45
46 if (appConfigData.contains("appName"))
47 cfg.appName = appConfigData["appName"].get<std::string>();
48
49 if (appConfigData.contains("isFullScreen"))
50 cfg.isFullScreen = appConfigData["isFullScreen"].get<bool>();
51
52 if (appConfigData.contains("screenWidth"))
53 cfg.screenWidth = appConfigData["screenWidth"].get<float>();
54
55 if (appConfigData.contains("screenHeight"))
56 cfg.screenHeight = appConfigData["screenHeight"].get<float>();
57 }
58 catch (const std::exception& e)
59 {
60 return cfg;
61 }
62
63 return cfg;
64}
static boost::filesystem::path GetConfigFilePath()
Returns absolute path to Config/app_config.json.
Definition utility.cpp:207

◆ ReadPackedFile()

std::vector< uint8_t > rendering_engine::Utility::ReadPackedFile ( const std::string & entryPath)
static

Reads raw bytes of a file stored inside Pack.bin.

Parameters
entryPathVirtual path inside the pack (e.g. "Textures/my.png").
Returns
Decoded file bytes, or empty vector on failure.

Definition at line 260 of file utility.cpp.

261{
262 std::vector<std::uint8_t> data;
263
264 if (!IsPackageProvided())
265 return data;
266
267 const path binPath = ResolveProjectRoot() / sContentPackageFilePath;
268 const path jsonPath = ResolveProjectRoot() / sContentPackEntriesFilePath;
269
270 if (!boost::filesystem::exists(binPath) ||
271 !boost::filesystem::exists(jsonPath))
272 {
273 std::cerr << "[Utility::ReadPackedFile] Missing Pack.bin or Pack.json\n";
274 return data;
275 }
276
278 // Check if this entry exists in Pack.json
279 auto it = sPackEntries.find(entryPath);
280 if (it == sPackEntries.end())
281 {
282 std::cerr << "[Utility::ReadPackedFile] No such packed entry: "
283 << entryPath << std::endl;
284 return data; // empty
285 }
286
287 const PackEntry& entry = it->second;
288
289 std::ifstream bin(binPath.string(), std::ios::binary);
290 if (!bin)
291 {
292 std::cerr << "[Utility::ReadPackedFile] Failed to open Pack.bin: "
293 << binPath.string() << std::endl;
294 return data;
295 }
296
297 // Read the memory region [offset, offset + size)
298
299 data.resize(entry.size);
300
301 bin.seekg(entry.offset, std::ios::beg);
302 if (!bin.good())
303 {
304 std::cerr << "[Utility::ReadPackedFile] Seek error for entry: "
305 << entryPath << std::endl;
306 return {};
307 }
308
309 bin.read(reinterpret_cast<char*>(data.data()), entry.size);
310 if (!bin.good())
311 {
312 std::cerr << "[Utility::ReadPackedFile] Read error for entry: "
313 << entryPath << std::endl;
314 return {};
315 }
316
317 return data;
318}
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:219
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition utility.cpp:212

◆ ReadShaderBinaryFile()

std::vector< char > rendering_engine::Utility::ReadShaderBinaryFile ( std::string const & filename)
static

Reads a binary shader file from disk.

Parameters
filenamePath to the shader file.
Returns
Vector containing the binary data.

Definition at line 66 of file utility.cpp.

67{
68 std::ifstream file(filename, std::ios::ate | std::ios::binary);
69
70 if (!file.is_open())
71 {
72 throw std::runtime_error("failed to open shader binary file!");
73 }
74
75 size_t fileSize = (size_t) file.tellg();
76 std::vector<char> buffer(fileSize);
77
78 file.seekg(0);
79 file.read(buffer.data(), fileSize);
80
81 file.close();
82
83 return buffer;
84}

◆ ResolveProjectRoot()

boost::filesystem::path rendering_engine::Utility::ResolveProjectRoot ( )
static

Resolves project root folder (handles Release/Debug/Binaries layouts).

Definition at line 182 of file utility.cpp.

183{
184 auto exeDir = boost::filesystem::canonical(boost::filesystem::path(boost::filesystem::current_path())); // default
185 if (exeDir.filename() == "Debug" || exeDir.filename() == "Release")
186 exeDir = exeDir.parent_path(); // step out of Debug/Release
187 if (exeDir.filename() == "Binaries")
188 exeDir = exeDir.parent_path(); // step out of Binaries
189 return exeDir;
190}

Member Data Documentation

◆ sAppConfigFilePath

path const rendering_engine::Utility::sAppConfigFilePath = path{} / "Config" / "app_config.json"
static

Definition at line 166 of file utility.hpp.

◆ sApplicationPath

path rendering_engine::Utility::sApplicationPath
static

Definition at line 156 of file utility.hpp.

◆ sBuildPath

path rendering_engine::Utility::sBuildPath
static

Definition at line 158 of file utility.hpp.

◆ sContentPackageFilePath

path const rendering_engine::Utility::sContentPackageFilePath = path{} / "Content" / "Pack.bin"
static

Definition at line 161 of file utility.hpp.

◆ sContentPackEntriesFilePath

path const rendering_engine::Utility::sContentPackEntriesFilePath = path{} / "Content" / "Pack.json"
static

Definition at line 162 of file utility.hpp.

◆ sContentRelativePathFolder

path const rendering_engine::Utility::sContentRelativePathFolder = path{} / "Content"
static

Definition at line 160 of file utility.hpp.

◆ sDefaultShadersBinaryRelativePath

path const rendering_engine::Utility::sDefaultShadersBinaryRelativePath = {"/Content/Shaders/"}
static

Definition at line 157 of file utility.hpp.

◆ sModelsRelativePathFolder

path const rendering_engine::Utility::sModelsRelativePathFolder = sContentRelativePathFolder / "Models"
static

Definition at line 164 of file utility.hpp.

◆ sShadersBinaryPath

path rendering_engine::Utility::sShadersBinaryPath
static

Definition at line 159 of file utility.hpp.

◆ sShadersRelativePathFolder

path const rendering_engine::Utility::sShadersRelativePathFolder = sContentRelativePathFolder / "Shaders"
static

Definition at line 165 of file utility.hpp.

◆ sTextureRelativePathFolder

path const rendering_engine::Utility::sTextureRelativePathFolder = sContentRelativePathFolder / "Textures"
static

Definition at line 163 of file utility.hpp.


The documentation for this class was generated from the following files: