Skip to content

Commit

Permalink
new input action system
Browse files Browse the repository at this point in the history
renamed constants for consistency
  • Loading branch information
durkisneer1 committed Dec 20, 2024
1 parent 35a5523 commit 500ac1b
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 134 deletions.
18 changes: 14 additions & 4 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ int main()

constexpr kn::Color bgColor = {21, 18, 37, 255};

kn::key::bind("left", {kn::S_LEFT, kn::S_a});
kn::key::bind("right", {kn::S_RIGHT, kn::S_d});
kn::input::bind("left", {
kn::InputAction(kn::S_a),
kn::InputAction(kn::S_LEFT),
kn::InputAction(kn::C_AXIS_LEFTX, false),
});
kn::input::bind("right", {
kn::InputAction(kn::S_d),
kn::InputAction(kn::S_RIGHT),
kn::InputAction(kn::C_AXIS_LEFTX, true),
});
kn::input::bind("jump", {
kn::InputAction(kn::S_SPACE),
kn::InputAction(kn::C_A),
});

const kn::TileMap tileMap("../example/assets/room.tmx");

Expand All @@ -23,10 +35,8 @@ int main()
const double dt = clock.tick() / 1000.0;

while (kn::window::pollEvent(event))
{
if (event.type == kn::KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
kn::window::close();
}

kn::window::clear(bgColor);

Expand Down
6 changes: 2 additions & 4 deletions example/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ Player::Player(const kn::TileMap& tileMap)

void Player::update(const double dt)
{
const auto* keys = kn::key::getPressed();
if (onGround)
{
if (keys[kn::S_SPACE] || kn::controller::isPressed(kn::CONTROLLER_A))
if (kn::input::isPressed("jump"))
{
velocity.y = -200;
onGround = false;
Expand All @@ -27,8 +26,7 @@ void Player::update(const double dt)
else
velocity.y += 980.7 * dt;

double xDir = kn::key::getDirection("left", "right").x + kn::controller::getLeftJoystick().x;
xDir = std::clamp(xDir, -1.0, 1.0);
const double xDir = kn::input::getDirection("left", "right").x;
if (xDir != 0.0)
{
animController.setAnim("walk");
Expand Down
47 changes: 27 additions & 20 deletions include/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,35 @@ constexpr SDL_KeyCode K_RIGHTPAREN = SDLK_RIGHTPAREN;
constexpr SDL_KeyCode K_UNDERSCORE = SDLK_UNDERSCORE;

// Mouse buttons
constexpr int MOUSE_LEFT = SDL_BUTTON_LEFT;
constexpr int MOUSE_MIDDLE = SDL_BUTTON_MIDDLE;
constexpr int MOUSE_RIGHT = SDL_BUTTON_RIGHT;
constexpr int MOUSE_X1 = SDL_BUTTON_X1;
constexpr int MOUSE_X2 = SDL_BUTTON_X2;
constexpr int M_LEFT = SDL_BUTTON_LEFT;
constexpr int M_MIDDLE = SDL_BUTTON_MIDDLE;
constexpr int M_RIGHT = SDL_BUTTON_RIGHT;
constexpr int M_SIDE1 = SDL_BUTTON_X1;
constexpr int M_SIDE2 = SDL_BUTTON_X2;

// Controller buttonsW
constexpr ControllerButton CONTROLLER_A = SDL_CONTROLLER_BUTTON_A;
constexpr ControllerButton CONTROLLER_B = SDL_CONTROLLER_BUTTON_B;
constexpr ControllerButton CONTROLLER_X = SDL_CONTROLLER_BUTTON_X;
constexpr ControllerButton CONTROLLER_Y = SDL_CONTROLLER_BUTTON_Y;
constexpr ControllerButton CONTROLLER_BACK = SDL_CONTROLLER_BUTTON_BACK;
constexpr ControllerButton CONTROLLER_GUIDE = SDL_CONTROLLER_BUTTON_GUIDE;
constexpr ControllerButton CONTROLLER_START = SDL_CONTROLLER_BUTTON_START;
constexpr ControllerButton CONTROLLER_LEFTSTICK = SDL_CONTROLLER_BUTTON_LEFTSTICK;
constexpr ControllerButton CONTROLLER_RIGHTSTICK = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
constexpr ControllerButton CONTROLLER_LEFTSHOULDER = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
constexpr ControllerButton CONTROLLER_RIGHTSHOULDER = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
constexpr ControllerButton CONTROLLER_DPAD_UP = SDL_CONTROLLER_BUTTON_DPAD_UP;
constexpr ControllerButton CONTROLLER_DPAD_DOWN = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
constexpr ControllerButton CONTROLLER_DPAD_LEFT = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
constexpr ControllerButton CONTROLLER_DPAD_RIGHT = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
constexpr ControllerButton C_A = SDL_CONTROLLER_BUTTON_A;
constexpr ControllerButton C_B = SDL_CONTROLLER_BUTTON_B;
constexpr ControllerButton C_X = SDL_CONTROLLER_BUTTON_X;
constexpr ControllerButton C_Y = SDL_CONTROLLER_BUTTON_Y;
constexpr ControllerButton C_BACK = SDL_CONTROLLER_BUTTON_BACK;
constexpr ControllerButton C_GUIDE = SDL_CONTROLLER_BUTTON_GUIDE;
constexpr ControllerButton C_START = SDL_CONTROLLER_BUTTON_START;
constexpr ControllerButton C_LEFTSTICK = SDL_CONTROLLER_BUTTON_LEFTSTICK;
constexpr ControllerButton C_RIGHTSTICK = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
constexpr ControllerButton C_LEFTSHOULDER = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
constexpr ControllerButton C_RIGHTSHOULDER = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
constexpr ControllerButton C_DPAD_UP = SDL_CONTROLLER_BUTTON_DPAD_UP;
constexpr ControllerButton C_DPAD_DOWN = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
constexpr ControllerButton C_DPAD_LEFT = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
constexpr ControllerButton C_DPAD_RIGHT = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;

constexpr ControllerAxis C_AXIS_LEFTX = SDL_CONTROLLER_AXIS_LEFTX;
constexpr ControllerAxis C_AXIS_LEFTY = SDL_CONTROLLER_AXIS_LEFTY;
constexpr ControllerAxis C_AXIS_RIGHTX = SDL_CONTROLLER_AXIS_RIGHTX;
constexpr ControllerAxis C_AXIS_RIGHTY = SDL_CONTROLLER_AXIS_RIGHTY;
constexpr ControllerAxis C_TRIGGERLEFT = SDL_CONTROLLER_AXIS_TRIGGERLEFT;
constexpr ControllerAxis C_TRIGGERRIGHT = SDL_CONTROLLER_AXIS_TRIGGERRIGHT;

constexpr unsigned int MILLISECONDS_PER_SECOND = 1000U;

Expand Down
4 changes: 2 additions & 2 deletions include/GameController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace controller
*
* @param button The button to check.
*
* @return True if the button is pressed, false otherwise. If the controller is not connected,
* false is returned.
* @return ``true`` if the button is pressed, ``false`` otherwise. If the controller is not
* connected, ``false`` is returned.
*/
[[nodiscard]] bool isPressed(ControllerButton button);

Expand Down
86 changes: 86 additions & 0 deletions include/Input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once

#include <string>
#include <vector>

#include "Constants.hpp"

namespace kn
{

enum class InputType
{
KEYBOARD,
MOUSE,
CONTROLLER_AXIS,
CONTROLLER_BUTTON
};

struct InputAction
{
InputType type;
union
{
Scancode key;
ControllerButton controllerButton;
int mouseButton;
struct
{
ControllerAxis axis;
bool isPositive;
} controllerAxis;
};

explicit InputAction(Scancode key);
InputAction(ControllerAxis axis, bool isPositive);
explicit InputAction(ControllerButton controllerButton);
explicit InputAction(int mouseButton);
};

namespace math
{
class Vec2;
} // namespace math

namespace input
{

/**
* @brief Bind a name to a set of input actions.
*
* @param name The name to bind to the input actions.
* @param actions The input actions to bind to the name.
*/
void bind(const std::string& name, const std::vector<InputAction>& actions);

/**
* @brief Unbind a name from the input actions.
*
* @param name The name to unbind from the input actions.
*/
void unbind(const std::string& name);

/**
* @brief Get a normalized direction vector based on the input actions.
*
* @param left The name of the input action for moving left.
* @param right The name of the input action for moving right.
* @param up The name of the input action for moving up.
* @param down The name of the input action for moving down.
*
* @return A normalized direction vector.
*/
[[nodiscard]] math::Vec2 getDirection(const std::string& left = "", const std::string& right = "",
const std::string& up = "", const std::string& down = "");

/**
* @brief Check if an input action is pressed.
*
* @param name The name of the input action to check.
*
* @return ``true`` if the input action is pressed, ``false`` otherwise.
*/
[[nodiscard]] bool isPressed(const std::string& name);

} // namespace input
} // namespace kn
43 changes: 1 addition & 42 deletions include/Key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include "Constants.hpp"

#include <string>
#include <vector>

namespace kn
{
namespace math
Expand All @@ -19,45 +16,7 @@ namespace key
*
* @return The keys that are currently pressed.
*/
const uint8_t* getPressed();

/**
* @brief Check if the argument key is pressed.
*
* @param name The name of the key binding.
*
* @return If the argument key is pressed.
*/
bool isPressed(const std::string& name);
[[nodiscard]] const uint8_t* getPressed();

/**
* @brief Bind a vector of keys to a string.
*
* @param name The name of the key binding.
* @param keys The keys to bind.
*
* @note If the name is empty, the function will return without binding the keys.
*/
void bind(const std::string& name, const std::vector<Scancode>& keys);

/**
* @brief Unbind the keys bound to the argument name.
*
* @param name The name of the key binding.
*/
void unbind(const std::string& name);

/**
* @brief Get the direction vector from the keys bound to the argument name.
*
* @param left The name of the key bindings for left movement.
* @param right The name of the key bindings for right movement.
* @param up The name of the key bindings for up movement.
* @param down The name of the key bindings for down movement.
*
* @return A direction vector.
*/
math::Vec2 getDirection(const std::string& left = "", const std::string& right = "",
const std::string& up = "", const std::string& down = "");
} // namespace key
} // namespace kn
1 change: 1 addition & 0 deletions include/KrakenEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Draw.hpp"
#include "Font.hpp"
#include "GameController.hpp"
#include "Input.hpp"
#include "Key.hpp"
#include "Math.hpp"
#include "Mouse.hpp"
Expand Down
12 changes: 6 additions & 6 deletions src/game_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ math::Vec2 getLeftJoystick()
if (!_controller)
return {};

math::Vec2 controllerDir = {SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_LEFTX),
SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_LEFTY)};
math::Vec2 controllerDir = {SDL_GameControllerGetAxis(_controller, C_AXIS_LEFTX),
SDL_GameControllerGetAxis(_controller, C_AXIS_LEFTY)};
controllerDir /= 32767.0;
if (controllerDir.getLength() > _deadZone)
return controllerDir;
Expand All @@ -27,8 +27,8 @@ math::Vec2 getRightJoystick()
if (!_controller)
return {};

math::Vec2 controllerDir = {SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_RIGHTX),
SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_RIGHTY)};
math::Vec2 controllerDir = {SDL_GameControllerGetAxis(_controller, C_AXIS_RIGHTX),
SDL_GameControllerGetAxis(_controller, C_AXIS_RIGHTY)};
controllerDir /= 32767.0;
if (controllerDir.getLength() > _deadZone)
return controllerDir;
Expand All @@ -41,15 +41,15 @@ double getLeftTrigger()
if (!_controller)
return 0.0;

return SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) / 32768.0;
return SDL_GameControllerGetAxis(_controller, C_TRIGGERLEFT) / 32768.0;
}

double getRightTrigger()
{
if (!_controller)
return 0.0;

return SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) / 32768.0;
return SDL_GameControllerGetAxis(_controller, C_TRIGGERRIGHT) / 32768.0;
}

bool isPressed(const ControllerButton button)
Expand Down
Loading

0 comments on commit 500ac1b

Please sign in to comment.