Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/actions/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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();
}

Expand All @@ -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!");
}
Expand All @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions include/asw/modules/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")) { /* ... */ }

Check warning on line 18 in include/asw/modules/action.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the misleading "/*" characters.

See more on https://sonarcloud.io/project/issues?id=AdsGames_asw&issues=AZ0WD8_9EnrKnygj9C1l&open=AZ0WD8_9EnrKnygj9C1l&pullRequest=44
/// @endcode

#ifndef ASW_ACTION_H
Expand Down Expand Up @@ -56,7 +57,8 @@
};

/// @brief A single input binding — keyboard, mouse button, controller button, or controller axis.
using ActionBinding = std::variant<KeyBinding, MouseButtonBinding, ControllerButtonBinding, ControllerAxisBinding>;
using ActionBinding
= std::variant<KeyBinding, MouseButtonBinding, ControllerButtonBinding, ControllerAxisBinding>;

/// @brief Register a binding for a named action.
///
Expand All @@ -83,21 +85,21 @@
/// @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).
///
Expand Down
45 changes: 45 additions & 0 deletions include/asw/modules/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@
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

Check warning on line 142 in include/asw/modules/geometry.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make this member overloaded operator a hidden friend.

See more on https://sonarcloud.io/project/issues?id=AdsGames_asw&issues=AZ0VtKYFEnrKnygj6qWI&open=AZ0VtKYFEnrKnygj6qWI&pullRequest=44
{
return Vec2(x % scalar, y % scalar);
}

/// @brief Addition assignment operator for the Vec2 class.
///
/// @param other The vector to add.
Expand Down Expand Up @@ -182,6 +192,18 @@
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.
Expand Down Expand Up @@ -311,6 +333,16 @@
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

Check warning on line 341 in include/asw/modules/geometry.h

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make this member overloaded operator a hidden friend.

See more on https://sonarcloud.io/project/issues?id=AdsGames_asw&issues=AZ0VtKYFEnrKnygj6qWJ&open=AZ0VtKYFEnrKnygj6qWJ&pullRequest=44
{
return Vec3(x % scalar, y % scalar, z % scalar);
}

/// @brief Addition assignment operator for the Vec3 class.
///
/// @param other The vector to add.
Expand Down Expand Up @@ -363,6 +395,19 @@
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.
Expand Down
2 changes: 1 addition & 1 deletion include/asw/modules/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/modules/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/modules/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading