added imgui and config file to project

This commit is contained in:
Aaro Koivukangas
2025-05-20 03:16:08 +03:00
parent 8632aa2303
commit ede46ff7a0
12 changed files with 191 additions and 15 deletions
+25 -1
View File
@@ -21,17 +21,40 @@ FetchContent_Declare(
GIT_REPOSITORY https://github.com/Dav1dde/glad.git GIT_REPOSITORY https://github.com/Dav1dde/glad.git
GIT_TAG v0.1.36 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 set(SOURCES
src/galaxy.cpp src/galaxy.cpp
src/gwindow.cpp src/gwindow.cpp
src/grenderer.cpp src/grenderer.cpp
src/gmenu.cpp
src/gconfig.cpp
src/logger.cpp src/logger.cpp
src/gshader.cpp src/gshader.cpp
src/vertex.cpp src/vertex.cpp
src/todo.cpp src/todo.cpp
${GLAD_OUT_DIR}/src/glad.c ${GLAD_OUT_DIR}/src/glad.c
${IMGUI_SOURCES}
${IMGUI_BACKEND_SOURCES}
) )
set(GLAD_PROFILE "core" CACHE STRING "glad profile (core or compatibility)") 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 set(INCLUDE_DIRS
${CMAKE_BINARY_DIR}/generated/include # env-vars.h ${CMAKE_BINARY_DIR}/generated/include # env-vars.h
${GLAD_INCLUDE_DIRS} # glad/glad.h ${GLAD_INCLUDE_DIRS} # glad/glad.h
${imgui_SOURCE_DIR}
) )
add_library(galaxy-engine-lib ${SOURCES}) add_library(galaxy-engine-lib ${SOURCES})
+28
View File
@@ -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
+4 -3
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "gshader.h" #include "gconfig.h"
#include "grenderer.h" #include "grenderer.h"
#include "gwindow.h" #include "gwindow.h"
#include "logger.h" #include "logger.h"
@@ -9,8 +9,8 @@ class Galaxy {
public: public:
Galaxy(); Galaxy();
~Galaxy(); ~Galaxy();
Galaxy(GWindow *w, GRenderer *r, Logger *l) Galaxy(GWindow *w, GRenderer *r, Logger *l, GConfig *c)
: window(w), renderer(r), logger(l) {}; : window(w), renderer(r), logger(l), config(c) {};
void init(); void init();
void run(); void run();
@@ -18,6 +18,7 @@ private:
Logger *logger; Logger *logger;
GWindow *window; GWindow *window;
GRenderer *renderer; GRenderer *renderer;
GConfig *config;
}; };
static void glfw_errorCallback(int error, const char *description); static void glfw_errorCallback(int error, const char *description);
+18
View File
@@ -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;
};
+20
View File
@@ -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;
};
+9 -2
View File
@@ -1,20 +1,27 @@
#pragma once #pragma once
#include "gwindow.h" #include "gconfig.h"
#include "gmenu.h"
#include "gshader.h" #include "gshader.h"
#include "gwindow.h"
#include "logger.h" #include "logger.h"
class GRenderer { class GRenderer {
public: public:
GRenderer(GWindow *w, Logger *l); GRenderer(GWindow *w, Logger *l, GConfig *c);
~GRenderer(); ~GRenderer();
void init(); void init();
void update(); void update();
void init_imgui();
void update_imgui();
private: private:
Logger *logger; Logger *logger;
GConfig *config;
GWindow *window; GWindow *window;
GShader *shader; GShader *shader;
GMenu *menu;
unsigned int VAO; unsigned int VAO;
unsigned int VBO; unsigned int VBO;
+3 -1
View File
@@ -1,10 +1,11 @@
#pragma once #pragma once
#include "logger.h" #include "logger.h"
#include "gconfig.h"
class GShader { class GShader {
public: 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(); ~GShader();
void init(); void init();
@@ -15,6 +16,7 @@ public:
private: private:
Logger *logger; Logger *logger;
GConfig *config;
unsigned int vertex_shader; unsigned int vertex_shader;
unsigned int fragment_shader; unsigned int fragment_shader;
unsigned int program; unsigned int program;
+4 -1
View File
@@ -1,5 +1,6 @@
#include "logger.h" #include "logger.h"
#include "galaxy.h" #include "galaxy.h"
#include "gconfig.h"
const int W_WIDTH = 800; const int W_WIDTH = 800;
const int W_HEIGHT = 600; const int W_HEIGHT = 600;
@@ -9,8 +10,9 @@ Galaxy::Galaxy() {
this->logger = logger; this->logger = logger;
LOG_INFO("Starting the engine..."); LOG_INFO("Starting the engine...");
GConfig *config = new GConfig(logger);
GWindow *window = new GWindow(W_WIDTH, W_HEIGHT, 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->window = window;
this->renderer = renderer; this->renderer = renderer;
@@ -23,6 +25,7 @@ Galaxy::~Galaxy() {
delete this->renderer; delete this->renderer;
delete this->window; delete this->window;
delete this->config;
delete this->logger; delete this->logger;
} }
+16
View File
@@ -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);
}
+50
View File
@@ -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());
}
+14 -2
View File
@@ -1,6 +1,8 @@
#include "glad/glad.h" #include "glad/glad.h"
#include "env-vars.h" #include "env-vars.h"
#include "gconfig.h"
#include "gmenu.h"
#include "grenderer.h" #include "grenderer.h"
#include "gshader.h" #include "gshader.h"
#include "logger.h" #include "logger.h"
@@ -17,13 +19,16 @@ unsigned int indices[] = {
1, 2, 3 // second triangle 1, 2, 3 // second triangle
}; };
GRenderer::GRenderer(GWindow *w, Logger *l) : window(w), logger(l), VAO(0), VBO(0) { GRenderer::GRenderer(GWindow *w, Logger *l, GConfig *c)
this->shader = new GShader(l); : 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() { GRenderer::~GRenderer() {
LOG_DEBUG("Shutting down renderer..."); LOG_DEBUG("Shutting down renderer...");
delete this->shader; delete this->shader;
delete this->menu;
} }
void GRenderer::init() { void GRenderer::init() {
@@ -47,9 +52,13 @@ void GRenderer::init() {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
if (this->config->show_menus)
this->menu->init();
} }
void GRenderer::update() { void GRenderer::update() {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@@ -57,5 +66,8 @@ void GRenderer::update() {
glBindVertexArray(this->VAO); glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
if (this->config->show_menus)
this->menu->update();
glfwSwapBuffers(this->window->getWindow()); glfwSwapBuffers(this->window->getWindow());
} }
-5
View File
@@ -36,11 +36,6 @@ void GShader::init() {
this->program = program; this->program = program;
glUseProgram(this->program); glUseProgram(this->program);
// Test variable to enable wireframe rendering
if (WIREFRAME_RENDERING) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
}; };
GShader::~GShader() { GShader::~GShader() {