1-
21/*******************************************************************************************
32*
4- * raylib [core_inputactionInputs ] example - presents a simple API for remapping input to actions
3+ * raylib [core ] example - input actions
54*
6- * Example complexity rating: [★☆☆☆] 1 /4
5+ * Example complexity rating: [★★☆☆] 2 /4
76*
87* Example originally created with raylib 5.5, last time updated with raylib 5.6
98*
10- * Example contributed by MonstersGoBoom and reviewed by Ramon Santamaria (@raysan5)
9+ * Example contributed by Jett (@JettMonstersGoBoom) and reviewed by Ramon Santamaria (@raysan5)
1110*
1211* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
1312* BSD-like license that allows static linking with closed source software
1413*
15- * Copyright (c) 2025 MonstersGoBoom
14+ * Copyright (c) 2025 Jett (@JettMonstersGoBoom)
1615*
1716********************************************************************************************/
1817
19- /*
20- Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons.
21- for example instead of
22- IsKeyDown(KEY_LEFT)
23- you'd use
24- IsActionDown(ACTION_LEFT)
25- 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
26- */
27-
18+ // Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons
19+ // For example instead of using `IsKeyDown(KEY_LEFT)`, you can use `IsActionDown(ACTION_LEFT)`
20+ // 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
2821
29- #include <stdio.h>
30- #include <stdlib.h>
31- #include <stdbool.h>
3222#include "raylib.h"
3323
34- // add your own action types here
35-
36- typedef enum ActionType
37- {
38- NO_ACTION ,
24+ //----------------------------------------------------------------------------------
25+ // Types and Structures Definition
26+ //----------------------------------------------------------------------------------
27+ typedef enum ActionType {
28+ NO_ACTION = 0 ,
3929 ACTION_UP ,
4030 ACTION_DOWN ,
4131 ACTION_LEFT ,
@@ -44,42 +34,137 @@ typedef enum ActionType
4434 MAX_ACTION
4535} ActionType ;
4636
47- // struct for key and button inputs
48- typedef struct ActionInput
49- {
37+ // Key and button inputs
38+ typedef struct ActionInput {
5039 int key ;
5140 int button ;
5241} ActionInput ;
5342
54- // gamepad index, change this if you have multiple gamepads.
55- int gamepadIndex = 0 ;
56- static ActionInput actionInputs [MAX_ACTION ] = {0 };
43+ //----------------------------------------------------------------------------------
44+ // Global Variables Definition
45+ //----------------------------------------------------------------------------------
46+ static int gamepadIndex = 0 ; // Gamepad default index
47+ static ActionInput actionInputs [MAX_ACTION ] = { 0 };
48+
49+ //----------------------------------------------------------------------------------
50+ // Module Functions Declaration
51+ //----------------------------------------------------------------------------------
52+ static bool IsActionPressed (int action ); // Check action key/button pressed
53+ static bool IsActionReleased (int action ); // Check action key/button released
54+ static bool IsActionDown (int action ); // Check action key/button down
5755
58- // combines IsKeyPressed and IsGameButtonPressed to one action
59- bool isActionPressed (int action )
56+ static void SetActionsDefault (void ); // Set the "default" keyset
57+ static void SetActionsCursor (void ); // Set the "alternate" keyset
58+
59+ //------------------------------------------------------------------------------------
60+ // Program main entry point
61+ //------------------------------------------------------------------------------------
62+ int main (void )
6063{
61- if (action < MAX_ACTION )
62- return (IsKeyPressed (actionInputs [action ].key ) || IsGamepadButtonPressed (gamepadIndex , actionInputs [action ].button ));
63- return (false);
64+ // Initialization
65+ //--------------------------------------------------------------------------------------
66+ const int screenWidth = 800 ;
67+ const int screenHeight = 450 ;
68+
69+ InitWindow (screenWidth , screenHeight , "raylib [core] example - input actions" );
70+
71+ // Set default actions
72+ char actionSet = 0 ;
73+ SetActionsDefault ();
74+
75+ Vector2 position = (Vector2 ){ 400.0f , 200.0f };
76+ Vector2 size = (Vector2 ){ 40.0f , 40.0f };
77+
78+ SetTargetFPS (60 );
79+ //--------------------------------------------------------------------------------------
80+
81+ // Main game loop
82+ while (!WindowShouldClose ()) // Detect window close button or ESC key
83+ {
84+ // Update
85+ //----------------------------------------------------------------------------------
86+ gamepadIndex = 0 ; // set this to gamepad being checked
87+ if (IsActionDown (ACTION_UP )) position .y -= 2 ;
88+ if (IsActionDown (ACTION_DOWN )) position .y += 2 ;
89+ if (IsActionDown (ACTION_LEFT )) position .x -= 2 ;
90+ if (IsActionDown (ACTION_RIGHT )) position .x += 2 ;
91+ if (IsActionPressed (ACTION_FIRE ))
92+ {
93+ position .x = (screenWidth - size .x )/2 ;
94+ position .y = (screenHeight - size .y )/2 ;
95+ }
96+
97+ // Switch control scheme by pressing TAB
98+ if (IsKeyPressed (KEY_TAB ))
99+ {
100+ actionSet = !actionSet ;
101+ if (actionSet == 0 ) SetActionsDefault ();
102+ else SetActionsCursor ();
103+ }
104+ //----------------------------------------------------------------------------------
105+
106+ // Draw
107+ //----------------------------------------------------------------------------------
108+ BeginDrawing ();
109+
110+ ClearBackground (GRAY );
111+
112+ DrawRectangleV (position , size , RED );
113+
114+ DrawText ((actionSet == 0 )? "Current input set: WASD (default)" : "Current input set: Cursor" , 10 , 10 , 20 , WHITE );
115+ DrawText ("Use TAB key to toggles Actions keyset" , 10 , 50 , 20 , GREEN );
116+
117+ EndDrawing ();
118+ //----------------------------------------------------------------------------------
119+ }
120+
121+ // De-Initialization
122+ //--------------------------------------------------------------------------------------
123+ CloseWindow (); // Close window and OpenGL context
124+ //--------------------------------------------------------------------------------------
125+
126+ return 0 ;
127+ }
128+
129+ //----------------------------------------------------------------------------------
130+ // Module Functions Definition
131+ //----------------------------------------------------------------------------------
132+ // Check action key/button pressed
133+ // NOTE: Combines key pressed and gamepad button pressed in one action
134+ static bool IsActionPressed (int action )
135+ {
136+ bool result = false;
137+
138+ if (action < MAX_ACTION ) result = (IsKeyPressed (actionInputs [action ].key ) || IsGamepadButtonPressed (gamepadIndex , actionInputs [action ].button ));
139+
140+ return result ;
64141}
65142
66- // combines IsKeyReleased and IsGameButtonReleased to one action
67- bool isActionReleased (int action )
143+ // Check action key/button released
144+ // NOTE: Combines key released and gamepad button released in one action
145+ static bool IsActionReleased (int action )
68146{
69- if (action < MAX_ACTION )
70- return (IsKeyReleased (actionInputs [action ].key ) || IsGamepadButtonReleased (gamepadIndex , actionInputs [action ].button ));
71- return (false);
147+ bool result = false;
148+
149+ if (action < MAX_ACTION ) result = (IsKeyReleased (actionInputs [action ].key ) || IsGamepadButtonReleased (gamepadIndex , actionInputs [action ].button ));
150+
151+ return result ;
72152}
73153
74- // combines IsKeyDown and IsGameButtonDown to one action
75- bool isActionDown (int action )
154+ // Check action key/button down
155+ // NOTE: Combines key down and gamepad button down in one action
156+ static bool IsActionDown (int action )
76157{
77- if (action < MAX_ACTION )
78- return (IsKeyDown (actionInputs [action ].key ) || IsGamepadButtonDown (gamepadIndex , actionInputs [action ].button ));
79- return (false);
158+ bool result = false;
159+
160+ if (action < MAX_ACTION ) result = (IsKeyDown (actionInputs [action ].key ) || IsGamepadButtonDown (gamepadIndex , actionInputs [action ].button ));
161+
162+ return result ;
80163}
81- // define the "default" keyset. here WASD and gamepad buttons on the left side for movement
82- void DefaultActions ()
164+
165+ // Set the "default" keyset
166+ // NOTE: Here WASD and gamepad buttons on the left side for movement
167+ static void SetActionsDefault (void )
83168{
84169 actionInputs [ACTION_UP ].key = KEY_W ;
85170 actionInputs [ACTION_DOWN ].key = KEY_S ;
@@ -94,8 +179,9 @@ void DefaultActions()
94179 actionInputs [ACTION_FIRE ].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ;
95180}
96181
97- // define the "alternate" keyset. here Cursor Keys and gamepad buttons on the right side for movement
98- void CursorActions ()
182+ // Set the "alternate" keyset
183+ // NOTE: Here cursor keys and gamepad buttons on the right side for movement
184+ static void SetActionsCursor (void )
99185{
100186 actionInputs [ACTION_UP ].key = KEY_UP ;
101187 actionInputs [ACTION_DOWN ].key = KEY_DOWN ;
@@ -109,66 +195,3 @@ void CursorActions()
109195 actionInputs [ACTION_RIGHT ].button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT ;
110196 actionInputs [ACTION_FIRE ].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN ;
111197}
112-
113- //------------------------------------------------------------------------------------
114- // Program main entry point
115- //------------------------------------------------------------------------------------
116-
117- int main (int argc , char * * argv )
118- {
119- const int screenWidth = 800 ;
120- const int screenHeight = 450 ;
121-
122- InitWindow (screenWidth , screenHeight , "raylib [core] example - input via actions" );
123- SetWindowState (FLAG_WINDOW_RESIZABLE );
124- SetTargetFPS (60 );
125-
126- // set default actions
127- char actionSet = 0 ;
128- DefaultActions ();
129-
130- Vector2 position = (Vector2 ){100 , 100 };
131- Vector2 size = (Vector2 ){32 , 32 };
132-
133- while (!WindowShouldClose ()) // Detect window close button or ESC key
134- {
135- //----------------------------------------------------------------------------------
136- BeginDrawing ();
137- ClearBackground (DARKGRAY );
138- DrawText (actionSet == 0 ? "WASD Default Set" : "Cursor Set" , 0 , 0 , 18 , WHITE );
139- DrawText ("Tab key toggles keyset" , 0 , 18 , 18 , WHITE );
140- DrawRectangleV (position , size , RED );
141- EndDrawing ();
142-
143- gamepadIndex = 0 ; // set this to gamepad being checked
144- if (isActionDown (ACTION_UP ))
145- position .y -= 2 ;
146- if (isActionDown (ACTION_DOWN ))
147- position .y += 2 ;
148- if (isActionDown (ACTION_LEFT ))
149- position .x -= 2 ;
150- if (isActionDown (ACTION_RIGHT ))
151- position .x += 2 ;
152- if (isActionPressed (ACTION_FIRE ))
153- {
154- position .x = (screenWidth - size .x )/2 ;
155- position .y = (screenHeight - size .y )/2 ;
156- }
157-
158- // switch control scheme by pressing TAB
159- if (IsKeyPressed (KEY_TAB ))
160- {
161- actionSet = !actionSet ;
162- if (actionSet == 0 )
163- DefaultActions ();
164- else
165- CursorActions ();
166- }
167- //----------------------------------------------------------------------------------
168- }
169-
170- // De-Initialization
171- //--------------------------------------------------------------------------------------
172- CloseWindow (); // Close window and OpenGL context
173- return 0 ;
174- }
0 commit comments