Advertisement
Guest User

LWJGL Crash Issue

a guest
Oct 1st, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. //-------LWJGLTest.java---------
  2.  
  3. import org.lwjgl.*;
  4. import org.lwjgl.glfw.*;
  5. import org.lwjgl.opengl.*;
  6.  
  7. import static org.lwjgl.glfw.Callbacks.*;
  8. import static org.lwjgl.glfw.GLFW.*;
  9. import static org.lwjgl.opengl.GL11.*;
  10. import static org.lwjgl.system.MemoryUtil.*;
  11.  
  12. public class LWJGLTest extends Thread{
  13.     // The window handle
  14.     private long window;
  15.  
  16.     public void run() {
  17.         new LWJGLTest();
  18.         System.out.println("Hello LWJGL " + Version.getVersion() + "!");
  19.  
  20.         try {
  21.             init();
  22.             loop();
  23.             System.out.println("ouch");
  24.  
  25.             // Free the window callbacks and destroy the window
  26.             glfwFreeCallbacks(window);
  27.             glfwDestroyWindow(window);
  28.         } finally {
  29.             // Terminate GLFW and free the error callback
  30.             System.out.println("ouch");
  31.  
  32.             glfwTerminate();
  33.             glfwSetErrorCallback(null).free();
  34.         }
  35.     }
  36.  
  37.     private void init() {
  38.         // Setup an error callback. The default implementation
  39.         // will print the error message in System.err.
  40.         GLFWErrorCallback.createPrint(System.err).set();
  41.  
  42.         // Initialize GLFW. Most GLFW functions will not work before doing this.
  43.         if ( !glfwInit() )
  44.             throw new IllegalStateException("Unable to initialize GLFW");
  45.  
  46.         // Configure our window
  47.         glfwDefaultWindowHints(); // optional, the current window hints are already the default
  48.         glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
  49.         glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
  50.  
  51.         int WIDTH = 300;
  52.         int HEIGHT = 300;
  53.  
  54.         // Create the window
  55.         window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
  56.         if ( window == NULL )
  57.             throw new RuntimeException("Failed to create the GLFW window");
  58.  
  59.         // Setup a key callback. It will be called every time a key is pressed, repeated or released.
  60.         glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
  61.             if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
  62.                 glfwSetWindowShouldClose(window, true); // We will detect this in our rendering loop
  63.         });
  64.  
  65.         // Get the resolution of the primary monitor
  66.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  67.         // Center our window
  68.         glfwSetWindowPos(
  69.             window,
  70.             (vidmode.width() - WIDTH) / 2,
  71.             (vidmode.height() - HEIGHT) / 2
  72.         );
  73.  
  74.         // Make the OpenGL context current
  75.         glfwMakeContextCurrent(window);
  76.         // Enable v-sync
  77.         glfwSwapInterval(1);
  78.  
  79.         // Make the window visible
  80.         glfwShowWindow(window);
  81.     }
  82.     private float color = 1.0f;
  83.     private void loop() {
  84.         // This line is critical for LWJGL's interoperation with GLFW's
  85.         // OpenGL context, or any context that is managed externally.
  86.         // LWJGL detects the context that is current in the current thread,
  87.         // creates the GLCapabilities instance and makes the OpenGL
  88.         // bindings available for use.
  89.         GL.createCapabilities();
  90.         // Set the clear color
  91.  
  92.         // Run the rendering loop until the user has attempted to close
  93.         // the window or has pressed the ESCAPE key.
  94.         boolean increment = true;
  95.         while ( !glfwWindowShouldClose(window) ) {
  96.             if (increment) {
  97.                 color += 0.01f;
  98.             } else {
  99.                 color -= 0.01f;
  100.             }
  101.             if (color > 1) {
  102.                 color = 1;
  103.                 increment = false;
  104.             }
  105.             if (color < 0) {
  106.                 color = 0;
  107.                 increment = true;
  108.             }
  109.             System.out.println("running...");
  110.             glClearColor(color, (1f - color), 0.0f, 0.0f);
  111.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
  112.  
  113.             glfwSwapBuffers(window); // swap the color buffers
  114.             // Poll for window events. The key callback above will only be
  115.             // invoked during this call.
  116.             glfwPollEvents();
  117.  
  118.         }
  119.     }
  120.  
  121.     public static void main(String[] args) {
  122.         //Test class contains a 5 second delay followed by loading ColorModel
  123.         TestClass test = new TestClass();
  124.         test.start();
  125.         new LWJGLTest().run();
  126.     }
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. //---------TestClass.java
  139.  
  140.  
  141.  
  142.  
  143.  
  144. import java.awt.image.ColorModel;
  145.  
  146. public class TestClass extends Thread{
  147.     public void run() {
  148.         try {
  149.             Thread.sleep(5000);
  150.         } catch (InterruptedException e) {
  151.             // TODO Auto-generated catch block
  152.             e.printStackTrace();
  153.         }
  154.         System.out.println("About to run ColorModel.getRGBdefault()");
  155.         try {
  156.             ColorModel.getRGBdefault();
  157.         } catch (Exception e) {
  158.             e.printStackTrace();
  159.         }
  160.        
  161.         System.out.println("Ran successfully");
  162.         try {
  163.             Thread.sleep(5000);
  164.         } catch (InterruptedException e) {
  165.             // TODO Auto-generated catch block
  166.             e.printStackTrace();
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement