diff options
-rw-r--r-- | src/demo.cpp | 48 | ||||
-rw-r--r-- | src/imgui_context.cpp | 28 | ||||
-rw-r--r-- | src/imgui_context.hpp | 11 | ||||
-rw-r--r-- | src/sdl_context.hpp | 2 |
4 files changed, 48 insertions, 41 deletions
diff --git a/src/demo.cpp b/src/demo.cpp index 8e4181a..64d75a7 100644 --- a/src/demo.cpp +++ b/src/demo.cpp @@ -1,10 +1,10 @@ #include "demo.hpp" +#include "gl_context.hpp" +#include "imgui_context.hpp" #include "sdl_context.hpp" #include "sdl_main_window.hpp" #include "stb_image.h" -#include "gl_context.hpp" -#include "imgui_context.hpp" #include <SDL.h> #include <SDL_opengl.h> @@ -85,9 +85,9 @@ bool LoadTextureFromFile(const fud::String& filename, GLuint* out_texture, int* std::vector<char> fileData{}; fileData.resize(fileSize); - auto readStatus = inFile.read(fileData.data(), fileSize, fileSize); - if (readStatus != FileStatus::Success) { - spdlog::error("bad read {} {}", filename.c_str(), FileStatusToString(readStatus)); + auto readResult = inFile.read(fileData.data(), fileSize, fileSize); + if (readResult.status != FileStatus::Success) { + spdlog::error("bad read {} {}", filename.c_str(), FileStatusToString(readResult.status)); return false; } @@ -97,30 +97,15 @@ bool LoadTextureFromFile(const fud::String& filename, GLuint* out_texture, int* int demo(const fud::String& m_filename) { - // Setup SDL SdlContext sdlContext{}; SdlMainWindow mainWindow{sdlContext}; - // Create window with graphics context GlContext glContext{mainWindow}; - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - // ImGui::StyleColorsLight(); + ImguiContext imguiContext{glContext, sdlContext, mainWindow}; // Setup Platform/Renderer backends - ImGui_ImplSDL2_InitForOpenGL(mainWindow.window(), glContext.context()); - ImGui_ImplOpenGL3_Init(sdlContext.glslVersion()); - int my_image_width = 0; int my_image_height = 0; GLuint my_image_texture = 0; @@ -156,14 +141,7 @@ int demo(const fud::String& m_filename) // Main loop bool done = false; -#ifdef __EMSCRIPTEN__ - // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the - // imgui.ini file. You may manually call LoadIniSettingsFromMemory() to load settings from your own storage. - io.IniFilename = nullptr; - EMSCRIPTEN_MAINLOOP_BEGIN -#else while (!done) -#endif { // Poll and handle events (inputs, window resize, etc.) // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your @@ -231,7 +209,8 @@ int demo(const fud::String& m_filename) ImGui::SameLine(); ImGui::Text("counter = %d", counter); - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + auto framerate = imguiContext.getIO().Framerate; + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / framerate, framerate); ImGui::End(); } @@ -248,7 +227,8 @@ int demo(const fud::String& m_filename) // Rendering ImGui::Render(); - glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); + const auto& imguiIO = imguiContext.getIO(); + glViewport(0, 0, static_cast<int>(imguiIO.DisplaySize.x), static_cast<int>(imguiIO.DisplaySize.y)); glClearColor( clear_color.x * clear_color.w, clear_color.y * clear_color.w, @@ -258,14 +238,6 @@ int demo(const fud::String& m_filename) ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); SDL_GL_SwapWindow(mainWindow.window()); } -#ifdef __EMSCRIPTEN__ - EMSCRIPTEN_MAINLOOP_END; -#endif - - // Cleanup - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplSDL2_Shutdown(); - ImGui::DestroyContext(); // return ImageResult::okay(image); diff --git a/src/imgui_context.cpp b/src/imgui_context.cpp index 7d972fd..49b74ca 100644 --- a/src/imgui_context.cpp +++ b/src/imgui_context.cpp @@ -1,14 +1,40 @@ #include "imgui_context.hpp" +#include <imgui/imgui_impl_opengl3.h> +#include <imgui/imgui_impl_sdl2.h> + namespace bookmouse { -ImguiContext::ImguiContext() +ImguiContext::ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMainWindow& mainWindow) { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + + setIOFlag(ImGuiConfigFlags_NavEnableKeyboard); + setIOFlag(ImGuiConfigFlags_NavEnableGamepad); + + ImGui::StyleColorsDark(); + // ImGui::StyleColorsLight(); + + ImGui_ImplSDL2_InitForOpenGL(mainWindow.window(), glContext.context()); + ImGui_ImplOpenGL3_Init(sdlContext.glslVersion()); } ImguiContext::~ImguiContext() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); } +void ImguiContext::setIOFlag(ImGuiConfigFlags_ flag) +{ + ImGui::GetIO().ConfigFlags |= flag; +} + +const ImGuiIO& ImguiContext::getIO() +{ + return ImGui::GetIO(); +} } // namespace bookmouse diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp index a625865..520a711 100644 --- a/src/imgui_context.hpp +++ b/src/imgui_context.hpp @@ -1,13 +1,22 @@ #ifndef IMGUI_CONTEXT_HPP #define IMGUI_CONTEXT_HPP +#include "sdl_context.hpp" +#include "sdl_main_window.hpp" +#include "gl_context.hpp" +#include <imgui/imgui.h> + namespace bookmouse { class ImguiContext { public: - ImguiContext(); + ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMainWindow& mainWindow); ~ImguiContext(); + const ImGuiIO& getIO(); + + void setIOFlag(ImGuiConfigFlags_ flag); + private: }; diff --git a/src/sdl_context.hpp b/src/sdl_context.hpp index 7b96df7..77aad72 100644 --- a/src/sdl_context.hpp +++ b/src/sdl_context.hpp @@ -12,7 +12,7 @@ class SdlContext { int setAttribute(SDL_GLattr attr, int value) const; - consteval const char* glslVersion() + constexpr const char* glslVersion() { #if defined(IMGUI_IMPL_OPENGL_ES2) // GL ES 2.0 + GLSL 100 |