net.bryanbergen.lwjgl.display.Window.java Source code

Java tutorial

Introduction

Here is the source code for net.bryanbergen.lwjgl.display.Window.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.bryanbergen.lwjgl.display;

import net.bryanbergen.lwjgl.display.primitives.Dimension;
import net.bryanbergen.lwjgl.input.keyboard.KeyListener;
import net.bryanbergen.lwjgl.input.keyboard.Keyboard;
import net.bryanbergen.lwjgl.input.mouse.Mouse;
import net.bryanbergen.lwjgl.input.mouse.MouseListener;
import org.lwjgl.glfw.GLFW;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengles.GLES20.GL_FALSE;
import static org.lwjgl.opengles.GLES20.GL_TRUE;
import static org.lwjgl.system.MemoryUtil.NULL;

/**
 *
 * @author bryan
 */
public class Window {

    public static class Builder {

        private final Dimension dimension;
        private String title = "";
        private Color background = Color.BLACK;
        private boolean vsync;
        private boolean fullscreen;
        private boolean visible;
        private boolean resizable;

        public Builder(Dimension dimension) {
            this.dimension = dimension;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setBackground(Color background) {
            this.background = background;
            return this;
        }

        public Builder setVsync(boolean vsync) {
            this.vsync = vsync;
            return this;
        }

        public Builder setFullscreen(boolean fullscreen) {
            this.fullscreen = fullscreen;
            return this;
        }

        public Builder setVisible(boolean visible) {
            this.visible = visible;
            return this;
        }

        public Builder setResizable(boolean resizable) {
            this.resizable = resizable;
            return this;
        }

        public Window build() {
            return new Window(this);
        }
    }

    private long windowHandle;

    private String title;
    private Dimension dimension;
    private Color background;
    private boolean vsync;
    private boolean fullscreen;
    private boolean visible;
    private boolean resizable;

    private Keyboard keyboard;
    private Mouse mouse;
    private GLFWErrorCallback errorCallback;
    private GLFWFramebufferSizeCallback framebufferSizeCallback;
    private GLFWWindowSizeCallback windowSizeCallback;

    private Window(Builder builder) {
        this.title = builder.title;
        this.dimension = builder.dimension;
        this.vsync = builder.vsync;
        this.fullscreen = builder.fullscreen;
        this.visible = builder.visible;
        this.resizable = builder.resizable;
        this.background = builder.background;
        initGLFW();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Color getBackground() {
        return background;
    }

    public void setBackground(Color background) {
        this.background = background;
        GL11.glClearColor((float) background.getRed(), (float) background.getGreen(), (float) background.getBlue(),
                (float) background.getAlpha());
    }

    public Dimension getDimension() {
        return dimension;
    }

    public void setDimension(Dimension dimension) {
        this.dimension = dimension;
        GLFW.glfwSetWindowSize(windowHandle, dimension.getWidth(), dimension.getHeight());
    }

    public boolean isVsync() {
        return vsync;
    }

    public void setVsync(boolean vsync) {
        this.vsync = vsync;
        GLFW.glfwSwapInterval(vsync ? 1 : 0);
    }

    public boolean isFullscreen() {
        return fullscreen;
    }

    public void setFullscreen(boolean fullscreen) {
        //TODO(Bryan) add full screen adjustment logic later
        this.fullscreen = fullscreen;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
        if (visible) {
            GLFW.glfwShowWindow(windowHandle);
        } else {
            GLFW.glfwHideWindow(windowHandle);
        }
    }

    public boolean isResizable() {
        return resizable;
    }

    public void clear() {
        GL11.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    public void update() {
        keyboard.update();
        GLFW.glfwSwapBuffers(windowHandle);
        GLFW.glfwPollEvents();
    }

    public void dispose() {
        keyboard.release();
        mouse.release();
        //        mouseButtonCallback.release();
        //        cursorPositionCallback.release();
        framebufferSizeCallback.release();
        windowSizeCallback.release();
        GLFW.glfwTerminate();
        errorCallback.release();
    }

    public boolean shouldClose() {
        return GLFW.glfwWindowShouldClose(windowHandle) == GL_TRUE;
    }

    public void close() {
        GLFW.glfwSetWindowShouldClose(windowHandle, GL_TRUE);
    }

    public void addKeyListener(KeyListener listener) {
        keyboard.registerListener(listener);
    }

    public void removeKeyListener(KeyListener listener) {
        keyboard.unRegisterListener(listener);
    }

    public void addMouseListener(MouseListener listener) {
        mouse.registerMouseListener(listener);
    }

    public void removeMouseListener(MouseListener listener) {
        mouse.unRegisterMouseListener(listener);
    }

    private void initGLFW() {
        if (GLFW.glfwInit() != GL_TRUE) {
            throw new IllegalStateException("Failed to initialize GLFW");
        }

        GLFW.glfwDefaultWindowHints();
        GLFW.glfwWindowHint(GLFW_VISIBLE, visible ? GL_TRUE : GL_FALSE);
        GLFW.glfwWindowHint(GLFW_RESIZABLE, resizable ? GL_TRUE : GL_FALSE);

        windowHandle = GLFW.glfwCreateWindow(dimension.getWidth(), dimension.getHeight(), title,
                fullscreen ? GLFW.glfwGetPrimaryMonitor() : NULL, NULL);

        if (windowHandle == NULL) {
            throw new RuntimeException("Failed to initialized GLFW Window");
        }

        GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(windowHandle, (vidmode.width() - dimension.getWidth()) / 2,
                (vidmode.height() - dimension.getHeight()) / 2);

        GLFW.glfwMakeContextCurrent(windowHandle);

        initCallbacks();

        GLFW.glfwSwapInterval(vsync ? 1 : 0);
        GL.createCapabilities();

        GL11.glClearColor((float) background.getRed(), (float) background.getGreen(), (float) background.getBlue(),
                (float) background.getAlpha());

    }

    private void initCallbacks() {
        errorCallback = GLFWErrorCallback.createPrint();
        errorCallback.set();

        framebufferSizeCallback = new GLFWFramebufferSizeCallback() {
            @Override
            public void invoke(long window, int width, int height) {
                GL11.glViewport(0, 0, width, height);
            }
        };
        framebufferSizeCallback.set(windowHandle);

        windowSizeCallback = new GLFWWindowSizeCallback() {
            @Override
            public void invoke(long window, int width, int height) {
                dimension.setWidth(width);
                dimension.setHeight(height);
            }
        };
        windowSizeCallback.set(windowHandle);
        keyboard = new Keyboard();
        keyboard.set(windowHandle);
        mouse = new Mouse();
        mouse.set(windowHandle);
    }
}