Compare commits

...

23 Commits

Author SHA1 Message Date
12a95a3efe gitignore: Adding windows build directory 2026-02-11 14:54:23 +01:00
992f8a3e3a project: create the windows toolchain
Some of the code is still linux only
2026-02-11 14:53:17 +01:00
384c811a29 grid: add a guide grid 2026-01-15 09:13:53 +01:00
a4c9774c28 Merge pull request 'lju/apply-loaded-pattern' (#4) from lju/apply-loaded-pattern into main
Reviewed-on: #4
2026-01-14 20:51:40 +00:00
3e7e7e2547 patern_preview/world: Place selection into the world 2026-01-14 17:34:15 +01:00
5c9b60163a patern_preview: Adding patern preview
This adds a preview of the patern placement, checking out of bound
2026-01-14 17:19:50 +01:00
6f5ec1d811 Actualiser README.md
Fix typo
2026-01-14 15:20:38 +00:00
327a298383 Merge pull request 'lju/load-paterns' (#3) from lju/load-paterns into main
Reviewed-on: #3
2026-01-14 12:18:57 +00:00
546d9f24b5 paterns_menu: load patern from the selected file 2026-01-14 12:58:44 +01:00
3d89ec74c9 paterns_menu: list and display paterns 2026-01-14 12:14:04 +01:00
c4725847cc context: Add program directory's path 2026-01-13 15:30:30 +01:00
8f7b735820 Merge pull request 'lju/refactorize' (#2) from lju/refactorize into main
Reviewed-on: #2
2026-01-13 13:50:58 +00:00
81f1bc5e72 paterns_menu: Move it to it's own class 2026-01-13 14:49:12 +01:00
55341973b3 selection: Create it's dedicated class 2026-01-13 13:39:02 +01:00
f45c83dd77 selection_menu: Move it to it's dedicated class 2026-01-13 13:03:36 +01:00
416d3a2be9 settings_menu: Create a dedicated class for the settings 2026-01-12 22:22:53 +01:00
24d8a092e4 control_menu: Add rules related updates 2026-01-12 20:43:50 +01:00
9e2e7b562d control_menu: Move processing and display code into it's correspondind methods 2026-01-12 16:29:12 +01:00
c00c430382 world: Use context at contruction to simplify it's call 2026-01-12 15:44:04 +01:00
95a7d6ea9a Adding controls to control_menu 2026-01-10 22:55:53 +01:00
8aee4cdc53 Adding new context with world & config_json 2026-01-10 22:40:17 +01:00
24729d11f3 Update file headers 2026-01-10 22:01:15 +01:00
e8c0c460d9 Disable selection when patern menu is selected 2026-01-10 21:40:24 +01:00
29 changed files with 1162 additions and 399 deletions

1
.gitignore vendored
View File

@@ -5,3 +5,4 @@ _deps
CMakeSettings.json
.vs
.cache
build-win-x86_64

View File

@@ -8,11 +8,11 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(WIN32)
set(IMGUI_BACKEND "imgui_impl_dx11.cpp") # DirectX 11 for Windows
set(IMGUI_BACKEND "imgui_impl_dx11.cpp") # DirectX 11 for Windows
elseif(APPLE)
set(IMGUI_BACKEND "imgui_impl_metal.mm") # Metal for macOS
set(IMGUI_BACKEND "imgui_impl_metal.mm") # Metal for macOS
elseif(UNIX)
set(IMGUI_BACKEND "imgui_impl_opengl3.cpp") # OpenGL for Linux
set(IMGUI_BACKEND "imgui_impl_opengl3.cpp") # OpenGL for Linux
endif()
# includes
@@ -21,25 +21,25 @@ include_directories(./includes)
# raylib
include(FetchContent)
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 5.5
GIT_SHALLOW 1
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 5.5
GIT_SHALLOW 1
)
FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED)
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_GAMES OFF CACHE BOOL "" FORCE)
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_GAMES OFF CACHE BOOL "" FORCE)
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
# imgui
FetchContent_Declare(
imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG docking
imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG docking
)
FetchContent_MakeAvailable(imgui)
@@ -48,9 +48,9 @@ set(imgui_SOURCE_DIR ${CMAKE_BINARY_DIR}/_deps/imgui-src)
# rlimgui
FetchContent_Declare(
rlImGui
GIT_REPOSITORY https://github.com/raylib-extras/rlImGui.git
GIT_TAG main
rlImGui
GIT_REPOSITORY https://github.com/raylib-extras/rlImGui.git
GIT_TAG main
)
FetchContent_MakeAvailable(rlImGui)
set(rlImGui_SOURCE_DIR ${CMAKE_BINARY_DIR}/_deps/rlimgui-src)
@@ -58,24 +58,32 @@ set(rlImGui_SOURCE_DIR ${CMAKE_BINARY_DIR}/_deps/rlimgui-src)
# nlohmann::json
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
)
FetchContent_MakeAvailable(json)
include_directories(${imgui_SOURCE_DIR} ${rlImGui_SOURCE_DIR})
set(SRC_CXX_FILES "./src/main.cpp"
"./src/rules.cpp"
"./src/world.cpp"
"./src/render.cpp"
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
"${imgui_SOURCE_DIR}/imgui.cpp"
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
"${imgui_SOURCE_DIR}/imgui_widgets.cpp"
"${imgui_SOURCE_DIR}/imgui_tables.cpp"
"${imgui_SOURCE_DIR}/backends/${IMGUI_BACKEND}"
set(SRC_CXX_FILES "./src/main.cpp"
"./src/rules.cpp"
"./src/world.cpp"
"./src/grid.cpp"
"./src/render.cpp"
"./src/control_menu.cpp"
"./src/settings_menu.cpp"
"./src/selection_menu.cpp"
"./src/selection.cpp"
"./src/paterns_menu.cpp"
"./src/patern_preview.cpp"
"./src/snapping.cpp"
"${rlImGui_SOURCE_DIR}/rlImGui.cpp"
"${imgui_SOURCE_DIR}/imgui.cpp"
"${imgui_SOURCE_DIR}/imgui_draw.cpp"
"${imgui_SOURCE_DIR}/imgui_widgets.cpp"
"${imgui_SOURCE_DIR}/imgui_tables.cpp"
"${imgui_SOURCE_DIR}/backends/${IMGUI_BACKEND}"
)
# Setup the example
@@ -83,3 +91,25 @@ add_executable(${NAME} ${SRC_CXX_FILES})
# Link raylib and raylib-cpp
target_link_libraries(${NAME} PUBLIC raylib nlohmann_json::nlohmann_json)
# Windows cross-compilation rule
if (NOT DEFINED CMAKE_CROSSCOMPILE_WINDOWS_HELPER_ADDED)
set(CMAKE_CROSSCOMPILE_WINDOWS_HELPER_ADDED TRUE)
set(WIN_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/toolchain-mingw-w64-x86_64.cmake" CACHE PATH "Toolchain for Win x86_64")
add_custom_target(cmake-win
COMMAND ${CMAKE_COMMAND} -E echo "Configuring Windows x86_64 build in: ${CMAKE_SOURCE_DIR}/build-win-x86_64"
COMMAND ${CMAKE_COMMAND}
-S ${CMAKE_SOURCE_DIR}
-B ${CMAKE_SOURCE_DIR}/build-win-x86_64
-G "Ninja"
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_TOOLCHAIN_FILE=${WIN_TOOLCHAIN_FILE}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_SOURCE_DIR}/build-win-x86_64 -- -v
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Configure & build for Windows x86_64 (MinGW-w64)"
VERBATIM
)
endif()

View File

@@ -6,7 +6,7 @@ This is a simple Game Of Life editor written in C++ with raylib & dearImGUI
## How to compile the project
Ensure you have g++, cmake and the thendependencies of raylib
Ensure you have g++, cmake and the dependencies of raylib
```shell
mkdir build

41
includes/context.hpp Normal file
View File

@@ -0,0 +1,41 @@
/*
* 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;
class PaternPreview;
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;
std::shared_ptr<PaternPreview> patern_preview = nullptr;
nlohmann::json config_json;
std::filesystem::path program_dir;
} 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

28
includes/grid.hpp Normal file
View File

@@ -0,0 +1,28 @@
/*
* File name: grid.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 <memory>
#include <settings_menu.hpp>
#include <world.hpp>
namespace gol {
class Grid {
public:
Grid(std::shared_ptr<ctx>);
~Grid() = default;
void display();
private:
std::shared_ptr<ctx> context_;
};
}; // namespace gol

View File

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

39
includes/paterns_menu.hpp Normal file
View File

@@ -0,0 +1,39 @@
/*
* 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
#include <memory>
#include <context.hpp>
#include <vector>
#include <map>
#include <string>
namespace gol {
class PaternsMenu {
public:
PaternsMenu(std::shared_ptr<ctx>);
~PaternsMenu() = default;
void Toogle();
bool isOpen();
void display();
void refresh();
private:
bool loadPatern(std::string &path);
bool is_open_ = false;
std::shared_ptr<ctx> context_ = nullptr;
std::map<std::string,std::string> paterns_paths_list_;
std::vector<std::string> paterns_name_list_;
public:
int patern_width_ = 0;
int patern_height_ = 0;
std::vector<uint32_t> loaded_patern_;
};
} // 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

18
includes/snapping.hpp Normal file
View File

@@ -0,0 +1,18 @@
/*
* File name: snapping.hpp
* 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);
Vector2i screenToGrid(Vector2 screen, int 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();
@@ -28,6 +37,7 @@ public:
void setCell(int x, int y);
void resize(int width, int height); // destructive
std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size);
void setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &data);
// Private members
private:

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

27
src/grid.cpp Normal file
View File

@@ -0,0 +1,27 @@
/*
* File name: grid.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <raylib.h>
#include <grid.hpp>
namespace gol {
Grid::Grid(std::shared_ptr<ctx> context) : context_(context) {}
void Grid::display() {
auto cell_size = context_->settings_menu->getCellSize();
for (int j = 0; j < context_->world->getHeight(); j++) {
for (int i = 0; i < context_->world->getWidth(); i++) {
DrawRectangleLines(i * cell_size, j * cell_size, cell_size, cell_size,
GRAY);
}
}
}
} // namespace gol

View File

@@ -1,33 +1,37 @@
/*
* 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>
#include <patern_preview.hpp>
#include <grid.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 +51,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 +62,87 @@ 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"],
// Get program directory
char path_buf[1024];
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1);
if (len == -1) {
std::cerr << "Failed to determine program directory" << std::endl;
return 1;
}
context->program_dir = std::filesystem::path(path_buf).parent_path();
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>(context);
context->patern_preview = std::make_shared<gol::PaternPreview>(context);
gol::Grid grid(context);
// 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);
context->paterns_menu->refresh();
// 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->patern_preview->update();
context->selection_menu->update();
context->settings_menu->update();
// Accumulate time and update simulation at the adjusted speed
deltaTimeAccumulator += deltaTime;
@@ -246,145 +150,30 @@ 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);
}
grid.display();
context->render->display(context->world);
context->selection->display();
context->patern_preview->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;

69
src/patern_preview.cpp Normal file
View File

@@ -0,0 +1,69 @@
/*
* File name: patern_preview.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
#include <patern_preview.hpp>
#include <paterns_menu.hpp>
#include <settings_menu.hpp>
#include <snapping.hpp>
#include <world.hpp>
namespace gol {
PaternPreview::PaternPreview(std::shared_ptr<ctx> context)
: context_(context) {}
void PaternPreview::start() { is_started = true; }
void PaternPreview::update() {
if (!is_started) return;
mouse_pos_ = GetMousePosition();
if (ImGui::IsMouseClicked(1)) {
is_started = false;
return;
}
if (ImGui::IsMouseClicked(0) && !is_unplacable_) {
auto selection_pos =
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
Vector2i size = {context_->paterns_menu->patern_width_,
context_->paterns_menu->patern_height_};
context_->world->setSelection(selection_pos, size,
context_->paterns_menu->loaded_patern_);
is_started = false;
}
// mouse should pass through any present windows
}
void PaternPreview::display() {
if (!is_started) return;
auto cell_size = context_->settings_menu->getCellSize();
auto mouse_in_grid =
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
is_unplacable_ =
((mouse_in_grid.x + context_->paterns_menu->patern_width_ >
context_->world->getWidth()) ||
(mouse_in_grid.y + context_->paterns_menu->patern_height_ >
context_->world->getHeight()));
for (int j = 0; j < context_->paterns_menu->patern_height_; j++) {
for (int i = 0; i < context_->paterns_menu->patern_width_; i++) {
auto cell =
context_->paterns_menu
->loaded_patern_[i + (j * context_->paterns_menu->patern_width_)];
if (cell) {
DrawRectangle((i + mouse_in_grid.x) * cell_size,
(j + mouse_in_grid.y) * cell_size, cell_size, cell_size,
(is_unplacable_) ? RED : BLUE);
}
}
}
}
}; // namespace gol

99
src/paterns_menu.cpp Normal file
View File

@@ -0,0 +1,99 @@
/*
* File name: paterns_menu.cpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <imgui.h>
#include <raylib.h>
#include <rlImGui.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <paterns_menu.hpp>
#include <patern_preview.hpp>
#include <sstream>
namespace gol {
PaternsMenu::PaternsMenu(std::shared_ptr<ctx> ctx) : context_(ctx) {}
void PaternsMenu::Toogle() { is_open_ = !is_open_; }
bool PaternsMenu::isOpen() { return is_open_; }
void PaternsMenu::refresh() {
paterns_name_list_.clear();
paterns_paths_list_.clear();
auto paterns_path = context_->program_dir / "paterns";
if (std::filesystem::exists(paterns_path) &&
std::filesystem::is_directory(paterns_path)) {
for (const auto &entry :
std::filesystem::directory_iterator(paterns_path)) {
if (!std::filesystem::is_directory(entry) &&
entry.path().has_filename()) {
paterns_paths_list_[entry.path().filename()] = entry.path();
paterns_name_list_.push_back(entry.path().filename());
}
}
}
}
bool PaternsMenu::loadPatern(std::string &path) {
std::ifstream file(path);
std::string file_data;
if (!file.is_open()) {
std::cerr << "Failure in opening patern : " << path << std::endl;
return false;
}
loaded_patern_.clear();
try {
std::getline(file, file_data);
std::stringstream ss(file_data);
std::string width, height, data;
std::getline(ss, width, '|');
std::getline(ss, height, '|');
std::getline(ss, data, '|');
patern_width_ = std::stoi(width);
patern_height_ = std::stoi(height);
for (int i = 0; i < patern_width_ * patern_height_; i++) {
loaded_patern_.push_back((data[i] == '1') ? 1 : 0);
}
} catch (std::exception &e) {
std::cerr << "Failure in loading patern : " << path << std::endl;
return false;
}
return true;
}
void PaternsMenu::display() {
if (is_open_) {
ImGuiWindowFlags paterns_flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::SetNextWindowSize(ImVec2(200, 250), ImGuiCond_Always);
ImGui::Begin("paterns", &is_open_, paterns_flags);
for (auto patern_name : paterns_name_list_) {
ImGui::PushID(patern_name.c_str());
if (ImGui::Button(patern_name.c_str()) &&
loadPatern(paterns_paths_list_[patern_name])) {
context_->patern_preview->start();
}
ImGui::PopID();
ImGui::SameLine(ImGui::GetWindowWidth() - 57.);
std::string del_id = patern_name.c_str();
del_id.append("_del");
ImGui::PushID(del_id.c_str());
if (ImGui::Button("delete")) {
std::filesystem::remove(paterns_paths_list_[patern_name]);
refresh();
}
ImGui::PopID();
}
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

107
src/selection_menu.cpp Normal file
View File

@@ -0,0 +1,107 @@
/*
* 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>
#include <paterns_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")) {
// Create paterns dir if not present
std::filesystem::path paterns_dir = context_->program_dir / "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();
context_->paterns_menu->refresh();
}
}
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

19
src/snapping.cpp Normal file
View File

@@ -0,0 +1,19 @@
/*
* File name: snapping.hpp
* Author: lejulien
* Date created: 01-01-1970 00:59:59
// Date modified: 12-01-2026 21:30:10
* ------
*/
#include <snapping.hpp>
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,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; }
@@ -143,7 +143,6 @@ void World::resize(int width, int height) {
}
std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) {
// We assume the selection is in the grid for now
std::vector<uint32_t> data = {static_cast<uint32_t>(size.x),
static_cast<uint32_t>(size.y)};
for (int y = origin.y; y < origin.y + size.y; y++) {
@@ -153,3 +152,12 @@ std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) {
}
return data;
}
void World::setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &data) {
for (int y = 0; y < size.y; y++) {
for (int x = 0; x < size.x; x++) {
(*_data)[(x + origin.x) + (y + origin.y) * _width] = data[x + y * size.x];
}
}
}

View File

@@ -0,0 +1,21 @@
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_CROSSCOMPILING TRUE)
set(MINGW_TRIPLET "x86_64-w64-mingw32")
set(CMAKE_C_COMPILER ${MINGW_TRIPLET}-gcc)
set(CMAKE_CXX_COMPILER ${MINGW_TRIPLET}-g++)
set(CMAKE_RC_COMPILER ${MINGW_TRIPLET}-windres)
set(CMAKE_AR ${MINGW_TRIPLET}-ar)
set(CMAKE_RANLIB ${MINGW_TRIPLET}-ranlib)
set(CMAKE_OBJCOPY ${MINGW_TRIPLET}-objcopy)
set(CMAKE_FIND_ROOT_PATH /usr/${MINGW_TRIPLET})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_C_FLAGS_INIT "-static -static-libgcc -static-libstdc++")
set(CMAKE_CXX_FLAGS_INIT "-static -static-libgcc -static-libstdc++")