Changed the AnimatedSprite to AnimatedTexture
This commit is contained in:
parent
0ef950a5ea
commit
39d8eb9247
4 changed files with 182 additions and 173 deletions
|
|
@ -1,172 +0,0 @@
|
||||||
package ei.engine.scene;
|
|
||||||
|
|
||||||
import java.awt.Rectangle;
|
|
||||||
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();
|
|
||||||
|
|
||||||
//Sets the location
|
|
||||||
super.setTranslationGL();
|
|
||||||
//the rotation
|
|
||||||
super.setRotationGL(
|
|
||||||
textures.get(currentAnimation)[textureId].getImageWidth()/2,
|
|
||||||
textures.get(currentAnimation)[textureId].getImageHeight()/2);
|
|
||||||
//Sets the scale of the sprite
|
|
||||||
super.setScaleGL();
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// restore the model view matrix to prevent contamination
|
|
||||||
GL11.glPopMatrix();
|
|
||||||
//goes to the next texture
|
|
||||||
textureId++;
|
|
||||||
if(textures.get(currentAnimation).length >= textureId){
|
|
||||||
textureId = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the bound of this class
|
|
||||||
*/
|
|
||||||
public Rectangle getBound() {
|
|
||||||
return new Rectangle(
|
|
||||||
(int)getLocation().getX(),
|
|
||||||
(int)getLocation().getY(),
|
|
||||||
textures.get(currentAnimation)[textureId].getImageWidth(),
|
|
||||||
textures.get(currentAnimation)[textureId].getImageHeight());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -101,7 +101,6 @@ public class Sprite extends Entity {
|
||||||
|
|
||||||
// bind to the appropriate texture for this sprite
|
// bind to the appropriate texture for this sprite
|
||||||
texture.bindGL();
|
texture.bindGL();
|
||||||
GL11.glColor3f(1,1,1);
|
|
||||||
|
|
||||||
// draw a quad textured to match the sprite
|
// draw a quad textured to match the sprite
|
||||||
GL11.glBegin(GL11.GL_QUADS);
|
GL11.glBegin(GL11.GL_QUADS);
|
||||||
|
|
|
||||||
172
src/ei/engine/texture/AnimatedTexture.java
Normal file
172
src/ei/engine/texture/AnimatedTexture.java
Normal file
|
|
@ -0,0 +1,172 @@
|
||||||
|
package ei.engine.texture;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses a sequence of images to "Animate"
|
||||||
|
*
|
||||||
|
* @author Ziver Koc
|
||||||
|
*/
|
||||||
|
public class AnimatedTexture extends Texture {
|
||||||
|
/** 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 AnimatedTexture(String name) {
|
||||||
|
super();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind the specified GL context to a texture
|
||||||
|
*
|
||||||
|
* @param gl The GL context to bind to
|
||||||
|
*/
|
||||||
|
public void bindGL() {
|
||||||
|
if(currentAnimation != null){
|
||||||
|
textures.get(currentAnimation)[textureId].bindGL();
|
||||||
|
textureId++;
|
||||||
|
if(textures.get(currentAnimation).length >= textureId){
|
||||||
|
textureId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the height of the image
|
||||||
|
*
|
||||||
|
* @param height The height of the image
|
||||||
|
*/
|
||||||
|
public void setHeight(int height) {
|
||||||
|
textures.get(currentAnimation)[textureId].setHeight(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the width of the image
|
||||||
|
*
|
||||||
|
* @param width The width of the image
|
||||||
|
*/
|
||||||
|
public void setWidth(int width) {
|
||||||
|
textures.get(currentAnimation)[textureId].setWidth(width);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the target of the buffer
|
||||||
|
*
|
||||||
|
* @return The target of the buffer
|
||||||
|
*/
|
||||||
|
public int getGLTarget() {
|
||||||
|
return textures.get(currentAnimation)[textureId].getGLTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the height of the original image
|
||||||
|
*
|
||||||
|
* @return The height of the original image
|
||||||
|
*/
|
||||||
|
public int getImageHeight() {
|
||||||
|
return textures.get(currentAnimation)[textureId].getImageHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the width of the original image
|
||||||
|
*
|
||||||
|
* @return The width of the original image
|
||||||
|
*/
|
||||||
|
public int getImageWidth() {
|
||||||
|
return textures.get(currentAnimation)[textureId].getImageWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the height of the physical texture
|
||||||
|
*
|
||||||
|
* @return The height of physical texture
|
||||||
|
*/
|
||||||
|
public float getHeight() {
|
||||||
|
return textures.get(currentAnimation)[textureId].getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the width of the physical texture
|
||||||
|
*
|
||||||
|
* @return The width of physical texture
|
||||||
|
*/
|
||||||
|
public float getWidth() {
|
||||||
|
return textures.get(currentAnimation)[textureId].getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the height of this texture
|
||||||
|
*
|
||||||
|
* @param texHeight The height of the texture
|
||||||
|
*/
|
||||||
|
public void setTextureHeight(float texHeight) {
|
||||||
|
textures.get(currentAnimation)[textureId].setTextureHeight(texHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the width of this texture
|
||||||
|
*
|
||||||
|
* @param texWidth The width of the texture
|
||||||
|
*/
|
||||||
|
public void setTextureWidth(float texWidth) {
|
||||||
|
textures.get(currentAnimation)[textureId].setTextureWidth(texWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -33,6 +33,15 @@ public class Texture {
|
||||||
/** The ratio of the height of the image to the texture */
|
/** The ratio of the height of the image to the texture */
|
||||||
private float heightRatio;
|
private float heightRatio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a empty texture
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Texture(){
|
||||||
|
this.target = -1;
|
||||||
|
this.textureID = -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new texture
|
* Create a new texture
|
||||||
*
|
*
|
||||||
|
|
@ -51,6 +60,7 @@ public class Texture {
|
||||||
*/
|
*/
|
||||||
public void bindGL() {
|
public void bindGL() {
|
||||||
GL11.glBindTexture(target, textureID);
|
GL11.glBindTexture(target, textureID);
|
||||||
|
GL11.glColor3f(1,1,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue