2007-03-08 19:17:23 +00:00
|
|
|
package ei.engine.scene;
|
|
|
|
|
|
|
|
|
|
import org.lwjgl.opengl.GL11;
|
|
|
|
|
|
2007-03-15 19:52:28 +00:00
|
|
|
import ei.engine.texture.Texture;
|
|
|
|
|
import ei.engine.texture.TextureLoader;
|
2007-03-08 19:17:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2007-03-13 18:37:22 +00:00
|
|
|
public class Sprite extends Entity {
|
2007-03-08 19:17:23 +00:00
|
|
|
/** The texture that stores the image for this sprite */
|
|
|
|
|
private Texture texture;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2007-03-13 18:37:22 +00:00
|
|
|
super(name);
|
|
|
|
|
texture = null;
|
2007-03-08 19:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-12 19:13:08 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new sprite from a specified texture.
|
|
|
|
|
*
|
|
|
|
|
* @param name The name of the sprite
|
|
|
|
|
* @param texture The texture to use
|
|
|
|
|
*/
|
|
|
|
|
public Sprite(String name, Texture texture) {
|
2007-03-13 18:37:22 +00:00
|
|
|
super(name);
|
2007-03-12 19:13:08 +00:00
|
|
|
this.texture = texture;
|
2007-03-13 18:37:22 +00:00
|
|
|
}
|
2007-03-12 19:13:08 +00:00
|
|
|
|
2007-03-08 19:17:23 +00:00
|
|
|
/**
|
|
|
|
|
* 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) {
|
2007-03-13 18:37:22 +00:00
|
|
|
super(name);
|
2007-03-21 19:02:53 +00:00
|
|
|
this.texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
|
2007-03-08 19:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
}
|
2007-03-17 16:29:39 +00:00
|
|
|
|
2007-03-26 16:05:19 +00:00
|
|
|
protected void bindTexture(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 19:17:23 +00:00
|
|
|
/**
|
|
|
|
|
* Draw the sprite
|
|
|
|
|
*/
|
|
|
|
|
public void render() {
|
|
|
|
|
// store the current model matrix
|
|
|
|
|
GL11.glPushMatrix();
|
2007-03-18 17:38:58 +00:00
|
|
|
|
|
|
|
|
//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
|
2007-03-27 17:51:02 +00:00
|
|
|
super.setTranslationGL(-texture.getImageWidth()/2,-texture.getImageHeight()/2,0);
|
2007-03-17 16:29:39 +00:00
|
|
|
|
2007-03-08 19:17:23 +00:00
|
|
|
// bind to the appropriate texture for this sprite
|
2007-03-17 16:29:39 +00:00
|
|
|
texture.bindGL();
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glColor3f(1,1,1);
|
|
|
|
|
|
2007-03-08 19:17:23 +00:00
|
|
|
// draw a quad textured to match the sprite
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glBegin(GL11.GL_QUADS);
|
2007-03-08 19:17:23 +00:00
|
|
|
{
|
2007-03-17 16:29:39 +00:00
|
|
|
//Vertex at the upper left
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glTexCoord2f(0, 0);
|
|
|
|
|
GL11.glVertex2f(0, 0);
|
2007-03-17 16:29:39 +00:00
|
|
|
//Vertex at the down left
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glTexCoord2f(0, texture.getHeight());
|
|
|
|
|
GL11.glVertex2f(0, texture.getImageHeight());
|
2007-03-17 16:29:39 +00:00
|
|
|
//Vertex at the upper right
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
|
|
|
|
|
GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());
|
2007-03-17 16:29:39 +00:00
|
|
|
//Vertex at the down right
|
2007-03-12 19:13:08 +00:00
|
|
|
GL11.glTexCoord2f(texture.getWidth(), 0);
|
|
|
|
|
GL11.glVertex2f(texture.getImageWidth(),0);
|
2007-03-08 19:17:23 +00:00
|
|
|
}
|
|
|
|
|
GL11.glEnd();
|
2007-03-17 16:29:39 +00:00
|
|
|
|
2007-03-08 19:17:23 +00:00
|
|
|
// restore the model view matrix to prevent contamination
|
|
|
|
|
GL11.glPopMatrix();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|