Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / org / example / Window.java

package org.example;

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Window {
    private static Window window;
    private int width = 800;
    private int height = 600;
    String title = "HelloWorld";
    private long glfwWindow;
    private final List<IGameComponent> iGameComponents = new ArrayList();

    private Window() {
    }

    public static Window get() {
        if (window == null) {
            window = new Window();
        }

        return window;
    }

    public void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");
        init();
        loop();
        glfwFreeCallbacks(this.glfwWindow);
        glfwDestroyWindow(this.glfwWindow);
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }

    private void init() {
        GLFWErrorCallback.createPrint(System.err).set();
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
        glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE);

        this.glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);
        if ( this.glfwWindow == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> {
            if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
                glfwSetWindowShouldClose(window, true);
        });

        // Get the thread stack and push a new frame
        try ( MemoryStack stack = stackPush() ) {
            IntBuffer pWidth = stack.mallocInt(1); // int*
            IntBuffer pHeight = stack.mallocInt(1); // int*

            // Get the window size passed to glfwCreateWindow
            glfwGetWindowSize(this.glfwWindow, pWidth, pHeight);

            // Get the resolution of the primary monitor
            GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

            // Center the window
            glfwSetWindowPos(
                    this.glfwWindow,
                    (vidmode.width() - pWidth.get(0)) / 2,
                    (vidmode.height() - pHeight.get(0)) / 2
            );
        }


        glfwMakeContextCurrent(this.glfwWindow);
        glfwSwapInterval(1);
        glfwShowWindow(this.glfwWindow);

        //--------------------------------------------------------------
        KeyInput.init(this.glfwWindow);
        iGameComponents.add(new ColorController());
        for (IGameComponent gameComponent : iGameComponents) {
            gameComponent.init();
        }
        //--------------------------------------------------------------
    }

    private void loop() {
        GL.createCapabilities();

        while (!glfwWindowShouldClose(this.glfwWindow)) {
            //---------------------------------------------------------------
            // 毎フレームのキー状態を更新
            KeyInput.updateKeyStates();

            //GameComponentのupdate処理
            for (IGameComponent gameComponents : iGameComponents) {
                gameComponents.update();
            }
            //---------------------------------------------------------------

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア
            glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換
            glfwPollEvents(); // ウィンドウイベントをポーリング
        }

    }

}