Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package lwjglgame;
  2.  
  3. import org.lwjgl.*;
  4. import org.lwjgl.glfw.*;
  5. import org.lwjgl.opengl.*;
  6. import org.lwjgl.system.*;
  7.  
  8. import java.nio.*;
  9.  
  10. import static org.lwjgl.glfw.Callbacks.*;
  11. import static org.lwjgl.glfw.GLFW.*;
  12. import static org.lwjgl.opengl.GL11.*;
  13. import static org.lwjgl.system.MemoryStack.*;
  14.  
  15. public class Main {
  16.  
  17.     // The window handle
  18.     private long window;
  19.    
  20.     public void run(){
  21.         System.out.println("lwjgl " + Version.getVersion() + "");
  22.        
  23.         init();
  24.         loop();
  25.        
  26.         glfwFreeCallbacks(window);
  27.         glfwDestroyWindow(window);
  28.        
  29.         glfwTerminate();
  30.         glfwSetErrorCallback(null).free();
  31.     }
  32.    
  33.     private void init(){
  34.         GLFWErrorCallback.createPrint(System.err).set();
  35.        
  36.         if(!glfwInit())
  37.             throw new IllegalStateException("unable to init glfw");
  38.        
  39.         glfwSetKeyCallback(window, (window, key, scancode, action, mode) -> {
  40.             if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
  41.                 glfwSetWindowShouldClose(window, true);
  42.         });
  43.        
  44.         try(MemoryStack stack = stackPush()){
  45.             IntBuffer pwidth = stack.mallocInt(1);
  46.             IntBuffer pheight = stack.mallocInt(1);
  47.            
  48.             glfwGetWindowSize(window, pwidth, pheight);
  49.             GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  50.            
  51.            
  52.             glfwSetWindowPos(
  53.                     window,
  54.                     (vidmode.width() - pwidth.get(0)) / 2,
  55.                     (vidmode.height() - pheight.get(0)) / 2
  56.             );
  57.         }
  58.        
  59.         glfwMakeContextCurrent(window);
  60.         glfwSwapInterval(1);
  61.         glfwShowWindow(window);
  62.     }
  63.    
  64.     private void loop(){
  65.         GL.createCapabilities();
  66.        
  67.         glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  68.        
  69.         while(!glfwWindowShouldClose(window)){
  70.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  71.            
  72.             glfwSwapBuffers(window);
  73.            
  74.             glfwPollEvents();
  75.         }
  76.     }
  77.    
  78.     public static void main(String[] args) {
  79.         new Main().run();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement