Advertisement
SrinjoySS01

Untitled

Oct 26th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package me.srinjoy.engine;
  2.  
  3. import org.lwjgl.Version;
  4. import org.lwjgl.glfw.GLFWErrorCallback;
  5. import org.lwjgl.opengl.GL;
  6.  
  7. import java.util.Objects;
  8.  
  9. import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
  10. import static org.lwjgl.opengl.GL11.*;
  11. import static org.lwjgl.system.MemoryUtil.*;
  12. import static org.lwjgl.glfw.GLFW.*;
  13.  
  14. public class Window {
  15.     private final int width;
  16.     private final int height;
  17.     private final String title;
  18.     private static Window window = null;
  19.     private long glfwWindow;
  20.     private float r, g, b, a;
  21.     private boolean fadeToBlack = false;
  22.  
  23.     private Window() {
  24.         this.width = 1366;
  25.         this.height = 768;
  26.         this.title = "Minecraft";
  27.         r = 1;
  28.         g = 1;
  29.         b = 1;
  30.         a = 1;
  31.     }
  32.  
  33.     public static Window getInstance(){
  34.         return window == null ? window = new Window() : window;
  35.     }
  36.  
  37.     public void create() {
  38.         System.out.println("LWJGL " + Version.getVersion() + "!");
  39.         init();
  40.         loop();
  41.         glfwFreeCallbacks(glfwWindow);
  42.         glfwDestroyWindow(glfwWindow);
  43.         glfwTerminate();
  44.         Objects.requireNonNull(glfwSetErrorCallback(null)).free();
  45.     }
  46.  
  47.     private void loop() {
  48.         while (!glfwWindowShouldClose(glfwWindow)){
  49.             glfwPollEvents();
  50.             glClearColor(r, g, b, a);
  51.             glClear(GL_COLOR_BUFFER_BIT);
  52.             glfwSwapBuffers(glfwWindow);
  53.             String str = title + " \t" + (char) (Math.random() * 1000);
  54.             if (fadeToBlack) {
  55.                 r = Math.max(r - 0.01f, 0);
  56.                 g = Math.max(g - 0.01f, 0);
  57.                 b = Math.max(b - 0.01f, 0);
  58.                 a = Math.max(a - 0.01f, 0);
  59.             }
  60.             if (KeyListener.isKeyPressed(GLFW_KEY_W))
  61.                 fadeToBlack = true;
  62.  
  63.             glfwSetWindowTitle(glfwWindow, str);
  64.         }
  65.     }
  66.  
  67.     private void init() {
  68.         GLFWErrorCallback.createPrint(System.err).set();
  69.         if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW.");
  70.         // window properties
  71.         glfwDefaultWindowHints();
  72.         glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
  73.         glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  74.         glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
  75.  
  76.         // creating the window
  77.         glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);
  78.         if (glfwWindow == NULL) throw new IllegalStateException("Failed to create the GLFW window!");
  79.  
  80.         // setting callbacks
  81.         glfwSetCursorPosCallback(glfwWindow, MouseListener::mousePosCallback);
  82.         glfwSetMouseButtonCallback(glfwWindow, MouseListener::mouseButtonCallback);
  83.         glfwSetScrollCallback(glfwWindow, MouseListener::mouseScrollCallback);
  84.         glfwSetKeyCallback(glfwWindow, KeyListener::keyCallback);
  85.  
  86.         glfwMakeContextCurrent(glfwWindow);
  87.         glfwSwapInterval(1);
  88.         glfwShowWindow(glfwWindow);
  89.  
  90.         GL.createCapabilities();
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement