2013-10-08 78 views
0

所以我想實現的是,在一定的時間週期變化的質感四, 我不知道我是否應該只是加載與該或紋理周圍,只是亂七八糟的一個ArrayList我是否應該使用精靈。四與動畫紋理LWJGL

所以我的問題是什麼是做到這一點的最好辦法,你可以告訴我一個例子。

+0

爲什麼你不只是兩個,然後看看自己什麼是最好的? – Vallentin

+0

我做了下來波紋管 –

+0

下面波紋管在哪裏? – Vallentin

回答

0

我做了兩種可能性。
所以我試了兩個,這似乎合法地很快我喜歡這個版本更因爲簡單。

這一項工作的方式,是你加了幾個.png文件的使用addTexture(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; 
} 
}