diff --git a/imgui.ini b/imgui.ini new file mode 100644 index 0000000..1329d8c --- /dev/null +++ b/imgui.ini @@ -0,0 +1,26 @@ +[Window][Debug##Default] +Pos=60,60 +Size=400,400 +Collapsed=0 + +[Window][Terrain Controls] +Pos=178,120 +Size=568,264 +Collapsed=0 + +[Window][DockSpaceViewport_11111111] +Size=1280,720 +Collapsed=0 + +[Window][DockSpace Demo] +Size=1280,720 +Collapsed=0 + +[Window][TerraVisor] +Pos=0,0 +Size=1280,720 +Collapsed=0 + +[Docking][Data] +DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 CentralNode=1 + diff --git a/source/main.cpp b/source/main.cpp index f2081d2..59af58d 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,19 +1,25 @@ #include #include #include +#include +#include +#include #include #include #include "scene.h" +const int TARGET_FPS = 60; +const auto FRAME_DURATION = std::chrono::milliseconds(1000 / TARGET_FPS); + int main(){ if (!glfwInit()) { std::cerr << "Failed to initialize GLFW" << std::endl; return -1; } - GLFWwindow* window = glfwCreateWindow(800, 600, "TerraVisor", nullptr, nullptr); + GLFWwindow* window = glfwCreateWindow(1280, 720, "TerraVisor", nullptr, nullptr); if(!window) { std::cerr << "Failed to create GLFW window" << std::endl; glfwTerminate(); @@ -34,12 +40,28 @@ int main(){ ImGui::CreateContext(); ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 150"); + + auto lastFrameTime = std::chrono::high_resolution_clock::now(); while (!glfwWindowShouldClose(window)) { + auto frameStart = std::chrono::high_resolution_clock::now(); + Scene::Idle(); Scene::Display(window); glfwPollEvents(); + + auto frameEnd = std::chrono::high_resolution_clock::now(); + auto frameDuration = frameEnd - frameStart; + + if (frameDuration < FRAME_DURATION) { + std::this_thread::sleep_for(FRAME_DURATION - frameDuration); + } + + auto currentFrameTime = std::chrono::high_resolution_clock::now(); + auto actualFrameDuration = std::chrono::duration_cast(currentFrameTime - lastFrameTime).count(); + + lastFrameTime = currentFrameTime; } // Cleanup ImGui diff --git a/source/scene.cpp b/source/scene.cpp index 77712e4..63fa0f7 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -1,6 +1,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -9,10 +13,76 @@ #include "Scene.h" void Scene::DrawGUI(GLFWwindow* window) { + // Begin ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + + // Enable docking + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + ImGui::NewFrame(); + + static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; + + // Add menu bar + ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; + + // Style windows + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->WorkPos); + ImGui::SetNextWindowSize(viewport->WorkSize); + ImGui::SetNextWindowViewport(viewport->ID); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; + window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; + window_flags |= ImGuiWindowFlags_NoBackground; + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + ImGui::Begin("TerraVisor", nullptr, window_flags); + ImGui::PopStyleVar(3); + + // Submit the DockSpace + if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) + { + ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); + ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); + } + + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Options")) + { + ImGui::Text("Nothing Here Yet, Check Back Later!"); + + ImGui::EndMenu(); + } + + ImGui::EndMenuBar(); + } + + ImGui::End(); + + //Draw Gui + ImGui::Begin("Terrain Controls"); + if (ImGui::Button("Quit")) + { + glfwSetWindowShouldClose(window, GLFW_TRUE); + } + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::End(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } void Scene::Display(GLFWwindow* window) { glClear(GL_COLOR_BUFFER_BIT); + + Scene::DrawGUI(window); + glfwSwapBuffers(window); }