package io.gitlab.jfronny.inceptum; import imgui.ImGui; import imgui.ImGuiIO; import imgui.flag.ImGuiConfigFlags; import imgui.gl3.ImGuiImplGl3; import imgui.glfw.ImGuiImplGlfw; import io.gitlab.jfronny.inceptum.frontend.cli.CommandArgs; import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta; import io.gitlab.jfronny.inceptum.util.ConfigHolder; import io.gitlab.jfronny.inceptum.util.MetaHolder; import io.gitlab.jfronny.inceptum.util.Utils; import io.gitlab.jfronny.inceptum.util.api.account.AccountManager; import io.gitlab.jfronny.inceptum.frontend.gui.window.Window; import io.gitlab.jfronny.inceptum.util.mds.ModsDirScanner; import org.lwjgl.glfw.Callbacks; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL32; import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryUtil; import java.io.IOException; import java.io.InputStream; import java.nio.IntBuffer; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; public class InceptumGui { public static final Set WINDOWS = new LinkedHashSet<>(); private static final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw(); private static final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3(); /** * Pointer to the native GLFW window. */ protected static long handle; private static String glslVersion = null; public static void main(CommandArgs args, Runnable exec) { AccountManager.loadAccounts(); Utils.LOGGER.info("Initializing UI"); try { for (Path path : Utils.ls(MetaHolder.INSTANCE_DIR)) { if (Files.exists(path.resolve("instance.json"))) ModsDirScanner.get(path.resolve("mods"), Utils.loadObject(path.resolve("instance.json"), InstanceMeta.class)).start(); } } catch (IOException e) { Utils.LOGGER.error("Could not initialize MDS"); } init(); exec.run(); run(); dispose(); } public static void open(Window window) { WINDOWS.add(window); } /** * Method to initialize application. */ public static void init() { initWindow(); initImGui(); imGuiGlfw.init(handle, true); imGuiGl3.init(glslVersion); } /** * Method to dispose all used application resources and destroy its window. */ public static void dispose() { imGuiGl3.dispose(); imGuiGlfw.dispose(); Callbacks.glfwFreeCallbacks(handle); GLFW.glfwDestroyWindow(handle); GLFW.glfwTerminate(); Objects.requireNonNull(GLFW.glfwSetErrorCallback(null)).free(); } /** * Method to create and initialize GLFW window. */ protected static void initWindow() { GLFWErrorCallback.createPrint(System.err).set(); if (!GLFW.glfwInit()) { throw new IllegalStateException("Unable to initialize GLFW"); } decideGlGlslVersions(); GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE); GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE); handle = GLFW.glfwCreateWindow(10, 10, "Inceptum", MemoryUtil.NULL, MemoryUtil.NULL); if (handle == MemoryUtil.NULL) { throw new RuntimeException("Failed to create the GLFW window"); } try (MemoryStack stack = MemoryStack.stackPush()) { final IntBuffer pWidth = stack.mallocInt(1); // int* final IntBuffer pHeight = stack.mallocInt(1); // int* GLFW.glfwGetWindowSize(handle, pWidth, pHeight); final GLFWVidMode vidmode = Objects.requireNonNull(GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor())); GLFW.glfwSetWindowPos(handle, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2); } GLFW.glfwMakeContextCurrent(handle); GL.createCapabilities(); GLFW.glfwSwapInterval(GLFW.GLFW_TRUE); clearBuffer(); renderBuffer(); } private static void decideGlGlslVersions() { final boolean isMac = System.getProperty("os.name").toLowerCase().contains("mac"); if (isMac) { glslVersion = "#version 150"; GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); // 3.2+ only GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); // Required on Mac } else { glslVersion = "#version 130"; GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 0); } } /** * Method to initialize Dear ImGui context. Could be overridden to do custom Dear ImGui setup before application start. */ protected static void initImGui() { ImGui.createContext(); ImGuiIO io = ImGui.getIO(); io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable); io.setConfigViewportsNoAutoMerge(true); try (InputStream is = Inceptum.class.getClassLoader().getResourceAsStream("font.ttf")) { assert is != null; io.setFontDefault(io.getFonts().addFontFromMemoryTTF(is.readAllBytes(), 16f)); } catch (IOException e) { Utils.LOGGER.error("Could not load font", e); } applyTheme(); } /** * Main application loop. */ public static void run() { while (!GLFW.glfwWindowShouldClose(handle)) { //frame clearBuffer(); imGuiGlfw.newFrame(); ImGui.newFrame(); //render if (WINDOWS.isEmpty()) exit(); else { for (Window window : WINDOWS.toArray(new Window[0])) { if (window.isNew()) window.preFirstDraw(); String title = window.getName() + "##" + System.identityHashCode(window); if (ImGui.begin(title, window.getOpenState(), window.getFlags())) window.draw(); ImGui.end(); if (!window.getOpenState().get() && !window.isClosed()) window.close(); } } //end frame ImGui.render(); imGuiGl3.renderDrawData(ImGui.getDrawData()); if (ImGui.getIO().hasConfigFlags(ImGuiConfigFlags.ViewportsEnable)) { final long backupWindowPtr = GLFW.glfwGetCurrentContext(); ImGui.updatePlatformWindows(); ImGui.renderPlatformWindowsDefault(); GLFW.glfwMakeContextCurrent(backupWindowPtr); } renderBuffer(); } } private static void clearBuffer() { GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT); } private static void renderBuffer() { GLFW.glfwSwapBuffers(handle); GLFW.glfwPollEvents(); } public static void exit() { GLFW.glfwSetWindowShouldClose(handle, true); } public static void applyTheme() { if (ConfigHolder.CONFIG.darkTheme) ImGui.styleColorsDark(); else ImGui.styleColorsLight(); } }