rename files based on their classes

This commit is contained in:
2026-02-28 22:30:00 +01:00
parent 809fbf1b93
commit 9ab96c5903
22 changed files with 677 additions and 657 deletions

View File

@ -72,4 +72,4 @@ constexpr Color BLOCK_COLOR = DARKBLUE;
constexpr Color TARGET_BLOCK_COLOR = RED;
constexpr Color WALL_COLOR = BLACK;
#endif
#endif

View File

@ -1,7 +1,7 @@
#ifndef INPUT_HPP_
#define INPUT_HPP_
#include "camera.hpp"
#include "orbit_camera.hpp"
#include "state_manager.hpp"
#include <functional>

View File

@ -0,0 +1,114 @@
#ifndef MASS_SPRING_SYSTEM_HPP_
#define MASS_SPRING_SYSTEM_HPP_
#include "octree.hpp"
#include "util.hpp"
#include "config.hpp"
#include <raylib.h>
#include <raymath.h>
#ifdef THREADPOOL
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#endif
#define BS_THREAD_POOL_NATIVE_EXTENSIONS
#include <BS_thread_pool.hpp>
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif
#endif
class mass_spring_system
{
public:
class mass
{
public:
Vector3 position = Vector3Zero();
Vector3 previous_position = Vector3Zero(); // for verlet integration
Vector3 velocity = Vector3Zero();
Vector3 force = Vector3Zero();
public:
mass() = delete;
explicit mass(const Vector3 _position)
: position(_position), previous_position(_position) {}
public:
auto clear_force() -> void;
auto calculate_velocity(float delta_time) -> void;
auto calculate_position(float delta_time) -> void;
auto verlet_update(float delta_time) -> void;
};
class spring
{
public:
size_t a;
size_t b;
public:
spring(const size_t _a, const size_t _b)
: a(_a), b(_b) {}
public:
static auto calculate_spring_force(mass& _a, mass& _b) -> void;
};
private:
#ifdef THREADPOOL
BS::thread_pool<> threads;
#endif
public:
octree tree;
// This is the main ownership of all the states/masses/springs.
std::vector<mass> masses;
std::vector<spring> springs;
public:
mass_spring_system()
#ifdef THREADPOOL
: threads(std::thread::hardware_concurrency() - 1, set_thread_name)
#endif
{
infoln("Using Barnes-Hut + Octree repulsion force calculation.");
#ifdef THREADPOOL
infoln("Thread-pool: {} threads.", threads.get_thread_count());
#else
infoln("Thread-pool: Disabled.");
#endif
}
mass_spring_system(const mass_spring_system& copy) = delete;
auto operator=(const mass_spring_system& copy) -> mass_spring_system& = delete;
mass_spring_system(mass_spring_system& move) = delete;
auto operator=(mass_spring_system&& move) -> mass_spring_system& = delete;
private:
#ifdef THREADPOOL
static auto set_thread_name(size_t idx) -> void;
#endif
auto build_octree() -> void;
public:
auto clear() -> void;
auto add_mass() -> void;
auto add_spring(size_t a, size_t b) -> void;
auto clear_forces() -> void;
auto calculate_spring_forces() -> void;
auto calculate_repulsion_forces() -> void;
auto verlet_update(float delta_time) -> void;
auto center_masses() -> void;
};
#endif

View File

@ -1,205 +0,0 @@
#ifndef PHYSICS_HPP_
#define PHYSICS_HPP_
#include "config.hpp"
#include "octree.hpp"
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <raylib.h>
#include <raymath.h>
#include <thread>
#include <variant>
#include <vector>
#include "util.hpp"
#ifdef THREADPOOL
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#endif
#define BS_THREAD_POOL_NATIVE_EXTENSIONS
#include <BS_thread_pool.hpp>
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif
#endif
#ifdef TRACY
#include <tracy/Tracy.hpp>
#endif
class mass
{
public:
Vector3 position = Vector3Zero();
Vector3 previous_position = Vector3Zero(); // for verlet integration
Vector3 velocity = Vector3Zero();
Vector3 force = Vector3Zero();
public:
mass() = delete;
explicit mass(const Vector3 _position) : position(_position), previous_position(_position)
{}
public:
auto clear_force() -> void;
auto calculate_velocity(float delta_time) -> void;
auto calculate_position(float delta_time) -> void;
auto verlet_update(float delta_time) -> void;
};
class spring
{
public:
size_t a;
size_t b;
public:
spring(const size_t _a, const size_t _b) : a(_a), b(_b)
{}
public:
static auto calculate_spring_force(mass& _a, mass& _b) -> void;
};
class mass_spring_system
{
private:
#ifdef THREADPOOL
BS::thread_pool<> threads;
#endif
public:
octree tree;
// This is the main ownership of all the states/masses/springs.
std::vector<mass> masses;
std::vector<spring> springs;
public:
mass_spring_system()
#ifdef THREADPOOL
: threads(std::thread::hardware_concurrency() - 1, set_thread_name)
#endif
{
infoln("Using Barnes-Hut + Octree repulsion force calculation.");
#ifdef THREADPOOL
infoln("Thread-pool: {} threads.", threads.get_thread_count());
#else
infoln("Thread-pool: Disabled.");
#endif
}
mass_spring_system(const mass_spring_system& copy) = delete;
auto operator=(const mass_spring_system& copy) -> mass_spring_system& = delete;
mass_spring_system(mass_spring_system& move) = delete;
auto operator=(mass_spring_system&& move) -> mass_spring_system& = delete;
private:
#ifdef THREADPOOL
static auto set_thread_name(size_t idx) -> void;
#endif
auto build_octree() -> void;
public:
auto clear() -> void;
auto add_mass() -> void;
auto add_spring(size_t a, size_t b) -> void;
auto clear_forces() -> void;
auto calculate_spring_forces() -> void;
auto calculate_repulsion_forces() -> void;
auto verlet_update(float delta_time) -> void;
auto center_masses() -> void;
};
class threaded_physics
{
struct add_mass
{};
struct add_spring
{
size_t a;
size_t b;
};
struct clear_graph
{};
using command = std::variant<add_mass, add_spring, clear_graph>;
struct physics_state
{
#ifdef TRACY
TracyLockable(std::mutex, command_mtx);
#else
std::mutex command_mtx;
#endif
std::queue<command> pending_commands;
#ifdef TRACY
TracyLockable(std::mutex, data_mtx);
#else
std::mutex data_mtx;
#endif
std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd;
Vector3 mass_center = Vector3Zero();
int ups = 0;
size_t mass_count = 0; // For debug
size_t spring_count = 0; // For debug
std::vector<Vector3> masses; // Read by renderer
bool data_ready = false;
bool data_consumed = true;
std::atomic<bool> running{true};
};
private:
std::thread physics;
public:
physics_state state;
public:
threaded_physics() : physics(physics_thread, std::ref(state))
{}
threaded_physics(const threaded_physics& copy) = delete;
auto operator=(const threaded_physics& copy) -> threaded_physics& = delete;
threaded_physics(threaded_physics&& move) = delete;
auto operator=(threaded_physics&& move) -> threaded_physics& = delete;
~threaded_physics()
{
state.running = false;
state.data_ready_cnd.notify_all();
state.data_consumed_cnd.notify_all();
physics.join();
}
private:
static auto physics_thread(physics_state& state) -> void;
public:
auto add_mass_cmd() -> void;
auto add_spring_cmd(size_t a, size_t b) -> void;
auto clear_cmd() -> void;
auto add_mass_springs_cmd(size_t num_masses,
const std::vector<std::pair<size_t, size_t>>& springs) -> void;
};
#endif

View File

