Get ImGui docking working

This commit is contained in:
Jack Christensen 2024-08-11 21:20:17 -04:00
parent fc7219d6c9
commit dc89084bf3
3 changed files with 119 additions and 1 deletions

26
imgui.ini Normal file
View File

@ -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

View File

@ -1,19 +1,25 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <chrono>
#include <thread>
#include <imgui.h>
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include "scene.h" #include "scene.h"
const int TARGET_FPS = 60;
const auto FRAME_DURATION = std::chrono::milliseconds(1000 / TARGET_FPS);
int main(){ int main(){
if (!glfwInit()) { if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl; std::cerr << "Failed to initialize GLFW" << std::endl;
return -1; return -1;
} }
GLFWwindow* window = glfwCreateWindow(800, 600, "TerraVisor", nullptr, nullptr); GLFWwindow* window = glfwCreateWindow(1280, 720, "TerraVisor", nullptr, nullptr);
if(!window) { if(!window) {
std::cerr << "Failed to create GLFW window" << std::endl; std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
@ -35,11 +41,27 @@ int main(){
ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 150"); ImGui_ImplOpenGL3_Init("#version 150");
auto lastFrameTime = std::chrono::high_resolution_clock::now();
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
auto frameStart = std::chrono::high_resolution_clock::now();
Scene::Idle(); Scene::Idle();
Scene::Display(window); Scene::Display(window);
glfwPollEvents(); 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<std::chrono::milliseconds>(currentFrameTime - lastFrameTime).count();
lastFrameTime = currentFrameTime;
} }
// Cleanup ImGui // Cleanup ImGui

View File

@ -1,6 +1,10 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@ -9,10 +13,76 @@
#include "Scene.h" #include "Scene.h"
void Scene::DrawGUI(GLFWwindow* window) { 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) { void Scene::Display(GLFWwindow* window) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
Scene::DrawGUI(window);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }