com.bossletsplays.borage.engine.core.Display.java Source code

Java tutorial

Introduction

Here is the source code for com.bossletsplays.borage.engine.core.Display.java

Source

/*   Copyright 2016 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.bossletsplays.borage.engine.core;

import static org.lwjgl.glfw.GLFW.*;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;

/**
 * <strong>Project:</strong> BORAGE <br>
 * <strong>File:</strong> Display.java [Class]
 * 
 * <p>
 * This class manages the display window for the game.
 * </p>
 *
 * @author Matthew Rogers
 * @since @VERSION@
 */
public class Display {

    /** The title of the window. */
    private static String title;

    /** The width of the window. */
    private static int width;

    /** The height of the window. */
    private static int height;

    /** The pointer or handle to the window. */
    private static long window;

    /**
     * Creates the window and enables the OpenGL context.
     *
     * @param title The title of the window
     * @param width The width of the window
     * @param height The height of the window
     * @author Matthew Rogers
     * @since @VERSION@
     */
    public static void create(String title, int width, int height) {
        Display.title = title;
        Display.width = width;
        Display.height = height;

        // Callback to catch and print GLFW errors
        glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err));

        // Initialize GLFW, exit the game if it fails
        if (glfwInit() == GLFW_FALSE) {
            System.err.println("GLFW failed to initialize");
            destroy();
            System.exit(1);
        }

        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // This needs to be false so we can move and center the window
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

        // Create the window with the desired dimensions and height.
        // The first 0 parameter will be taken care of when we implement fullscreen
        window = glfwCreateWindow(width, height, title, 0, 0);

        // Center the window
        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);

        // Set the OpenGL context created with the window to be the context used by the current (main) thread
        glfwMakeContextCurrent(window);
        glfwShowWindow(window); // Make the window visible
        GL.createCapabilities(); // LWJGL needs to find the OpenGL context created by GLFW and enable the OpenGL bindings so we can use them
    }

    /**
     * Destroys any resources GLFW or the Display has allocated.
     *
     * @author Matthew Rogers
     * @since @VERSION@
     */
    public static void destroy() {
        glfwTerminate();
    }

    /**
     * Updates the Display to render the output to the screen as well as polls
     * any pending GLFW events.
     *
     * @author Matthew Rogers
     * @since @VERSION@
     */
    public static void update() {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    /**
     * Checks if a window close was requested.
     *
     * @return true, if a window close was requested
     * @author Matthew Rogers
     * @since @VERSION@
     */
    public static boolean isCloseRequested() {
        return glfwWindowShouldClose(window) == GLFW_TRUE;
    }

}