0

だから私が達成しようとしているのは、特定の期間にわたってテクスチャを変更するクワッドです。テクスチャを含むArrayListをロードしてそれをいじるだけなのか、それともスプライトを使用する必要があるのか​​ わかりません。

だから私の質問は、これを行うための最良の方法は何かということです. そして、例を見せていただけますか.

4

1 に答える 1

0

私は両方の可能性を作りました。
だから私は両方を試してみました. これが機能する方法は、使用中または別のクラスから

いくつかの .PNG ファイルを追加することです。そして、残りはそれ自体が物語っています。私はcを推測します:addTexture(String ts)addTexture(String ts, int num)

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;
}
}
于 2013-10-10T09:35:11.610 に答える