summaryrefslogtreecommitdiff
path: root/src/imgui_context.cpp
blob: 99ecd3f173268381dde2cf94dc17522cb20da49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "imgui_context.hpp"

#include <SDL_opengl.h>
#include <imgui/imgui_impl_opengl3.h>
#include <imgui/imgui_impl_sdl2.h>

namespace bookmouse {

ImguiContext::ImguiContext(GlContext& glContext, SdlContext& sdlContext, SdlMainWindow& mainWindow) :
    m_mainWindow(mainWindow)
{
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();

    setIOFlag(ImGuiConfigFlags_NavEnableKeyboard);
    setIOFlag(ImGuiConfigFlags_NavEnableGamepad);

    ImGui::StyleColorsDark();
    // ImGui::StyleColorsLight();

    auto& style = ImGui::GetStyle();
    style.WindowRounding = 0.0f;// <- Set this on init or use ImGui::PushStyleVar()
    style.ChildRounding = 0.0f;
    style.FrameRounding = 0.0f;
    style.GrabRounding = 0.0f;
    style.PopupRounding = 0.0f;
    style.ScrollbarRounding = 0.0f;

    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) const
{
    ImGui::GetIO().ConfigFlags |= flag;
}

const ImGuiIO& ImguiContext::getIO() const
{
    return ImGui::GetIO();
}

ImGuiIO& ImguiContext::getIO()
{
    return ImGui::GetIO();
}

bool ImguiContext::processEvent(SDL_Event& event) const
{
    return ImGui_ImplSDL2_ProcessEvent(&event);
}

void ImguiContext::startFrame() const
{
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplSDL2_NewFrame();
    ImGui::NewFrame();
}

void ImguiContext::render() const
{
    ImGui::Render();
    const auto& imguiIO = getIO();
    const auto width = static_cast<int>(imguiIO.DisplaySize.x);
    const auto height = static_cast<int>(imguiIO.DisplaySize.y);
    glViewport(0, 0, width, height);
    constexpr ImVec4 clearColor{0.45f, 0.55f, 0.60f, 1.00f};
    glClearColor(clearColor.x * clearColor.w, clearColor.y * clearColor.w, clearColor.z * clearColor.w, clearColor.w);
    glClear(GL_COLOR_BUFFER_BIT);

    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    SDL_GL_SwapWindow(m_mainWindow.window());
}

} // namespace bookmouse