package com.javamarcher.window; import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; import static org.lwjgl.glfw.GLFW.glfwCreateWindow; import static org.lwjgl.glfw.GLFW.glfwDestroyWindow; import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor; import static org.lwjgl.glfw.GLFW.glfwGetVideoMode; import static org.lwjgl.glfw.GLFW.glfwInit; import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent; import static org.lwjgl.glfw.GLFW.glfwPollEvents; import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback; import static org.lwjgl.glfw.GLFW.glfwSetWindowPos; import static org.lwjgl.glfw.GLFW.glfwShowWindow; import static org.lwjgl.glfw.GLFW.glfwSwapBuffers; import static org.lwjgl.glfw.GLFW.glfwSwapInterval; import static org.lwjgl.glfw.GLFW.glfwTerminate; import static org.lwjgl.glfw.GLFW.glfwWindowHint; import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; import static org.lwjgl.opengl.GL11.glClear; import static org.lwjgl.opengl.GL11.glClearColor; import static org.lwjgl.system.MemoryUtil.NULL; import java.awt.event.WindowEvent; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; import com.javamarcher.gamestate.MainState; import com.javamarcher.gamestate.Notifiable; import com.javamarcher.util.DeltaTimeUtil; public class MainWindow { public static final int WIDTH = 1280; public static final int HEIGHT = 720; public static long WINDOW = 0; // The window handle private long window; // Game States private final Notifiable mainState = new MainState(); // Options window private final OptionWindow optionWindow = new OptionWindow("Options"); private static final DeltaTimeUtil deltaTimeUtil = new DeltaTimeUtil(); public void run() { try { initialize(); update(); // Free the window callbacks and destroy the window glfwFreeCallbacks(window); glfwDestroyWindow(window); } finally { // Terminate GLFW and free the error callback glfwTerminate(); glfwSetErrorCallback(null).free(); } } private void initialize() { // Setup an error callback. The default implementation // will print the error message in System.err. GLFWErrorCallback.createPrint(System.err).set(); // Initialize GLFW. Most GLFW functions will not work before doing this. if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); // Configure our window glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); // Create the window window = glfwCreateWindow(WIDTH, HEIGHT, "Java Marcher", NULL, NULL); WINDOW = window; if (window == NULL) throw new RuntimeException("Failed to create the GLFW window"); // Get the resolution of the primary monitor GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // Center our window glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2); // Make the OpenGL context current glfwMakeContextCurrent(window); // Enable v-sync glfwSwapInterval(1); // Make the window visible glfwShowWindow(window); } private void update() { GL.createCapabilities(); initializeStates(); glClearColor(0.4f, 0.4f, 0.4f, 0.0f); // Render until ESCAPE is pressed while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); updateStates(); glfwSwapBuffers(window); // swap the color buffers // Poll for window events. The key callback above will only be // invoked during this call. glfwPollEvents(); } optionWindow.dispatchEvent(new WindowEvent(optionWindow, WindowEvent.WINDOW_CLOSING)); // Destroy if loop ends mainState.destroy(); } private void initializeStates() { mainState.start(); } private void updateStates() { deltaTimeUtil.calculateDeltaTime(); mainState.update(deltaTimeUtil.getDeltaTime()); } public static double getDeltaTime() { return deltaTimeUtil.getDeltaTime(); } public static double getGlobalTime() { return deltaTimeUtil.getGlobalTime(); } }