Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
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 PackEntries & GetPackEntries ()
 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 175 of file utility.cpp.

176{
177 return sApplicationPath;
178}
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 180 of file utility.cpp.

181{
182 return sBuildPath;
183}
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 273 of file utility.cpp.

274{
276}
static std::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
Definition utility.cpp:238
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 248 of file utility.cpp.

249{
251}
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 263 of file utility.cpp.

264{
266}
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 205 of file utility.cpp.

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

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

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

◆ GetLogsFolderPath()

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

Returns absolute path to Logs folder.

Definition at line 278 of file utility.cpp.

279{
281}
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 258 of file utility.cpp.

259{
261}
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 290 of file utility.cpp.

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

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

186{
187 return sShadersBinaryPath;
188}
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 268 of file utility.cpp.

269{
271}
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 253 of file utility.cpp.

254{
256}
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 27 of file utility.cpp.

28{
29 static_cast<void>(argc);
30
31 sApplicationPath = std::filesystem::path(argv[0]);
32
33 sBuildPath = FindPath( "Build" );
35}
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 283 of file utility.cpp.

284{
285 const auto root = ResolveProjectRoot();
286 return std::filesystem::exists(root / sContentPackageFilePath) &&
287 std::filesystem::exists(root / sContentPackEntriesFilePath);
288}
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 37 of file utility.cpp.

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

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

332{
333 std::vector<std::uint8_t> data;
334
335 if (!IsPackageProvided())
336 return data;
337
338 const path binPath = ResolveProjectRoot() / sContentPackageFilePath;
339 const path jsonPath = ResolveProjectRoot() / sContentPackEntriesFilePath;
340
341 if (!std::filesystem::exists(binPath) ||
342 !std::filesystem::exists(jsonPath))
343 {
344 std::cerr << "[Utility::ReadPackedFile] Missing Pack.bin or Pack.json\n";
345 return data;
346 }
347
349 // Check if this entry exists in Pack.json
350 auto it = sPackEntries.find(entryPath);
351 if (it == sPackEntries.end())
352 {
353 std::cerr << "[Utility::ReadPackedFile] No such packed entry: "
354 << entryPath << std::endl;
355 return data; // empty
356 }
357
358 const PackEntry& entry = it->second;
359
360 std::ifstream bin(binPath.string(), std::ios::binary);
361 if (!bin)
362 {
363 std::cerr << "[Utility::ReadPackedFile] Failed to open Pack.bin: "
364 << binPath.string() << std::endl;
365 return data;
366 }
367
368 // Read the memory region [offset, offset + size)
369
370 data.resize(entry.size);
371
372 if (entry.offset > static_cast<std::size_t>(std::numeric_limits<std::streamoff>::max()))
373 {
374 std::cerr << "[Utility::ReadPackedFile] Entry offset exceeds supported file range: "
375 << entryPath << std::endl;
376 return {};
377 }
378
379 bin.seekg(static_cast<std::streamoff>(entry.offset), std::ios::beg);
380
381 if (!bin.good())
382 {
383 std::cerr << "[Utility::ReadPackedFile] Seek error for entry: "
384 << entryPath << std::endl;
385 return {};
386 }
387
388 if (entry.size >
389 static_cast<std::size_t>(std::numeric_limits<std::streamsize>::max()))
390 {
391 std::cerr << "[Utility::ReadPackedFile] Entry size exceeds supported read range: "
392 << entryPath << std::endl;
393 return {};
394 }
395
396 bin.read(
397 reinterpret_cast<char*>(data.data()),
398 static_cast<std::streamsize>(entry.size));
399
400 if (!bin.good())
401 {
402 std::cerr << "[Utility::ReadPackedFile] Read error for entry: "
403 << entryPath << std::endl;
404 return {};
405 }
406
407 return data;
408}
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:290
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition utility.cpp:283

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

117{
118 std::ifstream file(filename, std::ios::ate | std::ios::binary);
119
120 if (!file.is_open())
121 {
122 throw std::runtime_error("failed to open shader binary file!");
123 }
124
125 const auto fileSize = file.tellg();
126 if (fileSize < 0)
127 {
128 throw std::runtime_error("failed to determine shader binary file size!");
129 }
130
131 std::vector<char> buffer(static_cast<size_t>(fileSize));
132
133 file.seekg(0);
134 file.read(buffer.data(), static_cast<std::streamsize>(fileSize));
135
136 file.close();
137
138 return buffer;
139}

◆ ResolveProjectRoot()

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

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

Definition at line 238 of file utility.cpp.

239{
240 auto exeDir = std::filesystem::canonical(std::filesystem::path(std::filesystem::current_path())); // default
241 if (exeDir.filename() == "Debug" || exeDir.filename() == "Release")
242 exeDir = exeDir.parent_path(); // step out of Debug/Release
243 if (exeDir.filename() == "Binaries")
244 exeDir = exeDir.parent_path(); // step out of Binaries
245 return exeDir;
246}

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: