Skip to content

Commit 7d780d1

Browse files
committed
REVIEWED: New example format and requirements: core_input_actions #5211
1 parent a2a22e5 commit 7d780d1

File tree

8 files changed

+738
-113
lines changed

8 files changed

+738
-113
lines changed

examples/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ CORE = \
516516
core/core_custom_logging \
517517
core/core_drop_files \
518518
core/core_high_dpi \
519+
core/core_input_actions \
519520
core/core_input_gamepad \
520521
core/core_input_gestures \
521522
core/core_input_gestures_testbed \
@@ -524,7 +525,6 @@ CORE = \
524525
core/core_input_mouse_wheel \
525526
core/core_input_multitouch \
526527
core/core_input_virtual_controls \
527-
core/core_input_actions \
528528
core/core_random_sequence \
529529
core/core_random_values \
530530
core/core_render_texture \

examples/Makefile.Web

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ CORE = \
516516
core/core_custom_logging \
517517
core/core_drop_files \
518518
core/core_high_dpi \
519+
core/core_input_actions \
519520
core/core_input_gamepad \
520521
core/core_input_gestures \
521522
core/core_input_gestures_testbed \
@@ -732,6 +733,9 @@ core/core_drop_files: core/core_drop_files.c
732733
core/core_high_dpi: core/core_high_dpi.c
733734
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
734735

736+
core/core_input_actions: core/core_input_actions.c
737+
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
738+
735739
core/core_input_gamepad: core/core_input_gamepad.c
736740
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
737741
--preload-file core/resources/ps3.png@resources/ps3.png \

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ You may find it easier to use than other toolchains, especially when it comes to
1717
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
1818
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
1919

20-
## EXAMPLES COLLECTION [TOTAL: 163]
20+
## EXAMPLES COLLECTION [TOTAL: 164]
2121

22-
### category: core [37]
22+
### category: core [38]
2323

2424
Examples using raylib[core](../src/rcore.c) platform functionality like window creation, inputs, drawing modes and system functionality.
2525

@@ -62,6 +62,7 @@ Examples using raylib[core](../src/rcore.c) platform functionality like window c
6262
| [core_high_dpi](core/core_high_dpi.c) | <img src="core/core_high_dpi.png" alt="core_high_dpi" width="80"> | ⭐⭐☆☆ | 5.0 | 5.5 | [Jonathan Marler](https://github.com/marler8997) |
6363
| [core_render_texture](core/core_render_texture.c) | <img src="core/core_render_texture.png" alt="core_render_texture" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
6464
| [core_undo_redo](core/core_undo_redo.c) | <img src="core/core_undo_redo.png" alt="core_undo_redo" width="80"> | ⭐⭐⭐☆ | 5.5 | 5.6 | [Ramon Santamaria](https://github.com/raysan5) |
65+
| [core_input_actions](core/core_input_actions.c) | <img src="core/core_input_actions.png" alt="core_input_actions" width="80"> | ⭐⭐☆☆ | 5.5 | 5.6 | [Jett](https://github.com/JettMonstersGoBoom) |
6566

6667
### category: shapes [20]
6768

examples/core/core_input_actions.c

Lines changed: 133 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
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-
}
15.1 KB
Loading

examples/examples_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ core;core_automation_events;★★★☆;5.0;5.0;2023;2025;"Ramon Santamaria";@r
4444
core;core_high_dpi;★★☆☆;5.0;5.5;2025;2025;"Jonathan Marler";@marler8997
4545
core;core_render_texture;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
4646
core;core_undo_redo;★★★☆;5.5;5.6;2025;2025;"Ramon Santamaria";@raysan5
47+
core;core_input_actions;★★☆☆;5.5;5.6;2025;2025;"Jett";@JettMonstersGoBoom
4748
shapes;shapes_basic_shapes;★☆☆☆;1.0;4.2;2014;2025;"Ramon Santamaria";@raysan5
4849
shapes;shapes_bouncing_ball;★☆☆☆;2.5;2.5;2013;2025;"Ramon Santamaria";@raysan5
4950
shapes;shapes_colors_palette;★★☆☆;1.0;2.5;2014;2025;"Ramon Santamaria";@raysan5

0 commit comments

Comments
 (0)