Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
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. More...
 
static AppConfig ReadConfigFile ()
 Reads application settings from the JSON config file. More...
 
static std::vector< char > ReadShaderBinaryFile (std::string const &filename)
 Reads a binary shader file from disk. More...
 
static std::vector< std::string > GetListOfFilesInDirectory (std::string directory)
 Returns a list of full file paths in the given directory. More...
 
static boost::filesystem::path GetApplicationPath ()
 Returns the absolute path of the running application. More...
 
static boost::filesystem::path GetBuildPath ()
 Returns the build output directory path. More...
 
static boost::filesystem::path GetShadersBinaryPath ()
 Returns the directory path containing compiled shader binaries. More...
 
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. More...
 
static boost::filesystem::path ResolveProjectRoot ()
 Resolves project root folder (handles Release/Debug/Binaries layouts). More...
 
static boost::filesystem::path GetContentFolderPath ()
 Returns absolute path to Content. More...
 
static boost::filesystem::path GetTextureFolderPath ()
 Returns absolute path to Content/Textures. More...
 
static boost::filesystem::path GetModelsFolderPath ()
 Returns absolute path to Content/Models. More...
 
static boost::filesystem::path GetFontsFolderPath ()
 Returns absolute path to Content/Fonts. More...
 
static boost::filesystem::path GetShadersFolderPath ()
 Returns absolute path to Content/Shaders. More...
 
static boost::filesystem::path GetConfigFilePath ()
 Returns absolute path to Config/app_config.json. More...
 
static boost::filesystem::path GetLogsFolderPath ()
 Returns absolute path to Logs folder. More...
 
static bool IsPackageProvided ()
 Checks whether packed assets (Pack.bin / Pack.json) exist. More...
 
static const PackEntriesGetPackEntries ()
 Returns the manifest of packed files. More...
 
static std::vector< uint8_t > ReadPackedFile (const std::string &entryPath)
 Reads raw bytes of a file stored inside Pack.bin. More...
 

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 sFontsRelativePathFolder = sContentRelativePathFolder / "Fonts"
 
static boost::filesystem::path const sShadersRelativePathFolder = sContentRelativePathFolder / "Shaders"
 
static boost::filesystem::path const sAppConfigFilePath = path{} / "Config" / "app_config.json"
 
static boost::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 71 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 167 of file utility.cpp.

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

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

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

◆ GetConfigFilePath()

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

Returns absolute path to Config/app_config.json.

Definition at line 264 of file utility.cpp.

265{
267}
static boost::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
Definition: utility.cpp:229
static boost::filesystem::path const sAppConfigFilePath
Definition: utility.hpp:188

◆ GetContentFolderPath()

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

Returns absolute path to Content.

Definition at line 239 of file utility.cpp.

240{
242}
static boost::filesystem::path const sContentRelativePathFolder
Definition: utility.hpp:181

◆ GetFontsFolderPath()

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

Returns absolute path to Content/Fonts.

Definition at line 254 of file utility.cpp.

255{
257}
static boost::filesystem::path const sFontsRelativePathFolder
Definition: utility.hpp:186

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

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

◆ 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( boost::filesystem::exists( boost::filesystem::path( directory ) ) && boost::filesystem::is_directory( boost::filesystem::path( directory ) ) )
141 {
142 boost::filesystem::path pathToDirectory = boost::filesystem::path( directory );
143
144 if( boost::filesystem::is_directory( pathToDirectory ) )
145 {
146 for( boost::filesystem::directory_entry& x : boost::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 boost::filesystem::filesystem_error& ex )
160 {
161 std::cout << ex.what() << '\n';
162 }
163
164 return shaderFileNames;
165}

◆ GetLogsFolderPath()

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

Returns absolute path to Logs folder.

Definition at line 269 of file utility.cpp.

270{
272}
static boost::filesystem::path const sLogFolderPath
Definition: utility.hpp:189

◆ GetModelsFolderPath()

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

Returns absolute path to Content/Models.

Definition at line 249 of file utility.cpp.

250{
252}
static boost::filesystem::path const sModelsRelativePathFolder
Definition: utility.hpp:185

◆ GetPackEntries()

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

Returns the manifest of packed files.

Definition at line 281 of file utility.cpp.

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

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

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

◆ GetShadersFolderPath()

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

Returns absolute path to Content/Shaders.

Definition at line 259 of file utility.cpp.

260{
262}
static boost::filesystem::path const sShadersRelativePathFolder
Definition: utility.hpp:187

◆ GetTextureFolderPath()

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

Returns absolute path to Content/Textures.

Definition at line 244 of file utility.cpp.

245{
247}
static boost::filesystem::path const sTextureRelativePathFolder
Definition: utility.hpp:184

◆ 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 = boost::filesystem::path(argv[0]);
29
30 sBuildPath = FindPath( "Build" );
32}
static boost::filesystem::path const sDefaultShadersBinaryRelativePath
Definition: utility.hpp:178

◆ IsPackageProvided()

bool rendering_engine::Utility::IsPackageProvided ( )
static

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

Definition at line 274 of file utility.cpp.

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

◆ 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 boost::filesystem::path GetConfigFilePath()
Returns absolute path to Config/app_config.json.
Definition: utility.cpp:264

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

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

◆ 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()

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

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

Definition at line 229 of file utility.cpp.

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

Member Data Documentation

◆ sAppConfigFilePath

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

Definition at line 188 of file utility.hpp.

◆ sApplicationPath

path rendering_engine::Utility::sApplicationPath
static

Definition at line 177 of file utility.hpp.

◆ sBuildPath

path rendering_engine::Utility::sBuildPath
static

Definition at line 179 of file utility.hpp.

◆ sContentPackageFilePath

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

Definition at line 182 of file utility.hpp.

◆ sContentPackEntriesFilePath

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

Definition at line 183 of file utility.hpp.

◆ sContentRelativePathFolder

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

Definition at line 181 of file utility.hpp.

◆ sDefaultShadersBinaryRelativePath

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

Definition at line 178 of file utility.hpp.

◆ sFontsRelativePathFolder

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

Definition at line 186 of file utility.hpp.

◆ sLogFolderPath

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

Definition at line 189 of file utility.hpp.

◆ sModelsRelativePathFolder

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

Definition at line 185 of file utility.hpp.

◆ sShadersBinaryPath

path rendering_engine::Utility::sShadersBinaryPath
static

Definition at line 180 of file utility.hpp.

◆ sShadersRelativePathFolder

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

Definition at line 187 of file utility.hpp.

◆ sTextureRelativePathFolder

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

Definition at line 184 of file utility.hpp.


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