Compare commits

..

10 Commits

Author SHA1 Message Date
c35097b3d3 Merge pull request 'grid: add a guide grid' (#5) from lju/adding-grid into main
Reviewed-on: #5
2026-01-15 08:14:32 +00: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
15 changed files with 332 additions and 53 deletions

View File

@@ -69,12 +69,15 @@ include_directories(${imgui_SOURCE_DIR} ${rlImGui_SOURCE_DIR})
set(SRC_CXX_FILES "./src/main.cpp" set(SRC_CXX_FILES "./src/main.cpp"
"./src/rules.cpp" "./src/rules.cpp"
"./src/world.cpp" "./src/world.cpp"
"./src/grid.cpp"
"./src/render.cpp" "./src/render.cpp"
"./src/control_menu.cpp" "./src/control_menu.cpp"
"./src/settings_menu.cpp" "./src/settings_menu.cpp"
"./src/selection_menu.cpp" "./src/selection_menu.cpp"
"./src/selection.cpp" "./src/selection.cpp"
"./src/paterns_menu.cpp" "./src/paterns_menu.cpp"
"./src/patern_preview.cpp"
"./src/snapping.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"

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 ## 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 ```shell
mkdir build mkdir build

View File

@@ -22,6 +22,7 @@ class SelectionMenu;
class ControlMenu; class ControlMenu;
class Selection; class Selection;
class PaternsMenu; class PaternsMenu;
class PaternPreview;
typedef struct ctx { typedef struct ctx {
std::shared_ptr<World> world = nullptr; std::shared_ptr<World> world = nullptr;
@@ -32,7 +33,9 @@ typedef struct ctx {
std::shared_ptr<ControlMenu> control_menu = nullptr; std::shared_ptr<ControlMenu> control_menu = nullptr;
std::shared_ptr<Selection> selection = nullptr; std::shared_ptr<Selection> selection = nullptr;
std::shared_ptr<PaternsMenu> paterns_menu = nullptr; std::shared_ptr<PaternsMenu> paterns_menu = nullptr;
std::shared_ptr<PaternPreview> patern_preview = nullptr;
nlohmann::json config_json; nlohmann::json config_json;
std::filesystem::path program_dir;
} ctx; } ctx;
} // namespace gol } // 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

View File

@@ -8,17 +8,32 @@
#pragma once #pragma once
#include <memory>
#include <context.hpp>
#include <vector>
#include <map>
#include <string>
namespace gol { namespace gol {
class PaternsMenu { class PaternsMenu {
public: public:
PaternsMenu() = default; PaternsMenu(std::shared_ptr<ctx>);
~PaternsMenu() = default; ~PaternsMenu() = default;
void Toogle(); void Toogle();
bool isOpen(); bool isOpen();
void display(); void display();
void refresh();
private: private:
bool loadPatern(std::string &path);
bool is_open_ = false; 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 } // namespace gol

View File

@@ -1,5 +1,5 @@
/* /*
* File name: selection.cpp * File name: snapping.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 21:30:10 // Date modified: 12-01-2026 21:30:10
@@ -13,12 +13,6 @@
#include <raylib.h> #include <raylib.h>
#include <cmath> #include <cmath>
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),
static_cast<float>(round(screen.y / cell_size) * cell_size)};
}
Vector2i screenToGrid(Vector2 screen, int 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

@@ -37,6 +37,7 @@ public:
void setCell(int x, int y); void setCell(int x, int y);
void resize(int width, int height); // destructive void resize(int width, int height); // destructive
std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size); std::vector<uint32_t> getSelection(Vector2i &origin, Vector2i &size);
void setSelection(Vector2i &origin, Vector2i &size, std::vector<uint32_t> &data);
// Private members // Private members
private: private:

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

@@ -25,6 +25,8 @@
#include <selection_menu.hpp> #include <selection_menu.hpp>
#include <selection.hpp> #include <selection.hpp>
#include <paterns_menu.hpp> #include <paterns_menu.hpp>
#include <patern_preview.hpp>
#include <grid.hpp>
int main(int ac, char **av) { int main(int ac, char **av) {
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>(); std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
@@ -82,6 +84,15 @@ int main(int ac, char **av) {
context->config_json["fps"] = 800; context->config_json["fps"] = 800;
} }
// 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"], InitWindow(context->config_json["screen_width"], context->config_json["screen_height"],
&av[0][2]); &av[0][2]);
@@ -97,7 +108,9 @@ int main(int ac, char **av) {
context->render = std::make_shared<Render>(context->settings_menu->getCellSize()); context->render = std::make_shared<Render>(context->settings_menu->getCellSize());
context->selection_menu = std::make_shared<gol::SelectionMenu>(context); context->selection_menu = std::make_shared<gol::SelectionMenu>(context);
context->selection = std::make_shared<gol::Selection>(context); context->selection = std::make_shared<gol::Selection>(context);
context->paterns_menu = std::make_shared<gol::PaternsMenu>(); 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 // Speed handling values
float sim_speed = 1.0f; float sim_speed = 1.0f;
@@ -106,6 +119,7 @@ int main(int ac, char **av) {
// Setups // Setups
context->rules->setup(context->world); context->rules->setup(context->world);
context->paterns_menu->refresh();
// Diplay generations // Diplay generations
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
// Frames shinenigans // Frames shinenigans
@@ -126,6 +140,7 @@ int main(int ac, char **av) {
// Selection behaviour // Selection behaviour
context->selection->update(); context->selection->update();
context->patern_preview->update();
context->selection_menu->update(); context->selection_menu->update();
context->settings_menu->update(); context->settings_menu->update();
@@ -142,8 +157,10 @@ int main(int ac, char **av) {
} }
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
grid.display();
context->render->display(context->world); context->render->display(context->world);
context->selection->display(); context->selection->display();
context->patern_preview->display();
// Start ImGui frame // Start ImGui frame
rlImGuiBegin(); rlImGuiBegin();
context->control_menu->display(); context->control_menu->display();

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

View File

@@ -6,30 +6,94 @@
* ------ * ------
*/ */
#include <paterns_menu.hpp>
#include <imgui.h> #include <imgui.h>
#include <raylib.h> #include <raylib.h>
#include <rlImGui.h> #include <rlImGui.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <paterns_menu.hpp>
#include <patern_preview.hpp>
#include <sstream>
namespace gol { namespace gol {
void PaternsMenu::Toogle() { PaternsMenu::PaternsMenu(std::shared_ptr<ctx> ctx) : context_(ctx) {}
is_open_ = !is_open_;
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::isOpen() { bool PaternsMenu::loadPatern(std::string &path) {
return is_open_; 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() { void PaternsMenu::display() {
if (is_open_) { if (is_open_) {
ImGuiWindowFlags paterns_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize; ImGuiWindowFlags paterns_flags =
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always); ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
ImGui::SetNextWindowSize(ImVec2(200, 250), ImGuiCond_Always);
ImGui::Begin("paterns", &is_open_, paterns_flags); ImGui::Begin("paterns", &is_open_, paterns_flags);
ImGui::Button("refresh"); 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(); ImGui::End();
} }
} }
} // namespace gol } // namespace gol

View File

@@ -12,6 +12,7 @@
#include <filesystem> #include <filesystem>
#include <selection_menu.hpp> #include <selection_menu.hpp>
#include <paterns_menu.hpp>
namespace gol { namespace gol {
@@ -62,41 +63,38 @@ void SelectionMenu::display() {
rlImGuiImageSize(&selectionTexture_.texture, 200, 200); rlImGuiImageSize(&selectionTexture_.texture, 200, 200);
ImGui::InputText("patern_name", patern_name_, 255); ImGui::InputText("patern_name", patern_name_, 255);
if (ImGui::Button("Save")) { if (ImGui::Button("Save")) {
char path_buf[1024]; // Create paterns dir if not present
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1); std::filesystem::path paterns_dir = context_->program_dir / "paterns";
if (len != -1) { if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) {
// Create paterns dir if not present std::cerr << "Failed to create paterns directory" << std::endl;
std::filesystem::path paterns_dir = std::filesystem::path(path_buf).parent_path() / "paterns"; } else { // Could be optimized by early returning in a function
if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) { std::ofstream patern_file;
std::cerr << "Failed to create paterns directory" << std::endl; paterns_dir += '/';
} else { // Could be optimized by early returning in a function paterns_dir += patern_name_;
std::ofstream patern_file; patern_file.open(paterns_dir);
paterns_dir += '/'; if (!patern_file) {
paterns_dir += patern_name_; std::cerr << "Failed to create the patern file" << std::endl;
patern_file.open(paterns_dir); } else {
if (!patern_file) { auto sel_it = sel_data_.begin();
std::cerr << "Failed to create the patern file" << std::endl; sel_it += 2; // skip dimensions
} else { patern_file << sel_data_[0];
auto sel_it = sel_data_.begin(); patern_file << "|"; // Separator needed to split as ascii values
sel_it += 2; // skip dimensions patern_file << sel_data_[1];
patern_file << sel_data_[0]; patern_file << "|"; // Separator needed to split as ascii values
patern_file << "|"; // Separator needed to split as ascii values for (int j = 0; j < sel_data_[1]; j++) {
patern_file << sel_data_[1]; for (int i = 0; i < sel_data_[0]; i++) {
patern_file << "|"; // Separator needed to split as ascii values patern_file << std::to_string(*sel_it);
for (int j = 0; j < sel_data_[1]; j++) { if (*sel_it == 1) {
for (int i = 0; i < sel_data_[0]; i++) {
patern_file << std::to_string(*sel_it);
if (*sel_it == 1) {
}
sel_it++;
} }
sel_it++;
} }
patern_file << std::flush;
patern_file.close();
} }
patern_file << std::flush;
patern_file.close();
context_->paterns_menu->refresh();
} }
sel_ctrl_ = false;
} }
sel_ctrl_ = false;
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Discard")) { if (ImGui::Button("Discard")) {

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

@@ -143,7 +143,6 @@ void World::resize(int width, int height) {
} }
std::vector<uint32_t> World::getSelection(Vector2i &origin, Vector2i &size) { 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), std::vector<uint32_t> data = {static_cast<uint32_t>(size.x),
static_cast<uint32_t>(size.y)}; static_cast<uint32_t>(size.y)};
for (int y = origin.y; y < origin.y + size.y; 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; 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];
}
}
}