@ -10,14 +10,6 @@
#include <string>
#include <vector>
enum direction
{
nor = 1 << 0,
eas = 1 << 1,
sou = 1 << 2,
wes = 1 << 3,
};
// A state is represented by a string "MWHXYblocks", where M is "R"
// (restricted) or "F" (free), W is the board width, H is the board height, X
// is the target block x goal, Y is the target block y goal and blocks is an
@ -52,7 +44,7 @@ public:
const bool _target = false, const bool _immovable = false)
: x(_x), y(_y), width(_width), height(_height), target(_target), immovable(_immovable)
{
if (_x < 0 || _x + _width > 9 || _y < 0 || _y + _height > 9) {
if (_x < 0 || _x + _width > MAX_WIDTH || _y < 0 || _y + _height > MAX_HEIGHT) {
errln("Block must fit in a 9x9 board!");
exit(1);
}
@ -348,4 +340,4 @@ struct std::equal_to<std::pair<puzzle, puzzle>>
using win_condition = std::function<bool(const puzzle&)>;
#endif
#endif

View File

@ -1,9 +1,9 @@
#ifndef RENDERER_HPP_
#define RENDERER_HPP_
#include "camera.hpp"
#include "orbit_camera.hpp"
#include "config.hpp"
#include "input.hpp"
#include "input_handler.hpp"
#include "state_manager.hpp"
#include "user_interface.hpp"
@ -106,4 +106,4 @@ public:
size_t spring_count) -> void;
};
#endif
#endif

View File

@ -1,8 +1,8 @@
#ifndef STATE_MANAGER_HPP_
#define STATE_MANAGER_HPP_
#include "distance.hpp"
#include "physics.hpp"
#include "graph_distances.hpp"
#include "threaded_physics.hpp"
#include "puzzle.hpp"
#include <stack>
@ -151,4 +151,4 @@ public:
[[nodiscard]] auto was_edited() const -> bool;
};
#endif
#endif

View File

@ -0,0 +1,95 @@
#ifndef PHYSICS_HPP_
#define PHYSICS_HPP_
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <raylib.h>
#include <raymath.h>
#include <thread>
#include <variant>
#include <vector>
#ifdef TRACY
#include <tracy/Tracy.hpp>
#endif
class threaded_physics
{
struct add_mass {};
struct add_spring
{
size_t a;
size_t b;
};
struct clear_graph {};
using command = std::variant<add_mass, add_spring, clear_graph>;
struct physics_state
{
#ifdef TRACY
TracyLockable(std::mutex, command_mtx);
#else
std::mutex command_mtx;
#endif
std::queue<command> pending_commands;
#ifdef TRACY
TracyLockable(std::mutex, data_mtx);
#else
std::mutex data_mtx;
#endif
std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd;
Vector3 mass_center = Vector3Zero();
int ups = 0;
size_t mass_count = 0; // For debug
size_t spring_count = 0; // For debug
std::vector<Vector3> masses; // Read by renderer
bool data_ready = false;
bool data_consumed = true;
std::atomic<bool> running{true};
};
private:
std::thread physics;
public:
physics_state state;
public:
threaded_physics()
: physics(physics_thread, std::ref(state)) {}
threaded_physics(const threaded_physics& copy) = delete;
auto operator=(const threaded_physics& copy) -> threaded_physics& = delete;
threaded_physics(threaded_physics&& move) = delete;
auto operator=(threaded_physics&& move) -> threaded_physics& = delete;
~threaded_physics()
{
state.running = false;
state.data_ready_cnd.notify_all();
state.data_consumed_cnd.notify_all();
physics.join();
}
private:
static auto physics_thread(physics_state& state) -> void;
public:
auto add_mass_cmd() -> void;
auto add_spring_cmd(size_t a, size_t b) -> void;
auto clear_cmd() -> void;
auto add_mass_springs_cmd(size_t num_masses, const std::vector<std::pair<size_t, size_t>>& springs) -> void;
};
#endif

View File

@ -1,9 +1,9 @@
#ifndef GUI_HPP_
#define GUI_HPP_
#include "camera.hpp"
#include "orbit_camera.hpp"
#include "config.hpp"
#include "input.hpp"
#include "input_handler.hpp"
#include "state_manager.hpp"
#include <raylib.h>
@ -183,4 +183,4 @@ public:
auto draw(int fps, int ups, size_t mass_count, size_t spring_count) -> void;
};
#endif
#endif

View File

@ -24,6 +24,14 @@ struct overloads : Ts...
using Ts::operator()...;
};
enum direction
{
nor = 1 << 0,
eas = 1 << 1,
sou = 1 << 2,
wes = 1 << 3,
};
enum ctrl
{
reset = 0,
@ -91,4 +99,4 @@ auto errln(std::format_string<Args...> fmt, Args&&... args) -> void
<< std::format(fmt, std::forward<Args>(args)...) << std::endl;
}
#endif
#endif