com.valkrix.engine.Application.java Source code

Java tutorial

Introduction

Here is the source code for com.valkrix.engine.Application.java

Source

/*
 * This file is part of Game.
 *
 * Game is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Game is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.valkrix.engine;

import com.valkrix.ModelLoader;
import com.valkrix.ui.Window;
import org.lwjgl.opengl.GL;

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

/**
 * @author Nicholas Bailey
 */
public abstract class Application implements AutoCloseable {
    protected Window window;
    protected Keyboard keyboard;
    protected ModelLoader modelLoader;
    private boolean running;

    public Application() {
        if (!glfwInit()) {
            throw new RuntimeException("Unable to initialize GLFW.");
        }

        initContext();
    }

    public void start(String title, int width, int height, boolean fullscreen) {
        initContext();
        window = new Window(title, width, height, fullscreen);
        window.makeCurrent();
        GL.createCapabilities();
        init();

        running = true;
        float delta = 1.0f / 60.0f;
        while (isRunning()) {
            update(delta);
            display();

            window.swapBuffers();
            glfwPollEvents();
        }
    }

    public final boolean isRunning() {
        return running && !window.shouldClose();
    }

    @Override
    public final void close() {
        cleanup();

        running = false;

        if (modelLoader != null) {
            modelLoader.close();
        }

        glfwTerminate();
    }

    /**
     * This function is called before the {@code Window} is created in order
     * to setup the OpenGL context. All settings meant for the GLFW {@code Window}
     * should be set in a derived class in this function.
     * <p>
     * Example:
     * <code>
     *
     * @Override protected void initContext() {
     * glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     * glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     * glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
     * }
     * </code>
     */
    protected void initContext() {
        glfwDefaultWindowHints();
    }

    protected void init() {
    }

    protected void cleanup() {

    }

    protected Window getWindow() {
        return window;
    }

    protected Keyboard getKeyboard() {
        return keyboard;
    }

    protected abstract void update(float delta);

    protected abstract void display();
}