Skip to content

Commit

Permalink
Engine: reintroduced a suspend mode in WaitForNextFrame()
Browse files Browse the repository at this point in the history
Previously the engine was using allegro timers that were suspended by default when the player switched away from the game.
The commits f2ab428 and 15489ff  broke that behavior. Here we reintroduce it, now implementing the timer lock mechanic directly in the engine.
  • Loading branch information
ivan-mogilko committed May 24, 2021
1 parent 14896fc commit 81aa550
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Engine/ac/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ RoomStruct thisroom;

volatile int switching_away_from_game = 0;
volatile bool switched_away = false;
volatile bool game_update_suspend = false;
volatile char want_exit = 0, abort_engine = 0;
GameDataVersion loaded_game_file_version = kGameVersion_Undefined;
int frames_per_second=40;
Expand Down Expand Up @@ -1399,9 +1400,9 @@ void display_switch_out()
// Called when game looses input focus and must pause until focus is returned
void display_switch_out_suspend()
{
display_switch_out();

switching_away_from_game++;
game_update_suspend = true;
display_switch_out();

platform->PauseApplication();

Expand All @@ -1427,11 +1428,11 @@ void display_switch_out_suspend()
// Called whenever game gets input focus
void display_switch_in()
{
switched_away = false;
ags_clear_input_buffer();
// If auto lock option is set, lock mouse to the game window
if (usetup.mouse_auto_lock && scsystem.windowed)
Mouse::TryLockToWindow();
switched_away = false;
}

// Called when game gets input focus and must resume after pause
Expand All @@ -1456,6 +1457,7 @@ void display_switch_in_resume()
// TODO: find out if anything has to be done here for SDL backend

platform->ResumeApplication();
game_update_suspend = false;
}

void replace_tokens(const char*srcmes,char*destm, int maxlen) {
Expand Down
16 changes: 14 additions & 2 deletions Engine/ac/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
// http://www.opensource.org/licenses/artistic-license-2.0.php
//
//=============================================================================

#include "ac/timer.h"

#include "core/platform.h"
#include <thread>
#include "ac/sys_events.h"
#include "platform/base/agsplatformdriver.h"

extern volatile bool game_update_suspend;

namespace {

const auto MAXIMUM_FALL_BEHIND = 3;
Expand Down Expand Up @@ -60,6 +61,11 @@ void WaitForNextFrame()
// early exit if we're trying to maximise framerate
if (frameDuration <= std::chrono::milliseconds::zero()) {
next_frame_timestamp = now;
// suspend while the game is being switched out
while (game_update_suspend > 0) {
sys_evt_process_pending();
platform->YieldCPU();
}
return;
}

Expand All @@ -74,6 +80,12 @@ void WaitForNextFrame()
}

next_frame_timestamp += frameDuration;

// suspend while the game is being switched out
while (game_update_suspend > 0) {
sys_evt_process_pending();
platform->YieldCPU();
}
}

void skipMissedTicks()
Expand Down

0 comments on commit 81aa550

Please sign in to comment.