Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step sim when paused #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/flamegpu/visualiser/FLAMEGPU_Visualisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ bool FLAMEGPU_Visualisation::isReady() const {


void FLAMEGPU_Visualisation::lockMutex() {
auto lock_t = new std::lock_guard<std::mutex>(vis->getRenderBufferMutexPre());
lock = new LockHolder(vis->getRenderBufferMutex());
delete lock_t;
}
void FLAMEGPU_Visualisation::releaseMutex() {
if (lock) {
Expand Down
18 changes: 14 additions & 4 deletions src/flamegpu/visualiser/Visualiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,16 @@ void Visualiser::render() {
}
const float distance = speed * static_cast<float>(frameTime);
if (!modelConfig.isOrtho) {
if (state[SDL_SCANCODE_W]) {
if (state[SDL_SCANCODE_W] || state[SDL_SCANCODE_UP]) {
this->camera->move(distance);
}
if (state[SDL_SCANCODE_A]) {
if (state[SDL_SCANCODE_A] || state[SDL_SCANCODE_LEFT]) {
this->camera->strafe(-distance);
}
if (state[SDL_SCANCODE_S]) {
if (state[SDL_SCANCODE_S] || state[SDL_SCANCODE_DOWN]) {
this->camera->move(-distance);
}
if (state[SDL_SCANCODE_D]) {
if (state[SDL_SCANCODE_D] || state[SDL_SCANCODE_RIGHT]) {
this->camera->strafe(distance);
}
if (state[SDL_SCANCODE_Q]) {
Expand Down Expand Up @@ -902,6 +902,16 @@ void Visualiser::handleKeypress(SDL_Keycode keycode, int /*x*/, int /*y*/) {
stepsPerSecond = 0.0;
}
break;
case SDLK_o:
// If paused step the simulation, else do nothing
if (this->pause_guard) {
delete pause_guard;
pause_guard = nullptr;
const auto pause_guard_t = new std::lock_guard<std::mutex>(render_buffer_mutex_pre);
pause_guard = new std::lock_guard<std::mutex>(render_buffer_mutex);
delete pause_guard_t;
}
break;
case SDLK_l:
renderLines = !renderLines;
break;
Expand Down
14 changes: 13 additions & 1 deletion src/flamegpu/visualiser/Visualiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ class Visualiser : public ViewportExt {
* This must be locked before calling updateAgentStateBuffer()
* @see updateAgentStateBuffer(const std::string &, const std::string &, const unsigned int, float *, float *, float *, float *)
*/
std::mutex &getRenderBufferMutex() { return render_buffer_mutex; }
std::mutex& getRenderBufferMutex() { return render_buffer_mutex; }
/**
* Returns the mutex for simulation stepping (for the simulation)
* This must be locked before locking render_buffer_mutex, then released straight after the lock is achieved
* @see updateAgentStateBuffer(const std::string &, const std::string &, const unsigned int, float *, float *, float *, float *)
*/
std::mutex& getRenderBufferMutexPre() { return render_buffer_mutex_pre; }
/**
* Sets the value to be rendered to the HUD step counter (if enabled)
* @param stepCount The step value to be displayed
Expand Down Expand Up @@ -381,6 +387,12 @@ class Visualiser : public ViewportExt {
* Mutex is required to access render buffers for thread safety
*/
std::mutex render_buffer_mutex;
/**
* Double mutex to enable simulation stepping
* Visualiser must lock this prior to locking render_buffer_mutex, then release this after the render_buffer_mutex lock is achieved
* This allows sim stepping to block a re-lock of render_buffer_mutex
*/
std::mutex render_buffer_mutex_pre;
/**
* When this is not set to nullptr, it blocks the simulation from continuing
*/
Expand Down