Compare commits

..

5 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
8 changed files with 81 additions and 8 deletions

View File

@@ -69,6 +69,7 @@ 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"

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

@@ -27,6 +27,7 @@ private:
bool is_started = false; bool is_started = false;
std::shared_ptr<ctx> context_; std::shared_ptr<ctx> context_;
Vector2 mouse_pos_ = {0, 0}; Vector2 mouse_pos_ = {0, 0};
bool is_unplacable_ = false;
}; };
} // namespace gol } // namespace gol

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

@@ -26,6 +26,7 @@
#include <selection.hpp> #include <selection.hpp>
#include <paterns_menu.hpp> #include <paterns_menu.hpp>
#include <patern_preview.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>();
@@ -109,6 +110,7 @@ int main(int ac, char **av) {
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); context->paterns_menu = std::make_shared<gol::PaternsMenu>(context);
context->patern_preview = std::make_shared<gol::PaternPreview>(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;
@@ -155,6 +157,7 @@ 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(); context->patern_preview->display();

View File

@@ -30,24 +30,28 @@ void PaternPreview::update() {
is_started = false; is_started = false;
return; return;
} }
// if right click stop the preview if (ImGui::IsMouseClicked(0) && !is_unplacable_) {
// if left click, apply patern to current world 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 // mouse should pass through any present windows
} }
void PaternPreview::display() { void PaternPreview::display() {
if (!is_started) return; if (!is_started) return;
std::cout << "preview display started " << std::endl;
auto cell_size = context_->settings_menu->getCellSize(); auto cell_size = context_->settings_menu->getCellSize();
auto mouse_in_grid = auto mouse_in_grid =
screenToGrid(mouse_pos_, context_->settings_menu->getCellSize()); screenToGrid(mouse_pos_, context_->settings_menu->getCellSize());
bool is_unplacable = is_unplacable_ =
((mouse_in_grid.x + context_->paterns_menu->patern_width_ > ((mouse_in_grid.x + context_->paterns_menu->patern_width_ >
context_->world->getWidth()) || context_->world->getWidth()) ||
(mouse_in_grid.y + context_->paterns_menu->patern_height_ > (mouse_in_grid.y + context_->paterns_menu->patern_height_ >
context_->world->getHeight())); context_->world->getHeight()));
std::cout << "mx:" << mouse_in_grid.x << ", my:" << mouse_in_grid.y
<< ", cs:" << context_->settings_menu->getCellSize() << std::endl;
for (int j = 0; j < context_->paterns_menu->patern_height_; j++) { for (int j = 0; j < context_->paterns_menu->patern_height_; j++) {
for (int i = 0; i < context_->paterns_menu->patern_width_; i++) { for (int i = 0; i < context_->paterns_menu->patern_width_; i++) {
auto cell = auto cell =
@@ -56,7 +60,7 @@ void PaternPreview::display() {
if (cell) { if (cell) {
DrawRectangle((i + mouse_in_grid.x) * cell_size, DrawRectangle((i + mouse_in_grid.x) * cell_size,
(j + mouse_in_grid.y) * cell_size, cell_size, cell_size, (j + mouse_in_grid.y) * cell_size, cell_size, cell_size,
(is_unplacable) ? RED : BLUE); (is_unplacable_) ? RED : BLUE);
} }
} }
} }

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];
}
}
}