Compare commits
1 Commits
main
...
ceb83d5ab3
| Author | SHA1 | Date | |
|---|---|---|---|
| ceb83d5ab3 |
@@ -69,7 +69,6 @@ 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"
|
||||||
@@ -77,7 +76,6 @@ set(SRC_CXX_FILES "./src/main.cpp"
|
|||||||
"./src/selection.cpp"
|
"./src/selection.cpp"
|
||||||
"./src/paterns_menu.cpp"
|
"./src/paterns_menu.cpp"
|
||||||
"./src/patern_preview.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"
|
||||||
|
|||||||
@@ -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 dependencies of raylib
|
Ensure you have g++, cmake and the thendependencies of raylib
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
mkdir build
|
mkdir build
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
@@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <raylib.h>
|
|
||||||
|
|
||||||
#include <context.hpp>
|
#include <context.hpp>
|
||||||
|
|
||||||
namespace gol {
|
namespace gol {
|
||||||
@@ -26,8 +24,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool is_started = false;
|
bool is_started = false;
|
||||||
std::shared_ptr<ctx> context_;
|
std::shared_ptr<ctx> context_;
|
||||||
Vector2 mouse_pos_ = {0, 0};
|
|
||||||
bool is_unplacable_ = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gol
|
} // namespace gol
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ private:
|
|||||||
std::shared_ptr<ctx> context_ = nullptr;
|
std::shared_ptr<ctx> context_ = nullptr;
|
||||||
std::map<std::string,std::string> paterns_paths_list_;
|
std::map<std::string,std::string> paterns_paths_list_;
|
||||||
std::vector<std::string> paterns_name_list_;
|
std::vector<std::string> paterns_name_list_;
|
||||||
public:
|
|
||||||
int patern_width_ = 0;
|
int patern_width_ = 0;
|
||||||
int patern_height_ = 0;
|
int patern_height_ = 0;
|
||||||
std::vector<uint32_t> loaded_patern_;
|
std::vector<uint32_t> loaded_patern_;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* File name: snapping.hpp
|
* File name: selection.cpp
|
||||||
* 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,6 +13,12 @@
|
|||||||
#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))};
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ 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
27
src/grid.cpp
@@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
@@ -25,8 +25,6 @@
|
|||||||
#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>();
|
||||||
@@ -109,8 +107,6 @@ int main(int ac, char **av) {
|
|||||||
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);
|
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;
|
||||||
@@ -140,7 +136,6 @@ 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();
|
||||||
|
|
||||||
@@ -157,10 +152,8 @@ 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();
|
||||||
|
|||||||
@@ -6,64 +6,27 @@
|
|||||||
* ------
|
* ------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <imgui.h>
|
|
||||||
#include <raylib.h>
|
|
||||||
#include <rlImGui.h>
|
|
||||||
|
|
||||||
#include <patern_preview.hpp>
|
#include <patern_preview.hpp>
|
||||||
#include <paterns_menu.hpp>
|
|
||||||
#include <settings_menu.hpp>
|
|
||||||
#include <snapping.hpp>
|
|
||||||
#include <world.hpp>
|
|
||||||
|
|
||||||
namespace gol {
|
namespace gol {
|
||||||
|
|
||||||
PaternPreview::PaternPreview(std::shared_ptr<ctx> context)
|
PaternPreview::PaternPreview(std::shared_ptr<ctx> context): context_(context) {}
|
||||||
: context_(context) {}
|
|
||||||
|
|
||||||
void PaternPreview::start() { is_started = true; }
|
void PaternPreview::start() {
|
||||||
|
// TODO: load patern from pater menu
|
||||||
|
is_started = true;
|
||||||
|
}
|
||||||
|
|
||||||
void PaternPreview::update() {
|
void PaternPreview::update() {
|
||||||
if (!is_started) return;
|
if (!is_started) return;
|
||||||
mouse_pos_ = GetMousePosition();
|
// gather mousePos
|
||||||
if (ImGui::IsMouseClicked(1)) {
|
// if right click stop the preview
|
||||||
is_started = false;
|
// if left click, apply patern to current world
|
||||||
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
|
// mouse should pass through any present windows
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaternPreview::display() {
|
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
|
};
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ bool PaternsMenu::loadPatern(std::string &path) {
|
|||||||
}
|
}
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
std::cerr << "Failure in loading patern : " << path << std::endl;
|
std::cerr << "Failure in loading patern : " << path << std::endl;
|
||||||
return false;
|
return 1;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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))};
|
|
||||||
}
|
|
||||||
@@ -143,6 +143,7 @@ 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++) {
|
||||||
@@ -152,12 +153,3 @@ 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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user