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