153 lines
3.7 KiB
Java
153 lines
3.7 KiB
Java
|
|
package ei.engine.scene;
|
||
|
|
|
||
|
|
import java.io.IOException;
|
||
|
|
|
||
|
|
import org.lwjgl.opengl.GL11;
|
||
|
|
|
||
|
|
import ei.engine.math.Vector2I;
|
||
|
|
import ei.engine.util.Texture;
|
||
|
|
import ei.engine.util.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 Sprite {
|
||
|
|
/** The texture that stores the image for this sprite */
|
||
|
|
private Texture texture;
|
||
|
|
|
||
|
|
/** The location of this sprite in pixels*/
|
||
|
|
private Vector2I location;
|
||
|
|
|
||
|
|
/** The name of this sprite */
|
||
|
|
private String name;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new empty sprite
|
||
|
|
*
|
||
|
|
* @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 Sprite(String name) {
|
||
|
|
this.name = name;
|
||
|
|
texture = null;
|
||
|
|
location = new Vector2I(0,0);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new sprite from a specified image.
|
||
|
|
*
|
||
|
|
* @param name The name of the sprite
|
||
|
|
* @param ref A reference to the image on which this sprite should be based
|
||
|
|
*/
|
||
|
|
public Sprite(String name, String ref) {
|
||
|
|
try {
|
||
|
|
this.name = name;
|
||
|
|
texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
|
||
|
|
|
||
|
|
location = new Vector2I(texture.getImageWidth(),texture.getImageHeight());
|
||
|
|
} catch (IOException e) {
|
||
|
|
// a tad abrupt, but our purposes if you can't find a
|
||
|
|
// sprite's image you might as well give up.
|
||
|
|
System.err.println("Unable to load texture: "+ref);
|
||
|
|
System.exit(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the width of this sprite in pixels
|
||
|
|
*
|
||
|
|
* @return The width of this sprite in pixels
|
||
|
|
*/
|
||
|
|
public int getWidth() {
|
||
|
|
if(texture == null){
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return texture.getImageWidth();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the height of this sprite in pixels
|
||
|
|
*
|
||
|
|
* @return The height of this sprite in pixels
|
||
|
|
*/
|
||
|
|
public int getHeight() {
|
||
|
|
if(texture == null){
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return texture.getImageHeight();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return The name of this sprite
|
||
|
|
*/
|
||
|
|
public String getName() {
|
||
|
|
return name;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the height of this sprite in pixels
|
||
|
|
*
|
||
|
|
* @return The height of this sprite in pixels
|
||
|
|
*/
|
||
|
|
public int getLocation() {
|
||
|
|
if(texture == null){
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return texture.getImageHeight();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* set the location of this sprite in pixels
|
||
|
|
* @param x The x coordinate of the sprite
|
||
|
|
* @param y The y coordinate of the sprite
|
||
|
|
*/
|
||
|
|
public void setLocation(int x, int y) {
|
||
|
|
setLocation(new Vector2I(x,y));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* set the location of this sprite in pixels
|
||
|
|
* @param v The location of the sprite
|
||
|
|
*/
|
||
|
|
public void setLocation(Vector2I v) {
|
||
|
|
location = v;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Draw the sprite
|
||
|
|
*/
|
||
|
|
public void render() {
|
||
|
|
// store the current model matrix
|
||
|
|
GL11.glPushMatrix();
|
||
|
|
|
||
|
|
// bind to the appropriate texture for this sprite
|
||
|
|
texture.bind();
|
||
|
|
|
||
|
|
// translate to the right location and prepare to draw
|
||
|
|
GL11.glTranslatef(location.getX(), location.getY(), 0);
|
||
|
|
GL11.glColor3f(1,1,1);
|
||
|
|
|
||
|
|
// draw a quad textured to match the sprite
|
||
|
|
GL11.glBegin(GL11.GL_QUADS);
|
||
|
|
{
|
||
|
|
GL11.glTexCoord2f(0, 0);
|
||
|
|
GL11.glVertex2f(0, 0);
|
||
|
|
GL11.glTexCoord2f(0, texture.getImageWidth());
|
||
|
|
GL11.glVertex2f(0, texture.getImageHeight());
|
||
|
|
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
|
||
|
|
GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());
|
||
|
|
GL11.glTexCoord2f(texture.getWidth(), 0);
|
||
|
|
GL11.glVertex2f(texture.getImageWidth(),0);
|
||
|
|
}
|
||
|
|
GL11.glEnd();
|
||
|
|
|
||
|
|
// restore the model view matrix to prevent contamination
|
||
|
|
GL11.glPopMatrix();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|