wip: draw physics thread ups
This commit is contained in:
@ -44,6 +44,7 @@ auto main(int argc, char *argv[]) -> int {
|
||||
OrbitCamera3D camera;
|
||||
Renderer renderer(camera, state, input);
|
||||
|
||||
unsigned int ups;
|
||||
std::vector<Vector3> masses; // Read from physics
|
||||
std::vector<std::pair<std::size_t, std::size_t>> springs; // Read from physics
|
||||
|
||||
@ -61,6 +62,8 @@ auto main(int argc, char *argv[]) -> int {
|
||||
{
|
||||
std::unique_lock<LockableBase(std::mutex)> lock(physics.state.data_mtx);
|
||||
|
||||
ups = physics.state.ups;
|
||||
|
||||
// Only copy data if any has been produced
|
||||
if (physics.state.data_ready) {
|
||||
masses = physics.state.masses;
|
||||
@ -88,7 +91,7 @@ auto main(int argc, char *argv[]) -> int {
|
||||
renderer.DrawMassSprings(masses, springs);
|
||||
renderer.DrawKlotski();
|
||||
renderer.DrawMenu(masses, springs);
|
||||
renderer.DrawTextures();
|
||||
renderer.DrawTextures(ups);
|
||||
FrameMarkEnd("MainThread");
|
||||
}
|
||||
|
||||
|
||||
@ -5,8 +5,10 @@
|
||||
#include <BS_thread_pool.hpp>
|
||||
#include <algorithm>
|
||||
#include <cfloat>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
#include <ratio>
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <tracy/Tracy.hpp>
|
||||
@ -190,8 +192,21 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
||||
[&](const struct ClearGraph &cg) { mass_springs.Clear(); },
|
||||
};
|
||||
|
||||
std::chrono::time_point last = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> accumulator(0);
|
||||
std::chrono::duration<double> update_accumulator(0);
|
||||
unsigned int updates = 0;
|
||||
|
||||
while (state.running.load()) {
|
||||
FrameMarkStart("PhysicsThread");
|
||||
|
||||
// Time tracking
|
||||
std::chrono::time_point now = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> deltatime = now - last;
|
||||
accumulator += deltatime;
|
||||
update_accumulator += deltatime;
|
||||
last = now;
|
||||
|
||||
// Handle queued commands
|
||||
{
|
||||
std::lock_guard<LockableBase(std::mutex)> lock(state.command_mtx);
|
||||
@ -208,11 +223,15 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
||||
}
|
||||
|
||||
// Physics update
|
||||
// TODO: Respect thread-local deltatime + calculate UPS
|
||||
mass_springs.ClearForces();
|
||||
mass_springs.CalculateSpringForces();
|
||||
mass_springs.CalculateRepulsionForces();
|
||||
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
|
||||
if (accumulator.count() > TIMESTEP) {
|
||||
mass_springs.ClearForces();
|
||||
mass_springs.CalculateSpringForces();
|
||||
mass_springs.CalculateRepulsionForces();
|
||||
mass_springs.VerletUpdate(TIMESTEP * SIM_SPEED);
|
||||
|
||||
++updates;
|
||||
accumulator -= std::chrono::duration<double>(TIMESTEP);
|
||||
}
|
||||
|
||||
// Publish the positions for the renderer (copy)
|
||||
FrameMarkStart("PhysicsThreadProduceLock");
|
||||
@ -225,6 +244,13 @@ auto ThreadedPhysics::PhysicsThread(ThreadedPhysics::PhysicsState &state)
|
||||
break;
|
||||
}
|
||||
|
||||
if (update_accumulator.count() > 1.0) {
|
||||
// Update each second
|
||||
state.ups = updates;
|
||||
updates = 0;
|
||||
update_accumulator = std::chrono::duration<double>(0);
|
||||
}
|
||||
|
||||
state.masses.clear();
|
||||
state.masses.reserve(mass_springs.masses.size());
|
||||
for (const auto &mass : mass_springs.masses) {
|
||||
|
||||
@ -316,7 +316,7 @@ auto Renderer::DrawMenu(
|
||||
EndTextureMode();
|
||||
}
|
||||
|
||||
auto Renderer::DrawTextures() -> void {
|
||||
auto Renderer::DrawTextures(float ups) -> void {
|
||||
BeginDrawing();
|
||||
DrawTextureRec(menu_target.texture,
|
||||
Rectangle(0, 0, menu_target.texture.width,
|
||||
@ -330,6 +330,9 @@ auto Renderer::DrawTextures() -> void {
|
||||
Rectangle(0, 0, render_target.texture.width,
|
||||
-1 * render_target.texture.height),
|
||||
Vector2(GetScreenWidth() / 2.0, MENU_HEIGHT), WHITE);
|
||||
|
||||
DrawFPS(GetScreenWidth() / 2 + 10, MENU_HEIGHT + 10);
|
||||
DrawText(TextFormat("%.0f UPS", ups), GetScreenWidth() / 2 + 120,
|
||||
MENU_HEIGHT + 10, 20, ORANGE);
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user