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

Background Image Layer #2990

Open
SenexTech opened this issue Jan 22, 2024 · 13 comments
Open

Background Image Layer #2990

SenexTech opened this issue Jan 22, 2024 · 13 comments

Comments

@SenexTech
Copy link

I’m using Flutter with Flame both with the latest versions. Trying to set a background image for the game.
The issue is that background image does not fill the screen. It shows in the top left but only fills approx. half the height and width. However, if I create a Tile Layer and add a tile image in each corner they do show in the corners where they should be. I cant seem to get the background image to display properly and fill the screen but the map does.

Steps to reproduce

In Tiled, I created a Tiled Map with a width of 120 and height of 68 using 16x16 Tile size. I then added an Image layer with a image (1920x1080). The image fills the entire map as I would expect.

In flutter I load the map using levelGrid = await TiledComponent.load('level2.tmx', Vector2.all(16));

I set the camera using CameraComponent.withFixedResolution(width: 1920, height:1080 )

Set anchor using camera.viewfinder.anchor = Anchor.topLeft;

Flutter doctor output

[√] Flutter (Channel stable, 3.16.8, on Microsoft Windows [Version 10.0.19045.3930], locale en-US)
    • Flutter version 3.16.8 on channel stable at C:\src\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 67457e669f (5 days ago), 2024-01-16 16:22:29 -0800
    • Engine revision 6e2ea58a5c
    • Dart version 3.2.5
    • DevTools version 2.28.5

[√] 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\pcira\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.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.0 Preview 2.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Preview
    • Visual Studio Community 2022 version 17.9.34407.89
    • The current Visual Studio installation is a pre-release version. It may not be supported by Flutter yet.
    • Windows 10 SDK version 10.0.22621.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)

[√] VS Code (version 1.85.2)
    • VS Code at C:\Users\pcira\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.80.0

[√] Connected device (5 available)
    • Pixel 7 Pro (mobile) • 2B021FDH300DXJ • android-arm64  • Android 14 (API 34)
    • Pixel 2 XL (mobile)  • 710KPLC0118120 • android-arm64  • Android 11 (API 30)
    • Windows (desktop)    • windows        • windows-x64    • Microsoft Windows [Version 10.0.19045.3930]
    • Chrome (web)         • chrome         • web-javascript • Google Chrome 120.0.6099.218
    • Edge (web)           • edge           • web-javascript • Microsoft Edge 120.0.2210.144

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

• No issues found!

More environment information

  • flame_tiled: ^1.17.0
  • tiled: ^0.10.1
  • flame: ^1.11.0
  • Platform affected: android, Windows
  • Platform version affected: android 14, windows 10

sample1
result

@SenexTech SenexTech added the bug label Jan 22, 2024
@spydon
Copy link
Member

spydon commented Jan 22, 2024

Is the TiledComponent added to the world?

@SenexTech
Copy link
Author

SenexTech commented Jan 22, 2024 via email

@spydon
Copy link
Member

spydon commented Jan 22, 2024

It depends on what you are doing in UmfthWorld, but it looks correct. I guess level is the TiledComponent?

@SenexTech
Copy link
Author

SenexTech commented Jan 22, 2024 via email

@SenexTech
Copy link
Author

Woops. It looks like the change I made did not add the camera but it did remove it. That's why the image display changed. I'm using this code in the onLoad:
` cam = CameraComponent.withFixedResolution(width: 1920, height: 1080);
cam.viewfinder.anchor = Anchor.topCenter;

world.add(cam);`

Now the image displays in the center even though I set anchor to top left.

changed-cam

@spydon
Copy link
Member

spydon commented Jan 22, 2024

You should not add the camera to the world, the camera should be added to the game, and have a world that it is observing.

class UmfthGame extends FlameGame<UmfthWorld>
    with HasGameRef<UmfthGame>, HasCollisionDetection {
  UmfthGame({
    required this.level,
    required this.audioController,
    required this.screenWidth,
    required this.screenHeight,
    required this.ctx,
  }) : super(
          world: UmfthWorld(level: level),
          camera: CameraComponent.withFixedResolution(
            width: 1920,
            height:1080,
          )..anchor = Anchor.topLeft,
        );
}

Remove all references that you have to cam and just use the built-in camera which is called camera and is defined when you pass it in the constructor. Also, don't add the camera or the world anywhere, they are added automatically when they are passed in in the constructor.

@SenexTech
Copy link
Author

SenexTech commented Jan 22, 2024 via email

@spydon
Copy link
Member

spydon commented Jan 22, 2024

Ah right, the anchor is on the viewfinder (if you are going to move the camera later you don't want to set it to something else than center though, so it's better to do camera.viewfinder.position = Vector2(1920/2, 1080/2); in onLoad, but if you do want to change the anchor, just do camera.viewfinder.anchor = Anchor.topLeft; in onLoad instead.

@SenexTech
Copy link
Author

SenexTech commented Jan 22, 2024 via email

@SenexTech
Copy link
Author

Also, in addition to the above the object layer elements(Rectangles) placed in Tiled are not displaying in the correct position in the game. They seem to be off by around 200pixels. For example if I line up a rectangle object with a image element like a platform it shows the rectangle down and to the right in the game but perfect in Tiled

offset

@spydon
Copy link
Member

spydon commented Jan 22, 2024

I would suggest that you have a look at the flame_tiled example, since that one also uses a fixed resolution viewport and works fine.

@spydon spydon removed the bug label Jan 23, 2024
@SenexTech
Copy link
Author

Just to let you know that example did not help. It doesn't use a full screen image. It uses tiles which work fine in my example as well. The issue is when you use a full screen(map) image. Neither the Image Layer or Object layer approach works. I believe this is a bug in Flutter/Flame in that it doesn't support full screen images defined in Tiled

@ufrshubham
Copy link
Collaborator

@SenexTech can you try testing this with my branch? You'll need to add this in your pubspec

dependency_overrides:
  flame_tiled:
    git:
      url: https://github.com/flame-engine/flame.git
      ref: devkage/image-layer-paint-area-fix
      path: packages/flame_tiled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants