From d325479ab7123ab5f9b434b17bb30898406aaf32 Mon Sep 17 00:00:00 2001 From: Jett Date: Sun, 28 Sep 2025 15:19:02 -0400 Subject: [PATCH 1/3] adding core_input_actions action based input API vs direct input, allows remapping of buttons or keys to an action. --- examples/Makefile | 1 + examples/core/core_input_actions.c | 163 +++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 examples/core/core_input_actions.c diff --git a/examples/Makefile b/examples/Makefile index ee17bb3509d7..6d409b51186c 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -524,6 +524,7 @@ CORE = \ core/core_input_mouse_wheel \ core/core_input_multitouch \ core/core_input_virtual_controls \ + core/core_input_actions \ core/core_random_sequence \ core/core_random_values \ core/core_render_texture \ diff --git a/examples/core/core_input_actions.c b/examples/core/core_input_actions.c new file mode 100644 index 000000000000..b9b18c21d5b7 --- /dev/null +++ b/examples/core/core_input_actions.c @@ -0,0 +1,163 @@ + +/******************************************************************************************* +* +* raylib [core_input_actions] example - presents a simple API for remapping input to actions +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.5, last time updated with raylib 5.6 +* +* Example contributed by MonstersGoBoom and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025 MonstersGoBoom +* +********************************************************************************************/ + +/* + Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons. + for example instead of + IsKeyDown(KEY_LEFT) + you'd use + IsActionDown(ACTION_LEFT) + which can be reassigned to e.g. KEY_A and also assigned to a gamepad button. the action will trigger with either gamepad or keys + */ + +#include +#include +#include +#include "raylib.h" +// add your own action types here +enum +{ + NO_ACTION, + ACTION_UP, + ACTION_DOWN, + ACTION_LEFT, + ACTION_RIGHT, + ACTION_FIRE, + MAX_ACTION +} Action_Type; + +// struct for key and button inputs +typedef struct +{ + int key; + int button; +} Action_Input_t; + +// gamepad index, change this if you have multiple gamepads. +int GamePadID = 0; +static Action_Input_t _actions[MAX_ACTION] = {0}; + +// combines IsKeyPressed and IsGameButtonPressed to one action +bool isActionPressed(int action) +{ + if (action Date: Sun, 28 Sep 2025 16:44:46 -0400 Subject: [PATCH 2/3] adjusted formatting. --- examples/core/core_input_actions.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/core/core_input_actions.c b/examples/core/core_input_actions.c index b9b18c21d5b7..633c3ee85755 100644 --- a/examples/core/core_input_actions.c +++ b/examples/core/core_input_actions.c @@ -25,12 +25,13 @@ which can be reassigned to e.g. KEY_A and also assigned to a gamepad button. the action will trigger with either gamepad or keys */ + #include #include #include #include "raylib.h" // add your own action types here -enum +typedef enum ActionType { NO_ACTION, ACTION_UP, @@ -39,24 +40,24 @@ enum ACTION_RIGHT, ACTION_FIRE, MAX_ACTION -} Action_Type; +} ActionType; // struct for key and button inputs -typedef struct +typedef struct ActionInput { int key; int button; -} Action_Input_t; +} ActionInput; // gamepad index, change this if you have multiple gamepads. -int GamePadID = 0; -static Action_Input_t _actions[MAX_ACTION] = {0}; +int gamepadIndex = 0; +static ActionInput _actions[MAX_ACTION] = {0}; // combines IsKeyPressed and IsGameButtonPressed to one action bool isActionPressed(int action) { if (action Date: Sun, 28 Sep 2025 16:47:40 -0400 Subject: [PATCH 3/3] updates for struct name and fixed a typo --- examples/core/core_input_actions.c | 65 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/examples/core/core_input_actions.c b/examples/core/core_input_actions.c index 633c3ee85755..b8266a8f4642 100644 --- a/examples/core/core_input_actions.c +++ b/examples/core/core_input_actions.c @@ -1,7 +1,7 @@ /******************************************************************************************* * -* raylib [core_input_actions] example - presents a simple API for remapping input to actions +* raylib [core_inputactionInputs] example - presents a simple API for remapping input to actions * * Example complexity rating: [★☆☆☆] 1/4 * @@ -30,7 +30,9 @@ #include #include #include "raylib.h" + // add your own action types here + typedef enum ActionType { NO_ACTION, @@ -51,13 +53,13 @@ typedef struct ActionInput // gamepad index, change this if you have multiple gamepads. int gamepadIndex = 0; -static ActionInput _actions[MAX_ACTION] = {0}; +static ActionInput actionInputs[MAX_ACTION] = {0}; // combines IsKeyPressed and IsGameButtonPressed to one action bool isActionPressed(int action) { if (action