Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.srinjoy.engine;
- import org.lwjgl.Version;
- import org.lwjgl.glfw.GLFWErrorCallback;
- import org.lwjgl.opengl.GL;
- import java.util.Objects;
- import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.system.MemoryUtil.*;
- import static org.lwjgl.glfw.GLFW.*;
- public class Window {
- private final int width;
- private final int height;
- private final String title;
- private static Window window = null;
- private long glfwWindow;
- private float r, g, b, a;
- private boolean fadeToBlack = false;
- private Window() {
- this.width = 1366;
- this.height = 768;
- this.title = "Minecraft";
- r = 1;
- g = 1;
- b = 1;
- a = 1;
- }
- public static Window getInstance(){
- return window == null ? window = new Window() : window;
- }
- public void create() {
- System.out.println("LWJGL " + Version.getVersion() + "!");
- init();
- loop();
- glfwFreeCallbacks(glfwWindow);
- glfwDestroyWindow(glfwWindow);
- glfwTerminate();
- Objects.requireNonNull(glfwSetErrorCallback(null)).free();
- }
- private void loop() {
- while (!glfwWindowShouldClose(glfwWindow)){
- glfwPollEvents();
- glClearColor(r, g, b, a);
- glClear(GL_COLOR_BUFFER_BIT);
- glfwSwapBuffers(glfwWindow);
- String str = title + " \t" + (char) (Math.random() * 1000);
- if (fadeToBlack) {
- r = Math.max(r - 0.01f, 0);
- g = Math.max(g - 0.01f, 0);
- b = Math.max(b - 0.01f, 0);
- a = Math.max(a - 0.01f, 0);
- }
- if (KeyListener.isKeyPressed(GLFW_KEY_W))
- fadeToBlack = true;
- glfwSetWindowTitle(glfwWindow, str);
- }
- }
- private void init() {
- GLFWErrorCallback.createPrint(System.err).set();
- if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW.");
- // window properties
- glfwDefaultWindowHints();
- glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
- glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
- glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
- // creating the window
- glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);
- if (glfwWindow == NULL) throw new IllegalStateException("Failed to create the GLFW window!");
- // setting callbacks
- glfwSetCursorPosCallback(glfwWindow, MouseListener::mousePosCallback);
- glfwSetMouseButtonCallback(glfwWindow, MouseListener::mouseButtonCallback);
- glfwSetScrollCallback(glfwWindow, MouseListener::mouseScrollCallback);
- glfwSetKeyCallback(glfwWindow, KeyListener::keyCallback);
- glfwMakeContextCurrent(glfwWindow);
- glfwSwapInterval(1);
- glfwShowWindow(glfwWindow);
- GL.createCapabilities();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement