1.  
  2. import java.io.IOException;
  3. import javax.imageio.ImageIO;
  4. import java.io.File;
  5. import java.awt.image.BufferedImage;
  6.  
  7. import org.lwjgl.Sys;
  8. import org.lwjgl.input.Keyboard;
  9. import org.lwjgl.opengl.Display;
  10. import org.lwjgl.opengl.GL11;
  11. import org.lwjgl.opengl.DisplayMode;
  12.  
  13.  
  14. /**
  15.  * Basic game
  16.  *
  17.  * @author Name <email>
  18.  * @version 1.0
  19.  */
  20. public class Main {
  21.  
  22.    
  23.   /** Game title */
  24.   public static final String GAME_TITLE = "My Game";
  25.  
  26.   /** Desired frame time */
  27.   private static final int FRAMERATE = 60;
  28.  
  29.   /** Exit the game */
  30.   private static boolean finished;
  31.  
  32.   /** Angle of rotating square */
  33.   private static float angle;
  34.   public static String IMG = "matty.jpg";
  35.  
  36.  
  37.  
  38.   /**
  39.    * Application init
  40.    * @param args Commandline args
  41.    */
  42.   public static void main(String[] args) {
  43.     boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
  44.  
  45.     try {
  46.       init(fullscreen);
  47.       run();
  48.     } catch (Exception e) {
  49.       e.printStackTrace(System.err);
  50.       Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
  51.     } finally {
  52.       cleanup();
  53.     }
  54.     System.exit(0);
  55.   }
  56.  
  57.   public static void read_file(int [][] pix)
  58.   {
  59.      
  60.        
  61.         BufferedImage img;
  62.         try{
  63.         img = ImageIO.read(new File(IMG));
  64.        
  65.         pix = new int[img.getHeight()][img.getWidth()];
  66.          
  67.        
  68.         for(int i=0;i<img.getHeight();i++)
  69.         {
  70.               for(int j = 0; j < img.getWidth(); j++){
  71.                  
  72.                  
  73.                  pix[i][j] = img.getRGB(j,i);
  74.                   int r,g,b;
  75.                   r=(pix[i][j] >> 16) & 0xFF;
  76.                   g = (pix[i][j] >> 8) & 0xFF;
  77.                   b = pix[i][j] & 0xFF;
  78.                  
  79.                   System.out.println(r+"-"+g+"-"+b);
  80.               }
  81.         }
  82.  
  83.        
  84.         }
  85.         catch(IOException e){
  86.               //default error
  87.             System.out.println(e);
  88.         }
  89.   }
  90.  
  91.   private static void render() {
  92.         int[][] pix = new int[6][6];
  93.        
  94.         read_file(pix);
  95.        
  96.        
  97.        
  98.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  99.         GL11.glLoadIdentity();
  100.         GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), -1, 1);
  101.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  102.      
  103.         // clear the screen
  104.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  105.      
  106.         // center square according to screen size
  107.         GL11.glPushMatrix();
  108.         GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);
  109.      
  110.      
  111.          
  112.          
  113.           GL11.glBegin(GL11.GL_QUADS);
  114.          
  115.           for(int i=0;i<5;i++)
  116.               for(int j=0;j<5;j++)
  117.               {
  118.          
  119.             GL11.glVertex2i(-50*i, -50*i);
  120.             GL11.glVertex2i(50*i, -50*i);
  121.             GL11.glVertex2i(50*i, 50*i);
  122.             GL11.glVertex2i(-50*i, 50*i);
  123.            
  124.              System.out.println(pix[i][j]);
  125.               }
  126.            
  127.           GL11.glEnd();
  128.      
  129.         GL11.glPopMatrix();
  130.       }
  131.  
  132.  
  133.  
  134.   private static void init(boolean fullscreen) throws Exception {
  135.     // Create a fullscreen window with 1:1 orthographic 2D projection (default)
  136.     Display.setTitle(GAME_TITLE);
  137.    
  138.  
  139.     // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
  140.     Display.setVSyncEnabled(true);
  141.    
  142.     Display.setDisplayMode(new DisplayMode(640,480));
  143.    
  144.     // Create default display of 640x480
  145.     Display.create();
  146.   }
  147.  
  148.   /**
  149.    * Runs the game (the "main loop")
  150.    */
  151.   private static void run() {
  152.  
  153.     while (!finished) {
  154.       // Always call Window.update(), all the time - it does some behind the
  155.       // scenes work, and also displays the rendered output
  156.       Display.update();
  157.  
  158.       // Check for close requests
  159.       if (Display.isCloseRequested()) {
  160.     finished = true;
  161.       }
  162.  
  163.       // The window is in the foreground, so we should play the game
  164.       else if (Display.isActive()) {
  165.         logic();
  166.         render();
  167.         Display.sync(FRAMERATE);
  168.       }
  169.  
  170.       // The window is not in the foreground, so we can allow other stuff to run and
  171.       // infrequently update
  172.       else {
  173.         try {
  174.           Thread.sleep(100);
  175.         } catch (InterruptedException e) {
  176.         }
  177.         logic();
  178.  
  179.     // Only bother rendering if the window is visible or dirty
  180.         if (Display.isVisible() || Display.isDirty()) {
  181.           render();
  182.         }
  183.       }
  184.     }
  185.   }
  186.  
  187.   /**
  188.    * Do any game-specific cleanup
  189.    */
  190.   private static void cleanup() {
  191.     // Close the window
  192.     Display.destroy();
  193.   }
  194.  
  195.   /**
  196.    * Do all calculations, handle input, etc.
  197.    */
  198.   private static void logic() {
  199.     // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
  200.     if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
  201.       finished = true;
  202.     }
  203.  
  204.     // Rotate the square
  205.    
  206.   }
  207. }