mirror of
https://github.com/raysan5/raylib.git
synced 2025-10-17 23:31:47 +00:00
REVIEWED: New example format and requirements: core_input_actions
#5211
This commit is contained in:
@@ -516,6 +516,7 @@ CORE = \
|
||||
core/core_custom_logging \
|
||||
core/core_drop_files \
|
||||
core/core_high_dpi \
|
||||
core/core_input_actions \
|
||||
core/core_input_gamepad \
|
||||
core/core_input_gestures \
|
||||
core/core_input_gestures_testbed \
|
||||
@@ -524,7 +525,6 @@ 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 \
|
||||
|
@@ -516,6 +516,7 @@ CORE = \
|
||||
core/core_custom_logging \
|
||||
core/core_drop_files \
|
||||
core/core_high_dpi \
|
||||
core/core_input_actions \
|
||||
core/core_input_gamepad \
|
||||
core/core_input_gestures \
|
||||
core/core_input_gestures_testbed \
|
||||
@@ -732,6 +733,9 @@ core/core_drop_files: core/core_drop_files.c
|
||||
core/core_high_dpi: core/core_high_dpi.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
|
||||
|
||||
core/core_input_actions: core/core_input_actions.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
|
||||
|
||||
core/core_input_gamepad: core/core_input_gamepad.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
|
||||
--preload-file core/resources/ps3.png@resources/ps3.png \
|
||||
|
@@ -17,9 +17,9 @@ You may find it easier to use than other toolchains, especially when it comes to
|
||||
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
|
||||
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
|
||||
|
||||
## EXAMPLES COLLECTION [TOTAL: 163]
|
||||
## EXAMPLES COLLECTION [TOTAL: 164]
|
||||
|
||||
### category: core [37]
|
||||
### category: core [38]
|
||||
|
||||
Examples using raylib[core](../src/rcore.c) platform functionality like window creation, inputs, drawing modes and system functionality.
|
||||
|
||||
@@ -62,6 +62,7 @@ Examples using raylib[core](../src/rcore.c) platform functionality like window c
|
||||
| [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) |
|
||||
| [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) |
|
||||
| [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) |
|
||||
| [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) |
|
||||
|
||||
### category: shapes [20]
|
||||
|
||||
|
@@ -1,41 +1,31 @@
|
||||
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core_inputactionInputs] example - presents a simple API for remapping input to actions
|
||||
* raylib [core] example - input actions
|
||||
*
|
||||
* Example complexity rating: [★☆☆☆] 1/4
|
||||
* Example complexity rating: [★★☆☆] 2/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 contributed by Jett (@JettMonstersGoBoom) 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
|
||||
* Copyright (c) 2025 Jett (@JettMonstersGoBoom)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
/*
|
||||
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
|
||||
*/
|
||||
// Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons
|
||||
// For example instead of using `IsKeyDown(KEY_LEFT)`, you can 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "raylib.h"
|
||||
|
||||
// add your own action types here
|
||||
|
||||
typedef enum ActionType
|
||||
{
|
||||
NO_ACTION,
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
typedef enum ActionType {
|
||||
NO_ACTION = 0,
|
||||
ACTION_UP,
|
||||
ACTION_DOWN,
|
||||
ACTION_LEFT,
|
||||
@@ -44,42 +34,137 @@ typedef enum ActionType
|
||||
MAX_ACTION
|
||||
} ActionType;
|
||||
|
||||
// struct for key and button inputs
|
||||
typedef struct ActionInput
|
||||
{
|
||||
// Key and button inputs
|
||||
typedef struct ActionInput {
|
||||
int key;
|
||||
int button;
|
||||
} ActionInput;
|
||||
|
||||
// gamepad index, change this if you have multiple gamepads.
|
||||
int gamepadIndex = 0;
|
||||
static ActionInput actionInputs[MAX_ACTION] = {0};
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static int gamepadIndex = 0; // Gamepad default index
|
||||
static ActionInput actionInputs[MAX_ACTION] = { 0 };
|
||||
|
||||
// combines IsKeyPressed and IsGameButtonPressed to one action
|
||||
bool isActionPressed(int action)
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
static bool IsActionPressed(int action); // Check action key/button pressed
|
||||
static bool IsActionReleased(int action); // Check action key/button released
|
||||
static bool IsActionDown(int action); // Check action key/button down
|
||||
|
||||
static void SetActionsDefault(void); // Set the "default" keyset
|
||||
static void SetActionsCursor(void); // Set the "alternate" keyset
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
if (action<MAX_ACTION)
|
||||
return (IsKeyPressed(actionInputs[action].key) || IsGamepadButtonPressed(gamepadIndex, actionInputs[action].button));
|
||||
return (false);
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - input actions");
|
||||
|
||||
// Set default actions
|
||||
char actionSet = 0;
|
||||
SetActionsDefault();
|
||||
|
||||
Vector2 position = (Vector2){ 400.0f, 200.0f };
|
||||
Vector2 size = (Vector2){ 40.0f, 40.0f };
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
gamepadIndex = 0; // set this to gamepad being checked
|
||||
if (IsActionDown(ACTION_UP)) position.y -= 2;
|
||||
if (IsActionDown(ACTION_DOWN)) position.y += 2;
|
||||
if (IsActionDown(ACTION_LEFT)) position.x -= 2;
|
||||
if (IsActionDown(ACTION_RIGHT)) position.x += 2;
|
||||
if (IsActionPressed(ACTION_FIRE))
|
||||
{
|
||||
position.x = (screenWidth-size.x)/2;
|
||||
position.y = (screenHeight-size.y)/2;
|
||||
}
|
||||
|
||||
// Switch control scheme by pressing TAB
|
||||
if (IsKeyPressed(KEY_TAB))
|
||||
{
|
||||
actionSet = !actionSet;
|
||||
if (actionSet == 0) SetActionsDefault();
|
||||
else SetActionsCursor();
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(GRAY);
|
||||
|
||||
DrawRectangleV(position, size, RED);
|
||||
|
||||
DrawText((actionSet == 0)? "Current input set: WASD (default)" : "Current input set: Cursor", 10, 10, 20, WHITE);
|
||||
DrawText("Use TAB key to toggles Actions keyset", 10, 50, 20, GREEN);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// combines IsKeyReleased and IsGameButtonReleased to one action
|
||||
bool isActionReleased(int action)
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Check action key/button pressed
|
||||
// NOTE: Combines key pressed and gamepad button pressed in one action
|
||||
static bool IsActionPressed(int action)
|
||||
{
|
||||
if (action<MAX_ACTION)
|
||||
return (IsKeyReleased(actionInputs[action].key) || IsGamepadButtonReleased(gamepadIndex, actionInputs[action].button));
|
||||
return (false);
|
||||
bool result = false;
|
||||
|
||||
if (action < MAX_ACTION) result = (IsKeyPressed(actionInputs[action].key) || IsGamepadButtonPressed(gamepadIndex, actionInputs[action].button));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// combines IsKeyDown and IsGameButtonDown to one action
|
||||
bool isActionDown(int action)
|
||||
// Check action key/button released
|
||||
// NOTE: Combines key released and gamepad button released in one action
|
||||
static bool IsActionReleased(int action)
|
||||
{
|
||||
if (action<MAX_ACTION)
|
||||
return (IsKeyDown(actionInputs[action].key) || IsGamepadButtonDown(gamepadIndex, actionInputs[action].button));
|
||||
return (false);
|
||||
bool result = false;
|
||||
|
||||
if (action < MAX_ACTION) result = (IsKeyReleased(actionInputs[action].key) || IsGamepadButtonReleased(gamepadIndex, actionInputs[action].button));
|
||||
|
||||
return result;
|
||||
}
|
||||
// define the "default" keyset. here WASD and gamepad buttons on the left side for movement
|
||||
void DefaultActions()
|
||||
|
||||
// Check action key/button down
|
||||
// NOTE: Combines key down and gamepad button down in one action
|
||||
static bool IsActionDown(int action)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (action < MAX_ACTION) result = (IsKeyDown(actionInputs[action].key) || IsGamepadButtonDown(gamepadIndex, actionInputs[action].button));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Set the "default" keyset
|
||||
// NOTE: Here WASD and gamepad buttons on the left side for movement
|
||||
static void SetActionsDefault(void)
|
||||
{
|
||||
actionInputs[ACTION_UP].key = KEY_W;
|
||||
actionInputs[ACTION_DOWN].key = KEY_S;
|
||||
@@ -94,8 +179,9 @@ void DefaultActions()
|
||||
actionInputs[ACTION_FIRE].button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN;
|
||||
}
|
||||
|
||||
// define the "alternate" keyset. here Cursor Keys and gamepad buttons on the right side for movement
|
||||
void CursorActions()
|
||||
// Set the "alternate" keyset
|
||||
// NOTE: Here cursor keys and gamepad buttons on the right side for movement
|
||||
static void SetActionsCursor(void)
|
||||
{
|
||||
actionInputs[ACTION_UP].key = KEY_UP;
|
||||
actionInputs[ACTION_DOWN].key = KEY_DOWN;
|
||||
@@ -109,66 +195,3 @@ void CursorActions()
|
||||
actionInputs[ACTION_RIGHT].button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT;
|
||||
actionInputs[ACTION_FIRE].button = GAMEPAD_BUTTON_LEFT_FACE_DOWN;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - input via actions");
|
||||
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||
SetTargetFPS(60);
|
||||
|
||||
// set default actions
|
||||
char actionSet = 0;
|
||||
DefaultActions();
|
||||
|
||||
Vector2 position = (Vector2){100, 100};
|
||||
Vector2 size = (Vector2){32, 32};
|
||||
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(DARKGRAY);
|
||||
DrawText(actionSet == 0 ? "WASD Default Set" : "Cursor Set", 0, 0, 18, WHITE);
|
||||
DrawText("Tab key toggles keyset", 0, 18, 18, WHITE);
|
||||
DrawRectangleV(position, size, RED);
|
||||
EndDrawing();
|
||||
|
||||
gamepadIndex = 0; // set this to gamepad being checked
|
||||
if (isActionDown(ACTION_UP))
|
||||
position.y -= 2;
|
||||
if (isActionDown(ACTION_DOWN))
|
||||
position.y += 2;
|
||||
if (isActionDown(ACTION_LEFT))
|
||||
position.x -= 2;
|
||||
if (isActionDown(ACTION_RIGHT))
|
||||
position.x += 2;
|
||||
if (isActionPressed(ACTION_FIRE))
|
||||
{
|
||||
position.x = (screenWidth-size.x)/2;
|
||||
position.y = (screenHeight-size.y)/2;
|
||||
}
|
||||
|
||||
// switch control scheme by pressing TAB
|
||||
if (IsKeyPressed(KEY_TAB))
|
||||
{
|
||||
actionSet = !actionSet;
|
||||
if (actionSet == 0)
|
||||
DefaultActions();
|
||||
else
|
||||
CursorActions();
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
return 0;
|
||||
}
|
BIN
examples/core/core_input_actions.png
Normal file
BIN
examples/core/core_input_actions.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
@@ -44,6 +44,7 @@ core;core_automation_events;★★★☆;5.0;5.0;2023;2025;"Ramon Santamaria";@r
|
||||
core;core_high_dpi;★★☆☆;5.0;5.5;2025;2025;"Jonathan Marler";@marler8997
|
||||
core;core_render_texture;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
||||
core;core_undo_redo;★★★☆;5.5;5.6;2025;2025;"Ramon Santamaria";@raysan5
|
||||
core;core_input_actions;★★☆☆;5.5;5.6;2025;2025;"Jett";@JettMonstersGoBoom
|
||||
shapes;shapes_basic_shapes;★☆☆☆;1.0;4.2;2014;2025;"Ramon Santamaria";@raysan5
|
||||
shapes;shapes_bouncing_ball;★☆☆☆;2.5;2.5;2013;2025;"Ramon Santamaria";@raysan5
|
||||
shapes;shapes_colors_palette;★★☆☆;1.0;2.5;2014;2025;"Ramon Santamaria";@raysan5
|
||||
|
Reference in New Issue
Block a user