Skip to content

Commit

Permalink
GameEngine.h:
Browse files Browse the repository at this point in the history
* GameEngine now features four new callbacks:
  - on_halt_game_loop()
  - on_resume_game_loop()
  - on_enter_paused()
  - on_exit_paused().
* Technically, we're not really halting the game loop per se, but the sim_time is halted and frame counters are halted so basically we can consider it as such. Be warned though.
  • Loading branch information
razterizer committed Nov 23, 2024
1 parent 4b8f993 commit 69c40e2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions GameEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class GameEngine
int term_win_rows = 0;
int term_win_cols = 0;

OneShot trg_update_halted, trg_update_resumed;

bool handle_hiscores(const HiScoreItem& curr_hsi)
{
const int c_max_num_hiscores = 20;
Expand Down Expand Up @@ -231,12 +233,16 @@ class GameEngine
virtual void on_enter_game_loop() {}
virtual void on_exit_game_loop() {}
virtual void on_enter_game_over() {}
virtual void on_halt_game_loop() {}
virtual void on_resume_game_loop() {}
virtual void on_exit_game_over() {}
virtual void on_enter_you_won() {}
virtual void on_exit_you_won() {}
virtual void on_enter_input_hiscore() {}
virtual void on_exit_input_hiscore() {}
virtual void on_enter_hiscores() {}
virtual void on_enter_paused() {}
virtual void on_exit_paused() {}

public:
GameEngine(std::string_view exe_full_path,
Expand Down Expand Up @@ -345,7 +351,13 @@ class GameEngine
quit_confirm_button = YesNoButtons::No;
}
else if (m_params.enable_pause && pause)
{
math::toggle(paused);
if (paused)
on_enter_paused();
else
on_exit_paused();
}

if (!m_params.enable_quit_confirm_screen && quit)
{
Expand Down Expand Up @@ -502,13 +514,27 @@ class GameEngine

if (!show_title && !show_instructions && !show_quit_confirm && !show_input_hiscore && !show_hiscores && !paused)
{
if (trg_update_resumed.once())
{
on_resume_game_loop();
trg_update_halted.reset();
}

frame_ctr++;
for (auto& ad : anim_ctr_data)
if (frame_ctr % ad.anim_count_per_frame_count == 0)
ad.anim_ctr++;

sim_time_s += sim_dt_s;
}
else
{
if (trg_update_halted.once())
{
on_halt_game_loop();
trg_update_resumed.reset();
}
}

return true;
}
Expand Down

0 comments on commit 69c40e2

Please sign in to comment.