From ede46ff7a0967fc420c449eb8fffa280c2772147 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Tue, 20 May 2025 03:16:08 +0300 Subject: [PATCH] added imgui and config file to project --- CMakeLists.txt | 26 ++++++++++++++++++++++- imgui.ini | 28 +++++++++++++++++++++++++ include/galaxy.h | 7 ++++--- include/gconfig.h | 18 ++++++++++++++++ include/gmenu.h | 20 ++++++++++++++++++ include/grenderer.h | 11 ++++++++-- include/gshader.h | 4 +++- src/galaxy.cpp | 5 ++++- src/gconfig.cpp | 16 +++++++++++++++ src/gmenu.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/grenderer.cpp | 16 +++++++++++++-- src/gshader.cpp | 5 ----- 12 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 imgui.ini create mode 100644 include/gconfig.h create mode 100644 include/gmenu.h create mode 100644 src/gconfig.cpp create mode 100644 src/gmenu.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e7a1fe8..5cd0709 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,17 +21,40 @@ FetchContent_Declare( GIT_REPOSITORY https://github.com/Dav1dde/glad.git GIT_TAG v0.1.36 ) -FetchContent_MakeAvailable(glfw glad) +FetchContent_Declare( + imgui + GIT_REPOSITORY https://github.com/ocornut/imgui.git + GIT_TAG v1.91.9 +) + +FetchContent_MakeAvailable(glfw glad imgui) + + +set(IMGUI_SOURCES + ${imgui_SOURCE_DIR}/imgui.cpp + ${imgui_SOURCE_DIR}/imgui_demo.cpp + ${imgui_SOURCE_DIR}/imgui_draw.cpp + ${imgui_SOURCE_DIR}/imgui_tables.cpp + ${imgui_SOURCE_DIR}/imgui_widgets.cpp +) +set(IMGUI_BACKEND_SOURCES + ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp + ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp +) set(SOURCES src/galaxy.cpp src/gwindow.cpp src/grenderer.cpp + src/gmenu.cpp + src/gconfig.cpp src/logger.cpp src/gshader.cpp src/vertex.cpp src/todo.cpp ${GLAD_OUT_DIR}/src/glad.c + ${IMGUI_SOURCES} + ${IMGUI_BACKEND_SOURCES} ) set(GLAD_PROFILE "core" CACHE STRING "glad profile (core or compatibility)") @@ -41,6 +64,7 @@ configure_file(include/env-vars.h.in ${CMAKE_BINARY_DIR}/generated/include/env-v set(INCLUDE_DIRS ${CMAKE_BINARY_DIR}/generated/include # env-vars.h ${GLAD_INCLUDE_DIRS} # glad/glad.h + ${imgui_SOURCE_DIR} ) add_library(galaxy-engine-lib ${SOURCES}) diff --git a/imgui.ini b/imgui.ini new file mode 100644 index 0000000..54a3528 --- /dev/null +++ b/imgui.ini @@ -0,0 +1,28 @@ +[Window][Debug##Default] +Pos=60,60 +Size=400,400 + +[Window][Dear ImGui Demo] +Pos=650,20 +Size=550,680 + +[Window][Example: Console] +Pos=59,60 +Size=520,600 + +[Window][Example: Custom rendering] +Pos=60,60 +Size=623,414 + +[Window][Dear ImGui Demo/ResizableChild_478B81A3] +IsChild=1 +Size=499,136 + +[Window][Dear ImGui Demo/Red_BEEF922B] +IsChild=1 +Size=200,100 + +[Window][Default menu] +Pos=60,60 +Size=367,117 + diff --git a/include/galaxy.h b/include/galaxy.h index a058249..f798269 100644 --- a/include/galaxy.h +++ b/include/galaxy.h @@ -1,6 +1,6 @@ #pragma once -#include "gshader.h" +#include "gconfig.h" #include "grenderer.h" #include "gwindow.h" #include "logger.h" @@ -9,8 +9,8 @@ class Galaxy { public: Galaxy(); ~Galaxy(); - Galaxy(GWindow *w, GRenderer *r, Logger *l) - : window(w), renderer(r), logger(l) {}; + Galaxy(GWindow *w, GRenderer *r, Logger *l, GConfig *c) + : window(w), renderer(r), logger(l), config(c) {}; void init(); void run(); @@ -18,6 +18,7 @@ private: Logger *logger; GWindow *window; GRenderer *renderer; + GConfig *config; }; static void glfw_errorCallback(int error, const char *description); diff --git a/include/gconfig.h b/include/gconfig.h new file mode 100644 index 0000000..bc16467 --- /dev/null +++ b/include/gconfig.h @@ -0,0 +1,18 @@ +#pragma once + +#include "logger.h" + +class GConfig { +public: + GConfig(Logger *l); + ~GConfig(); + + void update_render_wireframe(); + + bool render_wireframe; + bool show_demo_menu; + bool show_menus; + +private: + Logger *logger; +}; diff --git a/include/gmenu.h b/include/gmenu.h new file mode 100644 index 0000000..90b9077 --- /dev/null +++ b/include/gmenu.h @@ -0,0 +1,20 @@ +#pragma once + +#include "gconfig.h" +#include "gwindow.h" +#include "logger.h" + +class GMenu { +public: + GMenu(Logger *l, GWindow *w, GConfig *c) : logger(l), window(w), config(c) {}; + ~GMenu() {}; + + void init(); + void update(); + void default_menu(); + +private: + Logger *logger; + GWindow *window; + GConfig *config; +}; diff --git a/include/grenderer.h b/include/grenderer.h index 086ad8b..954a910 100644 --- a/include/grenderer.h +++ b/include/grenderer.h @@ -1,20 +1,27 @@ #pragma once -#include "gwindow.h" +#include "gconfig.h" +#include "gmenu.h" #include "gshader.h" +#include "gwindow.h" #include "logger.h" class GRenderer { public: - GRenderer(GWindow *w, Logger *l); + GRenderer(GWindow *w, Logger *l, GConfig *c); ~GRenderer(); void init(); void update(); + void init_imgui(); + void update_imgui(); + private: Logger *logger; + GConfig *config; GWindow *window; GShader *shader; + GMenu *menu; unsigned int VAO; unsigned int VBO; diff --git a/include/gshader.h b/include/gshader.h index 81c6f32..f60f144 100644 --- a/include/gshader.h +++ b/include/gshader.h @@ -1,10 +1,11 @@ #pragma once #include "logger.h" +#include "gconfig.h" class GShader { public: - GShader(Logger *l) : logger(l), vertex_shader(0), fragment_shader(0) {}; + GShader(Logger *l, GConfig *c) : logger(l), config(c), vertex_shader(0), fragment_shader(0) {}; ~GShader(); void init(); @@ -15,6 +16,7 @@ public: private: Logger *logger; + GConfig *config; unsigned int vertex_shader; unsigned int fragment_shader; unsigned int program; diff --git a/src/galaxy.cpp b/src/galaxy.cpp index 94faf55..f301ec3 100644 --- a/src/galaxy.cpp +++ b/src/galaxy.cpp @@ -1,5 +1,6 @@ #include "logger.h" #include "galaxy.h" +#include "gconfig.h" const int W_WIDTH = 800; const int W_HEIGHT = 600; @@ -9,8 +10,9 @@ Galaxy::Galaxy() { this->logger = logger; LOG_INFO("Starting the engine..."); + GConfig *config = new GConfig(logger); GWindow *window = new GWindow(W_WIDTH, W_HEIGHT, logger); - GRenderer *renderer = new GRenderer(window, logger); + GRenderer *renderer = new GRenderer(window, logger, config); this->window = window; this->renderer = renderer; @@ -23,6 +25,7 @@ Galaxy::~Galaxy() { delete this->renderer; delete this->window; + delete this->config; delete this->logger; } diff --git a/src/gconfig.cpp b/src/gconfig.cpp new file mode 100644 index 0000000..5367baa --- /dev/null +++ b/src/gconfig.cpp @@ -0,0 +1,16 @@ +#include "glad/glad.h" + +#include "gconfig.h" +#include "logger.h" + +GConfig::GConfig(Logger *l) + : logger(l), render_wireframe(0), show_menus(1), show_demo_menu(0) {} +GConfig::~GConfig() {}; + +void GConfig::update_render_wireframe() { + // Test variable to enable wireframe rendering + if (this->render_wireframe) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +} diff --git a/src/gmenu.cpp b/src/gmenu.cpp new file mode 100644 index 0000000..bdd512b --- /dev/null +++ b/src/gmenu.cpp @@ -0,0 +1,50 @@ +#include "backends/imgui_impl_glfw.h" +#include "backends/imgui_impl_opengl3.h" +#include "imgui.h" + +#include "gmenu.h" + +void GMenu::init() { + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + io.ConfigFlags |= + ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= + ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL( + this->window->getWindow(), + true); // Second param install_callback=true will install GLFW callbacks + // and chain to existing ones. + ImGui_ImplOpenGL3_Init(); +} + +void GMenu::default_menu() { + ImGui::Begin("Default menu"); + ImGui::Text("Inside this menu you can edit the config variables"); + + ImGui::Checkbox("Render only wireframes", &this->config->render_wireframe); + ImGui::Checkbox("Show demo menu", &this->config->show_demo_menu); + ImGui::Checkbox("Show menus in general", &this->config->show_menus); + + ImGui::End(); +} + +void GMenu::update() { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + if (this->config->show_demo_menu) + ImGui::ShowDemoWindow(); // Show demo window! :) + + if (this->config->show_menus) + this->default_menu(); + + this->config->update_render_wireframe(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} diff --git a/src/grenderer.cpp b/src/grenderer.cpp index f43938f..fa468c7 100644 --- a/src/grenderer.cpp +++ b/src/grenderer.cpp @@ -1,6 +1,8 @@ #include "glad/glad.h" #include "env-vars.h" +#include "gconfig.h" +#include "gmenu.h" #include "grenderer.h" #include "gshader.h" #include "logger.h" @@ -17,13 +19,16 @@ unsigned int indices[] = { 1, 2, 3 // second triangle }; -GRenderer::GRenderer(GWindow *w, Logger *l) : window(w), logger(l), VAO(0), VBO(0) { - this->shader = new GShader(l); +GRenderer::GRenderer(GWindow *w, Logger *l, GConfig *c) + : window(w), logger(l), config(c), VAO(0), VBO(0) { + this->shader = new GShader(l, c); + this->menu = new GMenu(l, w, c); } GRenderer::~GRenderer() { LOG_DEBUG("Shutting down renderer..."); delete this->shader; + delete this->menu; } void GRenderer::init() { @@ -47,9 +52,13 @@ void GRenderer::init() { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + + if (this->config->show_menus) + this->menu->init(); } void GRenderer::update() { + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -57,5 +66,8 @@ void GRenderer::update() { glBindVertexArray(this->VAO); glDrawArrays(GL_TRIANGLES, 0, 3); + if (this->config->show_menus) + this->menu->update(); + glfwSwapBuffers(this->window->getWindow()); } diff --git a/src/gshader.cpp b/src/gshader.cpp index e206756..ba28c14 100644 --- a/src/gshader.cpp +++ b/src/gshader.cpp @@ -36,11 +36,6 @@ void GShader::init() { this->program = program; glUseProgram(this->program); - - // Test variable to enable wireframe rendering - if (WIREFRAME_RENDERING) { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } }; GShader::~GShader() {