From a8e88ae86f1d0df6d4408fe5b185d5a17a1fb8a2 Mon Sep 17 00:00:00 2001
From: Armin Co <armin.co@hs-bochum.de>
Date: Sun, 26 Apr 2020 17:08:50 +0200
Subject: [PATCH] Working playground with imgui.

GTK is broken at the moment.
---
 .gitmodules                    |   3 +-
 CMakeLists.txt                 |  13 +++-
 gtk_qpong_app/CMakeLists.txt   |  13 +---
 imgui_app/CMakeLists.txt       |  75 +++++++++++++++++--
 imgui_app/imgui_playground.cpp | 129 +++++++++++++++++++++++++++++++++
 qpong_core/CMakeLists.txt      |  15 ++--
 6 files changed, 219 insertions(+), 29 deletions(-)

diff --git a/.gitmodules b/.gitmodules
index 75e2a9f..7a2f014 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -6,4 +6,5 @@
 	url = https://github.com/armin-co/imgui.git
 [submodule "libs/cpp_starter_project"]
 	path = libs/cpp_starter_project
-	url = https://github.com/armin-co/cpp_starter_project.git
\ No newline at end of file
+	url = https://github.com/armin-co/cpp_starter_project.git
+	#
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 98f9a32..b1b255e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,13 +12,18 @@ option(release_type "Set the type of the releas (Debug/Release)." Release)
 set(CMAKE_CXX_STANDARD 17)
 
 
-## PkgConfig
+# PkgConfig
 find_package(PkgConfig REQUIRED)
 
+# On macOS some things are different ^^
+if(APPLE)
+    link_directories(/usr/local/lib)
+    include_directories(/usr/local/include)
 
-# @todo Check versions of glib etc. librarys.
-# On Ubuntu 19.10 there are warnings which are not present in Ubuntu 18.04
-# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
+    # This may be necessary on some systems.
+    message("ATTENTION: You probably have to export the PKG_CONFIG_PATH.")
+    message("For example: $ export PKG_CONFIG_PATH=\"/usr/local/lib:/usr/local/opt/zlib/lib/pkgconfig\"")
+endif()
 
 
 # Add qpong lib for core qpong functionality
diff --git a/gtk_qpong_app/CMakeLists.txt b/gtk_qpong_app/CMakeLists.txt
index 3333790..3da7cc6 100644
--- a/gtk_qpong_app/CMakeLists.txt
+++ b/gtk_qpong_app/CMakeLists.txt
@@ -1,10 +1,3 @@
-# Specific settings for macOS
-if(APPLE)
-    link_directories("/usr/local/lib")
-    message("ATTENTION: You probably have to export the PKG_CONFIG_PATH.")
-    message("For example: export PKG_CONFIG_PATH=\"/usr/local/lib:/usr/local/opt/zlib/lib/pkgconfig\"")
-endif()
-
 
 # Setup GTK configuration
 pkg_check_modules(GTKMM gtkmm-3.0)
@@ -15,11 +8,6 @@ link_directories(${GTKMM_LIBRARY_DIRS})
 include_directories(${GTKMM_INCLUDE_DIRS})
 include_directories(../qpong_core)
 
