AlmasB / FXGL

Java / JavaFX / Kotlin Game Library (Engine)
http://almasb.github.io/FXGL/
MIT License
4.34k stars 548 forks source link

AnimatedTexture animation is accelerated when the application is started for the first time #1380

Open starxg opened 1 month ago

starxg commented 1 month ago

Describe the bug AnimationChannel and AnimatedTexture speed inconsistencies

Expected behavior When I initially start it up, there seems to be a speeding up of the frame rate. This problem goes away when the programme runs for 2-3 seconds.

Screenshots

https://github.com/user-attachments/assets/986b98ca-2b66-40db-8143-73c95c8c2f49

Runtime info(please complete the following information): FXGL-21.1 (27.03.2024 07.52) on MAC M3 (J:21.0.3 FX:21.0.1)

Additional context / possible fixes

@Override
protected void initSettings(GameSettings settings) {

}

@Override
protected void initGame() {
    final Sunflower3 sunflower = new Sunflower3();
    sunflower.setX(200);
    sunflower.setY(200);
    FXGL.getGameWorld().addEntity(sunflower);
}

public static void main(String[] args) {
    launch(args);
}
public class Sunflower3 extends Entity {

    public Sunflower3() {
        addComponent(new SunflowerComponent());
    }

    private static class SunflowerComponent extends Component {
        private final AnimatedTexture texture;

        private SunflowerComponent() {

            List<Image> images = new ArrayList<>(24);
            for (int i = 1; i <= 24; i++) {
                images.add(FXGL.image("plant/sunflower/SunFlower" + StringUtils.leftPad(String.valueOf(i), 4, '0') + ".png"));
            }
            final AnimationChannel ac = new AnimationChannel(images, Duration.seconds(2));
            texture = new AnimatedTexture(ac);
            texture.loop();
        }

        @Override
        public void onAdded() {
            getEntity().getViewComponent().addChild(texture);
        }
    }
}

sunflower-images.zip

AlmasB commented 1 month ago

Hi, thanks for this. It's likely caused by the engine incorrectly calculating tpf / fps at the start. There is an open PR which should fix it in the future releases.

starxg commented 1 month ago

Hi, thanks for this. It's likely caused by the engine incorrectly calculating tpf / fps at the start. There is an open PR which should fix it in the future releases.

Thank you for your reply. Are there any plans for a release?