Rendering Engine 0.2.14
Modular Graphics Rendering Engine | v0.2.14
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 std::filesystem::path GetApplicationPath ()
 Returns the absolute path of the running application.
 
static std::filesystem::path GetBuildPath ()
 Returns the build output directory path.
 
static std::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 std::filesystem::path ResolveProjectRoot ()
 Resolves project root folder (handles Release/Debug/Binaries layouts).
 
static std::filesystem::path GetContentFolderPath ()
 Returns absolute path to Content.
 
static std::filesystem::path GetTextureFolderPath ()
 Returns absolute path to Content/Textures.
 
static std::filesystem::path GetModelsFolderPath ()
 Returns absolute path to Content/Models.
 
static std::filesystem::path GetFontsFolderPath ()
 Returns absolute path to Content/Fonts.
 
static std::filesystem::path GetShadersFolderPath ()
 Returns absolute path to Content/Shaders.
 
static std::filesystem::path GetConfigFilePath ()
 Returns absolute path to Config/app_config.json.
 
static std::filesystem::path GetLogsFolderPath ()
 Returns absolute path to Logs folder.
 
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 std::filesystem::path sApplicationPath
 
static std::filesystem::path const sDefaultShadersBinaryRelativePath = {"/Content/Shaders/"}
 
static std::filesystem::path sBuildPath
 
static std::filesystem::path sShadersBinaryPath
 
static std::filesystem::path const sContentRelativePathFolder = path{} / "Content"
 
static std::filesystem::path const sContentPackageFilePath = path{} / "Content" / "Pack.bin"
 
static std::filesystem::path const sContentPackEntriesFilePath = path{} / "Content" / "Pack.json"
 
static std::filesystem::path const sTextureRelativePathFolder = sContentRelativePathFolder / "Textures"
 
static std::filesystem::path const sModelsRelativePathFolder = sContentRelativePathFolder / "Models"
 
static std::filesystem::path const sFontsRelativePathFolder = sContentRelativePathFolder / "Fonts"
 
static std::filesystem::path const sShadersRelativePathFolder = sContentRelativePathFolder / "Shaders"
 
static std::filesystem::path const sAppConfigFilePath = path{} / "Config" / "app_config.json"
 
static std::filesystem::path const sLogFolderPath = path{} / "Logs"
 

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 72 of file utility.hpp.

Member Function Documentation

◆ GetApplicationPath()

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

Returns the absolute path of the running application.

Returns
Application path as std::filesystem::path.

Definition at line 167 of file utility.cpp.

168{
169 return sApplicationPath;
170}
static std::filesystem::path sApplicationPath
Definition utility.hpp:178

◆ GetBuildPath()

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

Returns the build output directory path.

Returns
Build path as std::filesystem::path.

Definition at line 172 of file utility.cpp.

173{
174 return sBuildPath;
175}
static std::filesystem::path sBuildPath
Definition utility.hpp:180

◆ GetConfigFilePath()

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

Returns absolute path to Config/app_config.json.

Definition at line 265 of file utility.cpp.

266{
268}
static std::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
Definition utility.cpp:230
static std::filesystem::path const sAppConfigFilePath
Definition utility.hpp:189

◆ GetContentFolderPath()

std::filesystem::path rendering_engine::Utility::GetContentFolderPath ( )
static

Returns absolute path to Content.

Definition at line 240 of file utility.cpp.

241{
243}
static std::filesystem::path const sContentRelativePathFolder
Definition utility.hpp:182

◆ GetFontsFolderPath()

std::filesystem::path rendering_engine::Utility::GetFontsFolderPath ( )
static

Returns absolute path to Content/Fonts.

Definition at line 255 of file utility.cpp.

256{
258}
static std::filesystem::path const sFontsRelativePathFolder
Definition utility.hpp:187

◆ 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 197 of file utility.cpp.

