-
-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a `HasPerformanceTracker` mixin on `Game`. This mixin will allow for tracking the update and render time of the game.
- Loading branch information
1 parent
d409193
commit 6270353
Showing
7 changed files
with
130 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/flame/lib/src/game/mixins/has_performance_tracker.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/game.dart'; | ||
|
||
/// A mixin that adds performance tracking to a game. | ||
mixin HasPerformanceTracker on Game { | ||
int _updateTime = 0; | ||
int _renderTime = 0; | ||
final _stopwatch = Stopwatch(); | ||
|
||
/// The time it took to update the game in milliseconds. | ||
int get updateTime => _updateTime; | ||
|
||
/// The time it took to render the game in milliseconds. | ||
int get renderTime => _renderTime; | ||
|
||
@override | ||
void update(double dt) { | ||
_stopwatch.reset(); | ||
_stopwatch.start(); | ||
super.update(dt); | ||
_stopwatch.stop(); | ||
_updateTime = _stopwatch.elapsedMilliseconds; | ||
} | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
_stopwatch.reset(); | ||
_stopwatch.start(); | ||
super.render(canvas); | ||
_stopwatch.stop(); | ||
_renderTime = _stopwatch.elapsedMilliseconds; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/flame/test/game/mixins/has_performance_tracker_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:io'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets( | ||
'tracks update and render times.', | ||
(widgetTester) async { | ||
final game = _GameWithPerformanceTracker( | ||
children: [_SlowComponent()], | ||
); | ||
|
||
expect(game.updateTime, 0); | ||
expect(game.renderTime, 0); | ||
|
||
await widgetTester.pumpFrames( | ||
GameWidget(game: game), | ||
const Duration(seconds: 1), | ||
); | ||
|
||
expect( | ||
game.updateTime, | ||
greaterThanOrEqualTo(_SlowComponent.duration.inMilliseconds), | ||
); | ||
expect( | ||
game.renderTime, | ||
greaterThanOrEqualTo(_SlowComponent.duration.inMilliseconds), | ||
); | ||
}, | ||
); | ||
} | ||
|
||
class _GameWithPerformanceTracker extends FlameGame with HasPerformanceTracker { | ||
_GameWithPerformanceTracker({super.children}); | ||
} | ||
|
||
class _SlowComponent extends Component { | ||
static const duration = Duration(milliseconds: 8); | ||
@override | ||
void update(double dt) => sleep(duration); | ||
@override | ||
void render(Canvas canvas) => sleep(duration); | ||
} |