summaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-06-05 02:21:00 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2024-06-05 02:21:00 +0100
commite4ed331cea817e55956200c9c116f8d11b54c86b (patch)
treeff152032ea715d1dbac39904c154baae6e9202d1 /ui
parentBuild and link imgui with sdl2 and opengl2 backends (diff)
downloadilt-e4ed331cea817e55956200c9c116f8d11b54c86b.tar.bz2
ilt-e4ed331cea817e55956200c9c116f8d11b54c86b.tar.xz
ilt-e4ed331cea817e55956200c9c116f8d11b54c86b.zip
Add imgui init and shutdown to appbase and gamemainwindow
Diffstat (limited to 'ui')
-rw-r--r--ui/applicationBase.cpp22
-rw-r--r--ui/applicationBase.h4
-rw-r--r--ui/gameMainWindow.cpp15
-rw-r--r--ui/gameMainWindow.h4
-rw-r--r--ui/imgui_wrap.h6
5 files changed, 51 insertions, 0 deletions
diff --git a/ui/applicationBase.cpp b/ui/applicationBase.cpp
index 2be1f6c..3fa6e2b 100644
--- a/ui/applicationBase.cpp
+++ b/ui/applicationBase.cpp
@@ -1,9 +1,17 @@
#include "applicationBase.h"
+#include "imgui_wrap.h"
#include <SDL2/SDL.h>
#include <stdexcept>
ApplicationBase::ApplicationBase()
{
+ initSDL();
+ initImGUI();
+}
+
+void
+ApplicationBase::initSDL() const
+{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
throw std::runtime_error(SDL_GetError());
}
@@ -24,7 +32,21 @@ ApplicationBase::ApplicationBase()
setGlAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
}
+void
+ApplicationBase::initImGUI() const
+{
+ // 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
+ io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable docking
+ io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable viewports
+}
+
ApplicationBase::~ApplicationBase()
{
SDL_Quit();
+ ImGui::DestroyContext();
}
diff --git a/ui/applicationBase.h b/ui/applicationBase.h
index 9d9b66b..082557d 100644
--- a/ui/applicationBase.h
+++ b/ui/applicationBase.h
@@ -9,4 +9,8 @@ public:
NO_COPY(ApplicationBase);
NO_MOVE(ApplicationBase);
+
+private:
+ void initSDL() const;
+ void initImGUI() const;
};
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp
index ccbcdba..8e93694 100644
--- a/ui/gameMainWindow.cpp
+++ b/ui/gameMainWindow.cpp
@@ -16,6 +16,12 @@
#include <glm/glm.hpp>
#include <memory>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#include "backends/imgui_impl_opengl3.h"
+#include "backends/imgui_impl_sdl2.h"
+#pragma GCC diagnostic pop
+
class GameMainToolbar : Mode<decltype(GameMainSelector::target)>, public Toolbar {
public:
explicit GameMainToolbar(GameMainSelector * gms_) :
@@ -30,11 +36,20 @@ public:
GameMainWindow::GameMainWindow(size_t w, size_t h) :
Window {w, h, "I Like Trains", SDL_WINDOW_OPENGL}, SceneRenderer {Window::size, 0}
{
+ ImGui_ImplSDL2_InitForOpenGL(m_window, glContext.get());
+ ImGui_ImplOpenGL3_Init();
+
uiComponents.create<ManualCameraController>(glm::vec2 {310'727'624, 494'018'810});
auto gms = uiComponents.create<GameMainSelector>(&camera, ScreenAbsCoord {w, h});
uiComponents.create<GameMainToolbar>(gms.get());
}
+GameMainWindow::~GameMainWindow()
+{
+ ImGui_ImplOpenGL3_Shutdown();
+ ImGui_ImplSDL2_Shutdown();
+}
+
void
GameMainWindow::tick(TickDuration)
{
diff --git a/ui/gameMainWindow.h b/ui/gameMainWindow.h
index a4e822e..594843d 100644
--- a/ui/gameMainWindow.h
+++ b/ui/gameMainWindow.h
@@ -8,11 +8,15 @@
class GameMainWindow : public Window, SceneRenderer, public SceneProvider {
public:
GameMainWindow(size_t w, size_t h);
+ ~GameMainWindow() override;
void tick(TickDuration) override;
void render() const override;
+ NO_MOVE(GameMainWindow);
+ NO_COPY(GameMainWindow);
+
private:
void content(const SceneShader &) const override;
void environment(const SceneShader &, const SceneRenderer &) const override;
diff --git a/ui/imgui_wrap.h b/ui/imgui_wrap.h
new file mode 100644
index 0000000..1d619a4
--- /dev/null
+++ b/ui/imgui_wrap.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#include "imgui.h" // IWYU pragma: export
+#pragma GCC diagnostic pop