From 27816a28d782e277117efe0fd27f68d902c701bf Mon Sep 17 00:00:00 2001 From: Allan Legemaate Date: Wed, 20 Sep 2023 22:58:37 -0400 Subject: [PATCH] fix: use singleton key and mouse states in keydown function (#11) --- .DS_Store | Bin 0 -> 6148 bytes .github/workflows/deploy-docs.yml | 2 +- CMakeLists.txt | 73 +++++++++-------- include/asw/modules/input.h | 129 +++++++++++++++--------------- src/modules/input.cpp | 24 ++++++ 5 files changed, 128 insertions(+), 100 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ca2b7874ee44cef1857d5487e8c8184223131c55 GIT binary patch literal 6148 zcmeH~F$w}f3`G;&La^D=avBfd4F=H@>;(h`8(BfodXDZ-CJ2t!BJu;tpJXO1`-+{7 zi0JxuSc&u^GJ~7S(n4d3ypw~RWiQwJa2ZeM@rat$Cvn!+@Lrnz*rt#G36KB@kN^q% z5COZlVY7KvMiL+a5_l4@??Zx{=Fn2rKOG1@0zf;I-LUpq0-CG<&7q|#Dlm=dL8DcD z46(YmLsOi~p`~hV7meXV4M3`}dgXhH(h?7~0-B+w9;*1Wg-e+&OK|2Hj6Nq_|Y zjDU8VVY9|d#ohY$dRE^>)z$?L_2URHKLJSWDqg_du%B!J&7q|#Dlq;CI0gn1_$q-1 D?w=C8 literal 0 HcmV?d00001 diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.yml index d3e28e6..291ee9f 100644 --- a/.github/workflows/deploy-docs.yml +++ b/.github/workflows/deploy-docs.yml @@ -1,7 +1,7 @@ name: Deploy Docs on: - pull_request: + push: branches: [main] permissions: diff --git a/CMakeLists.txt b/CMakeLists.txt index d1555e7..0ea4512 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.24) -project (asw VERSION 1.0.0 LANGUAGES CXX) +project(asw VERSION 0.3.5 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -21,15 +21,19 @@ add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) target_compile_options(${PROJECT_NAME} PRIVATE -O2 -Wall -Wextra -pedantic) # Add include -target_include_directories(${PROJECT_NAME} PUBLIC - $ - $ - PRIVATE src) +target_include_directories( + ${PROJECT_NAME} PUBLIC + $ + $ + PRIVATE src +) # Versioning -set_target_properties(${PROJECT_NAME} PROPERTIES - VERSION ${PROJECT_VERSION} - SOVERSION 1) +set_target_properties( + ${PROJECT_NAME} PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION 1 +) # Find libs if(NOT EMSCRIPTEN) @@ -47,27 +51,27 @@ if(EMSCRIPTEN) set(CMAKE_EXECUTABLE_SUFFIX ".html") target_compile_options( ${PROJECT_NAME} - PRIVATE - -sUSE_SDL=2 - -sUSE_SDL_IMAGE=2 - -sUSE_SDL_TTF=2 - -sUSE_SDL_MIXER=2 + PRIVATE + -sUSE_SDL=2 + -sUSE_SDL_IMAGE=2 + -sUSE_SDL_TTF=2 + -sUSE_SDL_MIXER=2 -sSDL2_IMAGE_FORMATS=["png"] ) target_link_libraries( - ${PROJECT_NAME} - -sWASM=1 - -sUSE_SDL=2 - -sUSE_SDL_IMAGE=2 - -sUSE_SDL_TTF=2 - -sUSE_SDL_MIXER=2 - -sSDL2_IMAGE_FORMATS=["png"] + ${PROJECT_NAME} + -sWASM=1 + -sUSE_SDL=2 + -sUSE_SDL_IMAGE=2 + -sUSE_SDL_TTF=2 + -sUSE_SDL_MIXER=2 + -sSDL2_IMAGE_FORMATS=["png"] -sDEMANGLE_SUPPORT=1 -sTOTAL_MEMORY=512MB ) set_target_properties( - ${PROJECT_NAME} - PROPERTIES + ${PROJECT_NAME} + PROPERTIES LINK_FLAGS "--preload-file ${CMAKE_CURRENT_LIST_DIR}/assets@/assets --use-preload-plugins" ) @@ -77,14 +81,15 @@ else(EMSCRIPTEN) if(MINGW) target_link_libraries(${PROJECT_NAME} -lmingw32) endif(MINGW) + target_link_libraries( - ${PROJECT_NAME} - -lm - ${SDL_MAIN_LIBRARY} - ${SDL_LIBRARY} - ${SDL_MIXER_LIBRARY} - ${SDL_IMAGE_LIBRARY} - ${SDL_TTF_LIBRARY} + ${PROJECT_NAME} + -lm + ${SDL_MAIN_LIBRARY} + ${SDL_LIBRARY} + ${SDL_MIXER_LIBRARY} + ${SDL_IMAGE_LIBRARY} + ${SDL_TTF_LIBRARY} ) endif(EMSCRIPTEN) @@ -98,12 +103,12 @@ configure_package_config_file( ) write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) diff --git a/include/asw/modules/input.h b/include/asw/modules/input.h index 1a46d36..f1ee4b3 100644 --- a/include/asw/modules/input.h +++ b/include/asw/modules/input.h @@ -21,42 +21,8 @@ namespace asw::input { /** * @brief Mouse state stores the current state of the mouse. It is updated by * the core. - * */ using MouseState = struct MouseState { - /** - * @brief Check if a button is down. - * - * @param button The button to check. - * @return true - If the button is down. - * @return false - If the button is not down. - */ - bool isButtonDown(asw::input::MouseButton button) { - return down[static_cast(button)]; - } - - /** - * @brief Check if a button was pressed since the last update. - * - * @param button The button to check. - * @return true - If the button was pressed. - * @return false - If the button was not pressed. - */ - bool wasButtonPressed(asw::input::MouseButton button) { - return pressed[static_cast(button)]; - } - - /** - * @brief Check if a button was released since the last update. - * - * @param button The button to check. - * @return true - If the button was released. - * @return false - If the button was not released. - */ - bool wasButtonReleased(asw::input::MouseButton button) { - return released[static_cast(button)]; - } - bool anyPressed{false}; int lastPressed{-1}; @@ -72,40 +38,43 @@ namespace asw::input { std::array down{false}; }; + /** + * @brief Global mouse state. + */ extern MouseState mouse; - using KeyState = struct KeyState { - /** - * @brief Check if a key is down. - * - * @param key The key to check. - * @return true - If the key is down. - * @return false - If the key is not down. - */ - bool isKeyDown(asw::input::Key key) { return down[static_cast(key)]; } - - /** - * @brief Check if a key was pressed since the last update. - * - * @param key The key to check. - * @return true - If the key was pressed. - * @return false - If the key was not pressed. - */ - bool wasKeyPressed(asw::input::Key key) { - return pressed[static_cast(key)]; - } - - /** - * @brief Check if a key was released since the last update. - * - * @param key The key to check. - * @return true - If the key was released. - * @return false - If the key was not released. - */ - bool wasKeyReleased(asw::input::Key key) { - return released[static_cast(key)]; - } + /** + * @brief Check if a button is down. + * + * @param button The button to check. + * @return true - If the button is down. + * @return false - If the button is not down. + */ + bool isButtonDown(asw::input::MouseButton button); + /** + * @brief Check if a button was pressed since the last update. + * + * @param button The button to check. + * @return true - If the button was pressed. + * @return false - If the button was not pressed. + */ + bool wasButtonPressed(asw::input::MouseButton button); + + /** + * @brief Check if a button was released since the last update. + * + * @param button The button to check. + * @return true - If the button was released. + * @return false - If the button was not released. + */ + bool wasButtonReleased(asw::input::MouseButton button); + + /** + * @brief Keyboard state stores the current state of the keyboard. It is + * updated by the core. + */ + using KeyState = struct KeyState { std::array pressed{false}; std::array released{false}; std::array down{false}; @@ -114,8 +83,38 @@ namespace asw::input { int lastPressed{-1}; }; + /** + * @brief Global keyboard state. + */ extern KeyState keyboard; + /** + * @brief Check if a key is down. + * + * @param key The key to check. + * @return true - If the key is down. + * @return false - If the key is not down. + */ + bool isKeyDown(asw::input::Key key); + + /** + * @brief Check if a key was pressed since the last update. + * + * @param key The key to check. + * @return true - If the key was pressed. + * @return false - If the key was not pressed. + */ + bool wasKeyPressed(asw::input::Key key); + + /** + * @brief Check if a key was released since the last update. + * + * @param key The key to check. + * @return true - If the key was released. + * @return false - If the key was not released. + */ + bool wasKeyReleased(asw::input::Key key); + void reset(); } // namespace asw::input diff --git a/src/modules/input.cpp b/src/modules/input.cpp index 90be8dd..1fc7ab4 100644 --- a/src/modules/input.cpp +++ b/src/modules/input.cpp @@ -33,4 +33,28 @@ void asw::input::reset() { for (auto& button : m_state.released) { button = false; } +} + +bool asw::input::isButtonDown(asw::input::MouseButton button) { + return mouse.down[static_cast(button)]; +} + +bool asw::input::wasButtonPressed(asw::input::MouseButton button) { + return mouse.pressed[static_cast(button)]; +} + +bool asw::input::wasButtonReleased(asw::input::MouseButton button) { + return mouse.released[static_cast(button)]; +} + +bool asw::input::isKeyDown(asw::input::Key key) { + return keyboard.down[static_cast(key)]; +} + +bool asw::input::wasKeyPressed(asw::input::Key key) { + return keyboard.pressed[static_cast(key)]; +} + +bool asw::input::wasKeyReleased(asw::input::Key key) { + return keyboard.released[static_cast(key)]; } \ No newline at end of file