198{
199 std::vector<std::string> imageFileNames;
200
201 try
202 {
203 //check if parameter string is directory
204 if(std::filesystem::exists(std::filesystem::path(directory)) && std::filesystem::is_directory(std::filesystem::path(directory)) )
205 {
206 std::filesystem::path pathToDirectory = std::filesystem::path(directory);
207
208 if(std::filesystem::is_directory(pathToDirectory) )
209 {
210 for(const std::filesystem::directory_entry& x : std::filesystem::directory_iterator(pathToDirectory) )
211 {
212 size_t dot = x.path().string().find_last_of(".");
213
214 if( extToSearch == x.path().string().substr(dot + 1) )
215 {
216 imageFileNames.push_back(x.path().string());
217 }
218 }
219 }
220 }
221 }
222 catch( const std::filesystem::filesystem_error& ex )
223 {
224 std::cout << ex.what() << '\n';
225 }
226
227 return imageFileNames;
228}

◆ 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 133 of file utility.cpp.

134{
135 std::vector<std::string> shaderFileNames;
136
137 try
138 {
139 //check if parameter string is directory
140 if( std::filesystem::exists( std::filesystem::path( directory ) ) && std::filesystem::is_directory(std::filesystem::path( directory ) ) )
141 {
142 std::filesystem::path pathToDirectory = std::filesystem::path( directory );
143
144 if(std::filesystem::is_directory( pathToDirectory ) )
145 {
146 for(const std::filesystem::directory_entry& x : std::filesystem::directory_iterator( pathToDirectory ) )
147 {
148 size_t dot = x.path().string().find_last_of( "." );
149
150 if( std::string{ "spv" } == x.path().string().substr( dot + 1 ) )
151 {
152 std::cout << "Shader binary file: " << x.path().string() << "\n";
153 shaderFileNames.push_back( x.path().string() );
154 }
155 }
156 }
157 }
158 }
159 catch( const std::filesystem::filesystem_error& ex )
160 {
161 std::cout << ex.what() << '\n';
162 }
163
164 return shaderFileNames;
165}

◆ GetLogsFolderPath()

std::filesystem::path rendering_engine::Utility::GetLogsFolderPath ( )
static

Returns absolute path to Logs folder.

Definition at line 270 of file utility.cpp.

271{
273}
static std::filesystem::path const sLogFolderPath
Definition utility.hpp:190

◆ GetModelsFolderPath()

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

Returns absolute path to Content/Models.

Definition at line 250 of file utility.cpp.

251{
253}
static std::filesystem::path const sModelsRelativePathFolder
Definition utility.hpp:186

◆ GetPackEntries()

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

Returns the manifest of packed files.

Definition at line 282 of file utility.cpp.

283{
285 return sPackEntries;
286
287 sPackEntries.clear();
288
289 // Path to Pack.json
290 const std::filesystem::path jsonPath = ResolveProjectRoot() / sContentPackEntriesFilePath;
291
292 if (!std::filesystem::exists(jsonPath))
293 {
294 sPackEntriesLoaded = true;
295 return sPackEntries; // empty
296 }
297
298 // Load JSON
299 std::ifstream f(jsonPath.string());
300 if (!f.is_open())
301 {
302 std::cerr << "[ERROR] Failed to open Pack.json\n";
303 sPackEntriesLoaded = true;
304 return sPackEntries;
305 }
306
307 nlohmann::json j;
308 f >> j;
309
310 // Parse entries
311 for (auto it = j.begin(); it != j.end(); ++it)
312 {
313 PackEntry entry;
314 entry.offset = it.value().value("offset", 0);
315 entry.size = it.value().value("size", 0);
316 sPackEntries[it.key()] = entry;
317 }
318
319 sPackEntriesLoaded = true;
320 return sPackEntries;
321}
static std::filesystem::path const sContentPackEntriesFilePath
Definition utility.hpp:184
static PackEntries sPackEntries
Definition utility.cpp:21
static bool sPackEntriesLoaded
Definition utility.cpp:22

◆ GetShadersBinaryPath()

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

Returns the directory path containing compiled shader binaries.

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

Definition at line 177 of file utility.cpp.

178{
179 return sShadersBinaryPath;
180}
static std::filesystem::path sShadersBinaryPath
Definition utility.hpp:181

◆ GetShadersFolderPath()

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

Returns absolute path to Content/Shaders.

