Added a AnimatedSprite for sequensed textures
This commit is contained in:
parent
2cf9def34c
commit
ec551064c4
4 changed files with 174 additions and 6 deletions
2
.project
2
.project
|
|
@ -12,6 +12,8 @@
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
|
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.jem.beaninfo.BeanInfoNature</nature>
|
||||||
</natures>
|
</natures>
|
||||||
</projectDescription>
|
</projectDescription>
|
||||||
|
|
|
||||||
7
log.txt
7
log.txt
|
|
@ -1,6 +1 @@
|
||||||
2007-03-21 22:49:30:390 # Loading texture: data/units/tank.png
|
2007-03-26 16:59:28:234 # Loading texture: data/particle.bmp
|
||||||
2007-03-21 22:49:31:187 # Loading texture: data/particle.bmp
|
|
||||||
2007-03-21 22:49:31:421 # Loading sound: data/sounds/test.wav
|
|
||||||
2007-03-21 22:49:31:437 # Sound sources: quantity=0
|
|
||||||
2007-03-21 22:49:31:437 # Stoping sound: source=0
|
|
||||||
2007-03-21 22:49:31:437 # Playing sound: source=0 buffer=261490744
|
|
||||||
|
|
|
||||||
167
src/ei/engine/scene/AnimatedSprite.java
Normal file
167
src/ei/engine/scene/AnimatedSprite.java
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
package ei.engine.scene;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
|
import ei.engine.texture.Texture;
|
||||||
|
import ei.engine.texture.TextureLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of sprite that uses an OpenGL quad and a texture
|
||||||
|
* to render a given image to the screen.
|
||||||
|
*
|
||||||
|
* @author Kevin Glass
|
||||||
|
* @author Brian Matzon
|
||||||
|
* @author Ziver Koc
|
||||||
|
*/
|
||||||
|
public class AnimatedSprite extends Entity {
|
||||||
|
/** The texture that stores the image for this sprite */
|
||||||
|
private HashMap<String,Texture[]> textures;
|
||||||
|
private String currentAnimation;
|
||||||
|
private int textureId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new empty AnimatedSprite
|
||||||
|
*
|
||||||
|
* @param window The window in which the sprite will be displayed
|
||||||
|
* @param ref A reference to the image on which this sprite should be based
|
||||||
|
*/
|
||||||
|
public AnimatedSprite(String name) {
|
||||||
|
super(name);
|
||||||
|
textures = new HashMap<String,Texture[]>();
|
||||||
|
currentAnimation = null;
|
||||||
|
textureId = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a vector of textures to the AnimationSprite
|
||||||
|
*
|
||||||
|
* @param name The name of the animation
|
||||||
|
* @param t The vector of textures
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean addAnimation(String name, Texture[] t){
|
||||||
|
if(!textures.containsKey(name)){
|
||||||
|
textures.put(name,t);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a vector of textures to the AnimationSprite
|
||||||
|
*
|
||||||
|
* @param name The name of the animation
|
||||||
|
* @param t The path to the textures
|
||||||
|
* @return true if successful else false
|
||||||
|
*/
|
||||||
|
public boolean addAnimation(String name, String[] s){
|
||||||
|
Texture[] t = new Texture[s.length];
|
||||||
|
|
||||||
|
for(int i=0; i<s.length ;i++){
|
||||||
|
t[i] = TextureLoader.getTextureLoaderInstance().getTexture(s[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return addAnimation(name,t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Play an animation
|
||||||
|
*
|
||||||
|
* @param name The name of the animation
|
||||||
|
* @return true if the animation exists else false
|
||||||
|
*/
|
||||||
|
public boolean playAnimation(String name){
|
||||||
|
if(textures.containsKey(name)){
|
||||||
|
currentAnimation = name;
|
||||||
|
textureId = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the width of the current animation of this sprite in pixels
|
||||||
|
*
|
||||||
|
* @return The width of this sprite in pixels
|
||||||
|
*/
|
||||||
|
public int getWidth() {
|
||||||
|
if(textures.get(currentAnimation)[textureId] == null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return textures.get(currentAnimation)[textureId].getImageWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the height of the current animation of this sprite in pixels
|
||||||
|
*
|
||||||
|
* @return The height of this sprite in pixels
|
||||||
|
*/
|
||||||
|
public int getHeight() {
|
||||||
|
if(textures.get(currentAnimation)[textureId] == null){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return textures.get(currentAnimation)[textureId].getImageHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the sprite
|
||||||
|
*/
|
||||||
|
public void render() {
|
||||||
|
if(currentAnimation != null){
|
||||||
|
// store the current model matrix
|
||||||
|
GL11.glPushMatrix();
|
||||||
|
//Reset The Current Modelview Matrix
|
||||||
|
GL11.glLoadIdentity();
|
||||||
|
|
||||||
|
//Sets the scale of the sprite
|
||||||
|
super.setScaleGL();
|
||||||
|
//Sets the location
|
||||||
|
super.setTranslationGL();
|
||||||
|
//the rotation
|
||||||
|
super.setRotationGL();
|
||||||
|
//and sets back the rotation so that the rotation pivot is the center of the sprite
|
||||||
|
super.setTranslationGL(
|
||||||
|
-textures.get(currentAnimation)[textureId].getImageWidth()/2,
|
||||||
|
-textures.get(currentAnimation)[textureId].getImageHeight()/2);
|
||||||
|
|
||||||
|
// bind to the appropriate texture for this sprite
|
||||||
|
textures.get(currentAnimation)[textureId].bindGL();
|
||||||
|
GL11.glColor3f(1,1,1);
|
||||||
|
|
||||||
|
// draw a quad textured to match the sprite
|
||||||
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
{
|
||||||
|
//Vertex at the upper left
|
||||||
|
GL11.glTexCoord2f(0, 0);
|
||||||
|
GL11.glVertex2f(0, 0);
|
||||||
|
//Vertex at the down left
|
||||||
|
GL11.glTexCoord2f(0, textures.get(currentAnimation)[textureId].getHeight());
|
||||||
|
GL11.glVertex2f(0, textures.get(currentAnimation)[textureId].getImageHeight());
|
||||||
|
//Vertex at the upper right
|
||||||
|
GL11.glTexCoord2f(
|
||||||
|
textures.get(currentAnimation)[textureId].getWidth(),
|
||||||
|
textures.get(currentAnimation)[textureId].getHeight());
|
||||||
|
GL11.glVertex2f(
|
||||||
|
textures.get(currentAnimation)[textureId].getImageWidth(),
|
||||||
|
textures.get(currentAnimation)[textureId].getImageHeight());
|
||||||
|
//Vertex at the down right
|
||||||
|
GL11.glTexCoord2f(textures.get(currentAnimation)[textureId].getWidth(), 0);
|
||||||
|
GL11.glVertex2f(textures.get(currentAnimation)[textureId].getImageWidth(),0);
|
||||||
|
}
|
||||||
|
GL11.glEnd();
|
||||||
|
|
||||||
|
|
||||||
|
//Reset The Current Modelview Matrix
|
||||||
|
GL11.glLoadIdentity();
|
||||||
|
// restore the model view matrix to prevent contamination
|
||||||
|
GL11.glPopMatrix();
|
||||||
|
//goes to the next texture
|
||||||
|
textureId++;
|
||||||
|
if(textures.get(currentAnimation).length >= textureId){
|
||||||
|
textureId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -74,6 +74,10 @@ public class Sprite extends Entity {
|
||||||
return texture.getImageHeight();
|
return texture.getImageHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void bindTexture(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw the sprite
|
* Draw the sprite
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue