所以我想要实现的是一个在一段时间内改变纹理的四边形,我不确定我是否应该只加载一个带有纹理的 ArrayList 并且只是搞乱它,或者我是否应该使用精灵。
所以我的问题是最好的方法是什么,你能给我举个例子吗?
我提出了两种可能性。
所以我尝试了这两种方法,这似乎非常快,我更喜欢这个版本,因为它很简单。
这个工作的方式是,您将几个 .PNG 文件添加到 usingaddTexture(String ts)
或addTexture(String ts, int num)
来自不同的类。其余的不言自明,我猜c:
package line.entity;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Timer;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class Animation extends Entity{
private ArrayList<Texture> textures;
private boolean ExtraBind = false;
public boolean Popped = false;
private float interval = 0.07f;
private int currentTexture = 0;
private String textForm = "PNG";
private Texture none;
private Timer texTime;
private Float[] c4f = {1f, 1f, 1f, 1f};
public Animation(float x, float y){
super(x, y);
}
public Animation(float x, float y, float w, float h){
super(x, y);
this.width = w;
this.height = h;
}
@Override
public void init() {
textures = new ArrayList<Texture>();
texTime = new Timer();
try {
none = TextureLoader.getTexture(textForm, getInputStream("/img/none.png"), true, GL11.GL_LINEAR);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("static-access")
@Override
public void update() {
if(isPopped() == true){
texTime.tick();
if(texTime.getTime() > interval){
texTime.set(0);
if(currentTexture < textures.size()){
currentTexture++;
}
}
}else{
currentTexture = 0;
}
}
@Override
public void draw() {
if(currentTexture < textures.size()){
if(ExtraBind){
glBindTexture(GL_TEXTURE_2D, textures.get(currentTexture).getTextureID());
}else{
textures.get(currentTexture).bind();
}
}else{
if(ExtraBind){
glBindTexture(GL_TEXTURE_2D, none.getTextureID());
}else{
none.bind();
}
}
glColor4f(c4f[0], c4f[1], c4f[2], c4f[3]);;
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x, y);
glTexCoord2f(1,0);
glVertex2f(x + width,y);
glTexCoord2f(1,1);
glVertex2f(x + width, y + height);
glTexCoord2f(0,1);
glVertex2f(x, y + height);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
}
public void addTexture(String ts){
try {
textures.add(TextureLoader.getTexture(textForm, getInputStream(ts), true, GL11.GL_LINEAR));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addTexture(String ts, int num){
try {
textures.add(num, TextureLoader.getTexture(textForm, getInputStream(ts), true,GL11.GL_LINEAR));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private InputStream getInputStream(String string)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bi, "PNG", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
} catch (IOException e)
{
System.out.println("Error: "+e);
}
return null;
}
}