Adding new context with world & config_json
This commit is contained in:
24
includes/context.hpp
Normal file
24
includes/context.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* File name: context.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 10-01-2026 22:24:41
|
||||
* ------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <world.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
typedef struct ctx {
|
||||
std::shared_ptr<World> world = nullptr;
|
||||
nlohmann::json config_json;
|
||||
} ctx;
|
||||
|
||||
} // namespace gol
|
||||
39
includes/control_menu.hpp
Normal file
39
includes/control_menu.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File name: control_menu.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 22:00:33
|
||||
// Date modified: 10-01-2026 22:18:25
|
||||
* ------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <context.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
class ControlMenu {
|
||||
public:
|
||||
ControlMenu(ctx context);
|
||||
~ControlMenu() = default;
|
||||
void update();
|
||||
void display();
|
||||
private:
|
||||
ctx context_;
|
||||
int fps_ctrl_ = false;
|
||||
int cell_size_ctrl_ = false;
|
||||
bool play_ctrl_ = true;
|
||||
bool step_ctrl_ = false;
|
||||
bool step_back_ctrl_ = false;
|
||||
bool rand_ctrl_ = false;
|
||||
bool edit_ctrl_ = false;
|
||||
bool clear_ctrl_ = false;
|
||||
bool settings_window_ = false;
|
||||
bool paterns_ctrl_ = false;
|
||||
int width_ctrl_ = false;
|
||||
int height_ctrl_ = false;
|
||||
bool dark_theme_ctrl_ = false;
|
||||
bool apply_ctrl_ = false;
|
||||
};
|
||||
|
||||
} // namespace gol
|
||||
17
src/control_menu.cpp
Normal file
17
src/control_menu.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* File name: control_menu.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 22:12:44
|
||||
// Date modified: 10-01-2026 22:21:20
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <control_menu.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
ControlMenu::ControlMenu(ctx context):context_(context) {
|
||||
// TODO: load content from json
|
||||
}
|
||||
|
||||
} // namespace gol
|
||||
126
src/main.cpp
126
src/main.cpp
@@ -2,7 +2,7 @@
|
||||
* File name: main.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 21:59:32
|
||||
// Date modified: 10-01-2026 21:59:38
|
||||
// Date modified: 10-01-2026 22:39:36
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -12,14 +12,15 @@
|
||||
#include <unistd.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../includes/render.hpp"
|
||||
#include "../includes/rules.hpp"
|
||||
#include "../includes/world.hpp"
|
||||
#include "../includes/types.hpp"
|
||||
#include "imgui.h"
|
||||
#include "raylib.h"
|
||||
#include "rlImGui.h"
|
||||
#include <imgui.h>
|
||||
#include <raylib.h>
|
||||
#include <rlImGui.h>
|
||||
|
||||
#include <render.hpp>
|
||||
#include <rules.hpp>
|
||||
#include <world.hpp>
|
||||
#include <types.hpp>
|
||||
#include <context.hpp>
|
||||
|
||||
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
||||
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
|
||||
@@ -32,9 +33,9 @@ Vector2i screenToGrid(Vector2 screen, int cell_size) {
|
||||
}
|
||||
|
||||
int main(int ac, char **av) {
|
||||
gol::ctx context;
|
||||
// Load or default config
|
||||
const std::string config_file_name = "config.json";
|
||||
nlohmann::json config_json;
|
||||
MenuState menu_state = MenuState::NONE;
|
||||
|
||||
std::fstream config_file(config_file_name, std::ios::in | std::ios::out);
|
||||
@@ -55,7 +56,7 @@ int main(int ac, char **av) {
|
||||
// Try reading the configuration
|
||||
try {
|
||||
config_file.seekg(0);
|
||||
config_file >> 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;
|
||||
@@ -66,49 +67,50 @@ int main(int ac, char **av) {
|
||||
config_file.close();
|
||||
|
||||
// Check config values or populate them
|
||||
if (!config_json.contains("cell_size") ||
|
||||
(config_json["cell_size"] < 4 || config_json["cell_size"] > 100)) {
|
||||
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 (!config_json.contains("screen_width") ||
|
||||
(config_json["screen_width"] < 800 ||
|
||||
config_json["screen_width"] > 1920)) {
|
||||
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 (!config_json.contains("screen_height") ||
|
||||
(config_json["screen_height"] < 600 ||
|
||||
config_json["screen_height"] > 1200)) {
|
||||
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 (!config_json.contains("dark_theme")) {
|
||||
config_json["dark_theme"] = true;
|
||||
if (!context.config_json.contains("dark_theme")) {
|
||||
context.config_json["dark_theme"] = true;
|
||||
}
|
||||
if (!config_json.contains("fps") ||
|
||||
(config_json["fps"] < 0 || config_json["fps"] > 30)) {
|
||||
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(config_json["screen_width"], config_json["screen_height"],
|
||||
InitWindow(context.config_json["screen_width"], context.config_json["screen_height"],
|
||||
&av[0][2]);
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
rlImGuiSetup(config_json["dark_theme"]);
|
||||
rlImGuiSetup(context.config_json["dark_theme"]);
|
||||
// Selection window
|
||||
|
||||
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
|
||||
|
||||
// Initialize objects
|
||||
World world(config_json["screen_width"].get<int>() /
|
||||
config_json["cell_size"].get<int>(),
|
||||
config_json["screen_height"].get<int>() /
|
||||
config_json["cell_size"].get<int>());
|
||||
context.world = std::make_shared<World>(
|
||||
context.config_json["screen_width"].get<int>() /
|
||||
context.config_json["cell_size"].get<int>(),
|
||||
context.config_json["screen_height"].get<int>() /
|
||||
context.config_json["cell_size"].get<int>());
|
||||
Rules rules = Rules();
|
||||
Render render(config_json["cell_size"]);
|
||||
Render render(context.config_json["cell_size"]);
|
||||
|
||||
// Imgui control menu
|
||||
int fps_ctrl = config_json["fps"].get<int>();
|
||||
int cell_size_ctrl = config_json["cell_size"].get<int>();
|
||||
int fps_ctrl = context.config_json["fps"].get<int>();
|
||||
int cell_size_ctrl = context.config_json["cell_size"].get<int>();
|
||||
bool play_ctrl = true;
|
||||
bool step_ctrl = false;
|
||||
bool step_back_ctrl = false;
|
||||
@@ -117,9 +119,9 @@ int main(int ac, char **av) {
|
||||
bool clear_ctrl = false;
|
||||
bool settings_window = false;
|
||||
bool paterns_ctrl = false;
|
||||
int width_ctrl = config_json["screen_width"].get<int>();
|
||||
int height_ctrl = config_json["screen_height"].get<int>();
|
||||
bool dark_theme_ctrl = config_json["dark_theme"].get<bool>();
|
||||
int width_ctrl = context.config_json["screen_width"].get<int>();
|
||||
int height_ctrl = context.config_json["screen_height"].get<int>();
|
||||
bool dark_theme_ctrl = context.config_json["dark_theme"].get<bool>();
|
||||
bool apply_ctrl = false;
|
||||
|
||||
// Speed handling values
|
||||
@@ -137,7 +139,7 @@ int main(int ac, char **av) {
|
||||
std::string sel_txt_input_hint("patern name");
|
||||
|
||||
// Setups
|
||||
rules.setup(&world);
|
||||
rules.setup(&(*context.world));
|
||||
// Diplay generations
|
||||
while (!WindowShouldClose()) {
|
||||
if (menu_state == MenuState::PLAY || menu_state == MenuState::EDIT) {
|
||||
@@ -153,11 +155,11 @@ int main(int ac, char **av) {
|
||||
auto gesture = GetGestureDetected();
|
||||
auto mousePos = GetMousePosition();
|
||||
if (rand_ctrl) {
|
||||
world.randomize();
|
||||
context.world->randomize();
|
||||
rand_ctrl = false;
|
||||
}
|
||||
if (clear_ctrl) {
|
||||
world.clear();
|
||||
context.world->clear();
|
||||
clear_ctrl = false;
|
||||
}
|
||||
if (edit_ctrl && play_ctrl) {
|
||||
@@ -176,8 +178,8 @@ int main(int ac, char **av) {
|
||||
if (edit_ctrl && IsMouseButtonPressed(0) &&
|
||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
menu_state = MenuState::EDIT;
|
||||
world.setCell(mousePos.x / config_json["cell_size"].get<int>(),
|
||||
mousePos.y / 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 (!edit_ctrl && !play_ctrl && !paterns_ctrl) {
|
||||
@@ -201,7 +203,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(world.getSelection(orig, sel_size));
|
||||
sel_data = std::move(context.world->getSelection(orig, sel_size));
|
||||
sel_ctrl = true;
|
||||
}
|
||||
}
|
||||
@@ -210,39 +212,39 @@ int main(int ac, char **av) {
|
||||
patern_name[0] = '\0';
|
||||
}
|
||||
if (step_ctrl) {
|
||||
world.saveCompressed();
|
||||
context.world->saveCompressed();
|
||||
rules.update();
|
||||
step_ctrl = false;
|
||||
}
|
||||
if (step_back_ctrl) {
|
||||
world.stepBack();
|
||||
context.world->stepBack();
|
||||
step_back_ctrl = false;
|
||||
}
|
||||
|
||||
if (apply_ctrl) {
|
||||
bool resize_needed = false;
|
||||
config_json["fps"] = fps_ctrl;
|
||||
if (config_json["screen_width"].get<int>() != width_ctrl ||
|
||||
config_json["screen_height"].get<int>() != height_ctrl) {
|
||||
config_json["screen_width"] = width_ctrl;
|
||||
config_json["screen_height"] = height_ctrl;
|
||||
context.config_json["fps"] = fps_ctrl;
|
||||
if (context.config_json["screen_width"].get<int>() != width_ctrl ||
|
||||
context.config_json["screen_height"].get<int>() != height_ctrl) {
|
||||
context.config_json["screen_width"] = width_ctrl;
|
||||
context.config_json["screen_height"] = height_ctrl;
|
||||
rlImGuiShutdown();
|
||||
SetWindowSize(width_ctrl, height_ctrl);
|
||||
rlImGuiSetup(dark_theme_ctrl);
|
||||
resize_needed = true;
|
||||
}
|
||||
if (cell_size_ctrl != config_json["cell_size"].get<int>() ||
|
||||
if (cell_size_ctrl != context.config_json["cell_size"].get<int>() ||
|
||||
resize_needed) {
|
||||
world.resize(config_json["screen_width"].get<int>() / cell_size_ctrl,
|
||||
config_json["screen_height"].get<int>() / cell_size_ctrl);
|
||||
context.world->resize(context.config_json["screen_width"].get<int>() / cell_size_ctrl,
|
||||
context.config_json["screen_height"].get<int>() / cell_size_ctrl);
|
||||
render.updateCellSize(cell_size_ctrl);
|
||||
rules.newWorld(&world);
|
||||
config_json["cell_size"] = cell_size_ctrl;
|
||||
rules.newWorld(&(*context.world));
|
||||
context.config_json["cell_size"] = cell_size_ctrl;
|
||||
}
|
||||
if (dark_theme_ctrl != config_json["dark_theme"]) {
|
||||
if (dark_theme_ctrl != context.config_json["dark_theme"]) {
|
||||
rlImGuiShutdown();
|
||||
rlImGuiSetup(dark_theme_ctrl);
|
||||
config_json["dark_theme"] = dark_theme_ctrl;
|
||||
context.config_json["dark_theme"] = dark_theme_ctrl;
|
||||
}
|
||||
apply_ctrl = false;
|
||||
settings_window = false;
|
||||
@@ -255,13 +257,13 @@ int main(int ac, char **av) {
|
||||
// Reset accumulator
|
||||
deltaTimeAccumulator -= timePerUpdate;
|
||||
if (menu_state == MenuState::PLAY) {
|
||||
world.saveCompressed();
|
||||
context.world->saveCompressed();
|
||||
rules.update();
|
||||
}
|
||||
}
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
render.display(&world);
|
||||
render.display(&(*context.world));
|
||||
if (selecting) {
|
||||
auto grid_mouse = snapToGrid(mousePos, cell_size_ctrl);
|
||||
bool sel_x_less = (sel_pos.x < grid_mouse.x);
|
||||
@@ -296,7 +298,7 @@ int main(int ac, char **av) {
|
||||
if (ImGui::Button((settings_window) ? "Hide settings" : "Show settings")) {
|
||||
settings_window = !settings_window;
|
||||
}
|
||||
ImGui::Text("Generation: %zu", world.getCycle());
|
||||
ImGui::Text("Generation: %zu", context.world->getCycle());
|
||||
ImGui::End();
|
||||
if (paterns_ctrl) {
|
||||
ImGuiWindowFlags paterns_flags =
|
||||
@@ -389,7 +391,7 @@ int main(int ac, char **av) {
|
||||
EndDrawing();
|
||||
}
|
||||
config_file.open(config_file_name, std::ios::out | std::ios::trunc);
|
||||
config_file << 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