mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-07 17:54:39 +08:00
examples : refactor common code
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
|
||||
#include "ggwave-common.h"
|
||||
|
||||
#include <imgui-extra/imgui_impl.h>
|
||||
|
||||
#include <SDL_opengl.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -16,3 +16,5 @@ bool GGWave_deinit();
|
||||
|
||||
// ImGui helpers
|
||||
|
||||
bool ImGui_beginFrame(SDL_Window * window);
|
||||
bool ImGui_endFrame(SDL_Window * window);
|
||||
|
||||
@@ -7,15 +7,12 @@
|
||||
#include <imgui-extra/imgui_impl.h>
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_audio.h>
|
||||
#include <SDL_opengl.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
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<std::mutex> 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<std::mutex> 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;
|
||||
|
||||
4
examples/third-party/CMakeLists.txt
vendored
4
examples/third-party/CMakeLists.txt
vendored
@@ -1,4 +1,4 @@
|
||||
if (NOT EMSCRIPTEN)
|
||||
add_subdirectory(imtui)
|
||||
add_subdirectory(imgui)
|
||||
endif()
|
||||
add_subdirectory(imtui)
|
||||
add_subdirectory(imgui)
|
||||
|
||||
71
examples/third-party/imgui/CMakeLists.txt
vendored
71
examples/third-party/imgui/CMakeLists.txt
vendored
@@ -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()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
ImGuiContext* ImGui_Init(SDL_Window* window, SDL_GLContext gl_context) {
|
||||
// Decide GL+GLSL versions
|
||||
#if __APPLE__
|
||||
|
||||
Reference in New Issue
Block a user