Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples] Add deadzone handling to core_input_gamepad example #4422

Merged
merged 2 commits into from Oct 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions examples/core/core_input_gamepad.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ int main(void)
Texture2D texPs3Pad = LoadTexture("resources/ps3.png");
Texture2D texXboxPad = LoadTexture("resources/xbox.png");

// Set axis deadzones
const float laxdz = 0.1f; // Left analog X
This conversation was marked as resolved.
Show resolved Hide resolved
const float laydz = 0.1f; // Left analog Y
const float raxdz = 0.1f; // Right analog X
const float raydz = 0.1f; // Right analog Y
const float ltrdz = -0.9f; // Left trigger
const float rtrdz = -0.9f; // Right trigger

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

Expand All @@ -67,6 +75,22 @@ int main(void)
{
DrawText(TextFormat("GP%d: %s", gamepad, GetGamepadName(gamepad)), 10, 10, 10, BLACK);

// Get axis values
float lax = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_X); // Left analog X
float lay = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_Y); // Left analog Y
float rax = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_X); // Right analog X
float ray = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_Y); // Right analog Y
float ltr = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER); // Left trigger
float rtr = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER); // Right trigger

// Calculate deadzones
if (lax > -laxdz && lax < laxdz) lax = 0.0f;
if (lay > -laydz && lay < laydz) lay = 0.0f;
if (rax > -raxdz && rax < raxdz) rax = 0.0f;
if (ray > -raydz && ray < raydz) ray = 0.0f;
if (ltr < ltrdz) ltr = -1.0f;
if (rtr < rtrdz) rtr = -1.0f;

if (TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_1) > -1 || TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_2) > -1)
{
DrawTexture(texXboxPad, 0, 0, DARKGRAY);
Expand Down Expand Up @@ -100,22 +124,22 @@ int main(void)
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
DrawCircle(259, 152, 39, BLACK);
DrawCircle(259, 152, 34, LIGHTGRAY);
DrawCircle(259 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_X)*20),
152 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_Y)*20), 25, leftGamepadColor);
DrawCircle(259 + (int)(lax*20),
152 + (int)(lay*20), 25, leftGamepadColor);

// Draw axis: right joystick
Color rightGamepadColor = BLACK;
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
DrawCircle(461, 237, 38, BLACK);
DrawCircle(461, 237, 33, LIGHTGRAY);
DrawCircle(461 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_X)*20),
237 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_Y)*20), 25, rightGamepadColor);
DrawCircle(461 + (int)(rax*20),
237 + (int)(ray*20), 25, rightGamepadColor);

// Draw axis: left-right triggers
DrawRectangle(170, 30, 15, 70, GRAY);
DrawRectangle(604, 30, 15, 70, GRAY);
DrawRectangle(170, 30, 15, (int)(((1 + GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER))/2)*70), RED);
DrawRectangle(604, 30, 15, (int)(((1 + GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER))/2)*70), RED);
DrawRectangle(170, 30, 15, (int)(((1 + ltr)/2)*70), RED);
DrawRectangle(604, 30, 15, (int)(((1 + rtr)/2)*70), RED);

//DrawText(TextFormat("Xbox axis LT: %02.02f", GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER)), 10, 40, 10, BLACK);
//DrawText(TextFormat("Xbox axis RT: %02.02f", GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER)), 10, 60, 10, BLACK);
Expand Down Expand Up @@ -150,24 +174,24 @@ int main(void)
// Draw axis: left joystick
Color leftGamepadColor = BLACK;
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
DrawCircle(319, 255, 35, leftGamepadColor);
DrawCircle(319, 255, 35, BLACK);
This conversation was marked as resolved.
Show resolved Hide resolved
DrawCircle(319, 255, 31, LIGHTGRAY);
DrawCircle(319 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_X) * 20),
255 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_Y) * 20), 25, leftGamepadColor);
DrawCircle(319 + (int)(lax*20),
255 + (int)(lay*20), 25, leftGamepadColor);

// Draw axis: right joystick
Color rightGamepadColor = BLACK;
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
DrawCircle(475, 255, 35, BLACK);
DrawCircle(475, 255, 31, LIGHTGRAY);
DrawCircle(475 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_X) * 20),
255 + (int)(GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_Y) * 20), 25, rightGamepadColor);
DrawCircle(475 + (int)(rax*20),
255 + (int)(ray*20), 25, rightGamepadColor);

// Draw axis: left-right triggers
DrawRectangle(169, 48, 15, 70, GRAY);
DrawRectangle(611, 48, 15, 70, GRAY);
DrawRectangle(169, 48, 15, (int)(((1 - GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER)) / 2) * 70), RED);
DrawRectangle(611, 48, 15, (int)(((1 - GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER)) / 2) * 70), RED);
DrawRectangle(169, 48, 15, (int)(((1 + ltr)/2)*70), RED);
DrawRectangle(611, 48, 15, (int)(((1 + rtr)/2)*70), RED);
}
else
{
Expand Down
Loading