diff --git a/CMakeLists.txt b/CMakeLists.txt index 302bd41..66a3d97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,9 +11,10 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps") find_package(fmt REQUIRED) find_package(glfw3 REQUIRED) find_package(glad REQUIRED) +find_package(asio REQUIRED) # Target setup add_executable(cuber "cuber.cpp" "cbt/window.cpp") target_include_directories(cuber PRIVATE ".") target_compile_features(cuber PRIVATE cxx_std_23) -target_link_libraries(cuber PRIVATE fmt::fmt glfw::glfw glad::glad) +target_link_libraries(cuber PRIVATE fmt::fmt glfw::glfw glad::glad asio::asio) diff --git a/cuber.cpp b/cuber.cpp index ce97fb5..37c2f15 100644 --- a/cuber.cpp +++ b/cuber.cpp @@ -3,6 +3,10 @@ #include "cbt/window.hpp" +#include + +#include + #include "glad/glad.h" auto main(int, char const*[]) -> int { @@ -12,8 +16,23 @@ auto main(int, char const*[]) -> int { return 1; } + asio::io_context io; + asio::signal_set signals(io, SIGINT, SIGTERM); + bool quit = false; + + signals.async_wait([&](auto, auto) { + quit = true; + io.stop(); + }); + + auto process_signals = [&]() -> void { + while (io.poll()) {} + }; + while (!w.should_close()) { - if (glfwGetKey(static_cast(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) { + process_signals(); + + if (quit || glfwGetKey(static_cast(w.raw()), GLFW_KEY_Q) == GLFW_PRESS) { break; } diff --git a/deps/Findasio.cmake b/deps/Findasio.cmake new file mode 100644 index 0000000..f0e4bbb --- /dev/null +++ b/deps/Findasio.cmake @@ -0,0 +1,70 @@ +# ============================================================================== +# Find asio +# ============================================================================== +# This module fetches the asio networking library. +# +# Targets provided: +# asio::asio - The asio library target +# +# Variables set: +# asio_FOUND - TRUE if asio is available +# asio_LIBRARIES - The asio library target (asio::asio) +# asio_INCLUDE_DIR - Include directories for asio +# asio_VERSION - Version of asio (if available) +# ============================================================================== + +if (DEFINED _FINDASIO_INCLUDED) + return() +endif() +set(_FINDASIO_INCLUDED TRUE) + +# Use the version passed to find_package(), or default to 1.32.0 +if (DEFINED asio_FIND_VERSION AND NOT asio_FIND_VERSION STREQUAL "") + set(ASIO_VERSION "${asio_FIND_VERSION}") +else() + set(ASIO_VERSION "1.30.2") +endif() + +message(STATUS "Fetching asio ${ASIO_VERSION}") + +include(FetchContent) + +find_program(GIT_EXECUTABLE git) +if (GIT_EXECUTABLE) + set(ASIO_FETCH_METHOD "GIT") +else() + message(FATAL_ERROR "Fetch method ZIP not supported") +endif() + +if (ASIO_FETCH_METHOD STREQUAL "GIT") + FetchContent_Declare( + asio + GIT_REPOSITORY https://github.com/mononerv/asio.git + GIT_TAG ${ASIO_VERSION} + ) +endif() + +FetchContent_MakeAvailable(asio) + +if (NOT TARGET asio::asio) + if (TARGET asio) + add_library(asio::asio ALIAS asio) + else() + message(FATAL_ERROR "Could not fetch asio; no target asio or asio::asio available") + endif() +endif() + +set(asio_FOUND TRUE) +set(asio_LIBRARIES asio::asio) +set(asio_VERSION "${ASIO_VERSION}") +get_target_property(_asio_inc asio::asio INTERFACE_INCLUDE_DIRECTORIES) +set(asio_INCLUDE_DIR "${_asio_inc}") + +# Mark asio includes as SYSTEM to suppress warnings from its headers +if (_asio_inc AND TARGET asio) + set_target_properties(asio PROPERTIES + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_asio_inc}" + ) +endif() + +set(ASIO_LICENSE_FILE "${asio_SOURCE_DIR}/LICENSE_1_0.txt" CACHE FILEPATH "Path to Asio license file")