Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
utility.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5#pragma once
6
7#include <iostream>
8#include <cstdint>
9#include <fstream>
10#include <string>
11#include <vector>
12#include "boost/filesystem.hpp"
13
15
16namespace rendering_engine
17{
18/**
19 * @struct AppConfig
20 * @brief Basic application settings loaded from a configuration file.
21 *
22 * This structure represents a minimal configuration schema for applications
23 * built using the Rendering Engine. Values are typically read from
24 * `Config/app_config.json` at startup and applied during window creation.
25 */
27{
28 /** @brief Name of the application */
29 std::string appName;
30 /** @brief Whether the application should start in full-screen mode. */
31 bool isFullScreen = false;
32 /** @brief Desired window width in pixels (ignored in full-screen mode). */
33 float screenWidth = 800.0f;
34 /** @brief Desired window height in pixels (ignored in full-screen mode). */
35 float screenHeight = 600.0f;
36 /** @brief Unicode scripts to preload for text rendering. */
37 std::vector<std::string> textScripts{"Latin"};
38 /** @brief Font sizes to preload at startup. */
39 std::vector<int> fontSizePreload{10};
40 /** @brief Logging verbosity level ("Error", "Warning", "Info", "Debug"). */
41 std::string logLevel{"Info"};
42 /** @brief Enable FPS smoothing and frame pacing behavior. */
43 bool useSmoothedFPS = true;
44 /** @brief Target frame rate (0 = uncapped). */
45 float targetFPS = 60.0f;
46 /** @brief Enable on-screen statistics overlay. */
47 bool showStatsOverlay = true;
48};
49
50/**
51 * @struct PackEntry
52 * @brief Metadata describing one file stored inside a packed asset archive.
53 */
55{
56 size_t offset;
57 size_t size;
58};
59
60using PackEntries = std::unordered_map<std::string, PackEntry>;
61
62/**
63 * @class Utility
64 * @brief Provides static helper methods for file I/O and path management.
65 *
66 * The Utility class centralizes filesystem operations such as locating shader binaries,
67 * listing files, and resolving build or application paths.
68 *
69 * All methods are static and thread-safe, designed for use throughout the engine.
70 */
71class RE_API Utility final
72{
73public:
74 /**
75 * @brief Initializes engine paths based on the executable location.
76 *
77 * Should be called once during startup to establish base directories for
78 * application, build, and shader assets.
79 *
80 * @param argc Command-line argument count.
81 * @param argv Command-line argument vector.
82 */
83 static void InitializePaths(int argc, char* argv[]);
84 /**
85 * @brief Reads application settings from the JSON config file.
86 *
87 * Missing or invalid fields fall back to default AppConfig values.
88 *
89 * @return Populated AppConfig structure.
90 */
91 static AppConfig ReadConfigFile();
92 /**
93 * @brief Reads a binary shader file from disk.
94 * @param filename Path to the shader file.
95 * @return Vector containing the binary data.
96 */
97 static std::vector<char> ReadShaderBinaryFile( std::string const & filename );
98 /**
99 * @brief Returns a list of full file paths in the given directory.
100 * @param directory Directory path to search.
101 * @return Vector of file paths.
102 */
103 static std::vector<std::string> GetListOfFilesInDirectory( std::string directory );
104 /**
105 * @brief Returns the absolute path of the running application.
106 * @return Application path as boost::filesystem::path.
107 */
108 static boost::filesystem::path GetApplicationPath();
109 /**
110 * @brief Returns the build output directory path.
111 * @return Build path as boost::filesystem::path.
112 */
113 static boost::filesystem::path GetBuildPath();
114 /**
115 * @brief Returns the directory path containing compiled shader binaries.
116 * @return Shader binary path as boost::filesystem::path.
117 */
118 static boost::filesystem::path GetShadersBinaryPath();
119 /**
120 * @brief Returns a list of file names in a directory matching the specified extension.
121 * @param directory Directory path.
122 * @param extToSearch File extension to match (e.g., ".spv").
123 * @return Vector of matching file names.
124 */
125 static std::vector<std::string> GetListOfFileNamesInDirectory(const char* directory, std::string extToSearch);
126
127 /// @brief Resolves project root folder (handles Release/Debug/Binaries layouts).
128 static boost::filesystem::path ResolveProjectRoot();
129
130 /// @brief Returns absolute path to Content.
131 static boost::filesystem::path GetContentFolderPath();
132
133 /// @brief Returns absolute path to Content/Textures.
134 static boost::filesystem::path GetTextureFolderPath();
135
136 /// @brief Returns absolute path to Content/Models.
137 static boost::filesystem::path GetModelsFolderPath();
138
139 /// @brief Returns absolute path to Content/Fonts.
140 static boost::filesystem::path GetFontsFolderPath();
141
142 /// @brief Returns absolute path to Content/Shaders.
143 static boost::filesystem::path GetShadersFolderPath();
144
145 /// @brief Returns absolute path to Config/app_config.json.
146 static boost::filesystem::path GetConfigFilePath();
147
148 /// @brief Returns absolute path to Logs folder.
149 static boost::filesystem::path GetLogsFolderPath();
150
151 /**
152 * @brief Checks whether packed assets (Pack.bin / Pack.json) exist.
153 */
154 static bool IsPackageProvided();
155
156 /**
157 * @brief Returns the manifest of packed files.
158 */
159 static const PackEntries& GetPackEntries();
160
161 /**
162 * @brief Reads raw bytes of a file stored inside Pack.bin.
163 *
164 * @param entryPath Virtual path inside the pack (e.g. "Textures/my.png").
165 * @return Decoded file bytes, or empty vector on failure.
166 */
167 static std::vector<uint8_t> ReadPackedFile(const std::string& entryPath);
168
169 private:
170 Utility();
171 Utility( const Utility& rhs );
172 Utility& operator=( const Utility& rhs );
173
174 static boost::filesystem::path FindPath( std::string fileOrFolderName, std::string searchingFrom = "../../" );
175
176 public:
177 static boost::filesystem::path sApplicationPath;
178 static boost::filesystem::path const sDefaultShadersBinaryRelativePath;
179 static boost::filesystem::path sBuildPath;
180 static boost::filesystem::path sShadersBinaryPath;
181 static boost::filesystem::path const sContentRelativePathFolder;
182 static boost::filesystem::path const sContentPackageFilePath;
183 static boost::filesystem::path const sContentPackEntriesFilePath;
184 static boost::filesystem::path const sTextureRelativePathFolder;
185 static boost::filesystem::path const sModelsRelativePathFolder;
186 static boost::filesystem::path const sFontsRelativePathFolder;
187 static boost::filesystem::path const sShadersRelativePathFolder;
188 static boost::filesystem::path const sAppConfigFilePath;
189 static boost::filesystem::path const sLogFolderPath;
190
191};
192
193}
Provides static helper methods for file I/O and path management.
Definition: utility.hpp:72
static boost::filesystem::path sApplicationPath
Definition: utility.hpp:177
static boost::filesystem::path sBuildPath
Definition: utility.hpp:179
static boost::filesystem::path const sContentRelativePathFolder
Definition: utility.hpp:181
static boost::filesystem::path const sDefaultShadersBinaryRelativePath
Definition: utility.hpp:178
static boost::filesystem::path const sAppConfigFilePath
Definition: utility.hpp:188
static boost::filesystem::path const sContentPackageFilePath
Definition: utility.hpp:182
static boost::filesystem::path const sLogFolderPath
Definition: utility.hpp:189
static boost::filesystem::path const sTextureRelativePathFolder
Definition: utility.hpp:184
static boost::filesystem::path const sContentPackEntriesFilePath
Definition: utility.hpp:183
static boost::filesystem::path const sShadersRelativePathFolder
Definition: utility.hpp:187
static boost::filesystem::path const sFontsRelativePathFolder
Definition: utility.hpp:186
static boost::filesystem::path const sModelsRelativePathFolder
Definition: utility.hpp:185
static boost::filesystem::path sShadersBinaryPath
Definition: utility.hpp:180
std::unordered_map< std::string, PackEntry > PackEntries
Definition: utility.hpp:60
#define RE_API
Basic application settings loaded from a configuration file.
Definition: utility.hpp:27
float targetFPS
Target frame rate (0 = uncapped).
Definition: utility.hpp:45
float screenWidth
Desired window width in pixels (ignored in full-screen mode).
Definition: utility.hpp:33
bool isFullScreen
Whether the application should start in full-screen mode.
Definition: utility.hpp:31
std::string appName
Name of the application.
Definition: utility.hpp:29
bool useSmoothedFPS
Enable FPS smoothing and frame pacing behavior.
Definition: utility.hpp:43
bool showStatsOverlay
Enable on-screen statistics overlay.
Definition: utility.hpp:47
float screenHeight
Desired window height in pixels (ignored in full-screen mode).
Definition: utility.hpp:35
std::vector< std::string > textScripts
Unicode scripts to preload for text rendering.
Definition: utility.hpp:37
std::string logLevel
Logging verbosity level ("Error", "Warning", "Info", "Debug").
Definition: utility.hpp:41
std::vector< int > fontSizePreload
Font sizes to preload at startup.
Definition: utility.hpp:39
Metadata describing one file stored inside a packed asset archive.
Definition: utility.hpp:55