Skip to content

Commit

Permalink
Merge pull request #79 from hecrj/feature/graceful-quit
Browse files Browse the repository at this point in the history
Add `Game::is_finished` and support graceful quits
  • Loading branch information
hecrj authored Aug 6, 2019
2 parents f6b4bf3 + 12afcf2 commit bcdc141
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ pub trait Game {
true
}

/// Returns whether the game is finished or not.
///
/// If this function returns true, the game will be closed gracefully.
///
/// By default, it always returns false.
fn is_finished(&self) -> bool {
false
}

/// Runs the [`Game`] with the given [`WindowSettings`].
///
/// You probably want to call this in your `main` function to run your game!
Expand Down Expand Up @@ -181,7 +190,7 @@ pub trait Game {
let mut timer = Timer::new(Self::TICKS_PER_SECOND);
let mut alive = true;

while alive {
while alive && !game.is_finished() {
debug.frame_started();
timer.update();

Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub trait UserInterface: Game {
let mut ui_cache =
Interface::compute(game.layout(window), &renderer).cache();

while alive {
while alive && !game.is_finished() {
debug.frame_started();
timer.update();

Expand Down

0 comments on commit bcdc141

Please sign in to comment.