Skip to content

Commit e9db998

Browse files
authored
Merge pull request #174 from Dragonfly911117/runtime-config
Runtime config
2 parents a1e573b + a1e92a1 commit e9db998

File tree

12 files changed

+144
-34
lines changed

12 files changed

+144
-34
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Dependencies.cmake)
2828

2929
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
3030
set(SRC_FILES
31+
${SRC_DIR}/config.cpp
32+
3133
${SRC_DIR}/Core/Context.cpp
3234
${SRC_DIR}/Core/DebugMessageCallback.cpp
3335
${SRC_DIR}/Core/VertexArray.cpp

cmake/Dependencies.cmake

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ FetchContent_Declare(
7676
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/imgui
7777
)
7878

79+
FetchContent_Declare(
80+
nlohmann_json
81+
82+
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
83+
URL_HASH MD5=c23a33f04786d85c29fda8d16b5f0efd
84+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/nlohmann_json
85+
)
86+
7987
set(BUILD_SHARED_LIBS FALSE)
8088

8189
set(SDL2IMAGE_INSTALL OFF)
@@ -94,10 +102,12 @@ set(SDL2MIXER_OGG OFF)
94102
set(SDL2MIXER_VORBIS OFF)
95103
set(SDL2MIXER_VOC OFF)
96104

105+
set(JSON_ImplicitConversions OFF)
106+
97107
# For Windows: Prevent overriding the parent project's compiler/linker settings
98108
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
99109

100-
FetchContent_MakeAvailable(sdl2 sdl2_image sdl2_ttf sdl2_mixer spdlog glm googletest)
110+
FetchContent_MakeAvailable(sdl2 sdl2_image sdl2_ttf sdl2_mixer spdlog glm googletest nlohmann_json)
101111

102112
add_compile_definitions(GLEW_NO_GLU)
103113

@@ -134,6 +144,7 @@ if (NOT ${imgui_POPULATED})
134144
)
135145
endif()
136146

