Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package lwjgltest;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.BorderLayout;
  5. import java.awt.Canvas;
  6. import org.lwjgl.LWJGLException;
  7. import org.lwjgl.opengl.Display;
  8. import org.lwjgl.opengl.GL11;
  9. import org.newdawn.slick.Color;
  10. import org.newdawn.slick.opengl.Texture;
  11. import org.newdawn.slick.opengl.TextureLoader;
  12.  
  13. public class GearsApplet extends Applet
  14. {
  15.  
  16.     /** The Canvas where the LWJGL Display is added */
  17.     Canvas m_Display_Parent;
  18.     int m_width, m_height;
  19.     /** Thread which runs the main game loop */
  20.     Thread m_GameThread;
  21.     /** is the game loop running */
  22.     boolean m_Running;
  23.     private Texture m_Texture;
  24.     boolean m_KeyDown;
  25.  
  26.     /**
  27.      * Once the Canvas is created its add notify method will call this method to
  28.      * start the LWJGL Display and game loop in another thread.
  29.      */
  30.     public void startLWJGL()
  31.     {System.out.println("2 " );
  32.         m_GameThread = new Thread()
  33.         {
  34.  
  35.             public void run()
  36.             {
  37.                 m_Running = true;
  38.                 try
  39.                 {
  40.                     Display.setParent(m_Display_Parent);
  41.                     //Display.setVSyncEnabled(true);
  42.                     Display.create();
  43.                     initGL();
  44.                     render();
  45.                 }
  46.                 catch (LWJGLException e)
  47.                 {
  48.                     e.printStackTrace();
  49.                 }
  50.  
  51.             }
  52.         };
  53.         m_GameThread.start();
  54.     }
  55.  
  56.     /**
  57.      * Tell game loop to stop running, after which the LWJGL Display will be destoryed.
  58.      * The main thread will wait for the Display.destroy() to complete
  59.      */
  60.     private void stopLWJGL()
  61.     {
  62.         m_Running = false;
  63.         try
  64.         {
  65.             m_GameThread.join();
  66.         }
  67.         catch (InterruptedException e)
  68.         {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.  
  73.     public void start()
  74.     {
  75.     }
  76.  
  77.     public void stop()
  78.     {
  79.     }
  80.  
  81.     /**
  82.      * Applet Destroy method will remove the canvas, before canvas is destroyed it will notify
  83.      * stopLWJGL() to stop main game loop and to destroy the Display
  84.      */
  85.     public void destroy()
  86.     {
  87.         remove(m_Display_Parent);
  88.         super.destroy();
  89.         System.out.println("Clear up");
  90.     }
  91.  
  92.     /**
  93.      * initialise applet by adding a canvas to it, this canvas will start the LWJGL Display and game loop
  94.      * in another thread. It will also stop the game loop and destroy the display on canvas removal when
  95.      * applet is destroyed.
  96.      */
  97.     public void init()
  98.     {
  99. System.out.println("1 " );
  100.         setLayout(new BorderLayout());
  101.         try
  102.         {
  103.             m_Display_Parent = new Canvas()
  104.             {
  105.  
  106.                 @Override
  107.                 public void addNotify()
  108.                 {
  109.                     super.addNotify();
  110.                     startLWJGL();
  111.                 }
  112.  
  113.                 @Override
  114.                 public void removeNotify()
  115.                 {
  116.                     stopLWJGL();
  117.                     super.removeNotify();
  118.                 }
  119.             };
  120.             m_width = getWidth();
  121.             m_height = getHeight();
  122.             m_Display_Parent.setSize(getWidth(), getHeight());
  123.             add(m_Display_Parent);
  124.             m_Display_Parent.setFocusable(true);
  125.             m_Display_Parent.requestFocus();
  126.             m_Display_Parent.setIgnoreRepaint(true);
  127.             //setResizable(true);
  128.             setVisible(true);
  129.         }
  130.         catch (Exception e)
  131.         {
  132.             System.err.println(e);
  133.             throw new RuntimeException("Unable to create display");
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * draw a quad with the image on it
  139.      */
  140.     public void render()
  141.     {
  142.         System.out.println("4 " );
  143.         Color.white.bind();
  144.         m_Texture.bind();
  145.         GL11.glBegin(GL11.GL_QUADS);
  146.         GL11.glTexCoord2f(0, 0);
  147.         GL11.glVertex2f(100, 100);
  148.         GL11.glTexCoord2f(1, 0);
  149.         GL11.glVertex2f(100 + m_Texture.getTextureWidth(), 100);
  150.         GL11.glTexCoord2f(1, 1);
  151.         GL11.glVertex2f(100 + m_Texture.getTextureWidth(), 100 + m_Texture.getTextureHeight());
  152.         GL11.glTexCoord2f(0, 1);
  153.         GL11.glVertex2f(100, 100 + m_Texture.getTextureHeight());
  154.         GL11.glEnd();
  155.     }
  156.  
  157.     protected void initGL()
  158.     {
  159.         try
  160.         {
  161.             System.out.println("3 " );
  162.             m_Texture = TextureLoader.getTexture("PNG", Thread.currentThread().getContextClassLoader().getResourceAsStream("./resource/infy.png"));
  163.             System.out.println("Texture loaded: " + m_Texture);
  164.             System.out.println(">> Image width: " + m_Texture.getImageWidth());
  165.             System.out.println(">> Image height: " + m_Texture.getImageWidth());
  166.             System.out.println(">> Texture width: " + m_Texture.getTextureWidth());
  167.             System.out.println(">> Texture height: " + m_Texture.getTextureHeight());
  168.             System.out.println(">> Texture ID: " + m_Texture.getTextureID());
  169.         }
  170.         catch (Exception e)
  171.         {
  172.             System.out.println("Problem loading texture");
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement