11
22/*******************************************************************************************
33*
4- * raylib [core_input_actions ] example - presents a simple API for remapping input to actions
4+ * raylib [core_inputactionInputs ] example - presents a simple API for remapping input to actions
55*
66* Example complexity rating: [★☆☆☆] 1/4
77*
3030#include <stdlib.h>
3131#include <stdbool.h>
3232#include "raylib.h"
33+
3334// add your own action types here
35+
3436typedef enum ActionType
3537{
3638 NO_ACTION ,
@@ -51,63 +53,67 @@ typedef struct ActionInput
5153
5254// gamepad index, change this if you have multiple gamepads.
5355int gamepadIndex = 0 ;
54- static ActionInput _actions [MAX_ACTION ] = {0 };
56+ static ActionInput actionInputs [MAX_ACTION ] = {0 };
5557
5658// combines IsKeyPressed and IsGameButtonPressed to one action
5759bool isActionPressed (int action )
5860{
5961 if (action < MAX_ACTION )
60- return (IsKeyPressed (_actions [action ].key ) || IsGamepadButtonPressed (gamepadIndex , _actions [action ].button ));
62+ return (IsKeyPressed (actionInputs [action ].key ) || IsGamepadButtonPressed (gamepadIndex , actionInputs [action ].button ));
6163 return (false);
6264}
6365
6466// combines IsKeyReleased and IsGameButtonReleased to one action
6567bool isActionReleased (int action )
6668{
6769 if (action < MAX_ACTION )
68- return (IsKeyReleased (_actions [action ].key ) || IsGamepadButtonReleased (gamepadIndex , _actions [action ].button ));
70+ return (IsKeyReleased (actionInputs [action ].key ) || IsGamepadButtonReleased (gamepadIndex , actionInputs [action ].button ));
6971 return (false);
7072}
7173
7274// combines IsKeyDown and IsGameButtonDown to one action
7375bool isActionDown (int action )
7476{
7577 if (action < MAX_ACTION )
76- return (IsKeyDown (_actions [action ].key ) || IsGamepadButtonDown (gamepadIndex , _actions [action ].button ));
78+ return (IsKeyDown (actionInputs [action ].key ) || IsGamepadButtonDown (gamepadIndex , actionInputs [action ].button ));
7779 return (false);
7880}
7981// define the "default" keyset. here WASD and gamepad buttons on the left side for movement
8082void DefaultActions ()
8183{
82- _actions [ACTION_UP ].key = KEY_W ;
83- _actions [ACTION_DOWN ].key = KEY_S ;
84- _actions [ACTION_LEFT ].key = KEY_A ;
85- _actions [ACTION_RIGHT ].key = KEY_D ;
86- _actions [ACTION_FIRE ].key = KEY_SPACE ;
87-
88- _actions [ACTION_UP ].button = GAMEPAD_BUTTON_LEFT_FACE_UP ;
89- _actions [ACTION_DOWN ].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN ;
90- _actions [ACTION_LEFT ].button = GAMEPAD_BUTTON_LEFT_FACE_LEFT ;
91- _actions [ACTION_RIGHT ].button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT ;
92- _actions [ACTION_FIRE ].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ;
84+ actionInputs [ACTION_UP ].key = KEY_W ;
85+ actionInputs [ACTION_DOWN ].key = KEY_S ;
86+ actionInputs [ACTION_LEFT ].key = KEY_A ;
87+ actionInputs [ACTION_RIGHT ].key = KEY_D ;
88+ actionInputs [ACTION_FIRE ].key = KEY_SPACE ;
89+
90+ actionInputs [ACTION_UP ].button = GAMEPAD_BUTTON_LEFT_FACE_UP ;
91+ actionInputs [ACTION_DOWN ].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN ;
92+ actionInputs [ACTION_LEFT ].button = GAMEPAD_BUTTON_LEFT_FACE_LEFT ;
93+ actionInputs [ACTION_RIGHT ].button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT ;
94+ actionInputs [ACTION_FIRE ].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ;
9395}
9496
9597// define the "alternate" keyset. here Cursor Keys and gamepad buttons on the right side for movement
9698void CursorActions ()
9799{
98- _actions [ACTION_UP ].key = KEY_UP ;
99- _actions [ACTION_DOWN ].key = KEY_DOWN ;
100- _actions [ACTION_LEFT ].key = KEY_LEFT ;
101- _actions [ACTION_RIGHT ].key = KEY_RIGHT ;
102- _actions [ACTION_FIRE ].key = KEY_SPACE ;
103-
104- _actions [ACTION_UP ].button = GAMEPAD_BUTTON_RIGHT_FACE_UP ;
105- _actions [ACTION_DOWN ].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ;
106- _actions [ACTION_LEFT ].button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT ;
107- _actions [ACTION_RIGHT ].button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT ;
108- _actions [ACTION_FIRE ].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN ;
100+ actionInputs [ACTION_UP ].key = KEY_UP ;
101+ actionInputs [ACTION_DOWN ].key = KEY_DOWN ;
102+ actionInputs [ACTION_LEFT ].key = KEY_LEFT ;
103+ actionInputs [ACTION_RIGHT ].key = KEY_RIGHT ;
104+ actionInputs [ACTION_FIRE ].key = KEY_SPACE ;
105+
106+ actionInputs [ACTION_UP ].button = GAMEPAD_BUTTON_RIGHT_FACE_UP ;
107+ actionInputs [ACTION_DOWN ].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ;
108+ actionInputs [ACTION_LEFT ].button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT ;
109+ actionInputs [ACTION_RIGHT ].button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT ;
110+ actionInputs [ACTION_FIRE ].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN ;
109111}
110112
113+ //------------------------------------------------------------------------------------
114+ // Program main entry point
115+ //------------------------------------------------------------------------------------
116+
111117int main (int argc , char * * argv )
112118{
113119 const int screenWidth = 800 ;
@@ -117,7 +123,7 @@ int main(int argc, char **argv)
117123 SetWindowState (FLAG_WINDOW_RESIZABLE );
118124 SetTargetFPS (60 );
119125
120- // set defaul actions
126+ // set default actions
121127 char actionSet = 0 ;
122128 DefaultActions ();
123129
@@ -160,6 +166,9 @@ int main(int argc, char **argv)
160166 }
161167 //----------------------------------------------------------------------------------
162168 }
169+
170+ // De-Initialization
171+ //--------------------------------------------------------------------------------------
163172 CloseWindow (); // Close window and OpenGL context
164173 return 0 ;
165174}
0 commit comments