settings_menu: Create a dedicated class for the settings
This commit is contained in:
125
src/main.cpp
125
src/main.cpp
@@ -2,7 +2,7 @@
|
||||
* File name: main.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 21:59:32
|
||||
// Date modified: 12-01-2026 20:34:37
|
||||
// Date modified: 12-01-2026 22:17:54
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <types.hpp>
|
||||
#include <context.hpp>
|
||||
#include <control_menu.hpp>
|
||||
#include <settings_menu.hpp>
|
||||
|
||||
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
||||
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
|
||||
@@ -34,7 +35,7 @@ Vector2i screenToGrid(Vector2 screen, int cell_size) {
|
||||
}
|
||||
|
||||
int main(int ac, char **av) {
|
||||
gol::ctx context;
|
||||
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
|
||||
// Load or default config
|
||||
const std::string config_file_name = "config.json";
|
||||
|
||||
@@ -56,7 +57,7 @@ int main(int ac, char **av) {
|
||||
// Try reading the configuration
|
||||
try {
|
||||
config_file.seekg(0);
|
||||
config_file >> context.config_json;
|
||||
config_file >> context->config_json;
|
||||
} catch (const nlohmann::json::parse_error &e) {
|
||||
std::cerr << "An error occured while loading config : " << e.what()
|
||||
<< std::endl;
|
||||
@@ -67,42 +68,43 @@ int main(int ac, char **av) {
|
||||
config_file.close();
|
||||
|
||||
// Check config values or populate them
|
||||
if (!context.config_json.contains("cell_size") ||
|
||||
(context.config_json["cell_size"] < 4 || context.config_json["cell_size"] > 100)) {
|
||||
context.config_json["cell_size"] = 10;
|
||||
if (!context->config_json.contains("cell_size") ||
|
||||
(context->config_json["cell_size"] < 4 || context->config_json["cell_size"] > 100)) {
|
||||
context->config_json["cell_size"] = 10;
|
||||
}
|
||||
if (!context.config_json.contains("screen_width") ||
|
||||
(context.config_json["screen_width"] < 800 ||
|
||||
context.config_json["screen_width"] > 1920)) {
|
||||
context.config_json["screen_width"] = 800;
|
||||
if (!context->config_json.contains("screen_width") ||
|
||||
(context->config_json["screen_width"] < 800 ||
|
||||
context->config_json["screen_width"] > 1920)) {
|
||||
context->config_json["screen_width"] = 800;
|
||||
}
|
||||
if (!context.config_json.contains("screen_height") ||
|
||||
(context.config_json["screen_height"] < 600 ||
|
||||
context.config_json["screen_height"] > 1200)) {
|
||||
context.config_json["screen_height"] = 600;
|
||||
if (!context->config_json.contains("screen_height") ||
|
||||
(context->config_json["screen_height"] < 600 ||
|
||||
context->config_json["screen_height"] > 1200)) {
|
||||
context->config_json["screen_height"] = 600;
|
||||
}
|
||||
if (!context.config_json.contains("dark_theme")) {
|
||||
context.config_json["dark_theme"] = true;
|
||||
if (!context->config_json.contains("dark_theme")) {
|
||||
context->config_json["dark_theme"] = true;
|
||||
}
|
||||
if (!context.config_json.contains("fps") ||
|
||||
(context.config_json["fps"] < 0 || context.config_json["fps"] > 30)) {
|
||||
context.config_json["fps"] = 800;
|
||||
if (!context->config_json.contains("fps") ||
|
||||
(context->config_json["fps"] < 0 || context->config_json["fps"] > 30)) {
|
||||
context->config_json["fps"] = 800;
|
||||
}
|
||||
|
||||
InitWindow(context.config_json["screen_width"], context.config_json["screen_height"],
|
||||
InitWindow(context->config_json["screen_width"], context->config_json["screen_height"],
|
||||
&av[0][2]);
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
rlImGuiSetup(context.config_json["dark_theme"]);
|
||||
rlImGuiSetup(context->config_json["dark_theme"]);
|
||||
// Selection window
|
||||
|
||||
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
|
||||
|
||||
// Initialize objects
|
||||
context.world = std::make_shared<World>(context);
|
||||
context.rules = std::make_shared<Rules>();
|
||||
Render render(context.config_json["cell_size"]);
|
||||
context->world = std::make_shared<World>(context);
|
||||
context->rules = std::make_shared<Rules>();
|
||||
context->settings_menu = std::make_shared<gol::SettingsMenu>(context);
|
||||
context->render = std::make_shared<Render>(context->settings_menu->getCellSize());
|
||||
|
||||
// Imgui control menu
|
||||
gol::ControlMenu control_menu(context);
|
||||
@@ -122,12 +124,12 @@ int main(int ac, char **av) {
|
||||
std::string sel_txt_input_hint("patern name");
|
||||
|
||||
// Setups
|
||||
context.rules->setup(&(*context.world));
|
||||
context->rules->setup(context->world);
|
||||
// Diplay generations
|
||||
while (!WindowShouldClose()) {
|
||||
// Frames shinenigans
|
||||
float deltaTime = GetFrameTime();
|
||||
sim_speed = control_menu.fps_ctrl_ / 10.0f;
|
||||
sim_speed = context->settings_menu->getFPS() / 10.0f;
|
||||
timePerUpdate = (1.0f / 10.0f) / sim_speed;
|
||||
|
||||
control_menu.update();
|
||||
@@ -137,20 +139,20 @@ int main(int ac, char **av) {
|
||||
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
control_menu.menu_state_ = MenuState::EDIT;
|
||||
context.world->setCell(mousePos.x / context.config_json["cell_size"].get<int>(),
|
||||
mousePos.y / context.config_json["cell_size"].get<int>());
|
||||
context->world->setCell(mousePos.x / context->config_json["cell_size"].get<int>(),
|
||||
mousePos.y / context->config_json["cell_size"].get<int>());
|
||||
}
|
||||
|
||||
// Selection behaviour
|
||||
if (!control_menu.edit_ctrl_ && !control_menu.play_ctrl_ && !control_menu.paterns_ctrl_ && !control_menu.settings_window_) {
|
||||
if (!control_menu.edit_ctrl_ && !control_menu.play_ctrl_ && !control_menu.paterns_ctrl_ && !context->settings_menu->isOpen()) {
|
||||
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
sel_pos = snapToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
sel_pos = snapToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
selecting = true;
|
||||
}
|
||||
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < control_menu.width_ctrl_ && mousePos.y >=0 && mousePos.y < control_menu.height_ctrl_ ) {
|
||||
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < context->settings_menu->getWidth() && mousePos.y >=0 && mousePos.y < context->settings_menu->getHeight() ) {
|
||||
selecting = false;
|
||||
Vector2i p1 = screenToGrid(sel_pos, control_menu.cell_size_ctrl_);
|
||||
Vector2i p2 = screenToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
Vector2i p1 = screenToGrid(sel_pos, context->settings_menu->getCellSize());
|
||||
Vector2i p2 = screenToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
// Get origin
|
||||
Vector2i orig = {
|
||||
(p1.x < p2.x)?p1.x:p2.x,
|
||||
@@ -163,7 +165,7 @@ int main(int ac, char **av) {
|
||||
};
|
||||
// Ensure there is at least one cell selected
|
||||
if (!(sel_size.x == 0 || sel_size.y == 0)) {
|
||||
sel_data = std::move(context.world->getSelection(orig, sel_size));
|
||||
sel_data = std::move(context->world->getSelection(orig, sel_size));
|
||||
sel_ctrl = true;
|
||||
}
|
||||
}
|
||||
@@ -171,35 +173,7 @@ int main(int ac, char **av) {
|
||||
if (!sel_ctrl) {
|
||||
patern_name[0] = '\0';
|
||||
}
|
||||
|
||||
if (control_menu.apply_ctrl_) {
|
||||
bool resize_needed = false;
|
||||
context.config_json["fps"] = control_menu.fps_ctrl_;
|
||||
if (context.config_json["screen_width"].get<int>() != control_menu.width_ctrl_ ||
|
||||
context.config_json["screen_height"].get<int>() != control_menu.height_ctrl_) {
|
||||
context.config_json["screen_width"] = control_menu.width_ctrl_;
|
||||
context.config_json["screen_height"] = control_menu.height_ctrl_;
|
||||
rlImGuiShutdown();
|
||||
SetWindowSize(control_menu.width_ctrl_, control_menu.height_ctrl_);
|
||||
rlImGuiSetup(control_menu.dark_theme_ctrl_);
|
||||
resize_needed = true;
|
||||
}
|
||||
if (control_menu.cell_size_ctrl_ != context.config_json["cell_size"].get<int>() ||
|
||||
resize_needed) {
|
||||
context.world->resize(context.config_json["screen_width"].get<int>() / control_menu.cell_size_ctrl_,
|
||||
context.config_json["screen_height"].get<int>() / control_menu.cell_size_ctrl_);
|
||||
render.updateCellSize(control_menu.cell_size_ctrl_);
|
||||
context.rules->newWorld(&(*context.world));
|
||||
context.config_json["cell_size"] = control_menu.cell_size_ctrl_;
|
||||
}
|
||||
if (control_menu.dark_theme_ctrl_ != context.config_json["dark_theme"]) {
|
||||
rlImGuiShutdown();
|
||||
rlImGuiSetup(control_menu.dark_theme_ctrl_);
|
||||
context.config_json["dark_theme"] = control_menu.dark_theme_ctrl_;
|
||||
}
|
||||
control_menu.apply_ctrl_ = false;
|
||||
control_menu.settings_window_ = false;
|
||||
}
|
||||
context->settings_menu->update();
|
||||
|
||||
// Accumulate time and update simulation at the adjusted speed
|
||||
deltaTimeAccumulator += deltaTime;
|
||||
@@ -208,15 +182,15 @@ int main(int ac, char **av) {
|
||||
// Reset accumulator
|
||||
deltaTimeAccumulator -= timePerUpdate;
|
||||
if (control_menu.menu_state_ == MenuState::PLAY) {
|
||||
context.world->saveCompressed();
|
||||
context.rules->update();
|
||||
context->world->saveCompressed();
|
||||
context->rules->update();
|
||||
}
|
||||
}
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
render.display(&(*context.world));
|
||||
context->render->display(context->world);
|
||||
if (selecting) {
|
||||
auto grid_mouse = snapToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
auto grid_mouse = snapToGrid(mousePos, context->settings_menu->getCellSize());
|
||||
bool sel_x_less = (sel_pos.x < grid_mouse.x);
|
||||
bool sel_y_less = (sel_pos.y < grid_mouse.y);
|
||||
DrawRectangleLines(((sel_x_less)? sel_pos.x: grid_mouse.x), ((sel_y_less)? sel_pos.y: grid_mouse.y),
|
||||
@@ -235,20 +209,7 @@ int main(int ac, char **av) {
|
||||
ImGui::Button("refresh");
|
||||
ImGui::End();
|
||||
}
|
||||
if (control_menu.settings_window_) {
|
||||
ImGuiWindowFlags settings_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::Begin("Settings", &control_menu.settings_window_, settings_flags);
|
||||
ImGui::SliderInt("Window Width", &control_menu.width_ctrl_, 800, 1920);
|
||||
ImGui::SliderInt("Window Height", &control_menu.height_ctrl_, 600, 1200);
|
||||
ImGui::SliderInt("FPS", &control_menu.fps_ctrl_, 0, 30);
|
||||
ImGui::SliderInt("Cell Size", &control_menu.cell_size_ctrl_, 4, 100);
|
||||
ImGui::Checkbox("Dark Theme", &control_menu.dark_theme_ctrl_);
|
||||
if (ImGui::Button("Save & Apply")) {
|
||||
control_menu.apply_ctrl_ = true;
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
context->settings_menu->display();
|
||||
if (sel_ctrl) {
|
||||
BeginTextureMode(selectionTexture);
|
||||
ClearBackground(BLACK);
|
||||
@@ -318,7 +279,7 @@ int main(int ac, char **av) {
|
||||
EndDrawing();
|
||||
}
|
||||
config_file.open(config_file_name, std::ios::out | std::ios::trunc);
|
||||
config_file << context.config_json.dump(2);
|
||||
config_file << context->config_json.dump(2);
|
||||
config_file.close();
|
||||
// Cleanup Selection texture
|
||||
UnloadRenderTexture(selectionTexture);
|
||||
|
||||
Reference in New Issue
Block a user