Browse Source

Now build and link with ImGui & ImGuiSDL

Godzil 3 years ago
parent
commit
0ae9c18242
4 changed files with 52 additions and 2 deletions
  1. 1 0
      CMakeLists.txt
  2. 32 0
      external/cmake/BuildImGui.cmake
  3. 3 1
      source/CMakeLists.txt
  4. 16 1
      source/Game.cpp

+ 1 - 0
CMakeLists.txt

@@ -7,6 +7,7 @@ project("GameEngine")
 
 include(GetGitRevisionDescription)
 include(GetLua)
+include(BuildImGui)
 
 git_describe(VERSION --tags --dirty=-dirty)
 

+ 32 - 0
external/cmake/BuildImGui.cmake

@@ -0,0 +1,32 @@
+cmake_minimum_required(VERSION 3.1)
+project(ImGui)
+include(ExternalProject)
+
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
+
+find_package(SDL2 REQUIRED)
+set(SDL2_INCDIR ${SDL2_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR} ${SDL2_TTF_INCLUDE_DIR} ${SDL2_MIXER_INCLUDE_DIR})
+
+add_library(ImGui STATIC)
+target_include_directories(ImGui PUBLIC ${SDL2_INCDIR})
+target_include_directories(ImGui PUBLIC external/imgui)
+target_include_directories(ImGui PUBLIC external/imgui/backends)
+
+file(GLOB ImG_SOURCES external/imgui/*.cpp external/imgui/backends/imgui_impl_sdl.cpp)
+file(GLOB ImG_HEADERS external/imgui/*.h external/imgui/backends/imgui_impl_sdl.h)
+
+target_sources(ImGui PRIVATE ${ImG_SOURCES} ${ImG_HEADERS})
+
+
+add_library(ImGuiSDL STATIC)
+message("SDL Dir: ${SDL2_INCDIR}")
+target_include_directories(ImGuiSDL PUBLIC ${SDL2_INCDIR})
+target_include_directories(ImGuiSDL PUBLIC external/imgui_sdl)
+target_include_directories(ImGuiSDL PUBLIC external/imgui)
+
+set(ImGSDL_SOURCES external/imgui_sdl/imgui_sdl.cpp)
+file(GLOB ImGSLD_HEADERS external/imgui_sdl/*.h)
+
+target_sources(ImGuiSDL PRIVATE ${ImGSDL_SOURCES} ${ImGSDL_HEADERS})

+ 3 - 1
source/CMakeLists.txt

@@ -23,4 +23,6 @@ target_include_directories(gameengine PUBLIC ${GLM_INCLUDE_DIR})
 target_link_libraries(gameengine ${SDL2_LIBS})
 target_link_libraries(gameengine ${LUA_LIBRARIES})
 target_link_libraries(gameengine glm::glm)
-target_link_libraries(gameengine sol2::sol2)
+target_link_libraries(gameengine sol2::sol2)
+target_link_libraries(gameengine ImGui)
+target_link_libraries(gameengine ImGuiSDL)

+ 16 - 1
source/Game.cpp

@@ -7,10 +7,14 @@
  * Created by Manoël Trapier on 09/02/2021.
  */
 
+#include <stdlib.h>
+
 #include <SDL.h>
 #include <SDL_image.h>
 #include <glm/glm.hpp>
-#include <stdlib.h>
+#include <imgui.h>
+#include <imgui_sdl.h>
+#include <backends/imgui_impl_sdl.h>
 
 #include <memory>
 #include <fstream>
@@ -101,6 +105,9 @@ void Game::Initialize()
 
     //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
 
+    ImGui::CreateContext();
+    ImGuiSDL::Initialize(this->renderer, Game::windowsWidth, Game::windowsHeight);
+
     this->camera.x = 0;
     this->camera.y = 0;
     this->camera.w = Game::windowsWidth;
@@ -111,6 +118,9 @@ void Game::Initialize()
 
 void Game::Destroy()
 {
+    ImGuiSDL::Deinitialize();
+    ImGui::DestroyContext();
+
     if (this->renderer)
     {
         SDL_DestroyRenderer(this->renderer);
@@ -325,6 +335,11 @@ void Game::Render()
     if (this->isDebug)
     {
         registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
+
+        ImGui::NewFrame();
+        ImGui::ShowDemoWindow();
+        ImGui::Render();
+        ImGuiSDL::Render(ImGui::GetDrawData());
     }
 
     SDL_RenderPresent(this->renderer);