Skip to content

Commit

Permalink
fix: use singleton key and mouse states in keydown function (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
alegemaate authored Sep 21, 2023
1 parent 0bf2536 commit 27816a2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 100 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Deploy Docs

on:
pull_request:
push:
branches: [main]

permissions:
Expand Down
73 changes: 39 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
target_include_directories(
${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
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)
Expand All @@ -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"
)
Expand All @@ -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)

Expand All @@ -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}
)
129 changes: 64 additions & 65 deletions include/asw/modules/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<int>(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<int>(button)];
}

bool anyPressed{false};
int lastPressed{-1};

Expand All @@ -72,40 +38,43 @@ namespace asw::input {
std::array<bool, ASW_NUM_MOUSE_BUTTONS> 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<int>(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<int>(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<int>(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<bool, ASW_NUM_KEYS> pressed{false};
std::array<bool, ASW_NUM_KEYS> released{false};
std::array<bool, ASW_NUM_KEYS> down{false};
Expand All @@ -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

Expand Down
24 changes: 24 additions & 0 deletions src/modules/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(button)];
}

bool asw::input::wasButtonPressed(asw::input::MouseButton button) {
return mouse.pressed[static_cast<int>(button)];
}

bool asw::input::wasButtonReleased(asw::input::MouseButton button) {
return mouse.released[static_cast<int>(button)];
}

bool asw::input::isKeyDown(asw::input::Key key) {
return keyboard.down[static_cast<int>(key)];
}

bool asw::input::wasKeyPressed(asw::input::Key key) {
return keyboard.pressed[static_cast<int>(key)];
}

bool asw::input::wasKeyReleased(asw::input::Key key) {
return keyboard.released[static_cast<int>(key)];
}

0 comments on commit 27816a2

Please sign in to comment.