Advertisement
Elec0

Basic LWJGL Images

Oct 25th, 2012 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.File;
  3.  
  4. import org.lwjgl.LWJGLException;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import static org.lwjgl.opengl.GL11.*;
  8. import org.newdawn.slick.Color;
  9. import org.newdawn.slick.opengl.Texture;
  10. import org.newdawn.slick.opengl.TextureLoader;
  11. import org.newdawn.slick.util.ResourceLoader;
  12.  
  13. public class main {
  14.     private Texture[] texture = new Texture[10];
  15.     private String tilesLocation = System.getProperty("user.dir") + "\\res\\tiles\\";
  16.    
  17.     public void start()
  18.     {
  19.         initGL(800, 600);
  20.         init();
  21.        
  22.        
  23.         while(!Display.isCloseRequested())
  24.         {
  25.             glClear(GL_COLOR_BUFFER_BIT);
  26.             render();
  27.             Display.update();
  28.             Display.sync(100);
  29.            
  30.             if(Display.isCloseRequested()){
  31.                 Display.destroy();
  32.                 System.exit(0);
  33.             }
  34.         }
  35.     }
  36.    
  37.     private void initGL(int width, int height)
  38.     {
  39.         try {
  40.             Display.setDisplayMode(new DisplayMode(width,height));
  41.             Display.create();
  42.             Display.setVSyncEnabled(true);
  43.         } catch (LWJGLException e) {
  44.             e.printStackTrace();
  45.             System.exit(0);
  46.         }
  47.  
  48.         glEnable(GL_TEXTURE_2D);              
  49.        
  50.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
  51.        
  52.         // enable alpha blending
  53.         glEnable(GL_BLEND);
  54.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  55.    
  56.         glViewport(0,0,width,height);
  57.         glMatrixMode(GL_MODELVIEW);
  58.  
  59.         glMatrixMode(GL_PROJECTION);
  60.         glLoadIdentity();
  61.         glOrtho(0, width, height, 0, 1, -1);
  62.         glMatrixMode(GL_MODELVIEW);
  63.        
  64.     }
  65.    
  66.     private void init()
  67.     {
  68.         try
  69.         {
  70.             int curI = 0;
  71.            
  72.             for(int i = 0; i < 1000; ++i)
  73.             {
  74.                 if(new File(tilesLocation + i + ".png").exists())
  75.                 {
  76.                     texture[curI] = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(tilesLocation + i + ".png"));
  77.  
  78.                     System.out.println("Texture loaded: "+texture[curI]);
  79.                     System.out.println(">> Image width: "+texture[curI].getImageWidth());
  80.                     System.out.println(">> Image height: "+texture[curI].getImageHeight());
  81.                     System.out.println(">> Texture width: "+texture[curI].getTextureWidth());
  82.                     System.out.println(">> Texture height: "+texture[curI].getTextureHeight());
  83.                     System.out.println(">> Texture ID: "+texture[curI].getTextureID());
  84.                     curI++;
  85.                     if(curI == 9) break;
  86.                 }
  87.                 else { System.out.println("false");}
  88.             }          
  89.         } catch(IOException e){
  90.             e.printStackTrace();
  91.             }
  92.     }
  93.    
  94.     private void render()
  95.     {
  96.         Color.white.bind();
  97.         float sx = 32, sy = 100;
  98.         float x, y;
  99.        
  100.         for(int i = 0; i < texture.length; ++i)
  101.         {
  102.             if(texture[i] != null)
  103.             {
  104.                 x = sx * i;
  105.                 y = sy;
  106.                
  107.                 texture[i].bind(); // or glBind(texture[i].getTextureID());
  108.                
  109.                 glBegin(GL_QUADS);
  110.                     glTexCoord2f(0,0);
  111.                     glVertex2f(x, y);
  112.                     glTexCoord2f(1,0);
  113.                     glVertex2f(x + texture[i].getTextureWidth(), y);
  114.                     glTexCoord2f(1, 1);
  115.                     glVertex2f(x + texture[i].getTextureWidth(), y + texture[i].getTextureHeight());
  116.                     glTexCoord2f(0, 1);
  117.                     glVertex2f(x, y + texture[i].getTextureHeight());
  118.                 glEnd();
  119.             }
  120.         }
  121.     }
  122.    
  123.     public static void main(String[] args) {
  124.         // TODO Auto-generated method stub
  125.         main images = new main();
  126.         images.start();
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement