Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
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) 2025 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};
37
38/**
39 * @struct PackEntry
40 * @brief Metadata describing one file stored inside a packed asset archive.
41 */
43{
44 size_t offset;
45 size_t size;
46};
47
48using PackEntries = std::unordered_map<std::string, PackEntry>;
49
50/**
51 * @class Utility
52 * @brief Provides static helper methods for file I/O and path management.
53 *
54 * The Utility class centralizes filesystem operations such as locating shader binaries,
55 * listing files, and resolving build or application paths.
56 *
57 * All methods are static and thread-safe, designed for use throughout the engine.
58 */
59class RE_API Utility final
60{
61public:
62 /**
63 * @brief Initializes engine paths based on the executable location.
64 *
65 * Should be called once during startup to establish base directories for
66 * application, build, and shader assets.
67 *
68 * @param argc Command-line argument count.
69 * @param argv Command-line argument vector.
70 */
71 static void InitializePaths(int argc, char* argv[]);
72 /**
73 * @brief Reads application settings from the JSON config file.
74 *
75 * Missing or invalid fields fall back to default AppConfig values.
76 *
77 * @return Populated AppConfig structure.
78 */
80 /**
81 * @brief Reads a binary shader file from disk.
82 * @param filename Path to the shader file.
83 * @return Vector containing the binary data.
84 */
85 static std::vector<char> ReadShaderBinaryFile( std::string const & filename );
86 /**
87 * @brief Returns a list of full file paths in the given directory.
88 * @param directory Directory path to search.
89 * @return Vector of file paths.
90 */
91 static std::vector<std::string> GetListOfFilesInDirectory( std::string directory );
92 /**
93 * @brief Returns the absolute path of the running application.
94 * @return Application path as boost::filesystem::path.
95 */
96 static boost::filesystem::path GetApplicationPath();
97 /**
98 * @brief Returns the build output directory path.
99 * @return Build path as boost::filesystem::path.
100 */
101 static boost::filesystem::path GetBuildPath();
102 /**
103 * @brief Returns the directory path containing compiled shader binaries.
104 * @return Shader binary path as boost::filesystem::path.
105 */
106 static boost::filesystem::path GetShadersBinaryPath();
107 /**
108 * @brief Returns a list of file names in a directory matching the specified extension.
109 * @param directory Directory path.
110 * @param extToSearch File extension to match (e.g., ".spv").
111 * @return Vector of matching file names.
112 */
113 static std::vector<std::string> GetListOfFileNamesInDirectory(const char* directory, std::string extToSearch);
114
115 /// @brief Resolves project root folder (handles Release/Debug/Binaries layouts).
116 static boost::filesystem::path ResolveProjectRoot();
117
118 /// @brief Returns absolute path to Content/Textures.
119 static boost::filesystem::path GetTextureFolderPath();
120
121 /// @brief Returns absolute path to Content/Models.
122 static boost::filesystem::path GetModelsFolderPath();
123
124 /// @brief Returns absolute path to Content/Shaders.
125 static boost::filesystem::path GetShadersFolderPath();
126
127 /// @brief Returns absolute path to Config/app_config.json.
128 static boost::filesystem::path GetConfigFilePath();
129
130 /**
131 * @brief Checks whether packed assets (Pack.bin / Pack.json) exist.
132 */
133 static bool IsPackageProvided();
134
135 /**
136 * @brief Returns the manifest of packed files.
137 */
138 static const PackEntries& GetPackEntries();
139
140 /**
141 * @brief Reads raw bytes of a file stored inside Pack.bin.
142 *
143 * @param entryPath Virtual path inside the pack (e.g. "Textures/my.png").
144 * @return Decoded file bytes, or empty vector on failure.
145 */
146 static std::vector<uint8_t> ReadPackedFile(const std::string& entryPath);
147
148 private:
149 Utility();
150 Utility( const Utility& rhs );
151 Utility& operator=( const Utility& rhs );
152
153 static boost::filesystem::path FindPath( std::string fileOrFolderName, std::string searchingFrom = "../../" );
154
155 public:
156 static boost::filesystem::path sApplicationPath;
157 static boost::filesystem::path const sDefaultShadersBinaryRelativePath;
158 static boost::filesystem::path sBuildPath;
159 static boost::filesystem::path sShadersBinaryPath;
160 static boost::filesystem::path const sContentRelativePathFolder;
161 static boost::filesystem::path const sContentPackageFilePath;
162 static boost::filesystem::path const sContentPackEntriesFilePath;
163 static boost::filesystem::path const sTextureRelativePathFolder;
164 static boost::filesystem::path const sModelsRelativePathFolder;
165 static boost::filesystem::path const sShadersRelativePathFolder;
166 static boost::filesystem::path const sAppConfigFilePath;
167
168};
169
170template <typename T>
171class Enum final
172{
173public:
174 class Iterator final
175 {
176 public:
177 using size_type = std::size_t;
178 using difference_type = std::ptrdiff_t;
179 using value_type = T;
180 using reference = T;
181 using pointer = T*;
182 using iterator_category = std::forward_iterator_tag;
183
184 Iterator() = default;
185 explicit Iterator(T value);
186 Iterator(const Iterator&) = default;
187 Iterator(Iterator&&) = default;
188 Iterator& operator=(const Iterator&) = default;
190 ~Iterator() = default;
191
192 T operator*() const;
194 Iterator operator++(int);
195
196 bool operator==(const Iterator& rhs) const;
197 bool operator!=(const Iterator& rhs) const;
198
199 private:
200 T mValue;
201 };
202
203 Iterator begin() const;
204 Iterator end() const;
205};
206
207template <typename T>
209 mValue(value)
210{
211}
212
213template <typename T>
214inline bool Enum<T>::Iterator::operator==(const Iterator& rhs) const
215{
216 return mValue == rhs.mValue;
217}
218
219template <typename T>
220inline bool Enum<T>::Iterator::operator!=(const Iterator& rhs) const
221{
222 return mValue != rhs.mValue;
223}
224
225template <typename T>
227{
228 if( mValue > T::End )
229 {
230 throw std::runtime_error("Cannot dereference end().");
231 }
232
233 return mValue;
234}
235
236template <typename T>
238{
239 if( mValue <= T::End )
240 {
241 mValue = T(static_cast<int>(mValue) + 1);
242 }
243 return *this;
244}
245
246template <typename T>
248{
249 Iterator temp = *this;
250 operator++();
251 return temp;
252}
253
254template <typename T>
255inline typename Enum<T>::Iterator Enum<T>::begin() const
256{
257 return Iterator(T::Begin);
258}
259
260template <typename T>
261inline typename Enum<T>::Iterator Enum<T>::end() const
262{
263 return Iterator(T(static_cast<int>(T::End) + 1));
264}
265}
Iterator & operator=(const Iterator &)=default
Iterator(Iterator &&)=default
Iterator(const Iterator &)=default
bool operator==(const Iterator &rhs) const
Definition utility.hpp:214
Iterator & operator=(Iterator &&)=default
bool operator!=(const Iterator &rhs) const
Definition utility.hpp:220
std::forward_iterator_tag iterator_category
Definition utility.hpp:182
Iterator begin() const
Definition utility.hpp:255
Iterator end() const
Definition utility.hpp:261
static boost::filesystem::path GetConfigFilePath()
Returns absolute path to Config/app_config.json.
Definition utility.cpp:207
static boost::filesystem::path GetShadersFolderPath()
Returns absolute path to Content/Shaders.
Definition utility.cpp:202
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.
Definition utility.cpp:149
static std::vector< char > ReadShaderBinaryFile(std::string const &filename)
Reads a binary shader file from disk.
Definition utility.cpp:66
static boost::filesystem::path GetApplicationPath()
Returns the absolute path of the running application.
Definition utility.cpp:120
static boost::filesystem::path GetShadersBinaryPath()
Returns the directory path containing compiled shader binaries.
Definition utility.cpp:130
static boost::filesystem::path sApplicationPath
Definition utility.hpp:156
static boost::filesystem::path sBuildPath
Definition utility.hpp:158
static boost::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
Definition utility.cpp:182
static boost::filesystem::path GetBuildPath()
Returns the build output directory path.
Definition utility.cpp:125
static boost::filesystem::path const sContentRelativePathFolder
Definition utility.hpp:160
static boost::filesystem::path const sDefaultShadersBinaryRelativePath
Definition utility.hpp:157
static boost::filesystem::path const sAppConfigFilePath
Definition utility.hpp:166
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition utility.cpp:219
static std::vector< std::string > GetListOfFilesInDirectory(std::string directory)
Returns a list of full file paths in the given directory.
Definition utility.cpp:86
static boost::filesystem::path const sContentPackageFilePath
Definition utility.hpp:161
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Definition utility.cpp:32
static boost::filesystem::path const sTextureRelativePathFolder
Definition utility.hpp:163
static boost::filesystem::path GetTextureFolderPath()
Returns absolute path to Content/Textures.
Definition utility.cpp:192
static boost::filesystem::path const sContentPackEntriesFilePath
Definition utility.hpp:162
static boost::filesystem::path const sShadersRelativePathFolder
Definition utility.hpp:165
static boost::filesystem::path const sModelsRelativePathFolder
Definition utility.hpp:164
static boost::filesystem::path sShadersBinaryPath
Definition utility.hpp:159
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
Definition utility.cpp:260
static boost::filesystem::path GetModelsFolderPath()
Returns absolute path to Content/Models.
Definition utility.cpp:197
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition utility.cpp:212
static void InitializePaths(int argc, char *argv[])
Initializes engine paths based on the executable location.
Definition utility.cpp:24
std::unordered_map< std::string, PackEntry > PackEntries
Definition utility.hpp:48
#define RE_API
Basic application settings loaded from a configuration file.
Definition utility.hpp:27
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
float screenHeight
Desired window height in pixels (ignored in full-screen mode).
Definition utility.hpp:35
Metadata describing one file stored inside a packed asset archive.
Definition utility.hpp:43
size_t offset
Definition utility.hpp:44
size_t size
Definition utility.hpp:45