Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package engine.io;
- import org.joml.Vector3f;
- import org.lwjgl.glfw.GLFW;
- import org.lwjgl.glfw.GLFWVidMode;
- import org.lwjgl.glfw.GLFWWindowSizeCallback;
- import org.lwjgl.opengl.GL;
- import org.lwjgl.opengl.GL11;
- public class Window {
- private int width,height;
- private String title;
- private long window;
- private Input input;
- private Vector3f background;
- private GLFWWindowSizeCallback sizeCallback;
- private boolean isResized;
- private boolean isFullscreen;
- private int xpos, ypos;
- public int frames;
- public static long time;
- public Window(int width, int height, String title) {
- this.width=width;
- this.height=height;
- this.title=title;
- }
- public void create() {
- if(!GLFW.glfwInit()) {
- System.err.println("Cant init GLFW.");
- return;
- }
- input = new Input();
- window = GLFW.glfwCreateWindow(width, height, title, isFullscreen ? GLFW.glfwGetPrimaryMonitor() : 0, 0);
- if(window == 0) {
- System.err.println("Cant create window.");
- return;
- }
- GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
- xpos = (vidMode.width() - width) / 2;
- ypos = (vidMode.height() - height) / 2;
- GLFW.glfwSetWindowPos(window, xpos, ypos);
- GLFW.glfwMakeContextCurrent(window);
- GL.createCapabilities();
- GL11.glEnable(GL11.GL_DEPTH_TEST);
- createCallbacks();
- GLFW.glfwShowWindow(window);
- GLFW.glfwSwapInterval(1);
- time = System.currentTimeMillis();
- }
- public void update() {
- if(isResized) {
- GL11.glViewport(0, 0, width, height);
- isResized=false;
- }
- GL11.glClearColor(background.x, background.y, background.z, 1.0f);
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- GLFW.glfwPollEvents();
- frames++;
- if(System.currentTimeMillis() > time + 1000) {
- GLFW.glfwSetWindowTitle(window, title + " | FPS: " + frames);
- time = System.currentTimeMillis();
- frames = 0;
- }
- }
- public void swapBuffers() {
- GLFW.glfwSwapBuffers(window);
- }
- private void createCallbacks() {
- sizeCallback = new GLFWWindowSizeCallback() {
- @Override
- public void invoke(long window, int w, int h) {
- width = w;
- height = h;
- isResized = true;
- }
- };
- GLFW.glfwSetKeyCallback(window,input.getKeyboardCallback());
- GLFW.glfwSetCursorPosCallback(window,input.getCursorCallback());
- GLFW.glfwSetMouseButtonCallback(window,input.getMouseCallback());
- GLFW.glfwSetWindowSizeCallback(window, sizeCallback);
- GLFW.glfwSetScrollCallback(window, input.getMouseScrollCallback());
- }
- public void destroy() {
- input.destroy();
- sizeCallback.free();
- GLFW.glfwWindowShouldClose(window);
- GLFW.glfwDestroyWindow(window);
- GLFW.glfwTerminate();
- }
- public boolean shouldClose() {
- return GLFW.glfwWindowShouldClose(window);
- }
- public void setBackgroundColor(Vector3f color) {
- background=color;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public String getTitle() {
- return title;
- }
- public long getWindow() {
- return window;
- }
- public boolean isFullscreen() {
- return isFullscreen;
- }
- public void setFullscreen(boolean isFullscreen) {
- this.isFullscreen = isFullscreen;
- isResized = true;
- int[] xposa = {
- xpos
- };
- int[] yposa = {
- ypos
- };
- if(isFullscreen) {
- GLFW.glfwGetWindowPos(window, xposa, yposa);
- GLFW.glfwSetWindowMonitor(window, GLFW.glfwGetPrimaryMonitor(), 0, 0, width, height, 0);
- } else {
- GLFW.glfwSetWindowMonitor(window, 0, xpos, ypos, width, height, 0);
- }
- }
- }
Add Comment
Please, Sign In to add comment