flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.23k stars 902 forks source link

Flame isn't Rendering Sprites #2771

Closed hilmiyafia closed 1 year ago

hilmiyafia commented 1 year ago

Current bug behavior

Flame is not rendering the sprites sometimes. It appears for a moment after pressing "back" (closing the app), or after going "home" and go back to the app. But after closing, and running the app again, it doesn't render again too. See this video: https://youtu.be/gIdxXewLPNU

Expected behavior

Flame is always rendering the sprites.

Steps to reproduce

I build this as an APK and install it on my phone:

import 'package:flutter/material.dart';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';

void main() {
  Flame.device.setLandscape();
  runApp(const MaterialApp(home: SafeArea(child: Scaffold(body:
    GameWidget<Engine>.controlled(gameFactory: Engine.new),
  ))));
}

class Engine extends FlameGame {
  late Scene scene;
  late CameraComponent cameraComponent;

  @override
  Future<void> onLoad() async {
    await images.load("cube.png");
    scene = Scene();
    cameraComponent = CameraComponent(world: scene);
    addAll([scene, cameraComponent]);
  }
}

class Scene extends World with HasGameRef<Engine> {
  late List<SpriteComponent> sprites;

  @override
  Future<void> onLoad() async {
    sprites = List.empty(growable: true);
    for (int x = -1; x < 2; x++) {
      for (int y = -1; y < 2; y++) {
        sprites.add(SpriteComponent(
          anchor: Anchor.center,
          position: Vector2(x * 96, y * 96),
          size: Vector2(64, 64),
          sprite: Sprite(game.images.fromCache("cube.png")),
        ));
      }
    }
    addAll(sprites);
  }
}

Flutter doctor output

[√] Flutter (Channel stable, 3.13.5, on Microsoft Windows [Version 10.0.22621.2283], locale en-US)
    • Flutter version 3.13.5 on channel stable at C:\Users\MSI\Documents\Flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 12fccda598 (5 days ago), 2023-09-19 13:56:11 -0700
    • Engine revision bd986c5ed2
    • Dart version 3.1.2
    • DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\MSI\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.6.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.6.33829.357
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2022.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[√] Connected device (3 available)
    • CPH1937 (mobile)  • 7410004d • android-arm64  • Android 11 (API 30)
    • Windows (desktop) • windows  • windows-x64    • Microsoft Windows [Version 10.0.22621.2283]
    • Edge (web)        • edge     • web-javascript • Microsoft Edge 117.0.2045.36

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

More environment information

Log information

No error on the log

More information

spydon commented 1 year ago

That's very strange, can you try if the same thing is happening on 1.8.2? Maybe this has something to do with the auto pause feature that was introduced in 1.9.0

hilmiyafia commented 1 year ago

@spydon Yes I think it's okay in 1.8.2 👍

spydon commented 1 year ago

If you set pauseWhenBackgrounded = false, does it still happen?

hilmiyafia commented 1 year ago

@spydon No, I don't think so. I think that's fixed it...

spydon commented 1 year ago

@adil192 could you have a look at that? :)

adil192 commented 1 year ago

@hilmiyafia Are you calling pauseEngine and resumeEngine in your code?

adil192 commented 1 year ago

@spydon Are we able to add extra logging to flame for this?

hilmiyafia commented 1 year ago

@adil192 No I'm not. You can find my code above 👍

spydon commented 1 year ago

@spydon Are we able to add extra logging to flame for this?

We don't release versions with logging if that is what you meant? But it shouldn't be hard to reproduce this if you have a physical android device and then just add some local logging where needed.

adil192 commented 1 year ago

I can't reproduce this issue on my phone. See the repo here: https://github.com/adil192/flame_2771

adilh@DESKTOP-M5JKVNO MINGW64 ~/Documents/GitHub/flame_2771
$ flutter doctor -v
[✓] Flutter (Channel stable, 3.13.4, on Microsoft Windows [Version 10.0.22621.2283], locale en-GB)
    • Flutter version 3.13.4 on channel stable at C:\dev\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 367f9ea16b (13 days ago), 2023-09-12 23:27:53 -0500
    • Engine revision 9064459a8b
    • Dart version 3.1.2
    • DevTools version 2.25.0

[✓] Windows Version (Installed version of Windows is version 10 or higher)

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\adilh\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Users\adilh\AppData\Local\Programs\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe

[✓] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.1)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.7.34009.444
    • Windows 10 SDK version 10.0.22621.0

[✓] Android Studio (version 2022.3)
    • Android Studio at C:\Users\adilh\AppData\Local\Programs\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[✓] VS Code (version 1.82.2)
    • VS Code at C:\Users\adilh\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.72.0

[✓] Connected device (4 available)
    • Pixel 6 Pro (mobile) • 21041FDEE0029K • android-arm64  • Android 14 (API 34)
    • Windows (desktop)    • windows        • windows-x64    • Microsoft Windows [Version 10.0.22621.2283]
    • Chrome (web)         • chrome         • web-javascript • unknown
    • Edge (web)           • edge           • web-javascript • Microsoft Edge 117.0.2045.36

[✓] Network resources
    • All expected network resources are available.

• No issues found!

adilh@DESKTOP-M5JKVNO MINGW64 ~/Documents/GitHub/flame_2771 (main)
$
# pubspec.lock

  flame:
    dependency: "direct main"
    description:
      name: flame
      sha256: f1ed45b49db36f6650882900f3216516d5c2628400bad8b4e92d83c116d79b12
      url: "https://pub.dev"
    source: hosted
    version: "1.9.1"
adil192 commented 1 year ago

Maybe this is a bug affecting older versions of Android? I'm curious as to whether this bug happens if you replace GameWidget.controlled with the regular constructor:

void main() {
  final engine = Engine();
  runApp(MaterialApp(home: SafeArea(child: Scaffold(
    body: GameWidget(game: engine),
  ))));
}
hilmiyafia commented 1 year ago

@adil192 I don't think so, I can reproduce the error on the latest android api 34 in an emulator. Did you run the app through flutter build apk?

The problem still happens using regular constructor.

adil192 commented 1 year ago

Thanks, I can reproduce it now.

@spydon When the app is launched, we (sometimes) get AppLifecycleState.detached from https://github.com/flame-engine/flame/blob/bde34efef7264c91f49b237b589c74ba80a1554e/packages/flame/lib/src/game/game_widget/game_widget.dart#L257-L259

I propose to change this to not use WidgetsBinding.instance.lifecycleState but instead just hardcode AppLifecycleState.resumed for when the widget is created. What do you think?

void initCurrentGame() {
  // ...
  currentGame.lifecycleStateChange(AppLifecycleState.resumed);
}

Another option could be to wait a little before initing but I'm not sure how that would work

spydon commented 1 year ago

Thanks, I can reproduce it now.

@spydon When the app is launched, we (sometimes) get AppLifecycleState.detached from https://github.com/flame-engine/flame/blob/bde34efef7264c91f49b237b589c74ba80a1554e/packages/flame/lib/src/game/game_widget/game_widget.dart#L257-L259

I propose to change this to not use WidgetsBinding.instance.lifecycleState but instead just hardcode AppLifecycleState.resumed for when the widget is created. What do you think?

void initCurrentGame() {
  // ...
  currentGame.lifecycleStateChange(AppLifecycleState.resumed);
}

Another option could be to wait a little before initing but I'm not sure how that would work

Yeah, I think we'll have to go with hardcoding it then. Can you put up a PR?