diff options
Diffstat (limited to 'src/demo.cpp')
-rw-r--r-- | src/demo.cpp | 77 |
1 files changed, 14 insertions, 63 deletions
diff --git a/src/demo.cpp b/src/demo.cpp index c571006..8e4181a 100644 --- a/src/demo.cpp +++ b/src/demo.cpp @@ -1,6 +1,10 @@ #include "demo.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> @@ -13,7 +17,7 @@ #include <spdlog/spdlog.h> #include <vector> -namespace bookworm { +namespace bookmouse { // Simple helper function to load an image into a OpenGL texture with common settings bool LoadTextureFromMemory(const void* data, size_t data_size, GLuint* out_texture, int* out_width, int* out_height) @@ -93,63 +97,13 @@ bool LoadTextureFromFile(const fud::String& filename, GLuint* out_texture, int* int demo(const fud::String& m_filename) { - // Setup SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { - printf("Error: %s\n", SDL_GetError()); - return -1; - } - - // Decide GL+GLSL versions -#if defined(IMGUI_IMPL_OPENGL_ES2) - // GL ES 2.0 + GLSL 100 - const char* glsl_version = "#version 100"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#elif defined(__APPLE__) - // GL 3.2 Core + GLSL 150 - const char* glsl_version = "#version 150"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); -#else - // GL 3.0 + GLSL 130 - const char* glsl_version = "#version 130"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#endif - - // From 2.0.18: Enable native IME. -#ifdef SDL_HINT_IME_SHOW_UI - SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); -#endif + SdlContext sdlContext{}; + SdlMainWindow mainWindow{sdlContext}; // Create window with graphics context - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | - SDL_WINDOW_ALLOW_HIGHDPI); - SDL_Window* window = SDL_CreateWindow( - "Dear ImGui SDL2+OpenGL3 example", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - 1280, - 720, - window_flags); - if (window == nullptr) { - printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); - return -1; - } - SDL_GLContext gl_context = SDL_GL_CreateContext(window); - SDL_GL_MakeCurrent(window, gl_context); - SDL_GL_SetSwapInterval(1); // Enable vsync + GlContext glContext{mainWindow}; // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -164,8 +118,8 @@ int demo(const fud::String& m_filename) // ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - ImGui_ImplSDL2_InitForOpenGL(window, gl_context); - ImGui_ImplOpenGL3_Init(glsl_version); + ImGui_ImplSDL2_InitForOpenGL(mainWindow.window(), glContext.context()); + ImGui_ImplOpenGL3_Init(sdlContext.glslVersion()); int my_image_width = 0; int my_image_height = 0; @@ -226,10 +180,10 @@ int demo(const fud::String& m_filename) if (event.type == SDL_QUIT) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && - event.window.windowID == SDL_GetWindowID(window)) + event.window.windowID == SDL_GetWindowID(mainWindow.window())) done = true; } - if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) { + if (SDL_GetWindowFlags(mainWindow.window()) & SDL_WINDOW_MINIMIZED) { SDL_Delay(10); continue; } @@ -302,7 +256,7 @@ int demo(const fud::String& m_filename) clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - SDL_GL_SwapWindow(window); + SDL_GL_SwapWindow(mainWindow.window()); } #ifdef __EMSCRIPTEN__ EMSCRIPTEN_MAINLOOP_END; @@ -313,12 +267,9 @@ int demo(const fud::String& m_filename) ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); - SDL_GL_DeleteContext(gl_context); - SDL_DestroyWindow(window); - SDL_Quit(); // return ImageResult::okay(image); return 0; } -} // namespace bookworm +} // namespace bookmouse |