Compare commits
19 Commits
95a7d6ea9a
...
lju/window
| Author | SHA1 | Date | |
|---|---|---|---|
| 12a95a3efe | |||
| 992f8a3e3a | |||
| 384c811a29 | |||
| a4c9774c28 | |||
| 3e7e7e2547 | |||
| 5c9b60163a | |||
| 6f5ec1d811 | |||
| 327a298383 | |||
| 546d9f24b5 | |||
| 3d89ec74c9 | |||
| c4725847cc | |||
| 8f7b735820 | |||
| 81f1bc5e72 | |||
| 55341973b3 | |||
| f45c83dd77 | |||
| 416d3a2be9 | |||
| 24d8a092e4 | |||
| 9e2e7b562d | |||
| c00c430382 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ _deps
|
||||
CMakeSettings.json
|
||||
.vs
|
||||
.cache
|
||||
build-win-x86_64
|
||||
|
||||
@@ -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,25 +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"
|
||||
set(SRC_CXX_FILES "./src/main.cpp"
|
||||
"./src/rules.cpp"
|
||||
"./src/world.cpp"
|
||||
"./src/grid.cpp"
|
||||
"./src/render.cpp"
|
||||
"./src/control_menu.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}"
|
||||
"./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
|
||||
@@ -84,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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: context.hpp
|
||||
* Author: lejulien
|
||||
* Date created: 01-01-1970 00:59:59
|
||||
// Date modified: 10-01-2026 22:24:41
|
||||
// Date modified: 12-01-2026 21:30:10
|
||||
* ------
|
||||
*/
|
||||
|
||||
@@ -12,13 +12,30 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <world.hpp>
|
||||
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
|
||||
|
||||
@@ -2,39 +2,35 @@
|
||||
* File name: control_menu.hpp
|
||||
* Author: lejulien
|
||||
* 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
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <context.hpp>
|
||||
#include <types.hpp>
|
||||
|
||||
namespace gol {
|
||||
|
||||
class ControlMenu {
|
||||
public:
|
||||
ControlMenu(ctx context);
|
||||
ControlMenu(std::shared_ptr<ctx> context);
|
||||
~ControlMenu() = default;
|
||||
void update();
|
||||
void display();
|
||||
private:
|
||||
ctx context_;
|
||||
std::shared_ptr<ctx> context_;
|
||||
public: // Keep those public for easy access
|
||||
int fps_ctrl_ = false;
|
||||
int cell_size_ctrl_ = false;
|
||||
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;
|
||||
bool settings_window_ = false;
|
||||
bool paterns_ctrl_ = false;
|
||||
int width_ctrl_ = false;
|
||||
int height_ctrl_ = false;
|
||||
bool dark_theme_ctrl_ = false;
|
||||
bool apply_ctrl_ = false;
|
||||
};
|
||||
|
||||
} // namespace gol
|
||||
|
||||
28
includes/grid.hpp
Normal file
28
includes/grid.hpp
Normal 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
|
||||
33
includes/patern_preview.hpp
Normal file
33
includes/patern_preview.hpp
Normal 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
39
includes/paterns_menu.hpp
Normal 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
|
||||
@@ -2,10 +2,12 @@
|
||||
* File name: render.hpp
|
||||
* Author: lejulien
|
||||
* 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"
|
||||
|
||||
#pragma once
|
||||
@@ -16,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:
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: rules.hpp
|
||||
* Author: lejulien
|
||||
* 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:
|
||||
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
32
includes/selection.hpp
Normal 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
|
||||
38
includes/selection_menu.hpp
Normal file
38
includes/selection_menu.hpp
Normal 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
|
||||
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
|
||||
18
includes/snapping.hpp
Normal file
18
includes/snapping.hpp
Normal 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);
|
||||
@@ -2,18 +2,19 @@
|
||||
* File name: world.hpp
|
||||
* Author: lejulien
|
||||
* 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 <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
|
||||
#include "../includes/types.hpp"
|
||||
#include <types.hpp>
|
||||
#include <context.hpp>
|
||||
|
||||
#pragma once
|
||||
|
||||
@@ -22,7 +23,7 @@
|
||||
class World {
|
||||
|
||||
public:
|
||||
World(int width, int height);
|
||||
World(std::shared_ptr<gol::ctx> ctx);
|
||||
~World();
|
||||
|
||||
std::vector<bool> *getWorldData();
|
||||
@@ -36,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:
|
||||
|
||||
@@ -2,29 +2,87 @@
|
||||
* File name: control_menu.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 22:12:44
|
||||
// Date modified: 10-01-2026 22:43:36
|
||||
// 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(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;
|
||||
step_ctrl_ = false;
|
||||
step_back_ctrl_ = false;
|
||||
rand_ctrl_ = false;
|
||||
edit_ctrl_ = false;
|
||||
clear_ctrl_ = false;
|
||||
settings_window_ = 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;
|
||||
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
27
src/grid.cpp
Normal 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
|
||||
353
src/main.cpp
353
src/main.cpp
@@ -2,13 +2,12 @@
|
||||
* File name: main.cpp
|
||||
* Author: lejulien
|
||||
* Date created: 10-01-2026 21:59:32
|
||||
// Date modified: 10-01-2026 22:55:11
|
||||
// Date modified: 12-01-2026 22:17:54
|
||||
* ------
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <unistd.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
@@ -22,22 +21,17 @@
|
||||
#include <types.hpp>
|
||||
#include <context.hpp>
|
||||
#include <control_menu.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))};
|
||||
}
|
||||
#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) {
|
||||
gol::ctx context;
|
||||
std::shared_ptr<gol::ctx> context = std::make_shared<gol::ctx>();
|
||||
// Load or default config
|
||||
const std::string config_file_name = "config.json";
|
||||
MenuState menu_state = MenuState::NONE;
|
||||
|
||||
std::fstream config_file(config_file_name, std::ios::in | std::ios::out);
|
||||
|
||||
@@ -57,7 +51,7 @@ int main(int ac, char **av) {
|
||||
// Try reading the configuration
|
||||
try {
|
||||
config_file.seekg(0);
|
||||
config_file >> context.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;
|
||||
@@ -68,175 +62,87 @@ int main(int ac, char **av) {
|
||||
config_file.close();
|
||||
|
||||
// Check config values or populate them
|
||||
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 (!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 (!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 (!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 (!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 (!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 (!context.config_json.contains("dark_theme")) {
|
||||
context.config_json["dark_theme"] = true;
|
||||
if (!context->config_json.contains("dark_theme")) {
|
||||
context->config_json["dark_theme"] = true;
|
||||
}
|
||||
if (!context.config_json.contains("fps") ||
|
||||
(context.config_json["fps"] < 0 || context.config_json["fps"] > 30)) {
|
||||
context.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(context.config_json["screen_width"], context.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(context.config_json["dark_theme"]);
|
||||
// Selection window
|
||||
|
||||
RenderTexture2D selectionTexture = LoadRenderTexture(200, 200);
|
||||
rlImGuiSetup(context->config_json["dark_theme"]);
|
||||
|
||||
// Initialize objects
|
||||
context.world = std::make_shared<World>(
|
||||
context.config_json["screen_width"].get<int>() /
|
||||
context.config_json["cell_size"].get<int>(),
|
||||
context.config_json["screen_height"].get<int>() /
|
||||
context.config_json["cell_size"].get<int>());
|
||||
Rules rules = Rules();
|
||||
Render render(context.config_json["cell_size"]);
|
||||
|
||||
// Imgui control menu
|
||||
gol::ControlMenu control_menu(context);
|
||||
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(&(*context.world));
|
||||
context->rules->setup(context->world);
|
||||
context->paterns_menu->refresh();
|
||||
// Diplay generations
|
||||
while (!WindowShouldClose()) {
|
||||
if (menu_state == MenuState::PLAY || menu_state == MenuState::EDIT) {
|
||||
if (!control_menu.play_ctrl_ && !control_menu.edit_ctrl_) {
|
||||
menu_state = MenuState::NONE;
|
||||
}
|
||||
}
|
||||
// Frames shinenigans
|
||||
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;
|
||||
|
||||
context->control_menu->update();
|
||||
|
||||
auto gesture = GetGestureDetected();
|
||||
auto mousePos = GetMousePosition();
|
||||
if (control_menu.rand_ctrl_) {
|
||||
context.world->randomize();
|
||||
control_menu.rand_ctrl_ = false;
|
||||
}
|
||||
if (control_menu.clear_ctrl_) {
|
||||
context.world->clear();
|
||||
control_menu.clear_ctrl_ = false;
|
||||
}
|
||||
if (control_menu.edit_ctrl_ && control_menu.play_ctrl_) {
|
||||
if (menu_state == MenuState::PLAY) {
|
||||
menu_state = MenuState::EDIT;
|
||||
control_menu.play_ctrl_ = false;
|
||||
} else if (menu_state == MenuState::EDIT) {
|
||||
menu_state = MenuState::PLAY;
|
||||
control_menu.edit_ctrl_ = false;
|
||||
}
|
||||
} else if (control_menu.play_ctrl_) {
|
||||
menu_state = MenuState::PLAY;
|
||||
} else if (control_menu.edit_ctrl_) {
|
||||
menu_state = MenuState::EDIT;
|
||||
}
|
||||
if (control_menu.edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
if (context->control_menu->edit_ctrl_ && IsMouseButtonPressed(0) &&
|
||||
!ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
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>());
|
||||
}
|
||||
// Selection behaviour
|
||||
if (!control_menu.edit_ctrl_ && !control_menu.play_ctrl_ && !control_menu.paterns_ctrl_ && !control_menu.settings_window_) {
|
||||
if (gesture == GESTURE_TAP && !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) {
|
||||
sel_pos = snapToGrid(mousePos, control_menu.cell_size_ctrl_);
|
||||
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_ ) {
|
||||
selecting = false;
|
||||
Vector2i p1 = screenToGrid(sel_pos, control_menu.cell_size_ctrl_);
|
||||
Vector2i p2 = screenToGrid(mousePos, control_menu.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(context.world->getSelection(orig, sel_size));
|
||||
sel_ctrl = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!sel_ctrl) {
|
||||
patern_name[0] = '\0';
|
||||
}
|
||||
if (control_menu.step_ctrl_) {
|
||||
context.world->saveCompressed();
|
||||
rules.update();
|
||||
control_menu.step_ctrl_ = false;
|
||||
}
|
||||
if (control_menu.step_back_ctrl_) {
|
||||
context.world->stepBack();
|
||||
control_menu.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 (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_);
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
@@ -244,145 +150,30 @@ int main(int ac, char **av) {
|
||||
if (deltaTimeAccumulator >= timePerUpdate) {
|
||||
// Reset accumulator
|
||||
deltaTimeAccumulator -= timePerUpdate;
|
||||
if (menu_state == MenuState::PLAY) {
|
||||
context.world->saveCompressed();
|
||||
rules.update();
|
||||
if (context->control_menu->menu_state_ == MenuState::PLAY) {
|
||||
context->world->saveCompressed();
|
||||
context->rules->update();
|
||||
}
|
||||
}
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
render.display(&(*context.world));
|
||||
if (selecting) {
|
||||
auto grid_mouse = snapToGrid(mousePos, control_menu.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", &control_menu.play_ctrl_);
|
||||
if (ImGui::Button("Step")) {
|
||||
control_menu.step_ctrl_ = true;
|
||||
}
|
||||
if (ImGui::Button("Step Back")) {
|
||||
control_menu.step_back_ctrl_ = true;
|
||||
}
|
||||
ImGui::Checkbox("Edit", &control_menu.edit_ctrl_);
|
||||
ImGui::Checkbox("Clear", &control_menu.clear_ctrl_);
|
||||
ImGui::Checkbox("Randomize", &control_menu.rand_ctrl_);
|
||||
if (ImGui::Button((control_menu.paterns_ctrl_) ? "Hide paterns" : "Show paterns")) {
|
||||
control_menu.paterns_ctrl_ = !control_menu.paterns_ctrl_;
|
||||
}
|
||||
if (ImGui::Button((control_menu.settings_window_) ? "Hide settings" : "Show settings")) {
|
||||
control_menu.settings_window_ = !control_menu.settings_window_;
|
||||
}
|
||||
ImGui::Text("Generation: %zu", context.world->getCycle());
|
||||
ImGui::End();
|
||||
if (control_menu.paterns_ctrl_) {
|
||||
ImGuiWindowFlags paterns_flags =
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize;
|
||||
ImGui::SetNextWindowSize(ImVec2(150, 200), ImGuiCond_Always);
|
||||
ImGui::Begin("paterns", &control_menu.paterns_ctrl_, paterns_flags);
|
||||
ImGui::Button("refresh");
|
||||
ImGui::End();
|
||||
}
|
||||
if (control_menu.settings_window_) {
|
||||
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) {
|
||||
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 << context.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
69
src/patern_preview.cpp
Normal 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
99
src/paterns_menu.cpp
Normal 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
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: render.cpp
|
||||
* Author: lejulien
|
||||
* 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
|
||||
|
||||
void Render::display(World *world) {
|
||||
void Render::display(std::shared_ptr<World> world) {
|
||||
display_world(world->getWorldData(), world->getWidth(), world->getHeight());
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: rules.cpp
|
||||
* Author: lejulien
|
||||
* 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
|
||||
|
||||
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
68
src/selection.cpp
Normal 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
107
src/selection_menu.cpp
Normal 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
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
|
||||
19
src/snapping.cpp
Normal file
19
src/snapping.cpp
Normal 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))};
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
* File name: world.cpp
|
||||
* Author: lejulien
|
||||
* 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,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; }
|
||||
@@ -139,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++) {
|
||||
@@ -149,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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
toolchain-mingw-w64-x86_64.cmake
Normal file
21
toolchain-mingw-w64-x86_64.cmake
Normal 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++")
|
||||
|
||||
Reference in New Issue
Block a user