(LWJGL)如何加载动画GIFS?

时间:2015-03-27 19:01:59

标签: java 2d lwjgl gif tile

我正在制作一种2D平台游戏,当某件事发生时,我想要一个爆炸动画。我正在使用lwjgl加载纹理但是当我尝试加载gif时它只显示第一帧。那么有什么方法可以告诉它显示下一帧还是有一种特殊的方式来加载它?

1 个答案:

答案 0 :(得分:0)

您可以尝试以这种方式创建缓冲区:

    // create buffers for animated gifs
    if (logo != null) {
    logoBuffer = createImage(logo.getWidth(null), logo.getHeight(null));
    // add image observer, it will notify when next animated gif frame is ready
    offscreen.getGraphics().drawImage(logo, 0, 0, this);
    // in case image is not animated fill image buffer once
    imageUpdate(logo, ImageObserver.FRAMEBITS, 0, 0, 0, 0);
    /**
     * When an animated gif frame is ready to be drawn the ImageObserver
     * will call this method.
     *
     * The Image frame is copied into a buffer, which is then drawn.
     * This is done to prevent image tearing on gif animations.
     */
    public boolean imageUpdate(Image img, int flag, int x, int y, int width, int height) {

        // finish with this ImageObserver
        if (state == STATE_DONE) return false;

        // if image frame is ready to be drawn and is currently not being painted
        if (flag == ImageObserver.FRAMEBITS && !painting) {
            Image buffer;

            // select which buffer to fill
            if (img == logo) buffer = logoBuffer;
            else buffer = progressbarBuffer;

            Graphics g = buffer.getGraphics();

            // clear background on buffer
            g.setColor(bgColor);
            g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null));

            // buffer background is cleared, so draw logo under progressbar
            if (img == progressbar && logo != null) {
                g.drawImage(logoBuffer, progressbar.getWidth(null)/2-logo.getWidth(null)/2,
                                        progressbar.getHeight(null)/2-logo.getHeight(null)/2, null);
            }

            g.drawImage(img, 0, 0, this);
            g.dispose();

            repaint();
        }

        return true;
    }

由于您没有发布代码段,这可能就是您所需要的......