Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / views / Window.java
package gameEngine.views;
import gameEngine.Time;
import gameEngine.input.*;
import gameEngine.scenes.*;

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

import java.nio.*;
import java.util.HashMap;

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;
    public static String resourcePath = "GameEngine/resources/";
    private static Scene currentScene;
    public int width;
    public int height;
    private String title;
    private long glfwWindow;
    private Text fpsText;

    private EditorScene editorScene ;

    private Window() {
        this.width = 1200;
        this.height = 900;
        this.title = "PuniPuniGameEngine";
        init();
    }

    public static void changeScene(int newScene) {
        switch (newScene) {
            case 0:  // EditorScene
                if (window.editorScene == null) {
                    window.editorScene = new EditorScene(window.width, window.height);
                }
                currentScene = window.editorScene;
                break;
            case 1:  // GameScene
                if(currentScene != null && ((EditorScene) currentScene).editorEntities != null){
                    EditorScene editorScene = (EditorScene) currentScene;
                    currentScene = new GameScene(new HashMap<>(editorScene.editorEntities));
                }
                else currentScene = new GameScene();
                break;
            default:
                assert false : "Unknown Scene [" + newScene + "]";
                break;
        }
    }

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

        return window;
    }

    public void runFromEditor() {
        Window.changeScene(0);
        run();
    }

    public void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");
        fpsText = new Text(0,0,"",20);
        loop();
        glfwFreeCallbacks(glfwWindow);
        glfwDestroyWindow(glfwWindow);
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }

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

        //Configure GLFW
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
        glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE);

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

        glfwSetCursorPosCallback(glfwWindow, MouseInput::mousePosCallback);
        glfwSetMouseButtonCallback(glfwWindow, MouseInput::mouseButtonCallback);
        glfwSetScrollCallback(glfwWindow, MouseInput::mouseScrollCallBack);
        glfwSetKeyCallback(glfwWindow, KeyInput::keyCallback);

        // 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(glfwWindow, pWidth, pHeight);

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

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

        glfwMakeContextCurrent(glfwWindow);
        glfwSwapInterval(1);
        glfwShowWindow(glfwWindow);
        GL.createCapabilities();
    }

    private void loop() {
        while (!glfwWindowShouldClose(glfwWindow)) {
            glfwPollEvents();
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            float dt = Time.deltaTime;

            if (dt >= 0) {

                currentScene.update(dt);
                currentScene.processTasks();

                fpsText.setText("" + Time.getFPS());
                fpsText.setColor(Color.WHITE);
                fpsText.update();

                MouseInput.endFrame();
                KeyInput.endFrame();
            }
            glfwSwapBuffers(glfwWindow);
            Time.update();
        }
    }

    public Scene getScene(){
        return currentScene;
    }

}