Merge pull request 'lju/refactorize' (#2) from lju/refactorize into main

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
2026-01-13 13:50:58 +00:00
21 changed files with 837 additions and 397 deletions

View File

@@ -70,6 +70,11 @@ set(SRC_CXX_FILES "./src/main.cpp"
"./src/rules.cpp"
"./src/world.cpp"
"./src/render.cpp"
"./src/control_menu.cpp"
"./src/settings_menu.cpp"
"./src/selection_menu.cpp"
"./src/selection.cpp"
"./src/paterns_menu.cpp"
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
"${imgui_SOURCE_DIR}/imgui.cpp"
"${imgui_SOURCE_DIR}/imgui_draw.cpp"

38
includes/context.hpp Normal file
View File

@@ -0,0 +1,38 @@
/*
* File name: context.hpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#pragma once
#include <nlohmann/json.hpp>
#include <memory>
class World;
class Rules;
class Render;
namespace gol {
class SettingsMenu;
class SelectionMenu;
class ControlMenu;
class Selection;
class PaternsMenu;
typedef struct ctx {
std::shared_ptr<World> world = nullptr;
std::shared_ptr<Rules> rules = nullptr;
std::shared_ptr<Render> render = nullptr;
std::shared_ptr<SettingsMenu> settings_menu = nullptr;
std::shared_ptr<SelectionMenu> selection_menu = nullptr;
std::shared_ptr<ControlMenu> control_menu = nullptr;
std::shared_ptr<Selection> selection = nullptr;
std::shared_ptr<PaternsMenu> paterns_menu = nullptr;
nlohmann::json config_json;
} ctx;
} // namespace gol

36
includes/control_menu.hpp Normal file
View File

@@ -0,0 +1,36 @@
/*
* File name: control_menu.hpp
* Author: lejulien
* Date created: 10-01-2026 22:00:33
// Date modified: 12-01-2026 22:18:26
* ------
*/
#pragma once
#include <memory>
#include <context.hpp>
#include <types.hpp>
namespace gol {
class ControlMenu {
public:
ControlMenu(std::shared_ptr<ctx> context);
~ControlMenu() = default;
void update();
void display();
private:
std::shared_ptr<ctx> context_;
public: // Keep those public for easy access
MenuState menu_state_ = MenuState::NONE;
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;
};
} // namespace gol

24
includes/paterns_menu.hpp Normal file
View File

@@ -0,0 +1,24 @@
/*
* File name: paterns_menu.hpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#pragma once
namespace gol {
class PaternsMenu {
public:
PaternsMenu() = default;
~PaternsMenu() = default;
void Toogle();
bool isOpen();
void display();
private:
bool is_open_ = false;
};
} // namespace gol

View File

@@ -1,14 +1,12 @@
/* ************************************************************************** */
/* */
/* / ) */
/* render.hpp (\__/) ( ( */
/* ) ( ) ) */
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
/* ) `-------/ / */
/* Created: 2023/01/09 12:44:54 by lejulien ( / */
/* Updated: 2023/01/14 16:51:15 by lejulien \ | */
/* */
/* ************************************************************************** */
/*
* File name: render.hpp
* Author: lejulien
* Date created: 10-01-2026 21:54:12
// Date modified: 12-01-2026 21:56:03
* ------
*/
#include <memory>
#include "world.hpp"
@@ -20,7 +18,7 @@ public:
Render(int cell_size);
// Member function
void display(World *world);
void display(std::shared_ptr<World> world);
void updateCellSize(int new_size);
private:

View File

@@ -1,14 +1,10 @@
/* ************************************************************************** */
/* */
/* / ) */
/* rules.hpp (\__/) ( ( */
/* ) ( ) ) */
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
/* ) `-------/ / */
/* Created: 2023/01/09 12:16:47 by lejulien ( / */
/* Updated: 2023/01/14 16:47:04 by lejulien \ | */
/* */
/* ************************************************************************** */
/*
* File name: rules.hpp
* Author: lejulien
* Date created: 09-01-2026 23:59:55
// Date modified: 12-01-2026 21:58:17
* ------
*/
#include "world.hpp"
@@ -23,12 +19,12 @@ private:
public:
Rules();
void setup(World *world);
void newWorld(World *world);
void setup(std::shared_ptr<World> world);
void newWorld(std::shared_ptr<World> world);
void update();
private:
World *_world;
std::shared_ptr<World> _world;
std::vector<bool> _buffer;
int _width;
int _height;

32
includes/selection.hpp Normal file
View File

@@ -0,0 +1,32 @@
/*
* File name: selection.hpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#pragma once
#include <context.hpp>
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
namespace gol {
class Selection {
public:
Selection(std::shared_ptr<ctx>);
~Selection() = default;
void update();
void display();
private:
std::shared_ptr<ctx> context_ = nullptr;
Vector2 sel_pos_ = {0., 0.};
Vector2 mouse_pos_ = {0., 0.};
bool selecting_ = false;
};
} // namespace gol

View File

@@ -0,0 +1,38 @@
/*
* File name: selection_menu.hpp
* Author: lejulien
* Date created: 13-01-2026 22:12:44
// Date modified: 13-01-2026 22:18:58
* ------
*/
#pragma once
#include <memory>
#include <string>
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
#include <context.hpp>
namespace gol {
class SelectionMenu {
public:
SelectionMenu(std::shared_ptr<ctx> context);
~SelectionMenu();
void update();
void display();
void setSelection(std::vector<uint32_t> selection);
void open();
private:
std::shared_ptr<ctx> context_;
RenderTexture2D selectionTexture_;
std::vector<uint32_t> sel_data_ = {};
bool sel_ctrl_ = false;
char patern_name_[255];
};
} // namespace gol

View File

@@ -0,0 +1,46 @@
/*
* File name: settings_menu.hpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 22:16:29
* ------
*/
#pragma once
#include <memory>
#include <context.hpp>
namespace gol {
class SettingsMenu {
public:
SettingsMenu(std::shared_ptr<ctx> context);
~SettingsMenu() = default;
void update();
void display();
bool isOpen();
void Toogle();
// Getter/Setters
int getFPS();
void setFPS(int);
int getCellSize();
void setCellSize(int);
int getWidth();
void setWidth(int);
int getHeight();
void setHeight(int);
private:
std::shared_ptr<ctx> context_ = nullptr;
int fps_ctrl_ = false;
int cell_size_ctrl_ = false;
bool settings_window_ = false;
int width_ctrl_ = false;
int height_ctrl_ = false;
bool dark_theme_ctrl_ = false;
bool apply_ctrl_ = false;
};
} // namespace gol

24
includes/snapping.hpp Normal file
View File

@@ -0,0 +1,24 @@
/*
* File name: selection.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#pragma once
#include <types.hpp>
#include <raylib.h>
#include <cmath>
Vector2 snapToGrid(Vector2 screen, int cell_size) {
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
static_cast<float>(round(screen.y / cell_size) * cell_size)};
}
Vector2i screenToGrid(Vector2 screen, int cell_size) {
return {static_cast<int>(round(screen.x / cell_size)),
static_cast<int>(round(screen.y / cell_size))};
}

View File

@@ -1,3 +1,11 @@
/*
* File name: types.hpp
* Author: lejulien
* Date created: 09-01-2026 23:59:55
* Date modified: 10-01-2026 21:49:36
* ------
*/
#pragma once
enum class MenuState {

View File

@@ -1,11 +1,20 @@
/*
* File name: world.hpp
* Author: lejulien
* Date created: 09-01-2026 23:59:55
// Date modified: 12-01-2026 22:20:26
* ------
*/
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
#include <memory>
#include <time.h>
#include <vector>
#include "../includes/types.hpp"
#include <types.hpp>
#include <context.hpp>
#pragma once
@@ -14,7 +23,7 @@
class World {
public:
World(int width, int height);
World(std::shared_ptr<gol::ctx> ctx);
~World();
std::vector<bool> *getWorldData();

88
src/control_menu.cpp Normal file
View File

@@ -0,0 +1,88 @@
/*
* File name: control_menu.cpp
* Author: lejulien
* Date created: 10-01-2026 22:12:44
// Date modified: 12-01-2026 22:18:58
* ------
*/
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
#include <control_menu.hpp>
#include <settings_menu.hpp>
#include <paterns_menu.hpp>
#include <world.hpp>
#include <rules.hpp>
namespace gol {
ControlMenu::ControlMenu(std::shared_ptr<ctx> context):context_(context) {
}
void ControlMenu::update() {
if (menu_state_ == MenuState::PLAY || menu_state_ == MenuState::EDIT) {
if (!play_ctrl_ && !edit_ctrl_) {
menu_state_ = MenuState::NONE;
}
}
if (rand_ctrl_) {
context_->world->randomize();
rand_ctrl_ = false;
}
if (clear_ctrl_) {
context_->world->clear();
clear_ctrl_ = false;
}
if (edit_ctrl_ && play_ctrl_) {
if (menu_state_ == MenuState::PLAY) {
menu_state_ = MenuState::EDIT;
play_ctrl_ = false;
} else if (menu_state_ == MenuState::EDIT) {
menu_state_ = MenuState::PLAY;
edit_ctrl_ = false;
}
} else if (play_ctrl_) {
menu_state_ = MenuState::PLAY;
} else if (edit_ctrl_) {
menu_state_ = MenuState::EDIT;
}
if (step_ctrl_) {
context_->world->saveCompressed();
context_->rules->update();
step_ctrl_ = false;
}
if (step_back_ctrl_) {
context_->world->stepBack();
step_back_ctrl_ = false;
}
}
void ControlMenu::display() {
bool control_panel_open = true;
ImGuiWindowFlags control_panel_flags = ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoResize;
ImVec2 menu_pos(3, 3);
ImGui::SetNextWindowPos(menu_pos, ImGuiCond_Always);
ImGui::Begin("Control Panel", &control_panel_open, control_panel_flags);
ImGui::Checkbox("Play", &play_ctrl_);
if (ImGui::Button("Step")) {
step_ctrl_ = true;
}
if (ImGui::Button("Step Back")) {
step_back_ctrl_ = true;
}
ImGui::Checkbox("Edit", &edit_ctrl_);
ImGui::Checkbox("Clear", &clear_ctrl_);
ImGui::Checkbox("Randomize", &rand_ctrl_);
if (ImGui::Button((context_->paterns_menu->isOpen()) ? "Hide paterns" : "Show paterns")) {
context_->paterns_menu->Toogle();
}
if (ImGui::Button((context_->settings_menu->isOpen()) ? "Hide settings" : "Show settings")) {
context_->settings_menu->Toogle();
}
ImGui::Text("Generation: %zu", context_->world->getCycle());
ImGui::End();}
} // namespace gol

View File

@@ -1,33 +1,35 @@
/*
* File name: main.cpp
* Author: lejulien
* Date created: 10-01-2026 21:59:32
// Date modified: 12-01-2026 22:17:54
* ------
*/
#include <fstream>
#include <iostream>
#include <filesystem>
#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>
Vector2 snapToGrid(Vector2 screen, int cell_size) {
return {static_cast<float>(round(screen.x / cell_size) * cell_size),
static_cast<float>(round(screen.y / cell_size) * cell_size)};
}
Vector2i screenToGrid(Vector2 screen, int cell_size) {
return {static_cast<int>(round(screen.x / cell_size)),
static_cast<int>(round(screen.y / cell_size))};
}
#include <render.hpp>
#include <rules.hpp>
#include <world.hpp>
#include <types.hpp>
#include <context.hpp>
#include <control_menu.hpp>
#include <settings_menu.hpp>
#include <selection_menu.hpp>
#include <selection.hpp>
#include <paterns_menu.hpp>
int main(int ac, char **av) {
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
// 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);
@@ -47,7 +49,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;
@@ -58,187 +60,74 @@ 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"]);
// Selection window
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
rlImGuiSetup(context->config_json["dark_theme"]);
// 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>());
Rules rules = Rules();
Render render(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>();
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 = 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>();
bool apply_ctrl = false;
context->control_menu = std::make_shared<gol::ControlMenu>(context);
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());
context->selection_menu = std::make_shared<gol::SelectionMenu>(context);
context->selection = std::make_shared<gol::Selection>(context);
context->paterns_menu = std::make_shared<gol::PaternsMenu>();
// Speed handling values
float sim_speed = 1.0f;
float deltaTimeAccumulator = 0.0f;
float timePerUpdate = 1.0f / 10.0f;
// Selection window
Vector2 sel_pos = {0., 0.};
bool selecting = false;
std::vector<uint32_t> sel_data = {};
bool sel_ctrl = false;
char patern_name[255];
patern_name[0] = '\0';
std::string sel_txt_input_hint("patern name");
// Setups
rules.setup(&world);
context->rules->setup(context->world);
// Diplay generations
while (!WindowShouldClose()) {
if (menu_state == MenuState::PLAY || menu_state == MenuState::EDIT) {
if (!play_ctrl && !edit_ctrl) {
menu_state = MenuState::NONE;
}
}
// Frames shinenigans
float deltaTime = GetFrameTime();
sim_speed = fps_ctrl / 10.0f;
sim_speed = context->settings_menu->getFPS() / 10.0f;
timePerUpdate = (1.0f / 10.0f) / sim_speed;
context->control_menu->update();
auto gesture = GetGestureDetected();
auto mousePos = GetMousePosition();
if (rand_ctrl) {
world.randomize();
rand_ctrl = false;
}
if (clear_ctrl) {
world.clear();
clear_ctrl = false;
}
if (edit_ctrl && play_ctrl) {
if (menu_state == MenuState::PLAY) {
menu_state = MenuState::EDIT;
play_ctrl = false;
} else if (menu_state == MenuState::EDIT) {
menu_state = MenuState::PLAY;
edit_ctrl = false;
}
} else if (play_ctrl) {
menu_state = MenuState::PLAY;
} else if (edit_ctrl) {
menu_state = MenuState::EDIT;
}
if (edit_ctrl && IsMouseButtonPressed(0) &&
if (context->control_menu->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>());
}
// Selection behaviour
if (!edit_ctrl && !play_ctrl) {
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
sel_pos = snapToGrid(mousePos, cell_size_ctrl);
selecting = true;
}
if (ImGui::IsMouseReleased(0) && selecting == true && mousePos.x >=0 && mousePos.x < width_ctrl && mousePos.y >=0 && mousePos.y < height_ctrl ) {
selecting = false;
Vector2i p1 = screenToGrid(sel_pos, cell_size_ctrl);
Vector2i p2 = screenToGrid(mousePos, cell_size_ctrl);
// Get origin
Vector2i orig = {
(p1.x < p2.x)?p1.x:p2.x,
(p1.y < p2.y)?p1.y:p2.y,
};
// Get selection size
Vector2i sel_size = {
(p1.x > p2.x)?p1.x-p2.x:p2.x-p1.x,
(p1.y > p2.y)?p1.y-p2.y:p2.y-p1.y,
};
// 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_ctrl = true;
}
}
}
if (!sel_ctrl) {
patern_name[0] = '\0';
}
if (step_ctrl) {
world.saveCompressed();
rules.update();
step_ctrl = false;
}
if (step_back_ctrl) {
world.stepBack();
step_back_ctrl = false;
context->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>());
}
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;
rlImGuiShutdown();
SetWindowSize(width_ctrl, height_ctrl);
rlImGuiSetup(dark_theme_ctrl);
resize_needed = true;
}
if (cell_size_ctrl != 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);
render.updateCellSize(cell_size_ctrl);
rules.newWorld(&world);
config_json["cell_size"] = cell_size_ctrl;
}
if (dark_theme_ctrl != config_json["dark_theme"]) {
rlImGuiShutdown();
rlImGuiSetup(dark_theme_ctrl);
config_json["dark_theme"] = dark_theme_ctrl;
}
apply_ctrl = false;
settings_window = false;
}
// Selection behaviour
context->selection->update();
context->selection_menu->update();
context->settings_menu->update();
// Accumulate time and update simulation at the adjusted speed
deltaTimeAccumulator += deltaTime;
@@ -246,145 +135,28 @@ int main(int ac, char **av) {
if (deltaTimeAccumulator >= timePerUpdate) {
// Reset accumulator
deltaTimeAccumulator -= timePerUpdate;
if (menu_state == MenuState::PLAY) {
world.saveCompressed();
rules.update();
if (context->control_menu->menu_state_ == MenuState::PLAY) {
context->world->saveCompressed();
context->rules->update();
}
}
BeginDrawing();
ClearBackground(BLACK);
render.display(&world);
if (selecting) {
auto grid_mouse = snapToGrid(mousePos, cell_size_ctrl);
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),
((sel_x_less)? grid_mouse.x - sel_pos.x : sel_pos.x - grid_mouse.x),
((sel_y_less)? grid_mouse.y - sel_pos.y : sel_pos.y - grid_mouse.y),
RED);
}
context->render->display(context->world);
context->selection->display();
// Start ImGui frame
rlImGuiBegin();
bool control_panel_open = true;
ImGuiWindowFlags control_panel_flags = ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoResize;
ImVec2 menu_pos(3, 3);
ImGui::SetNextWindowPos(menu_pos, ImGuiCond_Always);
ImGui::Begin("Control Panel", &control_panel_open, control_panel_flags);
ImGui::Checkbox("Play", &play_ctrl);
if (ImGui::Button("Step")) {
step_ctrl = true;
}
if (ImGui::Button("Step Back")) {
step_back_ctrl = true;
}
ImGui::Checkbox("Edit", &edit_ctrl);
ImGui::Checkbox("Clear", &clear_ctrl);
ImGui::Checkbox("Randomize", &rand_ctrl);
if (ImGui::Button((paterns_ctrl) ? "Hide paterns" : "Show paterns")) {
paterns_ctrl = !paterns_ctrl;
}
if (ImGui::Button((settings_window) ? "Hide settings" : "Show settings")) {
settings_window = !settings_window;
}
ImGui::Text("Generation: %zu", world.getCycle());
ImGui::End();
if (paterns_ctrl) {
ImGuiWindowFlags paterns_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always);
ImGui::Begin("paterns", &paterns_ctrl, paterns_flags);
ImGui::Button("refresh");
ImGui::End();
}
if (settings_window) {
ImGuiWindowFlags settings_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::Begin("Settings", &settings_window, settings_flags);
ImGui::SliderInt("Window Width", &width_ctrl, 800, 1920);
ImGui::SliderInt("Window Height", &height_ctrl, 600, 1200);
ImGui::SliderInt("FPS", &fps_ctrl, 0, 30);
ImGui::SliderInt("Cell Size", &cell_size_ctrl, 4, 100);
ImGui::Checkbox("Dark Theme", &dark_theme_ctrl);
if (ImGui::Button("Save & Apply")) {
apply_ctrl = true;
}
ImGui::End();
}
if (sel_ctrl) {
BeginTextureMode(selectionTexture);
ClearBackground(BLACK);
auto max_size = (sel_data[0] > sel_data[1]) ? sel_data[0] : sel_data[1];
int fitted_width = 200 / max_size;
auto sel_it = sel_data.begin();
sel_it += 2; // skip dimensions
for (int j = 0; j < sel_data[1]; j++) {
for (int i = 0; i < sel_data[0]; i++) {
if (*sel_it == 1) {
DrawRectangle(i * fitted_width, (sel_data[1] -j-1) * fitted_width, fitted_width, fitted_width, WHITE);
}
sel_it++;
}
}
EndTextureMode();
ImGuiWindowFlags settings_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::Begin("Selection", &sel_ctrl, settings_flags);
rlImGuiImageSize(&selectionTexture.texture, 200, 200);
ImGui::InputText(sel_txt_input_hint.c_str(), patern_name, 255);
if (ImGui::Button("Save")) {
char path_buf[1024];
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1);
if (len != -1) {
// Create paterns dir if not present
std::filesystem::path paterns_dir = std::filesystem::path(path_buf).parent_path() / "paterns";
if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) {
std::cerr << "Failed to create paterns directory" << std::endl;
} else { // Could be optimized by early returning in a function
std::ofstream patern_file;
paterns_dir += '/';
paterns_dir += patern_name;
patern_file.open(paterns_dir);
if (!patern_file) {
std::cerr << "Failed to create the patern file" << std::endl;
} else {
auto sel_it = sel_data.begin();
sel_it += 2; // skip dimensions
patern_file << sel_data[0];
patern_file << "|"; // Separator needed to split as ascii values
patern_file << sel_data[1];
patern_file << "|"; // Separator needed to split as ascii values
for (int j = 0; j < sel_data[1]; j++) {
for (int i = 0; i < sel_data[0]; i++) {
patern_file << std::to_string(*sel_it);
if (*sel_it == 1) {
}
sel_it++;
}
}
patern_file << std::flush;
patern_file.close();
}
}
sel_ctrl = false;
}
}
ImGui::SameLine();
if (ImGui::Button("Discard")) {
sel_ctrl = false;
}
ImGui::End();
}
context->control_menu->display();
context->paterns_menu->display();
context->settings_menu->display();
context->selection_menu->display();
// End ImGui frame
rlImGuiEnd();
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);
rlImGuiShutdown();
CloseWindow();
return 0;

35
src/paterns_menu.cpp Normal file
View File

@@ -0,0 +1,35 @@
/*
* File name: paterns_menu.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <paterns_menu.hpp>
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
namespace gol {
void PaternsMenu::Toogle() {
is_open_ = !is_open_;
}
bool PaternsMenu::isOpen() {
return is_open_;
}
void PaternsMenu::display() {
if (is_open_) {
ImGuiWindowFlags paterns_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always);
ImGui::Begin("paterns", &is_open_, paterns_flags);
ImGui::Button("refresh");
ImGui::End();
}
}
} // namespace gol

View File

@@ -1,14 +1,10 @@
/* ************************************************************************** */
/* */
/* / ) */
/* render.cpp (\__/) ( ( */
/* ) ( ) ) */
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
/* ) `-------/ / */
/* Created: 2023/01/09 12:44:30 by lejulien ( / */
/* Updated: 2023/01/14 17:02:00 by lejulien \ | */
/* */
/* ************************************************************************** */
/*
* File name: render.cpp
* Author: lejulien
* Date created: 10-01-2026 21:49:04
// Date modified: 12-01-2026 21:54:56
* ------
*/
#include "../includes/render.hpp"
@@ -75,7 +71,7 @@ void Render::display_world(std::vector<bool> *data, int width, int height) {
// Render loop
void Render::display(World *world) {
void Render::display(std::shared_ptr<World> world) {
display_world(world->getWorldData(), world->getWidth(), world->getHeight());
}

View File

@@ -1,14 +1,10 @@
/* ************************************************************************** */
/* */
/* / ) */
/* rules.cpp (\__/) ( ( */
/* ) ( ) ) */
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
/* ) `-------/ / */
/* Created: 2023/01/09 12:18:36 by lejulien ( / */
/* Updated: 2023/01/14 16:47:01 by lejulien \ | */
/* */
/* ************************************************************************** */
/*
* File name: rules.cpp
* Author: lejulien
* Date created: 10-01-2026 21:49:14
// Date modified: 12-01-2026 21:59:00
* ------
*/
#include "../includes/rules.hpp"
#include "../includes/world.hpp"
@@ -71,13 +67,13 @@ void Rules::diag_neighbors(int &neighbors, int i, int j) {
// Member function
void Rules::setup(World *world) {
void Rules::setup(std::shared_ptr<World> world) {
_world = world;
_width = world->getWidth();
_height = world->getHeight();
}
void Rules::newWorld(World *world) {
void Rules::newWorld(std::shared_ptr<World> world) {
_world = world;
_width = world->getWidth();
_height = world->getHeight();

68
src/selection.cpp Normal file
View File

@@ -0,0 +1,68 @@
/*
* File name: selection.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <types.hpp>
#include <selection.hpp>
#include <selection_menu.hpp>
#include <control_menu.hpp>
#include <settings_menu.hpp>
#include <paterns_menu.hpp>
#include <world.hpp>
#include <snapping.hpp>
namespace gol {
Selection::Selection(std::shared_ptr<ctx> context): context_(context) {
}
void Selection::update() {
auto gesture = GetGestureDetected();
mouse_pos_ = GetMousePosition();
if (!context_->control_menu->edit_ctrl_ && !context_->control_menu->play_ctrl_ && !context_->paterns_menu->isOpen() && !context_->settings_menu->isOpen()) {
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
sel_pos_ = snapToGrid(mouse_pos_, context_->settings_menu->getCellSize());
selecting_ = true;
}
if (ImGui::IsMouseReleased(0) && selecting_ == true && mouse_pos_.x >=0 && mouse_pos_.x < context_->settings_menu->getWidth() && mouse_pos_.y >=0 && mouse_pos_.y < context_->settings_menu->getHeight() ) {
selecting_ = false;
Vector2i p1 = screenToGrid(sel_pos_, context_->settings_menu->getCellSize());
Vector2i p2 = screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
// Get origin
Vector2i orig = {
(p1.x < p2.x)?p1.x:p2.x,
(p1.y < p2.y)?p1.y:p2.y,
};
// Get selection size
Vector2i sel_size = {
(p1.x > p2.x)?p1.x-p2.x:p2.x-p1.x,
(p1.y > p2.y)?p1.y-p2.y:p2.y-p1.y,
};
// Ensure there is at least one cell selected
if (!(sel_size.x == 0 || sel_size.y == 0)) {
context_->selection_menu->setSelection(context_->world->getSelection(orig, sel_size));
context_->selection_menu->open();
}
}
}
}
void Selection::display() {
if (selecting_) {
auto grid_mouse = snapToGrid(mouse_pos_, 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),
((sel_x_less)? grid_mouse.x - sel_pos_.x : sel_pos_.x - grid_mouse.x),
((sel_y_less)? grid_mouse.y - sel_pos_.y : sel_pos_.y - grid_mouse.y),
RED);
}
}
} // namespace gol

109
src/selection_menu.cpp Normal file
View File

@@ -0,0 +1,109 @@
/*
* File name: selection_menu.cpp
* Author: lejulien
* Date created: 13-01-2026 22:12:44
// Date modified: 13-01-2026 22:18:58
* ------
*/
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <selection_menu.hpp>
namespace gol {
SelectionMenu::SelectionMenu(std::shared_ptr<ctx> context): context_(context) {
selectionTexture_ = LoadRenderTexture(200, 200);
patern_name_[0] = '\0';
}
SelectionMenu::~SelectionMenu() {
UnloadRenderTexture(selectionTexture_);
}
void SelectionMenu::update() {
if (!sel_ctrl_) {
patern_name_[0] = '\0';
}
}
void SelectionMenu::open() {
sel_ctrl_ = true;
}
void SelectionMenu::setSelection(std::vector<uint32_t> selection) {
sel_data_ = selection;
}
void SelectionMenu::display() {
if (sel_ctrl_) {
BeginTextureMode(selectionTexture_);
ClearBackground(BLACK);
auto max_size = (sel_data_[0] > sel_data_[1]) ? sel_data_[0] : sel_data_[1];
int fitted_width = 200 / max_size;
auto sel_it = sel_data_.begin();
sel_it += 2; // skip dimensions
for (int j = 0; j < sel_data_[1]; j++) {
for (int i = 0; i < sel_data_[0]; i++) {
if (*sel_it == 1) {
DrawRectangle(i * fitted_width, (sel_data_[1] -j-1) * fitted_width, fitted_width, fitted_width, WHITE);
}
sel_it++;
}
}
EndTextureMode();
ImGuiWindowFlags settings_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::Begin("Selection", &sel_ctrl_, settings_flags);
rlImGuiImageSize(&selectionTexture_.texture, 200, 200);
ImGui::InputText("patern_name", patern_name_, 255);
if (ImGui::Button("Save")) {
char path_buf[1024];
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1);
if (len != -1) {
// Create paterns dir if not present
std::filesystem::path paterns_dir = std::filesystem::path(path_buf).parent_path() / "paterns";
if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) {
std::cerr << "Failed to create paterns directory" << std::endl;
} else { // Could be optimized by early returning in a function
std::ofstream patern_file;
paterns_dir += '/';
paterns_dir += patern_name_;
patern_file.open(paterns_dir);
if (!patern_file) {
std::cerr << "Failed to create the patern file" << std::endl;
} else {
auto sel_it = sel_data_.begin();
sel_it += 2; // skip dimensions
patern_file << sel_data_[0];
patern_file << "|"; // Separator needed to split as ascii values
patern_file << sel_data_[1];
patern_file << "|"; // Separator needed to split as ascii values
for (int j = 0; j < sel_data_[1]; j++) {
for (int i = 0; i < sel_data_[0]; i++) {
patern_file << std::to_string(*sel_it);
if (*sel_it == 1) {
}
sel_it++;
}
}
patern_file << std::flush;
patern_file.close();
}
}
sel_ctrl_ = false;
}
}
ImGui::SameLine();
if (ImGui::Button("Discard")) {
sel_ctrl_ = false;
}
ImGui::End();
}
}
} // namespace gol

122
src/settings_menu.cpp Normal file
View File

@@ -0,0 +1,122 @@
/*
* File name: settings_menu.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 22:21:49
* ------
*/
#include <nlohmann/json.hpp>
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
#include <settings_menu.hpp>
#include <context.hpp>
#include <world.hpp>
#include <rules.hpp>
#include <render.hpp>
namespace gol {
SettingsMenu::SettingsMenu(std::shared_ptr<ctx> context): context_(context) {
fps_ctrl_ = context->config_json["fps"].get<int>();
cell_size_ctrl_ = context->config_json["cell_size"].get<int>();
settings_window_ = false;
width_ctrl_ = context->config_json["screen_width"].get<int>();
height_ctrl_ = context->config_json["screen_height"].get<int>();
dark_theme_ctrl_ = context->config_json["dark_theme"].get<bool>();
apply_ctrl_ = false;
}
void SettingsMenu::update() {
if (apply_ctrl_) {
bool resize_needed = false;
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_ != context_->config_json["cell_size"].get<int>() ||
resize_needed) {
context_->world->resize(context_->config_json["screen_width"].get<int>() / cell_size_ctrl_,
context_->config_json["screen_height"].get<int>() / cell_size_ctrl_);
context_->render->updateCellSize(cell_size_ctrl_);
context_->rules->newWorld(context_->world);
context_->config_json["cell_size"] = cell_size_ctrl_;
}
if (dark_theme_ctrl_ != context_->config_json["dark_theme"]) {
rlImGuiShutdown();
rlImGuiSetup(dark_theme_ctrl_);
context_->config_json["dark_theme"] = dark_theme_ctrl_;
}
apply_ctrl_ = false;
settings_window_ = false;
}
}
void SettingsMenu::display() {
if (settings_window_) {
ImGuiWindowFlags settings_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::Begin("Settings", &settings_window_, settings_flags);
ImGui::SliderInt("Window Width", &width_ctrl_, 800, 1920);
ImGui::SliderInt("Window Height", &height_ctrl_, 600, 1200);
ImGui::SliderInt("FPS", &fps_ctrl_, 0, 30);
ImGui::SliderInt("Cell Size", &cell_size_ctrl_, 4, 100);
ImGui::Checkbox("Dark Theme", &dark_theme_ctrl_);
if (ImGui::Button("Save & Apply")) {
apply_ctrl_ = true;
}
ImGui::End();
}
}
bool SettingsMenu::isOpen() {
return settings_window_;
}
void SettingsMenu::Toogle() {
settings_window_ = !settings_window_;
}
// Getters/Setters
int SettingsMenu::getFPS() {
return fps_ctrl_;
}
void SettingsMenu::setFPS(int new_fps) {
fps_ctrl_ = new_fps;
}
int SettingsMenu::getCellSize() {
return cell_size_ctrl_;
}
void SettingsMenu::setCellSize(int new_cell_size) {
cell_size_ctrl_ = new_cell_size;
}
int SettingsMenu::getWidth() {
return width_ctrl_;
}
void SettingsMenu::setWidth(int new_width) {
width_ctrl_ = new_width;
}
int SettingsMenu::getHeight() {
return height_ctrl_;
}
void SettingsMenu::setHeight(int new_height) {
height_ctrl_ = new_height;
}
} // namespace gol

View File

@@ -1,14 +1,10 @@
/* ************************************************************************** */
/* */
/* / ) */
/* world.cpp (\__/) ( ( */
/* ) ( ) ) */
/* By: lejulien <leo.julien.42@gmail.com> ={ }= / / */
/* ) `-------/ / */
/* Created: 2023/01/09 12:25:55 by lejulien ( / */
/* Updated: 2023/01/14 17:06:54 by lejulien \ | */
/* */
/* ************************************************************************** */
/*
* File name: world.cpp
* Author: lejulien
* Date created: 09-01-2026 23:59:55
// Date modified: 12-01-2026 22:14:46
* ------
*/
#include "../includes/world.hpp"
@@ -16,9 +12,13 @@
// Constructor and destructor
World::World(int width, int height) : _width(width), _height(height) {
World::World(std::shared_ptr<gol::ctx> context) {
_width = context->config_json["screen_width"].get<int>() /
context->config_json["cell_size"].get<int>();
_height = context->config_json["screen_height"].get<int>() /
context->config_json["cell_size"].get<int>();
// create world data
this->_data = new std::vector<bool>(width * height, false);
this->_data = new std::vector<bool>(_width * _height, false);
}
World::~World() { delete this->_data; }