From 88e9ed79f07d89a98fd5ac7f06ef76cf07855d4a Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sun, 29 Nov 2020 18:21:57 +0200 Subject: [PATCH] examples : refactor common code --- examples/CMakeLists.txt | 57 ++++++++------- examples/ggwave-common-sdl2.cpp | 34 +++++++++ examples/ggwave-common-sdl2.h | 2 + examples/ggwave-gui/main.cpp | 58 +++------------ examples/third-party/CMakeLists.txt | 4 +- examples/third-party/imgui/CMakeLists.txt | 71 +++++++++++-------- .../imgui/imgui-extra/imgui_impl.cpp | 2 + 7 files changed, 123 insertions(+), 105 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3c2c713..e9f8a79 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,35 @@ +# dependencies + find_package(Threads REQUIRED) +if (GGWAVE_SUPPORT_SDL2) + # SDL2 + + if (EMSCRIPTEN) + set(CMAKE_CXX_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s USE_SDL=2 -s ASSERTIONS=1 -s DISABLE_EXCEPTION_CATCHING=0 -s 'EXTRA_EXPORTED_RUNTIME_METHODS=[\"writeArrayToMemory\"]'") + unset(SDL2_INCLUDE_DIRS) + unset(SDL2_LIBRARIES) + endif() + + if (NOT EMSCRIPTEN) + find_package(SDL2) + + if (NOT USE_FINDSDL2 AND NOT SDL2_FOUND) + message(WARNING "Unable to find SDL2 library. It is either not installed or CMake cannot find it." + " In the latter case, setting the USE_FINDSDL2 variable might help:\n" + " $ cmake -D USE_FINDSDL2 .." + ) + + message(FATAL_ERROR "Aborting") + endif() + + string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES) + + message(STATUS "SDL2_INCLUDE_DIRS = ${SDL2_INCLUDE_DIRS}") + message(STATUS "SDL2_LIBRARIES = ${SDL2_LIBRARIES}") + endif() +endif() + # third-party add_subdirectory(third-party) @@ -14,42 +44,19 @@ target_link_libraries(ggwave-common PRIVATE ) if (GGWAVE_SUPPORT_SDL2) - # SDL2 - - if (EMSCRIPTEN) - set (CMAKE_CXX_FLAGS "-s ALLOW_MEMORY_GROWTH=1 -s USE_SDL=2 -s ASSERTIONS=1 -s DISABLE_EXCEPTION_CATCHING=0 -s 'EXTRA_EXPORTED_RUNTIME_METHODS=[\"writeArrayToMemory\"]'") - endif() + # ggwave-common-sdl2 add_library(ggwave-common-sdl2 ${GGWAVE_LIBRARY_TYPE} ggwave-common-sdl2.cpp ) - if (NOT EMSCRIPTEN) - find_package(SDL2) - - if (NOT USE_FINDSDL2 AND NOT SDL2_FOUND) - message(WARNING "Unable to find SDL2 library. It is either not installed or CMake cannot find it." - " In the latter case, setting the USE_FINDSDL2 variable might help:\n" - " $ cmake -D USE_FINDSDL2 .." - ) - - message(FATAL_ERROR "Aborting") - endif() - - string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES) - endif() - - message(STATUS "SDL2_INCLUDE_DIRS = ${SDL2_INCLUDE_DIRS}") - message(STATUS "SDL2_LIBRARIES = ${SDL2_LIBRARIES}") - - # ggwave-common-sdl2 - target_include_directories(ggwave-common-sdl2 PUBLIC ${SDL2_INCLUDE_DIRS} ) target_link_libraries(ggwave-common-sdl2 PRIVATE ggwave + imgui-sdl2 ${SDL2_LIBRARIES} ) endif() diff --git a/examples/ggwave-common-sdl2.cpp b/examples/ggwave-common-sdl2.cpp index 3875842..ece00c1 100644 --- a/examples/ggwave-common-sdl2.cpp +++ b/examples/ggwave-common-sdl2.cpp @@ -4,6 +4,10 @@ #include "ggwave-common.h" +#include + +#include + #include #ifdef __EMSCRIPTEN__ @@ -311,3 +315,33 @@ bool GGWave_deinit() { return true; } + +bool ImGui_beginFrame(SDL_Window * window) { + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ProcessEvent(&event); + if (event.type == SDL_QUIT) return false; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) return false; + } + + ImGui_NewFrame(window); + + return true; +} + +bool ImGui_endFrame(SDL_Window * window) { + // Rendering + int display_w, display_h; + SDL_GetWindowSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(0.0f, 0.0f, 0.0f, 0.4f); + glClear(GL_COLOR_BUFFER_BIT); + + ImGui::Render(); + ImGui_RenderDrawData(ImGui::GetDrawData()); + + SDL_GL_SwapWindow(window); + + return true; +} diff --git a/examples/ggwave-common-sdl2.h b/examples/ggwave-common-sdl2.h index 7c9387c..db8c91f 100644 --- a/examples/ggwave-common-sdl2.h +++ b/examples/ggwave-common-sdl2.h @@ -16,3 +16,5 @@ bool GGWave_deinit(); // ImGui helpers +bool ImGui_beginFrame(SDL_Window * window); +bool ImGui_endFrame(SDL_Window * window); diff --git a/examples/ggwave-gui/main.cpp b/examples/ggwave-gui/main.cpp index a074a22..b795895 100644 --- a/examples/ggwave-gui/main.cpp +++ b/examples/ggwave-gui/main.cpp @@ -7,15 +7,12 @@ #include #include -#include -#include #include #include #include #include -#include static SDL_Window * g_window; static void * g_gl_context; @@ -81,27 +78,6 @@ int main(int argc, char** argv) { ggWave->init(0, ""); - std::mutex mutex; - std::thread inputThread([&]() { - std::string inputOld = ""; - while (true) { - std::string input; - std::cout << "Enter text: "; - getline(std::cin, input); - if (input.empty()) { - std::cout << "Re-sending ... " << std::endl; - input = inputOld; - } else { - std::cout << "Sending ... " << std::endl; - } - { - std::lock_guard lock(mutex); - ggWave->init(input.size(), input.data()); - } - inputOld = input; - } - }); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { fprintf(stderr, "Error: %s\n", SDL_GetError()); return -1; @@ -124,38 +100,20 @@ int main(int argc, char** argv) { ImGui::Render(); while (true) { - { - std::lock_guard lock(mutex); - GGWave_mainLoop(); + GGWave_mainLoop(); + + if (ImGui_beginFrame(g_window) == false) { + break; } - SDL_Event event; - while (SDL_PollEvent(&event)) - { - ImGui_ProcessEvent(&event); - if (event.type == SDL_QUIT) {} - if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(g_window)) {} + if (ImGui::Button("Send")) { + std::string input("hello"); + ggWave->init(input.size(), input.data()); } - ImGui_NewFrame(g_window); - - ImGui::ShowDemoWindow(); - - // Rendering - int display_w, display_h; - SDL_GetWindowSize(g_window, &display_w, &display_h); - glViewport(0, 0, display_w, display_h); - glClearColor(0.0f, 0.0f, 0.0f, 0.4f); - glClear(GL_COLOR_BUFFER_BIT); - - ImGui::Render(); - ImGui_RenderDrawData(ImGui::GetDrawData()); - - SDL_GL_SwapWindow(g_window); + ImGui_endFrame(g_window); } - inputThread.join(); - GGWave_deinit(); return 0; diff --git a/examples/third-party/CMakeLists.txt b/examples/third-party/CMakeLists.txt index 60b0ddc..db51b52 100644 --- a/examples/third-party/CMakeLists.txt +++ b/examples/third-party/CMakeLists.txt @@ -1,4 +1,4 @@ if (NOT EMSCRIPTEN) - add_subdirectory(imtui) - add_subdirectory(imgui) endif() +add_subdirectory(imtui) +add_subdirectory(imgui) diff --git a/examples/third-party/imgui/CMakeLists.txt b/examples/third-party/imgui/CMakeLists.txt index 32e6f60..8885597 100644 --- a/examples/third-party/imgui/CMakeLists.txt +++ b/examples/third-party/imgui/CMakeLists.txt @@ -12,7 +12,7 @@ else (APPLE) unset(ADDITIONAL_LIBRARIES) endif (APPLE) -add_library(imgui SHARED +add_library(imgui ${GGWAVE_LIBRARY_TYPE} imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_demo.cpp @@ -32,36 +32,51 @@ target_link_libraries(imgui PRIVATE ) if (GGWAVE_SUPPORT_SDL2) - find_package(OpenGL REQUIRED) - find_package(SDL2 REQUIRED) - string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES) + if (NOT EMSCRIPTEN) + find_package(OpenGL REQUIRED) - add_library(imgui-sdl2 SHARED - imgui/examples/libs/gl3w/GL/gl3w.c - imgui-extra/imgui_impl.cpp - imgui-extra/imgui_impl_sdl.cpp - imgui-extra/imgui_impl_opengl3.cpp - ) + add_library(imgui-sdl2 ${GGWAVE_LIBRARY_TYPE} + imgui/examples/libs/gl3w/GL/gl3w.c + imgui-extra/imgui_impl.cpp + imgui-extra/imgui_impl_sdl.cpp + imgui-extra/imgui_impl_opengl3.cpp + ) - target_include_directories(imgui-sdl2 PUBLIC - imgui/examples/libs/gl3w - ${SDL2_INCLUDE_DIRS} - ) + target_include_directories(imgui-sdl2 PUBLIC + imgui/examples/libs/gl3w + ${SDL2_INCLUDE_DIRS} + ) - target_include_directories(imgui-sdl2 PRIVATE - imgui - imgui-extra - ) + target_include_directories(imgui-sdl2 PRIVATE + imgui + imgui-extra + ) - target_link_libraries(imgui-sdl2 PUBLIC - imgui - ${OPENGL_LIBRARIES} - ${SDL2_LIBRARIES} - ) + target_link_libraries(imgui-sdl2 PUBLIC + imgui + ${OPENGL_LIBRARIES} + ${SDL2_LIBRARIES} + ) - target_link_libraries(imgui-sdl2 PRIVATE - ${CMAKE_DL_LIBS} - ${CMAKE_THREAD_LIBS_INIT} - ${ADDITIONAL_LIBRARIES} - ) + target_link_libraries(imgui-sdl2 PRIVATE + ${CMAKE_DL_LIBS} + ${CMAKE_THREAD_LIBS_INIT} + ${ADDITIONAL_LIBRARIES} + ) + else() + add_library(imgui-sdl2 ${GGWAVE_LIBRARY_TYPE} + imgui-extra/imgui_impl.cpp + imgui-extra/imgui_impl_sdl.cpp + imgui-extra/imgui_impl_opengl3.cpp + ) + + target_include_directories(imgui-sdl2 PRIVATE + imgui + imgui-extra + ) + + target_link_libraries(imgui-sdl2 PUBLIC + imgui + ) + endif() endif() diff --git a/examples/third-party/imgui/imgui-extra/imgui_impl.cpp b/examples/third-party/imgui/imgui-extra/imgui_impl.cpp index 0edb746..72d971e 100644 --- a/examples/third-party/imgui/imgui-extra/imgui_impl.cpp +++ b/examples/third-party/imgui/imgui-extra/imgui_impl.cpp @@ -15,6 +15,8 @@ #include +#include + ImGuiContext* ImGui_Init(SDL_Window* window, SDL_GLContext gl_context) { // Decide GL+GLSL versions #if __APPLE__