lju/refactorize #2
@@ -71,6 +71,7 @@ set(SRC_CXX_FILES "./src/main.cpp"
|
|||||||
"./src/world.cpp"
|
"./src/world.cpp"
|
||||||
"./src/render.cpp"
|
"./src/render.cpp"
|
||||||
"./src/control_menu.cpp"
|
"./src/control_menu.cpp"
|
||||||
|
"./src/settings_menu.cpp"
|
||||||
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
|
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
|
||||||
"${imgui_SOURCE_DIR}/imgui.cpp"
|
"${imgui_SOURCE_DIR}/imgui.cpp"
|
||||||
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
|
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: context.hpp
|
* File name: context.hpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 01-01-1970 00:59:59
|
* Date created: 01-01-1970 00:59:59
|
||||||
// Date modified: 12-01-2026 20:31:50
|
// Date modified: 12-01-2026 21:30:10
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -14,12 +14,16 @@
|
|||||||
|
|
||||||
class World;
|
class World;
|
||||||
class Rules;
|
class Rules;
|
||||||
|
class Render;
|
||||||
|
|
||||||
namespace gol {
|
namespace gol {
|
||||||
|
class SettingsMenu;
|
||||||
|
|
||||||
typedef struct ctx {
|
typedef struct ctx {
|
||||||
std::shared_ptr<World> world = nullptr;
|
std::shared_ptr<World> world = nullptr;
|
||||||
std::shared_ptr<Rules> rules = nullptr;
|
std::shared_ptr<Rules> rules = nullptr;
|
||||||
|
std::shared_ptr<Render> render = nullptr;
|
||||||
|
std::shared_ptr<SettingsMenu> settings_menu = nullptr;
|
||||||
nlohmann::json config_json;
|
nlohmann::json config_json;
|
||||||
} ctx;
|
} ctx;
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
* File name: control_menu.hpp
|
* File name: control_menu.hpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 22:00:33
|
* Date created: 10-01-2026 22:00:33
|
||||||
// Date modified: 10-01-2026 22:45:10
|
// Date modified: 12-01-2026 22:18:26
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <context.hpp>
|
#include <context.hpp>
|
||||||
#include <types.hpp>
|
#include <types.hpp>
|
||||||
|
|
||||||
@@ -15,28 +17,21 @@ namespace gol {
|
|||||||
|
|
||||||
class ControlMenu {
|
class ControlMenu {
|
||||||
public:
|
public:
|
||||||
ControlMenu(ctx context);
|
ControlMenu(std::shared_ptr<ctx> context);
|
||||||
~ControlMenu() = default;
|
~ControlMenu() = default;
|
||||||
void update();
|
void update();
|
||||||
void display();
|
void display();
|
||||||
private:
|
private:
|
||||||
ctx context_;
|
std::shared_ptr<ctx> context_;
|
||||||
public: // Keep those public for easy access
|
public: // Keep those public for easy access
|
||||||
MenuState menu_state_ = MenuState::NONE;
|
MenuState menu_state_ = MenuState::NONE;
|
||||||
int fps_ctrl_ = false;
|
|
||||||
int cell_size_ctrl_ = false;
|
|
||||||
bool play_ctrl_ = true;
|
bool play_ctrl_ = true;
|
||||||
bool step_ctrl_ = false;
|
bool step_ctrl_ = false;
|
||||||
bool step_back_ctrl_ = false;
|
bool step_back_ctrl_ = false;
|
||||||
bool rand_ctrl_ = false;
|
bool rand_ctrl_ = false;
|
||||||
bool edit_ctrl_ = false;
|
bool edit_ctrl_ = false;
|
||||||
bool clear_ctrl_ = false;
|
bool clear_ctrl_ = false;
|
||||||
bool settings_window_ = false;
|
|
||||||
bool paterns_ctrl_ = false;
|
bool paterns_ctrl_ = false;
|
||||||
int width_ctrl_ = false;
|
|
||||||
int height_ctrl_ = false;
|
|
||||||
bool dark_theme_ctrl_ = false;
|
|
||||||
bool apply_ctrl_ = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gol
|
} // namespace gol
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
* File name: render.hpp
|
* File name: render.hpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 21:54:12
|
* Date created: 10-01-2026 21:54:12
|
||||||
// Date modified: 10-01-2026 22:00:37
|
// Date modified: 12-01-2026 21:56:03
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "world.hpp"
|
#include "world.hpp"
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -16,7 +18,7 @@ public:
|
|||||||
Render(int cell_size);
|
Render(int cell_size);
|
||||||
|
|
||||||
// Member function
|
// Member function
|
||||||
void display(World *world);
|
void display(std::shared_ptr<World> world);
|
||||||
void updateCellSize(int new_size);
|
void updateCellSize(int new_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: rules.hpp
|
* File name: rules.hpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 09-01-2026 23:59:55
|
* Date created: 09-01-2026 23:59:55
|
||||||
* Date modified: 10-01-2026 21:49:44
|
// Date modified: 12-01-2026 21:58:17
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -19,12 +19,12 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Rules();
|
Rules();
|
||||||
void setup(World *world);
|
void setup(std::shared_ptr<World> world);
|
||||||
void newWorld(World *world);
|
void newWorld(std::shared_ptr<World> world);
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
World *_world;
|
std::shared_ptr<World> _world;
|
||||||
std::vector<bool> _buffer;
|
std::vector<bool> _buffer;
|
||||||
int _width;
|
int _width;
|
||||||
int _height;
|
int _height;
|
||||||
|
|||||||
46
includes/settings_menu.hpp
Normal file
46
includes/settings_menu.hpp
Normal 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
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
* File name: world.hpp
|
* File name: world.hpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 09-01-2026 23:59:55
|
* Date created: 09-01-2026 23:59:55
|
||||||
* Date modified: 10-01-2026 21:49:31
|
// Date modified: 12-01-2026 22:20:26
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <memory>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
class World {
|
class World {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
World(gol::ctx &ctx);
|
World(std::shared_ptr<gol::ctx> ctx);
|
||||||
~World();
|
~World();
|
||||||
|
|
||||||
std::vector<bool> *getWorldData();
|
std::vector<bool> *getWorldData();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: control_menu.cpp
|
* File name: control_menu.cpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 22:12:44
|
* Date created: 10-01-2026 22:12:44
|
||||||
// Date modified: 12-01-2026 20:36:39
|
// Date modified: 12-01-2026 22:18:58
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -11,26 +11,20 @@
|
|||||||
#include <rlImGui.h>
|
#include <rlImGui.h>
|
||||||
|
|
||||||
#include <control_menu.hpp>
|
#include <control_menu.hpp>
|
||||||
|
#include <settings_menu.hpp>
|
||||||
#include <world.hpp>
|
#include <world.hpp>
|
||||||
#include <rules.hpp>
|
#include <rules.hpp>
|
||||||
|
|
||||||
namespace gol {
|
namespace gol {
|
||||||
|
|
||||||
ControlMenu::ControlMenu(ctx context):context_(context) {
|
ControlMenu::ControlMenu(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>();
|
|
||||||
play_ctrl_ = true;
|
play_ctrl_ = true;
|
||||||
step_ctrl_ = false;
|
step_ctrl_ = false;
|
||||||
step_back_ctrl_ = false;
|
step_back_ctrl_ = false;
|
||||||
rand_ctrl_ = false;
|
rand_ctrl_ = false;
|
||||||
edit_ctrl_ = false;
|
edit_ctrl_ = false;
|
||||||
clear_ctrl_ = false;
|
clear_ctrl_ = false;
|
||||||
settings_window_ = false;
|
|
||||||
paterns_ctrl_ = false;
|
paterns_ctrl_ = 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 ControlMenu::update() {
|
void ControlMenu::update() {
|
||||||
@@ -40,11 +34,11 @@ void ControlMenu::update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (rand_ctrl_) {
|
if (rand_ctrl_) {
|
||||||
context_.world->randomize();
|
context_->world->randomize();
|
||||||
rand_ctrl_ = false;
|
rand_ctrl_ = false;
|
||||||
}
|
}
|
||||||
if (clear_ctrl_) {
|
if (clear_ctrl_) {
|
||||||
context_.world->clear();
|
context_->world->clear();
|
||||||
clear_ctrl_ = false;
|
clear_ctrl_ = false;
|
||||||
}
|
}
|
||||||
if (edit_ctrl_ && play_ctrl_) {
|
if (edit_ctrl_ && play_ctrl_) {
|
||||||
@@ -61,12 +55,12 @@ void ControlMenu::update() {
|
|||||||
menu_state_ = MenuState::EDIT;
|
menu_state_ = MenuState::EDIT;
|
||||||
}
|
}
|
||||||
if (step_ctrl_) {
|
if (step_ctrl_) {
|
||||||
context_.world->saveCompressed();
|
context_->world->saveCompressed();
|
||||||
context_.rules->update();
|
context_->rules->update();
|
||||||
step_ctrl_ = false;
|
step_ctrl_ = false;
|
||||||
}
|
}
|
||||||
if (step_back_ctrl_) {
|
if (step_back_ctrl_) {
|
||||||
context_.world->stepBack();
|
context_->world->stepBack();
|
||||||
step_back_ctrl_ = false;
|
step_back_ctrl_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,10 +85,10 @@ void ControlMenu::display() {
|
|||||||
if (ImGui::Button((paterns_ctrl_) ? "Hide paterns" : "Show paterns")) {
|
if (ImGui::Button((paterns_ctrl_) ? "Hide paterns" : "Show paterns")) {
|
||||||
paterns_ctrl_ = !paterns_ctrl_;
|
paterns_ctrl_ = !paterns_ctrl_;
|
||||||
}
|
}
|
||||||
if (ImGui::Button((settings_window_) ? "Hide settings" : "Show settings")) {
|
if (ImGui::Button((context_->settings_menu->isOpen()) ? "Hide settings" : "Show settings")) {
|
||||||
settings_window_ = !settings_window_;
|
context_->settings_menu->Toogle();
|
||||||
}
|
}
|
||||||
ImGui::Text("Generation: %zu", context_.world->getCycle());
|
ImGui::Text("Generation: %zu", context_->world->getCycle());
|
||||||
ImGui::End();}
|
ImGui::End();}
|
||||||
|
|
||||||
} // namespace gol
|
} // namespace gol
|
||||||
|
|||||||
125
src/main.cpp
125
src/main.cpp
@@ -2,7 +2,7 @@
|
|||||||
* File name: main.cpp
|
* File name: main.cpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 21:59:32
|
* 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 <types.hpp>
|
||||||
#include <context.hpp>
|
#include <context.hpp>
|
||||||
#include <control_menu.hpp>
|
#include <control_menu.hpp>
|
||||||
|
#include <settings_menu.hpp>
|
||||||
|
|
||||||
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
Vector2 snapToGrid(Vector2 screen, int cell_size) {
|
||||||
return {static_cast<float>(round(screen.x / cell_size) * 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) {
|
int main(int ac, char **av) {
|
||||||
gol::ctx context;
|
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
|
||||||
// Load or default config
|
// Load or default config
|
||||||
const std::string config_file_name = "config.json";
|
const std::string config_file_name = "config.json";
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ int main(int ac, char **av) {
|
|||||||
// Try reading the configuration
|
// Try reading the configuration
|
||||||
try {
|
try {
|
||||||
config_file.seekg(0);
|
config_file.seekg(0);
|
||||||
config_file >> context.config_json;
|
config_file >> context->config_json;
|
||||||
} catch (const nlohmann::json::parse_error &e) {
|
} catch (const nlohmann::json::parse_error &e) {
|
||||||
std::cerr << "An error occured while loading config : " << e.what()
|
std::cerr << "An error occured while loading config : " << e.what()
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -67,42 +68,43 @@ int main(int ac, char **av) {
|
|||||||
config_file.close();
|
config_file.close();
|
||||||
|
|
||||||
// Check config values or populate them
|
// Check config values or populate them
|
||||||
if (!context.config_json.contains("cell_size") ||
|
if (!context->config_json.contains("cell_size") ||
|
||||||
(context.config_json["cell_size"] < 4 || context.config_json["cell_size"] > 100)) {
|
(context->config_json["cell_size"] < 4 || context->config_json["cell_size"] > 100)) {
|
||||||
context.config_json["cell_size"] = 10;
|
context->config_json["cell_size"] = 10;
|
||||||
}
|
}
|
||||||
if (!context.config_json.contains("screen_width") ||
|
if (!context->config_json.contains("screen_width") ||
|
||||||
(context.config_json["screen_width"] < 800 ||
|
(context->config_json["screen_width"] < 800 ||
|
||||||
context.config_json["screen_width"] > 1920)) {
|
context->config_json["screen_width"] > 1920)) {
|
||||||
context.config_json["screen_width"] = 800;
|
context->config_json["screen_width"] = 800;
|
||||||
}
|
}
|
||||||
if (!context.config_json.contains("screen_height") ||
|
if (!context->config_json.contains("screen_height") ||
|
||||||
(context.config_json["screen_height"] < 600 ||
|
(context->config_json["screen_height"] < 600 ||
|
||||||
context.config_json["screen_height"] > 1200)) {
|
context->config_json["screen_height"] > 1200)) {
|
||||||
context.config_json["screen_height"] = 600;
|
context->config_json["screen_height"] = 600;
|
||||||
}
|
}
|
||||||
if (!context.config_json.contains("dark_theme")) {
|
if (!context->config_json.contains("dark_theme")) {
|
||||||
context.config_json["dark_theme"] = true;
|
context->config_json["dark_theme"] = true;
|
||||||
}
|
}
|
||||||
if (!context.config_json.contains("fps") ||
|
if (!context->config_json.contains("fps") ||
|
||||||
(context.config_json["fps"] < 0 || context.config_json["fps"] > 30)) {
|
(context->config_json["fps"] < 0 || context->config_json["fps"] > 30)) {
|
||||||
context.config_json["fps"] = 800;
|
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]);
|
&av[0][2]);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
rlImGuiSetup(context.config_json["dark_theme"]);
|
rlImGuiSetup(context->config_json["dark_theme"]);
|
||||||
// Selection window
|
// Selection window
|
||||||
|
|
||||||
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
|
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
|
||||||
|
|
||||||
// Initialize objects
|
// Initialize objects
|
||||||
context.world = std::make_shared<World>(context);
|
context->world = std::make_shared<World>(context);
|
||||||
context.rules = std::make_shared<Rules>();
|
context->rules = std::make_shared<Rules>();
|
||||||
Render render(context.config_json["cell_size"]);
|
context->settings_menu = std::make_shared<gol::SettingsMenu>(context);
|
||||||
|
context->render = std::make_shared<Render>(context->settings_menu->getCellSize());
|
||||||
|
|
||||||
// Imgui control menu
|
// Imgui control menu
|
||||||
gol::ControlMenu control_menu(context);
|
gol::ControlMenu control_menu(context);
|
||||||
@@ -122,12 +124,12 @@ int main(int ac, char **av) {
|
|||||||
std::string sel_txt_input_hint("patern name");
|
std::string sel_txt_input_hint("patern name");
|
||||||
|
|
||||||
// Setups
|
// Setups
|
||||||
context.rules->setup(&(*context.world));
|
context->rules->setup(context->world);
|
||||||
// Diplay generations
|
// Diplay generations
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
// Frames shinenigans
|
// Frames shinenigans
|
||||||
float deltaTime = GetFrameTime();
|
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;
|
timePerUpdate = (1.0f / 10.0f) / sim_speed;
|
||||||
|
|
||||||
control_menu.update();
|
control_menu.update();
|
||||||
@@ -137,20 +139,20 @@ int main(int ac, char **av) {
|
|||||||
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||||
control_menu.menu_state_ = MenuState::EDIT;
|
control_menu.menu_state_ = MenuState::EDIT;
|
||||||
context.world->setCell(mousePos.x / 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>());
|
mousePos.y / context->config_json["cell_size"].get<int>());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Selection behaviour
|
// 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)) {
|
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;
|
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;
|
selecting = false;
|
||||||
Vector2i p1 = screenToGrid(sel_pos, control_menu.cell_size_ctrl_);
|
Vector2i p1 = screenToGrid(sel_pos, context->settings_menu->getCellSize());
|
||||||
Vector2i p2 = screenToGrid(mousePos, control_menu.cell_size_ctrl_);
|
Vector2i p2 = screenToGrid(mousePos, context->settings_menu->getCellSize());
|
||||||
// Get origin
|
// Get origin
|
||||||
Vector2i orig = {
|
Vector2i orig = {
|
||||||
(p1.x < p2.x)?p1.x:p2.x,
|
(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
|
// Ensure there is at least one cell selected
|
||||||
if (!(sel_size.x == 0 || sel_size.y == 0)) {
|
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;
|
sel_ctrl = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -171,35 +173,7 @@ int main(int ac, char **av) {
|
|||||||
if (!sel_ctrl) {
|
if (!sel_ctrl) {
|
||||||
patern_name[0] = '\0';
|
patern_name[0] = '\0';
|
||||||
}
|
}
|
||||||
|
context->settings_menu->update();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Accumulate time and update simulation at the adjusted speed
|
// Accumulate time and update simulation at the adjusted speed
|
||||||
deltaTimeAccumulator += deltaTime;
|
deltaTimeAccumulator += deltaTime;
|
||||||
@@ -208,15 +182,15 @@ int main(int ac, char **av) {
|
|||||||
// Reset accumulator
|
// Reset accumulator
|
||||||
deltaTimeAccumulator -= timePerUpdate;
|
deltaTimeAccumulator -= timePerUpdate;
|
||||||
if (control_menu.menu_state_ == MenuState::PLAY) {
|
if (control_menu.menu_state_ == MenuState::PLAY) {
|
||||||
context.world->saveCompressed();
|
context->world->saveCompressed();
|
||||||
context.rules->update();
|
context->rules->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
render.display(&(*context.world));
|
context->render->display(context->world);
|
||||||
if (selecting) {
|
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_x_less = (sel_pos.x < grid_mouse.x);
|
||||||
bool sel_y_less = (sel_pos.y < grid_mouse.y);
|
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),
|
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::Button("refresh");
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
if (control_menu.settings_window_) {
|
context->settings_menu->display();
|
||||||
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();
|
|
||||||
}
|
|
||||||
if (sel_ctrl) {
|
if (sel_ctrl) {
|
||||||
BeginTextureMode(selectionTexture);
|
BeginTextureMode(selectionTexture);
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
@@ -318,7 +279,7 @@ int main(int ac, char **av) {
|
|||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
config_file.open(config_file_name, std::ios::out | std::ios::trunc);
|
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();
|
config_file.close();
|
||||||
// Cleanup Selection texture
|
// Cleanup Selection texture
|
||||||
UnloadRenderTexture(selectionTexture);
|
UnloadRenderTexture(selectionTexture);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: render.cpp
|
* File name: render.cpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 21:49:04
|
* Date created: 10-01-2026 21:49:04
|
||||||
// Date modified: 10-01-2026 21:59:56
|
// Date modified: 12-01-2026 21:54:56
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ void Render::display_world(std::vector<bool> *data, int width, int height) {
|
|||||||
|
|
||||||
// Render loop
|
// Render loop
|
||||||
|
|
||||||
void Render::display(World *world) {
|
void Render::display(std::shared_ptr<World> world) {
|
||||||
display_world(world->getWorldData(), world->getWidth(), world->getHeight());
|
display_world(world->getWorldData(), world->getWidth(), world->getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: rules.cpp
|
* File name: rules.cpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 10-01-2026 21:49:14
|
* Date created: 10-01-2026 21:49:14
|
||||||
// Date modified: 10-01-2026 22:00:16
|
// Date modified: 12-01-2026 21:59:00
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -67,13 +67,13 @@ void Rules::diag_neighbors(int &neighbors, int i, int j) {
|
|||||||
|
|
||||||
// Member function
|
// Member function
|
||||||
|
|
||||||
void Rules::setup(World *world) {
|
void Rules::setup(std::shared_ptr<World> world) {
|
||||||
_world = world;
|
_world = world;
|
||||||
_width = world->getWidth();
|
_width = world->getWidth();
|
||||||
_height = world->getHeight();
|
_height = world->getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rules::newWorld(World *world) {
|
void Rules::newWorld(std::shared_ptr<World> world) {
|
||||||
_world = world;
|
_world = world;
|
||||||
_width = world->getWidth();
|
_width = world->getWidth();
|
||||||
_height = world->getHeight();
|
_height = world->getHeight();
|
||||||
|
|||||||
122
src/settings_menu.cpp
Normal file
122
src/settings_menu.cpp
Normal 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
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* File name: world.cpp
|
* File name: world.cpp
|
||||||
* Author: lejulien
|
* Author: lejulien
|
||||||
* Date created: 09-01-2026 23:59:55
|
* Date created: 09-01-2026 23:59:55
|
||||||
// Date modified: 10-01-2026 22:00:23
|
// Date modified: 12-01-2026 22:14:46
|
||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -12,11 +12,11 @@
|
|||||||
|
|
||||||
// Constructor and destructor
|
// Constructor and destructor
|
||||||
|
|
||||||
World::World(gol::ctx &context) {
|
World::World(std::shared_ptr<gol::ctx> context) {
|
||||||
_width = context.config_json["screen_width"].get<int>() /
|
_width = context->config_json["screen_width"].get<int>() /
|
||||||
context.config_json["cell_size"].get<int>();
|
context->config_json["cell_size"].get<int>();
|
||||||
_height = context.config_json["screen_height"].get<int>() /
|
_height = context->config_json["screen_height"].get<int>() /
|
||||||
context.config_json["cell_size"].get<int>();
|
context->config_json["cell_size"].get<int>();
|
||||||
// create world data
|
// create world data
|
||||||
this->_data = new std::vector<bool>(_width * _height, false);
|
this->_data = new std::vector<bool>(_width * _height, false);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user