2
\$\begingroup\$

I made the following class to handle 2D animation through GIF images. This is made using JME 3.1, Java 8 and this GIF library. Is what I'm doing optimal? Are there already other available libraries or methods that do this?

public class Animation2D {

    private final GifDecoder decoder = new GifDecoder();
    private final AWTLoader loader = new AWTLoader();
    private final SimpleApplication app;
    private int frameIndex;
    private float frameDelay = decoder.getFrameCount() > 0 ? decoder.getDelay(frameIndex) : 0;

    public Animation2D(SimpleApplication app, String path) {
        this.app = app;

        TextureKey key = new TextureKey(path, false);
        AssetInfo info = app.getAssetManager().locateAsset(key);

        try (InputStream stream = info.openStream()) {
            decoder.read(stream);
        } catch (IOException err) {
            MyGame.LOGGER.log(Level.SEVERE, "Count not find texture file!", err);
        }
    }

    public void update(Picture pic, float tpf) {
        frameDelay -= tpf;

        if (frameDelay < 0) {
            frameDelay = decoder.getDelay(frameIndex) / 1000F;

            Texture2D tex = new Texture2D(loader.load(decoder.getFrame(frameIndex), true));
            pic.setTexture(app.getAssetManager(), tex, true);

            if (frameIndex < decoder.getFrameCount() - 1) {
                ++frameIndex;
            } else {
                frameIndex = 0;
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.