diff --git a/examples/actions/main.cpp b/examples/actions/main.cpp index 693d83f..0494528 100644 --- a/examples/actions/main.cpp +++ b/examples/actions/main.cpp @@ -4,7 +4,7 @@ /// Demonstrates: /// - Registering named actions with bind_action() /// - Binding the same action to multiple input sources (keyboard + controller) -/// - is_action_down(), is_action_pressed(), is_action_released() +/// - get_action_down(), get_action_up(), get_action() /// - get_action_strength() for analogue movement via a controller axis /// /// Bindings: @@ -73,7 +73,7 @@ int main() while (!asw::core::is_exiting()) { asw::core::update(); - if (asw::input::is_action_pressed("quit")) { + if (asw::input::get_action("quit")) { asw::core::exit(); } @@ -99,7 +99,7 @@ int main() } // Fire - if (asw::input::is_action_pressed("fire")) { + if (asw::input::get_action("fire")) { fire_frames = 12; asw::log::info("Fire!"); } @@ -118,9 +118,9 @@ int main() } // Player box - const bool moving = asw::input::is_action_down("move_left") - || asw::input::is_action_down("move_right") || asw::input::is_action_down("move_up") - || asw::input::is_action_down("move_down"); + const bool moving = asw::input::get_action("move_left") + || asw::input::get_action("move_right") || asw::input::get_action("move_up") + || asw::input::get_action("move_down"); asw::draw::rect_fill({ pos, { box_size, box_size } }, moving ? asw::color::cornflowerblue : asw::color::steelblue); asw::draw::rect({ pos, { box_size, box_size } }, asw::color::white); diff --git a/include/asw/modules/action.h b/include/asw/modules/action.h index 7897e5b..50e95cd 100644 --- a/include/asw/modules/action.h +++ b/include/asw/modules/action.h @@ -11,10 +11,11 @@ /// Example: /// @code /// asw::input::bind_action("jump", asw::input::KeyBinding{asw::input::Key::Space}); -/// asw::input::bind_action("jump", asw::input::ControllerButtonBinding{asw::input::ControllerButton::A}); +/// asw::input::bind_action("jump", +/// asw::input::ControllerButtonBinding{asw::input::ControllerButton::A}); /// /// // In game loop: -/// if (asw::input::is_action_pressed("jump")) { /* ... */ } +/// if (asw::input::get_action_down("jump")) { /* ... */ } /// @endcode #ifndef ASW_ACTION_H @@ -56,7 +57,8 @@ struct ControllerAxisBinding { }; /// @brief A single input binding — keyboard, mouse button, controller button, or controller axis. -using ActionBinding = std::variant; +using ActionBinding + = std::variant; /// @brief Register a binding for a named action. /// @@ -83,21 +85,21 @@ void clear_actions(); /// @param name The action name. /// @return true if any binding transitioned to active this frame. /// -bool is_action_pressed(std::string_view name); +bool get_action_down(std::string_view name); /// @brief Check if an action was released this frame. /// /// @param name The action name. /// @return true if any binding transitioned to inactive this frame. /// -bool is_action_released(std::string_view name); +bool get_action_up(std::string_view name); /// @brief Check if an action is currently held down. /// /// @param name The action name. /// @return true if any binding is currently active. /// -bool is_action_down(std::string_view name); +bool get_action(std::string_view name); /// @brief Get the analogue strength of an action (0.0 – 1.0). /// diff --git a/include/asw/modules/geometry.h b/include/asw/modules/geometry.h index 0a84f4f..988a1c7 100644 --- a/include/asw/modules/geometry.h +++ b/include/asw/modules/geometry.h @@ -134,6 +134,16 @@ template class Vec2 { return Vec2(x / scalar, y / scalar); } + /// @brief Modulo the Vec2 object by a scalar + /// + /// @param scalar The scalar to modulo by + /// @return Vec2 The result of the modulo operation. + /// + Vec2 operator%(const T scalar) const + { + return Vec2(x % scalar, y % scalar); + } + /// @brief Addition assignment operator for the Vec2 class. /// /// @param other The vector to add. @@ -182,6 +192,18 @@ template class Vec2 { return *this; } + /// @brief Modulo assignment operator for the Vec2 class. + /// + /// @param scalar The scalar to modulo by. + /// @return Vec2& The result of the modulo operation. + /// + Vec2& operator%=(const T scalar) + { + x %= scalar; + y %= scalar; + return *this; + } + /// @brief Equality operator for the Vec2 class. /// /// @param other The vector to compare. @@ -311,6 +333,16 @@ template class Vec3 { return Vec3(x / scalar, y / scalar, z / scalar); } + /// @brief Modulo the Vec3 object by a scalar + /// + /// @param scalar The scalar to modulo by + /// @return Vec3 The result of the modulo + /// + Vec3 operator%(const T scalar) const + { + return Vec3(x % scalar, y % scalar, z % scalar); + } + /// @brief Addition assignment operator for the Vec3 class. /// /// @param other The vector to add. @@ -363,6 +395,19 @@ template class Vec3 { return *this; } + /// @brief Modulo assignment operator for the Vec3 class. + /// + /// @param scalar The scalar to modulo by. + /// @return Vec3& The result of the modulo operation. + /// + Vec3& operator%=(const T scalar) + { + x %= scalar; + y %= scalar; + z %= scalar; + return *this; + } + /// @brief Equality operator for the Vec3 class. /// /// @param other The vector to compare. diff --git a/include/asw/modules/input.h b/include/asw/modules/input.h index 197cac8..16a42bf 100644 --- a/include/asw/modules/input.h +++ b/include/asw/modules/input.h @@ -509,7 +509,7 @@ void _mouse_button_up(uint8_t button); void _mouse_motion(float x, float y, float delta_x, float delta_y); /// @brief Mouse wheel hook -void _mouse_wheel(float delta_x, float delta_y); +void _mouse_wheel(float delta_z); /// @brief Controller added hook void _controller_added(SDL_JoystickID id); diff --git a/src/modules/action.cpp b/src/modules/action.cpp index a76d286..fa718cd 100644 --- a/src/modules/action.cpp +++ b/src/modules/action.cpp @@ -138,19 +138,19 @@ void asw::input::clear_actions() action_map.clear(); } -bool asw::input::is_action_pressed(std::string_view name) +bool asw::input::get_action_down(std::string_view name) { auto it = action_map.find(std::string(name)); return it != action_map.end() && it->second.pressed; } -bool asw::input::is_action_released(std::string_view name) +bool asw::input::get_action_up(std::string_view name) { auto it = action_map.find(std::string(name)); return it != action_map.end() && it->second.released; } -bool asw::input::is_action_down(std::string_view name) +bool asw::input::get_action(std::string_view name) { auto it = action_map.find(std::string(name)); return it != action_map.end() && it->second.down; diff --git a/src/modules/assets.cpp b/src/modules/assets.cpp index eaec0a4..215ddb2 100644 --- a/src/modules/assets.cpp +++ b/src/modules/assets.cpp @@ -88,10 +88,13 @@ asw::Texture asw::assets::create_texture(int w, int h) asw::util::abort_on_error("Renderer not initialized"); } - SDL_Texture* text + SDL_Texture* txr = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); - return { text, [](SDL_Texture* t) { + SDL_SetTextureScaleMode(txr, SDL_SCALEMODE_NEAREST); + SDL_SetTextureBlendMode(txr, SDL_BLENDMODE_BLEND); + + return { txr, [](SDL_Texture* t) { if (asw::display::get_renderer() != nullptr) { SDL_DestroyTexture(t); } diff --git a/src/modules/core.cpp b/src/modules/core.cpp index d47f8eb..1d731cd 100644 --- a/src/modules/core.cpp +++ b/src/modules/core.cpp @@ -89,7 +89,7 @@ void asw::core::update() } case SDL_EVENT_MOUSE_WHEEL: { - asw::input::_mouse_wheel(e.wheel.x, e.wheel.y); + asw::input::_mouse_wheel(e.wheel.y); break; } diff --git a/src/modules/input.cpp b/src/modules/input.cpp index 618998e..831311e 100644 --- a/src/modules/input.cpp +++ b/src/modules/input.cpp @@ -262,9 +262,9 @@ void asw::input::_mouse_motion(float x, float y, float delta_x, float delta_y) mouse.change.y = delta_y; } -void asw::input::_mouse_wheel(float x, float y) +void asw::input::_mouse_wheel(float delta_z) { - mouse.z = y; + mouse.z = delta_z; } void asw::input::_controller_added(SDL_JoystickID id)