Skip to content

Commit

Permalink
fix(NA): black frame when activating overlays (#1093)
Browse files Browse the repository at this point in the history
* fix(NA): black flame when activating overlays

* more fixes

* Update packages/flame/lib/src/game/game_widget/game_widget.dart

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>

* Update packages/flame/lib/src/game/game_widget/game_widget.dart

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>

* Rename stuff

* call onremove on removing game from the widget tree

* update test

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
  • Loading branch information
renancaraujo and spydon committed Nov 17, 2021
1 parent 394b39f commit 85caf46
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/flame/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- `TextPaint` to use `TextStyle` (material) instead of `TextPaintConfig`
- Underlying `Shape`s in `ShapeComponent` transform with components position, size and angle
- `HitboxShape` takes parents ancestors transformations into consideration (not scaling)
- Fixed black frame when updating game widget (ex: adding overlays)
- Added possibility to extend `JoystickComponent`
- Renamed `FlameTester` to `GameTester`
- Modified `FlameTester` to be specific for `T extends FlameGame`
Expand Down
18 changes: 12 additions & 6 deletions packages/flame/lib/src/game/game_widget/game_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ class _GameWidgetState<T extends Game> extends State<GameWidget<T>> {

MouseCursor? _mouseCursor;

Future<void> get _loaderFuture {
final onLoad = widget.game.onLoadCache;
final onMount = widget.game.onMount;
return (onLoad ?? Future<void>.value()).then((_) => onMount());
}
Future<void> get loaderFuture => _loaderFuture ??= (() {
final onLoad = widget.game.onLoadCache;
final onMount = widget.game.onMount;
return (onLoad ?? Future<void>.value()).then((_) => onMount());
})();

Future<void>? _loaderFuture;

late FocusNode _focusNode;

Expand Down Expand Up @@ -194,6 +196,10 @@ class _GameWidgetState<T extends Game> extends State<GameWidget<T>> {
// Reset mouse cursor
_initMouseCursor();
addMouseCursorListener();

// Reset the loaderFuture so that onMount will run again (onLoad is still cached).
oldWidget.game.onRemove();
_loaderFuture = null;
}
}

Expand Down Expand Up @@ -311,7 +317,7 @@ class _GameWidgetState<T extends Game> extends State<GameWidget<T>> {
builder: (_, BoxConstraints constraints) {
widget.game.onGameResize(constraints.biggest.toVector2());
return FutureBuilder(
future: _loaderFuture,
future: loaderFuture,
builder: (_, snapshot) {
if (snapshot.hasError) {
final errorBuilder = widget.errorBuilder;
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/game/mixins/loadable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mixin Loadable {
/// implementing class, it is cached so that it can be reused when the parent
/// component/game/widget changes.
@internal
@nonVirtual
late Future<void>? onLoadCache = onLoad();

/// Called after the component has successfully run [onLoad] and before the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class _MyContainer extends StatefulWidget {
class _MyContainerState extends State<_MyContainer> {
double size = 300;

late final game = _MyGame(widget.events);

void causeResize() {
setState(() => size = 400);
}
Expand All @@ -132,7 +134,7 @@ class _MyContainerState extends State<_MyContainer> {
return Container(
width: size,
height: size,
child: GameWidget(game: _MyGame(widget.events)),
child: GameWidget(game: game),
);
}
}
Expand Down Expand Up @@ -194,7 +196,7 @@ void main() {
state.causeResize();

await tester.pump();
expect(events, ['onGameResize', 'onLoad', 'onMount']); // no onRemove
expect(events, ['onGameResize']); // no onRemove
final game =
tester.allWidgets.whereType<GameWidget<_MyGame>>().first.game;
expect(game.children, everyElement((Component c) => c.parent == game));
Expand Down

0 comments on commit 85caf46

Please sign in to comment.