Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lwjgltest;
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.GL11;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- public class GearsApplet extends Applet
- {
- /** The Canvas where the LWJGL Display is added */
- Canvas m_Display_Parent;
- int m_width, m_height;
- /** Thread which runs the main game loop */
- Thread m_GameThread;
- /** is the game loop running */
- boolean m_Running;
- private Texture m_Texture;
- boolean m_KeyDown;
- /**
- * Once the Canvas is created its add notify method will call this method to
- * start the LWJGL Display and game loop in another thread.
- */
- public void startLWJGL()
- {System.out.println("2 " );
- m_GameThread = new Thread()
- {
- public void run()
- {
- m_Running = true;
- try
- {
- Display.setParent(m_Display_Parent);
- //Display.setVSyncEnabled(true);
- Display.create();
- initGL();
- render();
- }
- catch (LWJGLException e)
- {
- e.printStackTrace();
- }
- }
- };
- m_GameThread.start();
- }
- /**
- * Tell game loop to stop running, after which the LWJGL Display will be destoryed.
- * The main thread will wait for the Display.destroy() to complete
- */
- private void stopLWJGL()
- {
- m_Running = false;
- try
- {
- m_GameThread.join();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- public void start()
- {
- }
- public void stop()
- {
- }
- /**
- * Applet Destroy method will remove the canvas, before canvas is destroyed it will notify
- * stopLWJGL() to stop main game loop and to destroy the Display
- */
- public void destroy()
- {
- remove(m_Display_Parent);
- super.destroy();
- System.out.println("Clear up");
- }
- /**
- * initialise applet by adding a canvas to it, this canvas will start the LWJGL Display and game loop
- * in another thread. It will also stop the game loop and destroy the display on canvas removal when
- * applet is destroyed.
- */
- public void init()
- {
- System.out.println("1 " );
- setLayout(new BorderLayout());
- try
- {
- m_Display_Parent = new Canvas()
- {
- @Override
- public void addNotify()
- {
- super.addNotify();
- startLWJGL();
- }
- @Override
- public void removeNotify()
- {
- stopLWJGL();
- super.removeNotify();
- }
- };
- m_width = getWidth();
- m_height = getHeight();
- m_Display_Parent.setSize(getWidth(), getHeight());
- add(m_Display_Parent);
- m_Display_Parent.setFocusable(true);
- m_Display_Parent.requestFocus();
- m_Display_Parent.setIgnoreRepaint(true);
- //setResizable(true);
- setVisible(true);
- }
- catch (Exception e)
- {
- System.err.println(e);
- throw new RuntimeException("Unable to create display");
- }
- }
- /**
- * draw a quad with the image on it
- */
- public void render()
- {
- System.out.println("4 " );
- Color.white.bind();
- m_Texture.bind();
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glTexCoord2f(0, 0);
- GL11.glVertex2f(100, 100);
- GL11.glTexCoord2f(1, 0);
- GL11.glVertex2f(100 + m_Texture.getTextureWidth(), 100);
- GL11.glTexCoord2f(1, 1);
- GL11.glVertex2f(100 + m_Texture.getTextureWidth(), 100 + m_Texture.getTextureHeight());
- GL11.glTexCoord2f(0, 1);
- GL11.glVertex2f(100, 100 + m_Texture.getTextureHeight());
- GL11.glEnd();
- }
- protected void initGL()
- {
- try
- {
- System.out.println("3 " );
- m_Texture = TextureLoader.getTexture("PNG", Thread.currentThread().getContextClassLoader().getResourceAsStream("./resource/infy.png"));
- System.out.println("Texture loaded: " + m_Texture);
- System.out.println(">> Image width: " + m_Texture.getImageWidth());
- System.out.println(">> Image height: " + m_Texture.getImageWidth());
- System.out.println(">> Texture width: " + m_Texture.getTextureWidth());
- System.out.println(">> Texture height: " + m_Texture.getTextureHeight());
- System.out.println(">> Texture ID: " + m_Texture.getTextureID());
- }
- catch (Exception e)
- {
- System.out.println("Problem loading texture");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement