matyklug

Window.java

Jul 8th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package engine.io;
  2.  
  3. import org.joml.Vector3f;
  4. import org.lwjgl.glfw.GLFW;
  5. import org.lwjgl.glfw.GLFWVidMode;
  6. import org.lwjgl.glfw.GLFWWindowSizeCallback;
  7. import org.lwjgl.opengl.GL;
  8. import org.lwjgl.opengl.GL11;
  9.  
  10. public class Window {
  11.     private int width,height;
  12.     private String title;
  13.     private long window;
  14.     private Input input;
  15.     private Vector3f background;
  16.     private GLFWWindowSizeCallback sizeCallback;
  17.     private boolean isResized;
  18.     private boolean isFullscreen;
  19.     private int xpos, ypos;
  20.    
  21.     public int frames;
  22.     public static long time;
  23.    
  24.     public Window(int width, int height, String title) {
  25.         this.width=width;
  26.         this.height=height;
  27.         this.title=title;
  28.     }
  29.    
  30.     public void create() {
  31.         if(!GLFW.glfwInit()) {
  32.             System.err.println("Cant init GLFW.");
  33.             return;
  34.         }
  35.         input = new Input();
  36.         window = GLFW.glfwCreateWindow(width, height, title, isFullscreen ? GLFW.glfwGetPrimaryMonitor() : 0, 0);
  37.         if(window == 0) {
  38.             System.err.println("Cant create window.");
  39.                 return;
  40.         }
  41.        
  42.         GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
  43.         xpos = (vidMode.width() - width) / 2;
  44.         ypos = (vidMode.height() - height) / 2;
  45.         GLFW.glfwSetWindowPos(window, xpos, ypos);
  46.        
  47.         GLFW.glfwMakeContextCurrent(window);
  48.         GL.createCapabilities();
  49.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  50.        
  51.         createCallbacks();
  52.        
  53.         GLFW.glfwShowWindow(window);
  54.        
  55.         GLFW.glfwSwapInterval(1);
  56.        
  57.         time = System.currentTimeMillis();
  58.     }
  59.    
  60.     public void update() {
  61.         if(isResized) {
  62.             GL11.glViewport(0, 0, width, height);
  63.             isResized=false;
  64.         }
  65.         GL11.glClearColor(background.x, background.y, background.z, 1.0f);
  66.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  67.         GLFW.glfwPollEvents();
  68.         frames++;
  69.         if(System.currentTimeMillis() > time + 1000) {
  70.             GLFW.glfwSetWindowTitle(window, title + " | FPS: " + frames);
  71.             time = System.currentTimeMillis();
  72.             frames = 0;
  73.         }
  74.     }
  75.    
  76.     public void swapBuffers() {
  77.         GLFW.glfwSwapBuffers(window);
  78.     }
  79.    
  80.     private void createCallbacks() {
  81.         sizeCallback = new GLFWWindowSizeCallback() {
  82.            
  83.             @Override
  84.             public void invoke(long window, int w, int h) {
  85.                 width = w;
  86.                 height = h;
  87.                 isResized = true;
  88.             }
  89.         };
  90.        
  91.         GLFW.glfwSetKeyCallback(window,input.getKeyboardCallback());
  92.         GLFW.glfwSetCursorPosCallback(window,input.getCursorCallback());
  93.         GLFW.glfwSetMouseButtonCallback(window,input.getMouseCallback());
  94.         GLFW.glfwSetWindowSizeCallback(window, sizeCallback);
  95.         GLFW.glfwSetScrollCallback(window, input.getMouseScrollCallback());
  96.     }
  97.    
  98.     public void destroy() {
  99.         input.destroy();
  100.         sizeCallback.free();
  101.         GLFW.glfwWindowShouldClose(window);
  102.         GLFW.glfwDestroyWindow(window);
  103.         GLFW.glfwTerminate();
  104.     }
  105.    
  106.     public boolean shouldClose() {
  107.         return GLFW.glfwWindowShouldClose(window);
  108.     }
  109.    
  110.     public void setBackgroundColor(Vector3f color) {
  111.         background=color;
  112.     }
  113.  
  114.     public int getWidth() {
  115.         return width;
  116.     }
  117.  
  118.     public int getHeight() {
  119.         return height;
  120.     }
  121.  
  122.     public String getTitle() {
  123.         return title;
  124.     }
  125.  
  126.     public long getWindow() {
  127.         return window;
  128.     }
  129.  
  130.     public boolean isFullscreen() {
  131.         return isFullscreen;
  132.     }
  133.  
  134.     public void setFullscreen(boolean isFullscreen) {
  135.         this.isFullscreen = isFullscreen;
  136.         isResized = true;
  137.         int[] xposa = {
  138.             xpos
  139.         };
  140.         int[] yposa = {
  141.                 ypos
  142.             };
  143.         if(isFullscreen) {
  144.             GLFW.glfwGetWindowPos(window, xposa, yposa);
  145.             GLFW.glfwSetWindowMonitor(window, GLFW.glfwGetPrimaryMonitor(), 0, 0, width, height, 0);
  146.         } else {
  147.             GLFW.glfwSetWindowMonitor(window, 0, xpos, ypos, width, height, 0);
  148.         }
  149.     }
  150.  
  151.    
  152.    
  153. }
Add Comment
Please, Sign In to add comment