switch-examples:

- added full controller support
- added multi-controller support
- added splitscreen support
- added touch screen support
- added full camera-controller support
- updated related examples
This commit is contained in:
Spencer Vaughn 2023-02-26 17:04:01 -06:00
commit 65a4a0092a

View file

@ -18,6 +18,7 @@
********************************************************************************************/
#include "raylib.h"
#include <switch.h>
// NOTE: Gamepad name ID depends on drivers and OS
#define XBOX360_LEGACY_NAME_ID "Xbox Controller"
@ -38,13 +39,15 @@ int main(void)
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
romfsInit();
SetConfigFlags(FLAG_MSAA_4X_HINT); // Set MSAA 4X hint before windows creation
InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
Texture2D texPs3Pad = LoadTexture("resources/ps3.png");
Texture2D texXboxPad = LoadTexture("resources/xbox.png");
Texture2D texPs3Pad = LoadTexture("romfs:/resources/ps3.png");
Texture2D texXboxPad = LoadTexture("romfs:/resources/xbox.png");
Texture2D texNinProPad = LoadTexture("romfs:/resources/switch_pro.png");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -66,7 +69,6 @@ int main(void)
if (IsGamepadAvailable(0))
{
DrawText(TextFormat("GP1: %s", GetGamepadName(0)), 10, 10, 10, BLACK);
if (TextIsEqual(GetGamepadName(0), XBOX360_NAME_ID) || TextIsEqual(GetGamepadName(0), XBOX360_LEGACY_NAME_ID))
{
DrawTexture(texXboxPad, 0, 0, DARKGRAY);
@ -98,13 +100,13 @@ int main(void)
DrawCircle(259, 152, 39, BLACK);
DrawCircle(259, 152, 34, LIGHTGRAY);
DrawCircle(259 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X)*20),
152 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK);
152 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK);
// Draw axis: right joystick
DrawCircle(461, 237, 38, BLACK);
DrawCircle(461, 237, 33, LIGHTGRAY);
DrawCircle(461 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X)*20),
237 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK);
DrawCircle(450, 240, 60, BLACK);
DrawCircle(450, 240, 33, LIGHTGRAY);
DrawCircle(450 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X)*20),
240 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK);
// Draw axis: left-right triggers
DrawRectangle(170, 30, 15, 70, GRAY);
@ -146,13 +148,13 @@ int main(void)
DrawCircle(319, 255, 35, BLACK);
DrawCircle(319, 255, 31, LIGHTGRAY);
DrawCircle(319 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X) * 20),
255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y) * 20), 25, BLACK);
255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y) * 20), 25, BLACK);
// Draw axis: right joystick
DrawCircle(475, 255, 35, BLACK);
DrawCircle(475, 255, 31, LIGHTGRAY);
DrawCircle(475 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X) * 20),
255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y) * 20), 25, BLACK);
255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y) * 20), 25, BLACK);
// Draw axis: left-right triggers
DrawRectangle(169, 48, 15, 70, GRAY);
@ -160,6 +162,59 @@ int main(void)
DrawRectangle(169, 48, 15, (int)(((1 - GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER)) / 2) * 70), RED);
DrawRectangle(611, 48, 15, (int)(((1 - GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER)) / 2) * 70), RED);
}
else if (TextIsEqual(GetGamepadName(0), "Nintendo Switch Pro Controller"))
{
DrawTexture(texNinProPad, screenWidth/2 - 400, screenHeight/2 - 352, DARKGRAY);
// Draw buttons: basic
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawCircle(screenWidth/2 + 92, 192, 15, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawCircle(screenWidth/2 - 99, 192, 15, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(screenWidth/2 + 143, 252, 25, GREEN);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(screenWidth/2 + 205, 308, 25, YELLOW);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(screenWidth/2 + 267, 252, 25, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(screenWidth/2 + 205, 196, 25, BLUE);
// Draw buttons: d-pad
// DrawRectangle(317, 202, 19, 71, BLACK);
// DrawRectangle(293, 228, 69, 19, BLACK);
DrawRectangle(499, 305, 40, 115, GRAY);
DrawRectangle(460, 346, 115, 40, GRAY);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(499, 305, 40, 30, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(499, 396, 40, 30, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(459, 346, 25, 40, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(550, 346, 26, 40, RED);
// Draw buttons: left-right back
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(259 + 230, 61, 20, RED);
if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(536 + 230, 61, 20, RED);
// Draw axis: left joystick
DrawCircle(421, 251, 70, BLACK);
DrawCircle(421, 251, 65, LIGHTGRAY);
DrawCircle(421 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X)*20),
251 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y)*-20), 35, BLACK);
// Draw axis: right joystick
DrawCircle(screenWidth/2 + 103, 360, 70, BLACK);
DrawCircle(screenWidth/2 + 103, 360, 65, LIGHTGRAY);
DrawCircle(screenWidth/2 + 103 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X)*20),
360 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y)*-20), 35, BLACK);
// joystick down press
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_THUMB)) DrawCircle(421 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X)*20),
251 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y)*-20), 35, RED);
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_THUMB)) DrawCircle(screenWidth/2 + 103 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X)*20),
360 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y)*-20), 35, RED);
// Draw axis: left-right triggers
DrawRectangle(170 + 230, 30, 15, 70, GRAY);
DrawRectangle(604 + 230, 30, 15, 70, GRAY);
DrawRectangle(170 + 230, 30, 15, (int)(((1 + GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER))/2)*70), RED);
DrawRectangle(604 + 230, 30, 15, (int)(((1 + GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER))/2)*70), RED);
//DrawText(TextFormat("Xbox axis LT: %02.02f", GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER)), 10, 40, 10, BLACK);
//DrawText(TextFormat("Xbox axis RT: %02.02f", GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER)), 10, 60, 10, BLACK);
}
else
{
DrawText("- GENERIC GAMEPAD -", 280, 180, 20, GRAY);
@ -192,9 +247,11 @@ int main(void)
//--------------------------------------------------------------------------------------
UnloadTexture(texPs3Pad);
UnloadTexture(texXboxPad);
UnloadTexture(texNinProPad);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
romfsExit();
return 0;
}