From d325479ab7123ab5f9b434b17bb30898406aaf32 Mon Sep 17 00:00:00 2001 From: Jett Date: Sun, 28 Sep 2025 15:19:02 -0400 Subject: [PATCH] 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 ee17bb350..6d409b511 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 000000000..b9b18c21d --- /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