Definition at line 260 of file utility.cpp.

261{
263}
static std::filesystem::path const sShadersRelativePathFolder
Definition utility.hpp:188

◆ GetTextureFolderPath()

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

Returns absolute path to Content/Textures.

Definition at line 245 of file utility.cpp.

246{
248}
static std::filesystem::path const sTextureRelativePathFolder
Definition utility.hpp:185

◆ 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 26 of file utility.cpp.

27{
28 sApplicationPath = std::filesystem::path(argv[0]);
29
30 sBuildPath = FindPath( "Build" );
32}
static std::filesystem::path const sDefaultShadersBinaryRelativePath
Definition utility.hpp:179

◆ IsPackageProvided()

bool rendering_engine::Utility::IsPackageProvided ( )
static

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

Definition at line 275 of file utility.cpp.

276{
277 const auto root = ResolveProjectRoot();
278 return std::filesystem::exists(root / sContentPackageFilePath) &&
279 std::filesystem::exists(root / sContentPackEntriesFilePath);
280}
static std::filesystem::path const sContentPackageFilePath
Definition utility.hpp:183

◆ 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 34 of file utility.cpp.

35{
36 AppConfig cfg;
37
38 std::ifstream f(GetConfigFilePath().string());
39 if (!f.is_open())
40 {
41 return cfg;
42 }
43
44 try
45 {
46 nlohmann::json appConfigData = nlohmann::json::parse(f);
47
48 if (appConfigData.contains("appName"))
49 cfg.appName = appConfigData["appName"].get<std::string>();
50
51 if (appConfigData.contains("isFullScreen"))
52 cfg.isFullScreen = appConfigData["isFullScreen"].get<bool>();
53
54 if (appConfigData.contains("screenWidth"))
55 cfg.screenWidth = appConfigData["screenWidth"].get<float>();
56
57 if (appConfigData.contains("screenHeight"))
58 cfg.screenHeight = appConfigData["screenHeight"].get<float>();
59
60 if (appConfigData.contains("text"))
61 {
62 const auto& textNode = appConfigData["text"];
63
64 if (textNode.contains("scripts") && textNode["scripts"].is_array())
65 {
66 for (const auto& script : textNode["scripts"])
67 {
68 if (script.is_string())
69 {
70 auto found = std::find(cfg.textScripts.begin(), cfg.textScripts.end(), script);
71 if(found == cfg.textScripts.end())
72 {
73 cfg.textScripts.push_back(script.get<std::string>());
74 }
75 }
76 }
77 }
78
79 if (textNode.contains("fontSizePreload") && textNode["fontSizePreload"].is_array())
80 {
81 for (const auto& fontSize : textNode["fontSizePreload"])
82 {
83 auto found = std::find(cfg.fontSizePreload.begin(), cfg.fontSizePreload.end(), fontSize);
84 if (found == cfg.fontSizePreload.end())
85 {
86 cfg.fontSizePreload.push_back(fontSize.get<int>());
87 }
88 }
89 }
90 }
91
92 if (appConfigData.contains("logLevel"))
93 cfg.logLevel = appConfigData["logLevel"].get<std::string>();
94
95 if (appConfigData.contains("useSmoothedFPS"))
96 cfg.useSmoothedFPS = appConfigData["useSmoothedFPS"].get<bool>();
97
98 if (appConfigData.contains("targetFPS"))
99 cfg.targetFPS = appConfigData["targetFPS"].get<float>();
100
101 if (appConfigData.contains("showStatsOverlay"))
102 cfg.showStatsOverlay = appConfigData["showStatsOverlay"].get<bool>();
103
104 }
105 catch (const std::exception& e)
106 {
107 return cfg;
108 }
109
110 return cfg;
111}
static std::filesystem::path GetConfigFilePath()
Returns absolute path to Config/app_config.json.
Definition utility.cpp:265

◆ 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 323 of file utility.cpp.

