So what I am trying to achieve is a quad that changes texture over a certain period of time, I am not sure if I should just load an ArrayList with textures and just mess around with that or whether I should using sprites.
So My question is what would be the best way to do this, And could you show me an example.
I made both possibilities.
So I tried both and this seems legitimatedly fast i like this version more because of the simplicity.
The way this one works, is you add a couple of .PNG files to the using addTexture(String ts)
or addTexture(String ts, int num)
from a different class. And well the rest speaks for itself I guess 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;
}
}