1 | /******************************************************************************************* |
2 | * |
3 | * raylib [core] example - Gamepad input |
4 | * |
5 | * NOTE: This example requires a Gamepad connected to the system |
6 | * raylib is configured to work with the following gamepads: |
7 | * - Xbox 360 Controller (Xbox 360, Xbox One) |
8 | * - PLAYSTATION(R)3 Controller |
9 | * Check raylib.h for buttons configuration |
10 | * |
11 | * This example has been created using raylib 2.5 (www.raylib.com) |
12 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
13 | * |
14 | * Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) |
15 | * |
16 | ********************************************************************************************/ |
17 | |
18 | #include "raylib.h" |
19 | |
20 | // NOTE: Gamepad name ID depends on drivers and OS |
21 | #if defined(PLATFORM_RPI) |
22 | #define XBOX360_NAME_ID "Microsoft X-Box 360 pad" |
23 | #define PS3_NAME_ID "PLAYSTATION(R)3 Controller" |
24 | #else |
25 | #define XBOX360_NAME_ID "Xbox 360 Controller" |
26 | #define PS3_NAME_ID "PLAYSTATION(R)3 Controller" |
27 | #endif |
28 | |
29 | int main(void) |
30 | { |
31 | // Initialization |
32 | //-------------------------------------------------------------------------------------- |
33 | const int screenWidth = 800; |
34 | const int screenHeight = 450; |
35 | |
36 | SetConfigFlags(FLAG_MSAA_4X_HINT); // Set MSAA 4X hint before windows creation |
37 | |
38 | InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input" ); |
39 | |
40 | Texture2D texPs3Pad = LoadTexture("resources/ps3.png" ); |
41 | Texture2D texXboxPad = LoadTexture("resources/xbox.png" ); |
42 | |
43 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
44 | //-------------------------------------------------------------------------------------- |
45 | |
46 | // Main game loop |
47 | while (!WindowShouldClose()) // Detect window close button or ESC key |
48 | { |
49 | // Update |
50 | //---------------------------------------------------------------------------------- |
51 | // ... |
52 | //---------------------------------------------------------------------------------- |
53 | |
54 | // Draw |
55 | //---------------------------------------------------------------------------------- |
56 | BeginDrawing(); |
57 | |
58 | ClearBackground(RAYWHITE); |
59 | |
60 | if (IsGamepadAvailable(GAMEPAD_PLAYER1)) |
61 | { |
62 | DrawText(FormatText("GP1: %s" , GetGamepadName(GAMEPAD_PLAYER1)), 10, 10, 10, BLACK); |
63 | |
64 | if (IsGamepadName(GAMEPAD_PLAYER1, XBOX360_NAME_ID)) |
65 | { |
66 | DrawTexture(texXboxPad, 0, 0, DARKGRAY); |
67 | |
68 | // Draw buttons: xbox home |
69 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(394, 89, 19, RED); |
70 | |
71 | // Draw buttons: basic |
72 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawCircle(436, 150, 9, RED); |
73 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawCircle(352, 150, 9, RED); |
74 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(501, 151, 15, BLUE); |
75 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(536, 187, 15, LIME); |
76 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(572, 151, 15, MAROON); |
77 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(536, 115, 15, GOLD); |
78 | |
79 | // Draw buttons: d-pad |
80 | DrawRectangle(317, 202, 19, 71, BLACK); |
81 | DrawRectangle(293, 228, 69, 19, BLACK); |
82 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(317, 202, 19, 26, RED); |
83 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(317, 202 + 45, 19, 26, RED); |
84 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(292, 228, 25, 19, RED); |
85 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(292 + 44, 228, 26, 19, RED); |
86 | |
87 | // Draw buttons: left-right back |
88 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(259, 61, 20, RED); |
89 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(536, 61, 20, RED); |
90 | |
91 | // Draw axis: left joystick |
92 | DrawCircle(259, 152, 39, BLACK); |
93 | DrawCircle(259, 152, 34, LIGHTGRAY); |
94 | DrawCircle(259 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_X)*20), |
95 | 152 - (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK); |
96 | |
97 | // Draw axis: right joystick |
98 | DrawCircle(461, 237, 38, BLACK); |
99 | DrawCircle(461, 237, 33, LIGHTGRAY); |
100 | DrawCircle(461 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_X)*20), |
101 | 237 - (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK); |
102 | |
103 | // Draw axis: left-right triggers |
104 | DrawRectangle(170, 30, 15, 70, GRAY); |
105 | DrawRectangle(604, 30, 15, 70, GRAY); |
106 | DrawRectangle(170, 30, 15, (((1.0f + GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_TRIGGER))/2.0f)*70), RED); |
107 | DrawRectangle(604, 30, 15, (((1.0f + GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_TRIGGER))/2.0f)*70), RED); |
108 | |
109 | //DrawText(FormatText("Xbox axis LT: %02.02f", GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_TRIGGER)), 10, 40, 10, BLACK); |
110 | //DrawText(FormatText("Xbox axis RT: %02.02f", GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_TRIGGER)), 10, 60, 10, BLACK); |
111 | } |
112 | else if (IsGamepadName(GAMEPAD_PLAYER1, PS3_NAME_ID)) |
113 | { |
114 | DrawTexture(texPs3Pad, 0, 0, DARKGRAY); |
115 | |
116 | // Draw buttons: ps |
117 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(396, 222, 13, RED); |
118 | |
119 | // Draw buttons: basic |
120 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawRectangle(328, 170, 32, 13, RED); |
121 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED); |
122 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(557, 144, 13, LIME); |
123 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(586, 173, 13, RED); |
124 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(557, 203, 13, VIOLET); |
125 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(527, 173, 13, PINK); |
126 | |
127 | // Draw buttons: d-pad |
128 | DrawRectangle(225, 132, 24, 84, BLACK); |
129 | DrawRectangle(195, 161, 84, 25, BLACK); |
130 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(225, 132, 24, 29, RED); |
131 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(225, 132 + 54, 24, 30, RED); |
132 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(195, 161, 30, 25, RED); |
133 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(195 + 54, 161, 30, 25, RED); |
134 | |
135 | // Draw buttons: left-right back buttons |
136 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(239, 82, 20, RED); |
137 | if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(557, 82, 20, RED); |
138 | |
139 | // Draw axis: left joystick |
140 | DrawCircle(319, 255, 35, BLACK); |
141 | DrawCircle(319, 255, 31, LIGHTGRAY); |
142 | DrawCircle(319 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_X)*20), |
143 | 255 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK); |
144 | |
145 | // Draw axis: right joystick |
146 | DrawCircle(475, 255, 35, BLACK); |
147 | DrawCircle(475, 255, 31, LIGHTGRAY); |
148 | DrawCircle(475 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_X)*20), |
149 | 255 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK); |
150 | |
151 | // Draw axis: left-right triggers |
152 | DrawRectangle(169, 48, 15, 70, GRAY); |
153 | DrawRectangle(611, 48, 15, 70, GRAY); |
154 | DrawRectangle(169, 48, 15, (((1.0f - GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_LEFT_TRIGGER))/2.0f)*70), RED); |
155 | DrawRectangle(611, 48, 15, (((1.0f - GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_AXIS_RIGHT_TRIGGER))/2.0f)*70), RED); |
156 | } |
157 | else |
158 | { |
159 | DrawText("- GENERIC GAMEPAD -" , 280, 180, 20, GRAY); |
160 | |
161 | // TODO: Draw generic gamepad |
162 | } |
163 | |
164 | DrawText(FormatText("DETECTED AXIS [%i]:" , GetGamepadAxisCount(GAMEPAD_PLAYER1)), 10, 50, 10, MAROON); |
165 | |
166 | for (int i = 0; i < GetGamepadAxisCount(GAMEPAD_PLAYER1); i++) |
167 | { |
168 | DrawText(FormatText("AXIS %i: %.02f" , i, GetGamepadAxisMovement(GAMEPAD_PLAYER1, i)), 20, 70 + 20*i, 10, DARKGRAY); |
169 | } |
170 | |
171 | if (GetGamepadButtonPressed() != -1) DrawText(FormatText("DETECTED BUTTON: %i" , GetGamepadButtonPressed()), 10, 430, 10, RED); |
172 | else DrawText("DETECTED BUTTON: NONE" , 10, 430, 10, GRAY); |
173 | } |
174 | else |
175 | { |
176 | DrawText("GP1: NOT DETECTED" , 10, 10, 10, GRAY); |
177 | |
178 | DrawTexture(texXboxPad, 0, 0, LIGHTGRAY); |
179 | } |
180 | |
181 | EndDrawing(); |
182 | //---------------------------------------------------------------------------------- |
183 | } |
184 | |
185 | // De-Initialization |
186 | //-------------------------------------------------------------------------------------- |
187 | UnloadTexture(texPs3Pad); |
188 | UnloadTexture(texXboxPad); |
189 | |
190 | CloseWindow(); // Close window and OpenGL context |
191 | //-------------------------------------------------------------------------------------- |
192 | |
193 | return 0; |
194 | } |