324{
325 std::vector<std::uint8_t> data;
326
327 if (!IsPackageProvided())
328 return data;
329
330 const path binPath = ResolveProjectRoot() / sContentPackageFilePath;
331 const path jsonPath = ResolveProjectRoot() / sContentPackEntriesFilePath;
332
333 if (!std::filesystem::exists(binPath) ||
334 !std::filesystem::exists(jsonPath))
335 {
336 std::cerr << "[Utility::ReadPackedFile] Missing Pack.bin or Pack.json\n";
337 return data;
338 }
339
341 // Check if this entry exists in Pack.json
342 auto it = sPackEntries.find(entryPath);
343 if (it == sPackEntries.end())
344 {
345 std::cerr << "[Utility::ReadPackedFile] No such packed entry: "
346 << entryPath << std::endl;
347 return data; // empty
348 }
349
350 const PackEntry& entry = it->second;
351
352 std::ifstream bin(binPath.string(), std::ios::binary);
353 if (!bin)
354 {
355 std::cerr << "[Utility::ReadPackedFile] Failed to open Pack.bin: "
356 << binPath.string() << std::endl;
357 return data;
358 }
359
360 // Read the memory region [offset, offset + size)
361
362 data.resize(entry.size);
363
364 bin.seekg(entry.offset, std::ios::beg);
365 if (!bin.good())
366 {
367 std::cerr << "[Utility::ReadPackedFile] Seek error for entry: "
368 << entryPath << std::endl;
369 return {};
370 }
371
372 bin.read(reinterpret_cast<char*>(data.data()), entry.size);
373 if (!bin.good())
374 {
375 std::cerr << "[Utility::ReadPackedFile] Read error for entry: "
376 << entryPath << std::endl;
377 return {};
378 }
379
380 return data;
381}
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:282
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition utility.cpp:275

◆ 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 113 of file utility.cpp.

114{
115 std::ifstream file(filename, std::ios::ate | std::ios::binary);
116
117 if (!file.is_open())
118 {
119 throw std::runtime_error("failed to open shader binary file!");
120 }
121
122 size_t fileSize = (size_t) file.tellg();
123 std::vector<char> buffer(fileSize);
124
125 file.seekg(0);
126 file.read(buffer.data(), fileSize);
127
128 file.close();
129
130 return buffer;
131}

◆ ResolveProjectRoot()

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

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

Definition at line 230 of file utility.cpp.

231{
232 auto exeDir = std::filesystem::canonical(std::filesystem::path(std::filesystem::current_path())); // default
233 if (exeDir.filename() == "Debug" || exeDir.filename() == "Release")
234 exeDir = exeDir.parent_path(); // step out of Debug/Release
235 if (exeDir.filename() == "Binaries")
236 exeDir = exeDir.parent_path(); // step out of Binaries
237 return exeDir;
238}

Member Data Documentation

◆ sAppConfigFilePath

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

Definition at line 189 of file utility.hpp.

◆ sApplicationPath

path rendering_engine::Utility::sApplicationPath
static

Definition at line 178 of file utility.hpp.

◆ sBuildPath

path rendering_engine::Utility::sBuildPath
static

Definition at line 180 of file utility.hpp.

◆ sContentPackageFilePath

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

Definition at line 183 of file utility.hpp.

◆ sContentPackEntriesFilePath

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

Definition at line 184 of file utility.hpp.

◆ sContentRelativePathFolder

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

Definition at line 182 of file utility.hpp.

◆ sDefaultShadersBinaryRelativePath

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

Definition at line 179 of file utility.hpp.

◆ sFontsRelativePathFolder

path const rendering_engine::Utility::sFontsRelativePathFolder = sContentRelativePathFolder / "Fonts"
static

Definition at line 187 of file utility.hpp.

◆ sLogFolderPath

path const rendering_engine::Utility::sLogFolderPath = path{} / "Logs"
static

Definition at line 190 of file utility.hpp.

◆ sModelsRelativePathFolder

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

Definition at line 186 of file utility.hpp.

◆ sShadersBinaryPath

path rendering_engine::Utility::sShadersBinaryPath
static

Definition at line 181 of file utility.hpp.

◆ sShadersRelativePathFolder

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

Definition at line 188 of file utility.hpp.

◆ sTextureRelativePathFolder

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

Definition at line 185 of file utility.hpp.


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