-if(APPLE)
-    include_directories(/usr/local/include)
-endif()
-
-
 # create the application
 add_executable( qpong.app 
     main.cpp 
@@ -33,5 +21,6 @@ add_executable( qpong.app
 target_link_libraries( qpong.app 
     qpong_core
     ${GTKMM_LIBRARIES}
+    fftw3
     m # math
 )
diff --git a/imgui_app/CMakeLists.txt b/imgui_app/CMakeLists.txt
index a1015dc..5ca9d70 100644
--- a/imgui_app/CMakeLists.txt
+++ b/imgui_app/CMakeLists.txt
@@ -1,10 +1,73 @@
-# Specific settings for macOS
+
+add_definitions(-DIMGUI_IMPL_OPENGL_LOADER_GL3W) # Define OpenGL loader for Dear ImGui
+
+# Dear ImGui include dirs
+include_directories(
+    ../libs/imgui 
+    ../libs/imgui/examples 
+    ../libs/imgui/examples/libs/gl3w
+)
+
+# Main Dear ImGui src files.
+set(
+    dear_im_gui_srcs
+    ../libs/imgui/imgui.cpp
+    ../libs/imgui/imgui_draw.cpp
+    ../libs/imgui/imgui_widgets.cpp
+)
+
+# Impl files from Dear ImGui.
+set(
+    dear_im_gui_impl_srcs
+    ../libs/imgui/examples/imgui_impl_sdl.cpp
+    ../libs/imgui/examples/imgui_impl_opengl3.cpp
+)
+
+# Dear ImGui lib
+add_library(
+    dear_im_gui
+    ${dear_im_gui_srcs}
+    ${dear_im_gui_impl_srcs}
+    ../libs/imgui/examples/libs/gl3w/GL/gl3w.c
+)
+
+# Application base on Dear ImGui
+add_executable(
+    imgui_playground.app 
+    imgui_playground.cpp
+)
+
+
+# Add all SDL2 dependencies.
+find_package(SDL2 REQUIRED)
+include_directories(${SDL2_INCLUDE_DIRS})
+
+# Link everything together
+target_link_libraries(
+    imgui_playground.app 
+    dear_im_gui
+    ${SDL2_LIBRARIES}
+    qpong_core
+)
+
+# Include macOS specific things.
 if(APPLE)
-    link_directories("/usr/local/lib")
-    message("ATTENTION: You probably have to export the PKG_CONFIG_PATH.")
-    message("For example: export PKG_CONFIG_PATH=\"/usr/local/lib:/usr/local/opt/zlib/lib/pkgconfig\"")
+    find_library(COCOA_LIBRARY Cocoa REQUIRED)
+    find_library(IOKIT_LIBRARY IOKit REQUIRED)
+    find_library(COREVID_LIBRARY CoreVideo REQUIRED)
+    message(${COCOA_LIBRARY})
+    message(${IOKIT_LIBRARY})
+    message(${COREVID_LIBRARY})
+    
+    target_link_libraries(
+        imgui_playground.app
+        ${COCOA_LIBRARY}
+        ${IOKIT_LIBRARY}
+        ${COREVID_LIBRARY}
+    )
+else(APPLE)
+    find_package(OpenGL REQUIRED)
+    target_link_libraries(imgui_playground.app OpenGL::GL ${CMAKE_DL_LIBS})
 endif()
 
 
-add_executable(imgui_playground.app 
-imgui_playground.cpp)
\ No newline at end of file
diff --git a/imgui_app/imgui_playground.cpp b/imgui_app/imgui_playground.cpp
index 78d3231..35ecb27 100644
--- a/imgui_app/imgui_playground.cpp
+++ b/imgui_app/imgui_playground.cpp
@@ -3,9 +3,138 @@
 /// @brief  Working example for Dear ImGui with OpenGL3 and SDL2
 ///
 
+#include "imgui.h"
+#include "imgui_impl_sdl.h"
+#include "imgui_impl_opengl3.h"
+#include <stdio.h>
+#include <SDL.h>
+
+#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
+#include <GL/gl3w.h>
+#endif
 
 int main(void)
 {
+    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 __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
+
+
+      // 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);
+    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
+    SDL_GL_MakeCurrent(window, gl_context);
+    SDL_GL_SetSwapInterval(1); // Enable vsync
+
+    // Initialize OpenGL loader
+    #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
+        bool err = gl3wInit() != 0;
+    #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
+        bool err = glewInit() != GLEW_OK;
+    #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
+        bool err = gladLoadGL() == 0;
+    #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
+        bool err = false;
+        glbinding::Binding::initialize();
+    #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
+        bool err = false;
+        glbinding::initialize([](const char* name) { return (glbinding::ProcAddress)SDL_GL_GetProcAddress(name); });
+    #else
+        bool err = false; // If you use IMGUI_IMPL_OPENGL_LOADER_CUSTOM, your loader is likely to requires some form of initialization.
+    #endif
+        if (err)
+        {
+            fprintf(stderr, "Failed to initialize OpenGL loader!\n");
+            return 1;
+        }
+
+    IMGUI_CHECKVERSION();
+    ImGui::CreateContext();
+    ImGuiIO& io = ImGui::GetIO(); (void)io;
+    ImGui::StyleColorsDark();
+    ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
+    ImGui_ImplOpenGL3_Init(glsl_version);
+
+
+    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
+    bool done = false;
+    while (!done)
+    {
+         SDL_Event event;
+        while (SDL_PollEvent(&event))
+        {
+            ImGui_ImplSDL2_ProcessEvent(&event);
+            if (event.type == SDL_QUIT)
+                done = true;
+            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
+                done = true;
+        }
+
+        ImGui_ImplOpenGL3_NewFrame();
+        ImGui_ImplSDL2_NewFrame(window);
+        ImGui::NewFrame();
+
+         {
+            static float f = 0.0f;
+            static int counter = 0;
+
+            ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
+
+            ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
+          
+            ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
+            ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
+
+            if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
+                counter++;
+            ImGui::SameLine();
+            ImGui::Text("counter = %d", counter);
+
+            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
+            ImGui::End();
+        }
+
+         // Rendering
+        ImGui::Render();
+        glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
+        glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
+        glClear(GL_COLOR_BUFFER_BIT);
+        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+        SDL_GL_SwapWindow(window);
+    }
+
+    ImGui_ImplOpenGL3_Shutdown();
+    ImGui_ImplSDL2_Shutdown();
+    ImGui::DestroyContext();
+
+    SDL_GL_DeleteContext(gl_context);
+    SDL_DestroyWindow(window);
+    SDL_Quit();
+
 
     return 0;
 }
\ No newline at end of file
diff --git a/qpong_core/CMakeLists.txt b/qpong_core/CMakeLists.txt
index 3e2f2ab..01180fb 100644
--- a/qpong_core/CMakeLists.txt
+++ b/qpong_core/CMakeLists.txt
@@ -1,15 +1,18 @@
-# Add macOS specific include path.
-if(APPLE)
-    include_directories(/usr/local/include)
-endif()
 
 # Core QPong library
-add_library(qpong_core Particle.cpp ImageBuffer.cpp Player.cpp)
+add_library(
+    qpong_core
+    Particle.cpp
+    ImageBuffer.cpp
+    Player.cpp
+)
 
 find_package(Threads)
 
-target_link_libraries( qpong_core
+target_link_libraries(
+    qpong_core
     ${CMAKE_THREAD_LIBS_INIT}
     fftw3
     fftw3_omp
+    m
 )
\ No newline at end of file
-- 
GitLab