Skip to content

Commit

Permalink
fix: Deprecate pause and resume in GameLoop (#1240)
Browse files Browse the repository at this point in the history
GameLoop methods pause() and resume() are marked as deprecated, with a recommendation to use start() and stop() instead.
  • Loading branch information
st-pasha authored Jan 16, 2022
1 parent 4d5adcb commit dc37053
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
38 changes: 16 additions & 22 deletions packages/flame/lib/src/game/game_loop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,41 @@ import 'package:flutter/scheduler.dart';

class GameLoop {
void Function(double dt) callback;
Duration previous = Duration.zero;
late Ticker _ticker;
Duration _previous = Duration.zero;
late final Ticker _ticker;

GameLoop(this.callback) {
_ticker = Ticker(_tick);
}

void _tick(Duration timestamp) {
final dt = _computeDeltaT(timestamp);
final durationDelta = timestamp - _previous;
final dt = durationDelta.inMicroseconds / Duration.microsecondsPerSecond;
_previous = timestamp;
callback(dt);
}

double _computeDeltaT(Duration now) {
final delta = previous == Duration.zero ? Duration.zero : now - previous;
previous = now;
return delta.inMicroseconds / Duration.microsecondsPerSecond;
}

void start() {
_ticker.start();
if (!_ticker.isActive) {
_ticker.start();
}
}

void stop() {
_ticker.stop();
_previous = Duration.zero;
}

void dispose() {
_ticker.dispose();
}

void pause() {
_ticker.muted = true;
previous = Duration.zero;
}
@Deprecated('Internal variable')
Duration get previous => _previous;

void resume() {
_ticker.muted = false;
// If the game has started paused, we need to start the ticker
// as it would not have been started yet
if (!_ticker.isActive) {
start();
}
}
@Deprecated('Use stop() instead')
void pause() => stop();

@Deprecated('Use start() instead')
void resume() => start();
}
5 changes: 2 additions & 3 deletions packages/flame/lib/src/game/game_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/widgets.dart' hide WidgetBuilder;
import 'game_loop.dart';
import 'mixins/game.dart';

// ignore: prefer_mixin
class GameRenderBox extends RenderBox with WidgetsBindingObserver {
BuildContext buildContext;
Game game;
Expand All @@ -30,8 +29,8 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver {

final gameLoop = this.gameLoop = GameLoop(gameLoopCallback);

game.pauseEngineFn = gameLoop.pause;
game.resumeEngineFn = gameLoop.resume;
game.pauseEngineFn = gameLoop.stop;
game.resumeEngineFn = gameLoop.start;

if (!game.paused) {
gameLoop.start();
Expand Down

0 comments on commit dc37053

Please sign in to comment.