147+
137148
set(DEPENDENCY_LINK_LIBRARIES
138149
${OPENGL_LIBRARY}
139150
glew_s
@@ -146,6 +157,8 @@ set(DEPENDENCY_LINK_LIBRARIES
146157
spdlog::spdlog
147158

148159
ImGui
160+
161+
nlohmann_json
149162
)
150163

151164
set(DEPENDENCY_INCLUDE_DIRS
@@ -154,4 +167,5 @@ set(DEPENDENCY_INCLUDE_DIRS
154167
${CMAKE_CURRENT_SOURCE_DIR}/lib/spdlog/include/
155168
${CMAKE_CURRENT_SOURCE_DIR}/lib/glm/
156169
${IMGUI_INCLUDE_DIR}
170+
${CMAKE_CURRENT_SOURCE_DIR}/lib/nlohmann_json/include/
157171
)

example/src/Giraffe.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ void Giraffe::Update() {
2222
auto &scale = m_Transform.scale;
2323
auto &rotation = m_Transform.rotation;
2424

25-
if (pos.y >= static_cast<float>(WINDOW_HEIGHT) / 2 ||
26-
pos.y + static_cast<float>(WINDOW_HEIGHT) / 2 <= 0) {
25+
if (pos.y >= static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2 ||
26+
pos.y + static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2 <= 0) {
2727
dir.y *= -1;
2828
}
29-
if (pos.x >= static_cast<float>(WINDOW_WIDTH) / 2 ||
30-
pos.x + static_cast<float>(WINDOW_WIDTH) / 2 <= 0) {
29+
if (pos.x >= static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2 ||
30+
pos.x + static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2 <= 0) {
3131
dir.x *= -1;
3232
}
3333

include/Core/Context.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ class Context {
4343
static std::shared_ptr<Context> s_Instance;
4444
bool m_Exit = false;
4545

46-
unsigned int m_WindowWidth = WINDOW_WIDTH;
47-
unsigned int m_WindowHeight = WINDOW_HEIGHT;
46+
unsigned int m_WindowWidth = PTSD_Config::WINDOW_WIDTH;
47+
unsigned int m_WindowHeight = PTSD_Config::WINDOW_HEIGHT;
4848

4949
// Can't access Time::s_Now, so using this variable to track time.
5050
Util::ms_t m_BeforeUpdateTime = Util::Time::GetElapsedTimeMs();
5151
};
52-
5352
} // namespace Core
5453

5554
#endif

include/config.hpp

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,69 @@
11
#ifndef CONFIG_H
22
#define CONFIG_H
33

4+
#include "Util/Logger.hpp"
45
#include "pch.hpp" // IWYU pragma: export
56

6-
#include "Util/Logger.hpp"
7+
namespace Core {
8+
class Context;
9+
}
710

8-
constexpr const char *TITLE = "Practical Tools for Simple Design";
11+
// clang-format off
12+
/**
13+
* To use, place a config.json file either in the current directory (.) or
14+
* the parent directory (..). If both directories contain the file, the one
15+
* in the current directory will be read.
16+
*
17+
* Example directory structure:
18+
*
19+
* @code{.md}
20+
* ├── build/
21+
* │ ├── YOUR_GAME
22+
* │ └── config.json <- place here
23+
* └── config.json <- or here
24+
* @endcode
25+
*
26+
* The config.json file can contain any number of configurations, each in
27+
* the following format:
28+
*
29+
* @code{.json}
30+
* {
31+
* "title": "<string, default is 'Practice-Tools-for-Simple-Design'>",
32+
* "window_pos_x": <int, default is SDL_WINDOWPOS_UNDEFINED (wherever the OS places the window)>,
33+
* "window_pos_y": <int, default is SDL_WINDOWPOS_UNDEFINED (wherever the OS places the window)>,
34+
* "window_width": <int, default is 1280>,
35+
* "window_height": <int, default is 720>,
36+
* "default_log_level": <int in range [0, 5], default is 2 (Util::Logger::Level::INFO)>,
37+
* "fps_cap": <int, default is 60>
38+
* }
39+
* @endcode
40+
*
41+
* If a key is not present in the config.json, the default value will be
42+
* used.
43+
*/
44+
// clang-format on
45+
struct PTSD_Config {
46+
public:
47+
static std::string_view TITLE;
948

10-
constexpr int WINDOW_POS_X = SDL_WINDOWPOS_UNDEFINED;
11-
constexpr int WINDOW_POS_Y = SDL_WINDOWPOS_UNDEFINED;
49+
static int WINDOW_POS_X;
1250

13-
constexpr unsigned int WINDOW_WIDTH = 1280;
14-
constexpr unsigned int WINDOW_HEIGHT = 720;
51+
static int WINDOW_POS_Y;
52+
static unsigned int WINDOW_WIDTH;
1553

16-
constexpr Util::Logger::Level DEFAULT_LOG_LEVEL = Util::Logger::Level::DEBUG;
54+
static unsigned int WINDOW_HEIGHT;
55+
static Util::Logger::Level DEFAULT_LOG_LEVEL;
1756

18-
/**
19-
* @brief FPS limit
20-
*
21-
* Set value to 0 to turn off FPS cap
22-
*/
23-
constexpr unsigned int FPS_CAP = 60;
57+
/**
58+
* @brief FPS limit
59+
*
60+
* Set value to 0 to turn off FPS cap
61+
*/
62+
static unsigned int FPS_CAP;
63+
64+
private:
65+
friend class Core::Context;
66+
static void Init();
67+
}; // namespace PTSD_Config
2468

2569
#endif

include/pch.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
#include <imgui_impl_opengl3.h>
2727
#include <imgui_impl_sdl2.h>
2828

29+
#include <nlohmann/json.hpp>
30+
2931
#endif

src/Core/Context.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ using Util::ms_t;
1515
namespace Core {
1616
Context::Context() {
1717
Util::Logger::Init();
18+
PTSD_Config::Init();
19+
Util::Logger::SetLevel(PTSD_Config::DEFAULT_LOG_LEVEL);
1820

1921
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
2022
LOG_ERROR("Failed to initialize SDL");
@@ -41,9 +43,10 @@ Context::Context() {
4143
LOG_ERROR(SDL_GetError());
4244
}
4345

44-
m_Window =
45-
SDL_CreateWindow(TITLE, WINDOW_POS_X, WINDOW_POS_Y, WINDOW_WIDTH,
46-
WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
46+
m_Window = SDL_CreateWindow(
47+
PTSD_Config::TITLE.data(), PTSD_Config::WINDOW_POS_X,
48+
PTSD_Config::WINDOW_POS_Y, PTSD_Config::WINDOW_WIDTH,
49+
PTSD_Config::WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
4750

4851
if (m_Window == nullptr) {
4952
LOG_ERROR("Failed to create window");
@@ -124,7 +127,8 @@ void Context::Update() {
124127
SDL_GL_SwapWindow(m_Window);
125128
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
126129

127-
constexpr ms_t frameTime = FPS_CAP != 0 ? 1000.0F / FPS_CAP : 0;
130+
static ms_t frameTime =
131+
PTSD_Config::FPS_CAP != 0 ? 1000.0F / PTSD_Config::FPS_CAP : 0;
128132
ms_t afterUpdate = Util::Time::GetElapsedTimeMs();
129133
ms_t updateTime = afterUpdate - m_BeforeUpdateTime;
130134
if (updateTime < frameTime) {

src/Util/Input.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ void Input::Update() {
8181
s_CursorPosition.x = static_cast<float>(x);
8282
s_CursorPosition.y = static_cast<float>(y);
8383

84-
s_CursorPosition.x -= static_cast<float>(WINDOW_WIDTH) / 2;
85-
s_CursorPosition.y =
86-
-(s_CursorPosition.y - static_cast<float>(WINDOW_HEIGHT) / 2);
84+
s_CursorPosition.x -= static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2;
85+
s_CursorPosition.y = -(s_CursorPosition.y -
86+
static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2);
8787

8888
s_Scroll = s_MouseMoving = false;
8989

src/Util/Logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Util {
66
void Logger::Init() {
77
spdlog::set_pattern("%n [%^%l%$] %v");
8-
SetLevel(DEFAULT_LOG_LEVEL);
8+
SetLevel(Level::INFO);
99
}
1010

1111
void Logger::SetLevel(Logger::Level level) {

src/Util/Text.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "Util/Text.hpp"
88
#include "Util/TransformUtils.hpp"
99

10-
#include "config.hpp"
11-
1210
namespace Util {
1311
Text::Text(const std::string &font, int fontSize, const std::string &text,
1412
const Util::Color &color)

0 commit comments

Comments
 (0)