Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: remove onTimingsCallback for Flutter 3.0 #1626

Merged
merged 1 commit into from May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/flame/lib/src/game/game_render_box.dart
Expand Up @@ -10,10 +10,7 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver {
Game game;
GameLoop? gameLoop;

GameRenderBox(this.buildContext, this.game) {
//ignore: deprecated_member_use_from_same_package
WidgetsBinding.instance!.addTimingsCallback(game.onTimingsCallback);
}
GameRenderBox(this.buildContext, this.game);

@override
bool get isRepaintBoundary => true;
Expand Down
31 changes: 20 additions & 11 deletions packages/flame/lib/src/game/mixins/fps_counter.dart
@@ -1,4 +1,4 @@
import 'package:flutter/scheduler.dart';
import 'dart:collection';

import '../../../game.dart';

Expand All @@ -11,19 +11,28 @@ const frameInterval =
'FPSCounter will be removed in v1.3.0',
)
mixin FPSCounter on Game {
List<FrameTiming> _previousTimings = [];
/// The sliding window size, i.e. the number of game ticks over which the fps
/// measure will be averaged.
final int windowSize = 60;

/// The queue of the recent game tick durations.
/// The length of this queue will not exceed [windowSize].
final Queue<double> window = Queue();

/// The sum of all values in the [window] queue.
double _sum = 0;

@override
void onTimingsCallback(List<FrameTiming> timings) =>
_previousTimings = timings;
void update(double dt) {
window.addLast(dt);
_sum += dt;
if (window.length > windowSize) {
_sum -= window.removeFirst();
}
}

/// Returns the FPS based on the frame times from [onTimingsCallback].
/// Get the current average FPS over the last [windowSize] frames.
double fps([int average = 1]) {
return _previousTimings.length *
_maxFrames /
_previousTimings.map((t) {
return (t.totalSpan.inMicroseconds ~/ frameInterval.inMicroseconds) +
1;
}).fold(0, (a, b) => a + b);
return window.isEmpty ? 0 : window.length / _sum;
}
}
6 changes: 0 additions & 6 deletions packages/flame/lib/src/game/mixins/game.dart
@@ -1,5 +1,3 @@
import 'dart:ui';

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -96,10 +94,6 @@ mixin Game {
/// Check [AppLifecycleState] for details about the events received.
void lifecycleStateChange(AppLifecycleState state) {}

/// Use for calculating the FPS.
@Deprecated('Use FPSComponent instead, will be removed in v1.3.0')
void onTimingsCallback(List<FrameTiming> timings) {}

/// Method to perform late initialization of the [Game] class.
///
/// Usually, this method is the main place where you initialize your [Game]
Expand Down