Skip to content

Commit

Permalink
fix: Pause forge2d when backgrounded
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Sep 12, 2023
1 parent 4e66424 commit eb4fc09
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/.cspell/gamedev_dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ arial
arities
arity
autofocus
backgrounded
backpressure
backquote
backtick
Expand Down Expand Up @@ -51,6 +52,7 @@ drawables
easings
flippable
focusable
foregrounded
fullscreen
gamepad
gamepads
Expand Down
10 changes: 8 additions & 2 deletions packages/flame/lib/src/game/game_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,17 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver {
}

void _bindLifecycleListener() {
_ambiguate(WidgetsBinding.instance)!.addObserver(this);
final binding = _ambiguate(WidgetsBinding.instance)!;
binding.addObserver(this);
didChangeAppLifecycleState(
binding.lifecycleState ?? AppLifecycleState.resumed,
);
}

void _unbindLifecycleListener() {
_ambiguate(WidgetsBinding.instance)!.removeObserver(this);
final binding = _ambiguate(WidgetsBinding.instance)!;
binding.removeObserver(this);
didChangeAppLifecycleState(AppLifecycleState.paused);
}

@override
Expand Down
41 changes: 41 additions & 0 deletions packages/flame_forge2d/lib/forge2d_game.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flame/game.dart';
import 'package:flame_forge2d/world_contact_listener.dart';
import 'package:flutter/material.dart';
import 'package:forge2d/forge2d.dart';

class Forge2DGame extends FlameGame {
Expand All @@ -8,6 +9,7 @@ class Forge2DGame extends FlameGame {
double zoom = defaultZoom,
Camera? camera,
ContactListener? contactListener,
this.pauseWhenBackgrounded = false,
}) : world = World(gravity ?? defaultGravity),
super(camera: camera ?? Camera()) {
// ignore: deprecated_member_use
Expand All @@ -21,6 +23,14 @@ class Forge2DGame extends FlameGame {

final World world;

/// Whether the game should pause when the app is backgrounded.
///
/// If true, the first update after the app is foregrounded will be skipped.
///
/// Defaults to false.
bool pauseWhenBackgrounded;
bool _pausedBecauseBackgrounded = false;

@override
void update(double dt) {
super.update(dt);
Expand All @@ -38,4 +48,35 @@ class Forge2DGame extends FlameGame {
Vector2 screenToFlameWorld(Vector2 position) {
return screenToWorld(position)..y *= -1;
}

@override
@mustCallSuper
void lifecycleStateChange(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
case AppLifecycleState.inactive:
if (_pausedBecauseBackgrounded) {
resumeEngine();
}
case AppLifecycleState.paused:
case AppLifecycleState.detached:
case AppLifecycleState.hidden:
if (pauseWhenBackgrounded) {
pauseEngine();
_pausedBecauseBackgrounded = true;
}
}
}

@override
void pauseEngine() {
_pausedBecauseBackgrounded = false;
super.pauseEngine();
}

@override
void resumeEngine() {
_pausedBecauseBackgrounded = false;
super.resumeEngine();
}
}
29 changes: 29 additions & 0 deletions packages/flame_forge2d/test/forge2d_game_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui' show AppLifecycleState;

import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -50,4 +52,31 @@ void main() {
});
},
);

group('pauseWhenBackgrounded:', () {
test('true', () async {
final game = Forge2DGame(
pauseWhenBackgrounded: true,
);

game.lifecycleStateChange(AppLifecycleState.paused);
expect(game.paused, true);

game.lifecycleStateChange(AppLifecycleState.resumed);
expect(game.paused, false);
});

test('false', () async {
final game = Forge2DGame(
// ignore: avoid_redundant_argument_values
pauseWhenBackgrounded: false,
);

game.lifecycleStateChange(AppLifecycleState.paused);
expect(game.paused, false);

game.lifecycleStateChange(AppLifecycleState.resumed);
expect(game.paused, false);
});
});
}

0 comments on commit eb4fc09

Please sign in to comment.