cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
conf.cpp
Go to the documentation of this file.
1#include "CAE/Application.hpp"
2
3#include "Utils/Logger.hpp"
4#include "Utils/Path.hpp"
5
6#include <nlohmann/json.hpp>
7
8#include <fstream>
9
10using json = nlohmann::json;
11
13{
14 auto confPath = utl::Path::resolveRelativeToCwd(path);
15 if (!utl::Path::existsFile(confPath))
16 {
17 utl::Logger::log("Config file does not exist: " + confPath.string(), utl::LogLevel::WARNING);
18 return {};
19 }
20
21 std::ifstream file(confPath);
22 if (!file.is_open())
23 {
24 utl::Logger::log("Failed to open config file: " + confPath.string(), utl::LogLevel::WARNING);
25 return {};
26 }
27
28 json j;
29 try
30 {
31 file >> j;
32 }
33 catch (const json::parse_error &e)
34 {
35 utl::Logger::log("Failed to parse JSON config (" + confPath.string() + "): " + std::string(e.what()),
37 return {};
38 }
39 EngineConfig config;
40 utl::Logger::log("Loading config: " + confPath.string(), utl::LogLevel::INFO);
41 if (j.contains("audio"))
42 {
43 const auto &audio = j["audio"];
44 if (audio.contains("masterVolume") && audio["masterVolume"].is_number_float())
45 {
46 config.audio_master_volume = audio["masterVolume"];
47 }
48 if (audio.contains("muted") && audio["muted"].is_boolean())
49 {
50 config.audio_muted = audio["muted"];
51 }
52 }
53 if (j.contains("camera"))
54 {
55 const auto &camera = j["camera"];
56 if (camera.contains("position") && camera["position"].is_array() && camera["position"].size() == 3)
57 {
58 if (const auto &position = camera["position"];
59 position[0].is_number_float() && position[1].is_number_float() && position[2].is_number_float())
60 {
61 config.camera_position.x = position[0];
62 config.camera_position.y = position[1];
63 config.camera_position.z = position[2];
64 }
65 }
66 if (camera.contains("rotation") && camera["rotation"].is_array() && camera["rotation"].size() == 3)
67 {
68 if (const auto &rotation = camera["rotation"];
69 rotation[0].is_number_float() && rotation[1].is_number_float() && rotation[2].is_number_float())
70 {
71 config.camera_rotation.x = rotation[0];
72 config.camera_rotation.y = rotation[1];
73 config.camera_rotation.z = rotation[2];
74 }
75 }
76 if (camera.contains("direction") && camera["direction"].is_array() && camera["direction"].size() == 3)
77 {
78 if (const auto &direction = camera["direction"];
79 direction[0].is_number_float() && direction[1].is_number_float() && direction[2].is_number_float())
80 {
81 config.camera_direction.x = direction[0];
82 config.camera_direction.y = direction[1];
83 config.camera_direction.z = direction[2];
84 }
85 }
86 if (camera.contains("movementSpeed") && camera["movementSpeed"].is_number_float())
87 {
88 config.camera_move_speed = camera["movementSpeed"];
89 }
90 if (camera.contains("rotationSpeed") && camera["rotationSpeed"].is_number_float())
91 {
92 config.camera_look_speed = camera["rotationSpeed"];
93 }
94 if (camera.contains("fov") && camera["fov"].is_number_unsigned())
95 {
96 config.camera_fov = camera["fov"];
97 }
98 if (camera.contains("nearPlane") && camera["nearPlane"].is_number_float())
99 {
100 config.camera_near_plane = camera["nearPlane"];
101 }
102 if (camera.contains("farPlane") && camera["farPlane"].is_number_float())
103 {
104 config.camera_far_plane = camera["farPlane"];
105 }
106 }
107 if (j.contains("log"))
108 {
109 if (const auto &log = j["log"]; log.contains("fps") && log["fps"].is_boolean())
110 {
111 config.log_fps = log["fps"];
112 }
113 }
114 if (j.contains("network"))
115 {
116 const auto &network = j["network"];
117 if (network.contains("host") && network["host"].is_string())
118 {
119 config.network_host = network["host"];
120 }
121 if (network.contains("port") && network["port"].is_number_unsigned())
122 {
123 config.network_port = network["port"];
124 }
125 }
126 if (j.contains("renderer"))
127 {
128 const auto &renderer = j["renderer"];
129 if (renderer.contains("vsync") && renderer["vsync"].is_boolean())
130 {
131 config.renderer_vsync = renderer["vsync"];
132 }
133 if (renderer.contains("frameRateLimit") && renderer["frameRateLimit"].is_number_unsigned())
134 {
135 config.renderer_frame_rate_limit = renderer["frameRateLimit"];
136 }
137 if (renderer.contains("clearColor") && renderer["clearColor"].is_array() && renderer["clearColor"].size() == 4)
138 {
139 if (const auto &clearColor = renderer["clearColor"];
140 clearColor[0].is_number_float() && clearColor[1].is_number_float() && clearColor[2].is_number_float() &&
141 clearColor[3].is_number_float())
142 {
143 config.renderer_clear_color.r = clearColor[0];
144 config.renderer_clear_color.g = clearColor[1];
145 config.renderer_clear_color.b = clearColor[2];
146 config.renderer_clear_color.a = clearColor[3];
147 }
148 }
149 }
150 if (j.contains("window"))
151 {
152 const auto &window = j["window"];
153 if (window.contains("width") && window["width"].is_number_unsigned())
154 {
155 config.window_width = window["width"];
156 }
157 if (window.contains("height") && window["height"].is_number_unsigned())
158 {
159 config.window_height = window["height"];
160 }
161 if (window.contains("fullscreen") && window["fullscreen"].is_boolean())
162 {
163 config.window_fullscreen = window["fullscreen"];
164 }
165 if (window.contains("name") && window["name"].is_string())
166 {
167 config.window_name = window["name"];
168 }
169 if (window.contains("iconPath") && window["iconPath"].is_string())
170 {
171 config.window_icon_path = utl::Path::resolveRelativeToExe(window["iconPath"]).string();
172 }
173 }
174
175 return config;
176}
This file contains the Application class declaration.
This file contains the Logger class.
This file contains Path resolution utilities.
static EngineConfig parseEngineConf(const std::string &path)
Parse the engine configuration file.
Definition conf.cpp:12
static void log(const std::string &message, const LogLevel &logLevel)
Log a message with a specific log level.
Definition Logger.hpp:71
static fs::path resolveRelativeToExe(const fs::path &relativePath)
Resolve a relative path to the executable directory.
Definition Path.hpp:121
static bool existsFile(const fs::path &path)
Check if a file exists.
Definition Path.hpp:61
static fs::path resolveRelativeToCwd(const fs::path &relativePath)
Definition Path.hpp:131
nlohmann::json json
Definition conf.cpp:10
Struct for engine configuration.
Definition Engine.hpp:28
Color renderer_clear_color
Definition Engine.hpp:48
uint16_t window_height
Definition Engine.hpp:54
uint16_t network_port
Definition Engine.hpp:44
uint16_t window_width
Definition Engine.hpp:53
glm::vec3 camera_rotation
Definition Engine.hpp:33
uint16_t renderer_frame_rate_limit
Definition Engine.hpp:47
float camera_far_plane
Definition Engine.hpp:39
float camera_look_speed
Definition Engine.hpp:36
std::string window_name
Definition Engine.hpp:56
std::string network_host
Definition Engine.hpp:43
glm::vec3 camera_position
Definition Engine.hpp:32
glm::vec3 camera_direction
Definition Engine.hpp:34
float audio_master_volume
Definition Engine.hpp:29
std::string window_icon_path
Definition Engine.hpp:57
float camera_move_speed
Definition Engine.hpp:35
float camera_near_plane
Definition Engine.hpp:38
bool window_fullscreen
Definition Engine.hpp:55