-
-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TimeTrackComponent and ChildCounterComponent (#2846)
Adds TimeTrackComponent and a ChildCounterComponent to help developers track down perfomance issues in their game.
- Loading branch information
1 parent
79262c7
commit 6269551
Showing
6 changed files
with
156 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export 'src/components/debug/child_counter_component.dart'; | ||
export 'src/components/debug/time_track_component.dart'; | ||
export 'src/components/fps_component.dart'; | ||
export 'src/components/fps_text_component.dart'; |
42 changes: 42 additions & 0 deletions
42
packages/flame/lib/src/components/debug/child_counter_component.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,42 @@ | ||
import 'dart:async'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/text.dart'; | ||
|
||
/// A debug component that shows the number of children of a given type from | ||
/// a target component. | ||
/// | ||
/// Add it to the game to start seeing the count. | ||
class ChildCounterComponent<T extends Component> extends TextComponent { | ||
ChildCounterComponent({ | ||
required this.target, | ||
super.position, | ||
}); | ||
|
||
final Component target; | ||
late String _label; | ||
|
||
@override | ||
FutureOr<void> onLoad() async { | ||
await super.onLoad(); | ||
_label = T.toString(); | ||
|
||
textRenderer = TextPaint( | ||
style: const TextStyle( | ||
color: Color(0xFFFFFFFF), | ||
fontSize: 12, | ||
), | ||
); | ||
|
||
add( | ||
TimerComponent( | ||
period: 1, | ||
repeat: true, | ||
onTick: () { | ||
text = '$_label: ${target.children.query<T>().length}'; | ||
}, | ||
), | ||
); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/flame/lib/src/components/debug/time_track_component.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,65 @@ | ||
import 'dart:async'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/text.dart'; | ||
|
||
/// A component that offers a simple way to track time spent in different parts | ||
/// of your game. | ||
/// | ||
/// This component is meant to be used for debugging purposes only and should | ||
/// not be added in production builds. | ||
/// | ||
/// To start tracking time, call [TimeTrackComponent.start] with an identifier | ||
/// and [TimeTrackComponent.end] with that same identifier to finish tracking. | ||
/// | ||
/// To see the recorded times, simply add this component to your game. | ||
class TimeTrackComponent extends TextComponent { | ||
TimeTrackComponent({super.position}); | ||
|
||
static final Map<String, int> _startTimes = {}; | ||
static final Map<String, int> _endTimes = {}; | ||
|
||
static void clear() { | ||
_startTimes.clear(); | ||
_endTimes.clear(); | ||
} | ||
|
||
static void start(String name) { | ||
_startTimes[name] = DateTime.now().microsecondsSinceEpoch; | ||
} | ||
|
||
static void end(String name) { | ||
_endTimes[name] = DateTime.now().microsecondsSinceEpoch; | ||
} | ||
|
||
@override | ||
FutureOr<void> onLoad() async { | ||
await super.onLoad(); | ||
|
||
textRenderer = TextPaint( | ||
style: const TextStyle( | ||
color: Color(0xFFFFFFFF), | ||
fontSize: 12, | ||
), | ||
); | ||
|
||
add( | ||
TimerComponent( | ||
period: 1, | ||
repeat: true, | ||
onTick: () { | ||
final sb = StringBuffer(); | ||
for (final entry in _startTimes.entries) { | ||
final name = entry.key; | ||
final startTime = entry.value; | ||
final endTime = _endTimes[name]; | ||
final duration = endTime == null ? 0 : endTime - startTime; | ||
sb.writeln('$name: $duration'); | ||
} | ||
text = sb.toString(); | ||
}, | ||
), | ||
); | ||
} | ||
} |