Advertisement
Guest User

LWJGL_Sprite.java

a guest
Oct 5th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3.  
  4. import org.lwjgl.opengl.GL11;
  5.  
  6.  
  7. public class LWJGL_Sprite {
  8.    
  9.    
  10.     private LWJGL_Texture texture;
  11.    
  12.     private float u1, v1, u2, v2, scalex, scaley;
  13.    
  14.     private float x, y, offsetx, offsety;
  15.    
  16.     public LWJGL_Sprite(String filename) {
  17.         try {
  18.             texture = new LWJGL_Texture(new FileInputStream(filename));
  19.         } catch (IOException e) {
  20.             // TODO Auto-generated catch block
  21.             e.printStackTrace();
  22.             System.exit(0);
  23.         }
  24.         offsetx = 0;
  25.         offsety = 0;
  26.         scalex = 1.0f;
  27.         scaley = 1.0f;
  28.     }
  29.    
  30.     public void init(){
  31.         this.setU1(0.0f); this.setV1(0.0f);
  32.         this.setU2(1.0f); this.setV2(1.0f);
  33.         texture.init();
  34.     }
  35.    
  36.     public void init(float u1, float v1, float u2, float v2){
  37.         this.setU1(u1); this.setV1(v1);
  38.         this.setU2(u2); this.setV2(v2);
  39.         texture.init();
  40.     }
  41.    
  42.     public void draw(float x, float y) {
  43.        
  44.         this.x = x;
  45.         this.y = y;
  46.        
  47.         texture.bind();
  48.        
  49.         GL11.glPushMatrix();
  50.        
  51.         GL11.glBegin(GL11.GL_QUADS);
  52.        
  53.         GL11.glTexCoord2f(u1, v1); GL11.glVertex2f(x - offsetx                    , y - offsety                                );
  54.         GL11.glTexCoord2f(u1, v2); GL11.glVertex2f(x - offsetx                    , y - offsety + texture.getHeight() * scaley * (v2-v1) );
  55.         GL11.glTexCoord2f(u2, v2); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety + texture.getHeight() * scaley * (v2-v1) );
  56.         GL11.glTexCoord2f(u2, v1); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety                                );
  57.        
  58.         GL11.glEnd();
  59.        
  60.         GL11.glPopMatrix();
  61.        
  62.         texture.unbind();
  63.     }
  64.    
  65.     public void draw() {
  66.        
  67.         texture.bind();
  68.        
  69.         GL11.glPushMatrix();
  70.        
  71.         GL11.glBegin(GL11.GL_QUADS);
  72.        
  73.         GL11.glTexCoord2f(u1, v1); GL11.glVertex2f(x - offsetx                    , y - offsety                                );
  74.         GL11.glTexCoord2f(u1, v2); GL11.glVertex2f(x - offsetx                    , y - offsety + texture.getHeight() * scaley * (v2-v1) );
  75.         GL11.glTexCoord2f(u2, v2); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety + texture.getHeight() * scaley * (v2-v1) );
  76.         GL11.glTexCoord2f(u2, v1); GL11.glVertex2f(x - offsetx + texture.getWidth() * scalex * (u2-u1), y - offsety                                );
  77.        
  78.         GL11.glEnd();
  79.        
  80.         GL11.glPopMatrix();
  81.        
  82.         texture.unbind();
  83.     }
  84.  
  85.     public float getU1() {
  86.         return u1;
  87.     }
  88.  
  89.     public void setU1(float u) {
  90.         this.u1 = u;
  91.     }
  92.  
  93.     public float getV1() {
  94.         return v1;
  95.     }
  96.  
  97.     public void setV1(float v) {
  98.         this.v1 = v;
  99.     }
  100.  
  101.     public float getU2() {
  102.         return u2;
  103.     }
  104.  
  105.     public void setU2(float u) {
  106.         this.u2 = u;
  107.     }
  108.  
  109.     public float getV2() {
  110.         return v2;
  111.     }
  112.  
  113.     public void setV2(float v) {
  114.         this.v2 = v;
  115.     }
  116.  
  117.     public float getScaleX() {
  118.         return scalex;
  119.     }
  120.  
  121.     public void setScaleX(float scalex) {
  122.         this.scalex = scalex;
  123.     }
  124.  
  125.     public float getScaleY() {
  126.         return scaley;
  127.     }
  128.  
  129.     public void setScaleY(float scaley) {
  130.         this.scaley = scaley;
  131.     }
  132.  
  133.     public float getOffsetX() {
  134.         return offsetx;
  135.     }
  136.  
  137.     public void setOffsetX(float offsetx) {
  138.         this.offsetx = offsetx;
  139.     }
  140.  
  141.     public float getOffsetY() {
  142.         return offsety;
  143.     }
  144.  
  145.     public void setOffsetY(float offsety) {
  146.         this.offsety = offsety;
  147.     }
  148.    
  149.     public int getWidth(){
  150.         return texture.getWidth();
  151.     }
  152.    
  153.     public int getHeight(){
  154.         return texture.getHeight();
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement