Guest User

Sprite.java

a guest
Nov 26th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package game;
  2.  
  3. import java.io.IOException;
  4. import org.lwjgl.opengl.GL11;
  5. import org.newdawn.slick.opengl.Texture;
  6. import org.newdawn.slick.opengl.TextureLoader;
  7. import org.newdawn.slick.util.ResourceLoader;
  8.  
  9. public class Sprite
  10. {
  11.     protected Texture texture;
  12.    
  13.     protected float x;
  14.     protected float y;
  15.    
  16.     private float textureX;
  17.     private float textureY;
  18.    
  19.    
  20.     public Sprite(String filename, float x, float y)
  21.     {  
  22.         try
  23.         {
  24.             texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("game/" + filename));
  25.                                                                                                                          
  26.         }
  27.         catch (IOException e)
  28.         {
  29.             e.printStackTrace();
  30.         }
  31.        
  32.         this.x = x;
  33.         this.y = y;
  34.        
  35.         textureX = (float)texture.getImageWidth()/texture.getTextureWidth();
  36.         textureY = (float)texture.getImageHeight()/texture.getTextureHeight();
  37.     }
  38.     public void draw()
  39.     {
  40.         GL11.glPushMatrix();
  41.         texture.bind();
  42.  
  43.         GL11.glTranslatef(x, y, 0);
  44.        
  45.         GL11.glBegin(GL11.GL_QUADS);
  46.         {  
  47.             GL11.glTexCoord2f(0,0);
  48.             GL11.glVertex2f(0,0);
  49.            
  50.             GL11.glTexCoord2f(textureX,0);
  51.             GL11.glVertex2f(texture.getImageWidth(),0);
  52.            
  53.             GL11.glTexCoord2f(textureX,textureY);
  54.             GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());
  55.            
  56.             GL11.glTexCoord2f(0,textureY);
  57.             GL11.glVertex2f(0,texture.getImageHeight());
  58.         }
  59.         GL11.glEnd();
  60.        
  61.         GL11.glPopMatrix();
  62.     }
  63.     //Getters
  64.     public float getX()
  65.     {
  66.         return x;
  67.     }
  68.     public float getY()
  69.     {
  70.         return y;
  71.     }
  72.     //Setters
  73.     public void setX(float x)
  74.     {
  75.         this.x = x;
  76.     }
  77.     public void setY(float y)
  78.     {
  79.         this.y = y;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment