Skip to content

Commit

Permalink
Pressing 'o' whilst sim is paused, allows the simulation to be stepped.
Browse files Browse the repository at this point in the history
This does nothing if the simulation isn't ready, e.g. if it hasn't completed a new step since the sim last paused.

Closes #124
  • Loading branch information
Robadob committed Mar 26, 2023
1 parent 4130904 commit a27a579
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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
10 changes: 10 additions & 0 deletions src/flamegpu/visualiser/Visualiser.cpp
Original file line number Diff line number Diff line change
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

0 comments on commit a27a579

Please sign in to comment.