Update and rename core_input_gamepad_virtual.c to core_virtual_Dpad.c

This commit is contained in:
Magnus Oblerion 2024-09-30 13:06:13 +02:00 committed by GitHub
parent e6c06f1f38
commit a9cdae8dba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
/*******************************************************************************************
*
* raylib [core] example - Minimal Virtual Dpad
* raylib [core] example - Virtual Dpad
*
* Example originally created with raylib 5.0, last time updated with raylib 5.0
*
@ -27,26 +27,26 @@ int main(void)
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - minimal virtual dpad");
InitWindow(screenWidth, screenHeight, "raylib [core] example - virtual dpad");
const int dpad_x = 90;
const int dpad_y = 300;
const int dpad_rad = 25;//radius of each pad
Color dpad_color = BLUE;
int dpad_keydown = -1;//-1 if not down, else 0,1,2,3
const int dpadX = 90;
const int dpadY = 300;
const int dpadRad = 25;//radius of each pad
Color dpadColor = BLUE;
int dpadKeydown = -1;//-1 if not down, else 0,1,2,3
// collider[4] with x,y
const float dpad_collider[4][2]=
const float dpadCollider[4][2]= // collider array with x,y position
{
{dpad_x,dpad_y-dpad_rad*1.5},//up
{dpad_x-dpad_rad*1.5,dpad_y},//left
{dpad_x+dpad_rad*1.5,dpad_y},//right
{dpad_x,dpad_y+dpad_rad*1.5}//down
{dpadX,dpadY-dpadRad*1.5},//up
{dpadX-dpadRad*1.5,dpadY},//left
{dpadX+dpadRad*1.5,dpadY},//right
{dpadX,dpadY+dpadRad*1.5}//down
};
const char dpad_label[4]="XYBA";
const char dpadLabel[4]="XYBA";//label of Dpad
float player_x=100;
float player_y=100;
float playerX=100;
float playerY=100;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@ -54,59 +54,63 @@ int main(void)
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// update --------------------------------------------------------------------------
dpad_keydown = -1; //reset
float input_x=0;
float input_y=0;
// Update
//--------------------------------------------------------------------------
dpadKeydown = -1; //reset
float inputX=0;
float inputY=0;
if(GetTouchPointCount()>0)
{//use touch pos
input_x = GetTouchX();
input_y = GetTouchY();
inputX = GetTouchX();
inputY = GetTouchY();
}
else
{//use mouse pos
input_x = GetMouseX();
input_y = GetMouseY();
inputX = GetMouseX();
inputY = GetMouseY();
}
for(int i=0;i<4;i++)
{
//test distance each collider and input < radius
if( fabsf(dpad_collider[i][1]-input_y) + fabsf(dpad_collider[i][0]-input_x) < dpad_rad)
if( fabsf(dpadCollider[i][1]-inputY) + fabsf(dpadCollider[i][0]-inputX) < dpadRad)
{
dpad_keydown = i;
dpadKeydown = i;
break;
}
}
// move player
switch(dpad_keydown){
case 0: player_y -= 50*GetFrameTime();
switch(dpadKeydown){
case 0: playerY -= 50*GetFrameTime();
break;
case 1: player_x -= 50*GetFrameTime();
case 1: playerX -= 50*GetFrameTime();
break;
case 2: player_x += 50*GetFrameTime();
case 2: playerX += 50*GetFrameTime();
break;
case 3: player_y += 50*GetFrameTime();
case 3: playerY += 50*GetFrameTime();
default:;
};
// draw ----------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Draw
//--------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
for(int i=0;i<4;i++)
{
//draw all pad
DrawCircle(dpad_collider[i][0],dpad_collider[i][1],dpad_rad,dpad_color);
if(i!=dpad_keydown)
DrawCircle(dpadCollider[i][0],dpadCollider[i][1],dpadRad,dpadColor);
if(i!=dpadKeydown)
{
//draw label
DrawText(TextSubtext(dpad_label,i,1),
dpad_collider[i][0]-5,
dpad_collider[i][1]-5,16,BLACK);
DrawText(TextSubtext(dpadLabel,i,1),
dpadCollider[i][0]-5,
dpadCollider[i][1]-5,16,BLACK);
}
}
DrawText("Player",player_x,player_y,16,BLACK);
DrawText("Player",playerX,playerY,16,BLACK);
EndDrawing();
//--------------------------------------------------------------------------
}
// De-Initialization