wip: integrating threaded decoupled physics
Current Issues: - HUGE memory leak - HUGE amount of needles copying - FillGraph() does thousands of lock_guards instead of one - Can no longer rely on new states appearing immediately - have to check each access - Physics run as fast as possible, no constant sim speed` - Irregular long freezes
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <thread>
|
||||
#include <tracy/Tracy.hpp>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@ -146,10 +147,10 @@ class ThreadedPhysics {
|
||||
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
|
||||
|
||||
struct PhysicsState {
|
||||
std::mutex command_mtx;
|
||||
TracyLockable(std::mutex, command_mtx);
|
||||
std::queue<Command> pending_commands;
|
||||
|
||||
std::mutex pos_mtx;
|
||||
TracyLockable(std::mutex, pos_mtx);
|
||||
std::vector<Mass> masses; // Read by renderer
|
||||
std::unordered_map<State, int> state_masses; // Read by renderer
|
||||
std::vector<Spring> springs; // Read by renderer
|
||||
@ -160,9 +161,11 @@ class ThreadedPhysics {
|
||||
};
|
||||
|
||||
private:
|
||||
PhysicsState state;
|
||||
std::thread physics;
|
||||
|
||||
public:
|
||||
PhysicsState state;
|
||||
|
||||
public:
|
||||
ThreadedPhysics() : physics(PhysicsThread, std::ref(state)) {}
|
||||
|
||||
@ -180,6 +183,11 @@ private:
|
||||
static auto PhysicsThread(PhysicsState &state) -> void;
|
||||
|
||||
public:
|
||||
auto AddMassCmd(const State &_state) -> void;
|
||||
|
||||
auto AddSpringCmd(const State &a, const State &b) -> void;
|
||||
|
||||
auto ClearCmd() -> void;
|
||||
};
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
|
||||
Reference in New Issue
Block a user