java - (LWJGL) 如何加载动画GIFS?

标签 java 2d lwjgl gif tile

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

最佳答案

以下是如何使用 Java 和 LWJGL 实现您所描述的步骤的总体思路:

使用动画 GIF 文件来表示爆炸动画。

使用 GIFDecoder 等库读取 GIF 帧并将其解码为单独的图像。

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

// Use GIFDecoder to read and decode the frames of the GIF
import gifdecoder.GIFDecoder;

// Create a GIFDecoder object to read the animation file
GIFDecoder decoder = new GIFDecoder();
decoder.read("explode.gif");

// Get the number of frames in the animation
int frameCount = decoder.getFrameCount();

// Create an array to store the frames of the animation
BufferedImage[] frames = new BufferedImage[frameCount];

// Decode the frames of the animation and store them in the array
for (int i = 0; i < frameCount; i++) {
    frames[i] = decoder.getFrame(i);
}

使用LWJGL的TextureLoader将动画的每一帧作为单独的纹理加载。

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

// Create an array to store the textures of the animation
Texture[] textures = new Texture[frameCount];

// Load each frame of the animation as a separate texture
for (int i = 0; i < frameCount; i++) {
    textures[i] = TextureLoader.getTexture("PNG", new ByteArrayInputStream(ImageIO.read(frames[i])));
}

使用LWJGL的渲染函数在适当的位置绘制动画的当前帧

textures[currentFrame].bind();

GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(x, y);
    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(x + width, y);
    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(x + width, y + height);
    GL11.glTexCoord2f

关于java - (LWJGL) 如何加载动画GIFS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308314/

相关文章:

algorithm - 界交点

java - 没有 Makefile 的项目

java - 匿名类方法访问

java - 当线程被销毁时,变量会被销毁吗?

c# - 在 Unity 2D 中翻转 2D Sprite 动画

graphics - 老游戏中的 "side scrolling hack"是什么?

java - LWJGL:Slick:在 3D 世界中绘制字体

java - OpenGL 中的纹理随时间变白

java - 实现 "reorderable"实体的最佳方式

java - android - 覆盖服务 move View