feat: Change docking settings, refactor code and add comments

This commit is contained in:
coderloff 2024-04-26 20:28:01 +04:00
parent 813c744a12
commit b23af2882c

View File

@ -5,9 +5,9 @@
#include "imgui_impl_glfw.h" #include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3.h"
// Global variables
std::string log_message; std::string log_message;
bool show_demo_window = true; int display_w = 800, display_h = 600;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
void debug(std::string message){ void debug(std::string message){
@ -17,9 +17,9 @@ void debug(std::string message){
void show_menu(){ void show_menu(){
if(ImGui::BeginMainMenuBar()){ if(ImGui::BeginMainMenuBar()){
if(ImGui::BeginMenu("File")){ if(ImGui::BeginMenu("File")){
if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */ } if (ImGui::MenuItem("Open..", "Ctrl+O")) { }
if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */ } if (ImGui::MenuItem("Save", "Ctrl+S")) { }
if (ImGui::MenuItem("Close", "Ctrl+W")) { show_another_window = false; } if (ImGui::MenuItem("Close", "Ctrl+W")) { }
ImGui::EndMenu(); ImGui::EndMenu();
} }
ImGui::EndMainMenuBar(); ImGui::EndMainMenuBar();
@ -70,14 +70,12 @@ void show_inspector(){
ImGui::Begin("Inspector"); ImGui::Begin("Inspector");
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) ImGui::Text("This is some useful text.");
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color ImGui::ColorEdit3("clear color", (float*)&clear_color);
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) if (ImGui::Button("Button"))
counter++; counter++;
ImGui::SameLine(); ImGui::SameLine();
ImGui::Text("counter = %d", counter); ImGui::Text("counter = %d", counter);
@ -86,12 +84,10 @@ void show_inspector(){
} }
int main(void){ int main(void){
GLFWwindow* window;
if(!glfwInit()) if(!glfwInit())
return -1; return -1;
window = glfwCreateWindow(640, 480, "Ferx", NULL, NULL); GLFWwindow* window = glfwCreateWindow(display_w, display_h, "Ferx", NULL, NULL);
if(!window){ if(!window){
glfwTerminate(); glfwTerminate();
return -1; return -1;
@ -108,72 +104,55 @@ int main(void){
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGuiStyle& style = ImGui::GetStyle();
ImVec4* colors = style.Colors;
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
while(!glfwWindowShouldClose(window)){ while(!glfwWindowShouldClose(window)){
glClear(GL_COLOR_BUFFER_BIT); // Set up draw data for rendering
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::DockSpaceOverViewport(); // Setup docking
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
colors[ImGuiCol_DockingEmptyBg] = clear_color;
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). // Show windows
// if (show_demo_window)
// ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
show_menu(); show_menu();
show_hierarchy(); show_hierarchy();
show_scene(); show_scene();
show_project(); show_project();
show_console(); show_console();
show_inspector(); show_inspector();
// 3. Show another simple window.
if (show_another_window)
{
ImGui::Begin("Another Window", &show_another_window,ImGuiWindowFlags_MenuBar); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
if (ImGui::BeginMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */ }
if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */ }
if (ImGui::MenuItem("Close", "Ctrl+W")) { show_another_window = false; }
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
ImGui::Text("Hello from another window!");
ImGui::End();
}
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h); glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h); glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::EndFrame();
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents();
} }
glfwTerminate(); // Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0; return 0;
} }