Skip to content

Commit d5e8ee7

Browse files
committed
Update core_input_gamepad.c
1 parent f512048 commit d5e8ee7

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

examples/core/core_input_gamepad.c

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ int main(void)
5050
const float rightStickDeadzoneY = 0.1f;
5151
const float leftTriggerDeadzone = -0.9f;
5252
const float rightTriggerDeadzone = -0.9f;
53+
54+
Rectangle vibrateButton = { 0 };
5355

5456
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
5557
//--------------------------------------------------------------------------------------
@@ -61,7 +63,12 @@ int main(void)
6163
{
6264
// Update
6365
//----------------------------------------------------------------------------------
64-
// ...
66+
if (IsKeyPressed(KEY_LEFT) && gamepad > 0) gamepad--;
67+
if (IsKeyPressed(KEY_RIGHT)) gamepad++;
68+
Vector2 mousePosition = GetMousePosition();
69+
70+
vibrateButton = (Rectangle){ 10, 70 + 20*GetGamepadAxisCount(gamepad) + 20, 75, 24 };
71+
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && CheckCollisionPointRec(mousePosition, vibrateButton)) SetGamepadVibration(gamepad, 1.0, 1.0, 1.0);
6572
//----------------------------------------------------------------------------------
6673

6774
// Draw
@@ -70,11 +77,6 @@ int main(void)
7077

7178
ClearBackground(RAYWHITE);
7279

73-
if (IsKeyPressed(KEY_LEFT) && gamepad > 0) gamepad--;
74-
if (IsKeyPressed(KEY_RIGHT)) gamepad++;
75-
Vector2 mousePosition = GetMousePosition();
76-
bool mousePressed = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
77-
7880
if (IsGamepadAvailable(gamepad))
7981
{
8082
DrawText(TextFormat("GP%d: %s", gamepad, GetGamepadName(gamepad)), 10, 10, 10, BLACK);
@@ -95,7 +97,8 @@ int main(void)
9597
if (leftTrigger < leftTriggerDeadzone) leftTrigger = -1.0f;
9698
if (rightTrigger < rightTriggerDeadzone) rightTrigger = -1.0f;
9799

98-
if (TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_1) > -1 || TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_2) > -1)
100+
if ((TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_1) > -1) ||
101+
(TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_2) > -1))
99102
{
100103
DrawTexture(texXboxPad, 0, 0, DARKGRAY);
101104

@@ -127,16 +130,14 @@ int main(void)
127130
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
128131
DrawCircle(259, 152, 39, BLACK);
129132
DrawCircle(259, 152, 34, LIGHTGRAY);
130-
DrawCircle(259 + (int)(leftStickX*20),
131-
152 + (int)(leftStickY*20), 25, leftGamepadColor);
133+
DrawCircle(259 + (int)(leftStickX*20), 152 + (int)(leftStickY*20), 25, leftGamepadColor);
132134

133135
// Draw axis: right joystick
134136
Color rightGamepadColor = BLACK;
135137
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
136138
DrawCircle(461, 237, 38, BLACK);
137139
DrawCircle(461, 237, 33, LIGHTGRAY);
138-
DrawCircle(461 + (int)(rightStickX*20),
139-
237 + (int)(rightStickY*20), 25, rightGamepadColor);
140+
DrawCircle(461 + (int)(rightStickX*20), 237 + (int)(rightStickY*20), 25, rightGamepadColor);
140141

141142
// Draw axis: left-right triggers
142143
DrawRectangle(170, 30, 15, 70, GRAY);
@@ -179,16 +180,14 @@ int main(void)
179180
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
180181
DrawCircle(319, 255, 35, BLACK);
181182
DrawCircle(319, 255, 31, LIGHTGRAY);
182-
DrawCircle(319 + (int)(leftStickX*20),
183-
255 + (int)(leftStickY*20), 25, leftGamepadColor);
183+
DrawCircle(319 + (int)(leftStickX*20), 255 + (int)(leftStickY*20), 25, leftGamepadColor);
184184

185185
// Draw axis: right joystick
186186
Color rightGamepadColor = BLACK;
187187
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
188188
DrawCircle(475, 255, 35, BLACK);
189189
DrawCircle(475, 255, 31, LIGHTGRAY);
190-
DrawCircle(475 + (int)(rightStickX*20),
191-
255 + (int)(rightStickY*20), 25, rightGamepadColor);
190+
DrawCircle(475 + (int)(rightStickX*20), 255 + (int)(rightStickY*20), 25, rightGamepadColor);
192191

193192
// Draw axis: left-right triggers
194193
DrawRectangle(169, 48, 15, 70, GRAY);
@@ -238,23 +237,20 @@ int main(void)
238237
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
239238
DrawCircle(345, 260, 40, BLACK);
240239
DrawCircle(345, 260, 35, LIGHTGRAY);
241-
DrawCircle(345 + (int)(leftStickX*20),
242-
260 + (int)(leftStickY*20), 25, leftGamepadColor);
240+
DrawCircle(345 + (int)(leftStickX*20), 260 + (int)(leftStickY*20), 25, leftGamepadColor);
243241

244242
// Draw axis: right joystick
245243
Color rightGamepadColor = BLACK;
246244
if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
247245
DrawCircle(465, 260, 40, BLACK);
248246
DrawCircle(465, 260, 35, LIGHTGRAY);
249-
DrawCircle(465 + (int)(rightStickX*20),
250-
260 + (int)(rightStickY*20), 25, rightGamepadColor);
247+
DrawCircle(465 + (int)(rightStickX*20), 260 + (int)(rightStickY*20), 25, rightGamepadColor);
251248

252249
// Draw axis: left-right triggers
253250
DrawRectangle(151, 110, 15, 70, GRAY);
254251
DrawRectangle(644, 110, 15, 70, GRAY);
255252
DrawRectangle(151, 110, 15, (int)(((1 + leftTrigger)/2)*70), RED);
256253
DrawRectangle(644, 110, 15, (int)(((1 + rightTrigger)/2)*70), RED);
257-
258254
}
259255

260256
DrawText(TextFormat("DETECTED AXIS [%i]:", GetGamepadAxisCount(gamepad)), 10, 50, 10, MAROON);
@@ -264,12 +260,8 @@ int main(void)
264260
DrawText(TextFormat("AXIS %i: %.02f", i, GetGamepadAxisMovement(gamepad, i)), 20, 70 + 20*i, 10, DARKGRAY);
265261
}
266262

267-
Rectangle vibrateButton = (Rectangle){10, 70 + 20*GetGamepadAxisCount(gamepad) + 20, 75, 10};
268-
if (mousePressed && CheckCollisionPointRec(mousePosition, vibrateButton)){
269-
SetGamepadVibration(gamepad, 1.0, 1.0, 1.0);
270-
}
263+
// Draw vibrate button
271264
DrawRectangleRec(vibrateButton, SKYBLUE);
272-
273265
DrawText("VIBRATE", vibrateButton.x + 14, vibrateButton.y + 1, 10, DARKGRAY);
274266

275267
if (GetGamepadButtonPressed() != GAMEPAD_BUTTON_UNKNOWN) DrawText(TextFormat("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED);

0 commit comments

Comments
 (0)