Skip to content

Commit

Permalink
Update and rename core_input_gamepad_virtual.c to core_virtual_Dpad.c
Browse files Browse the repository at this point in the history
  • Loading branch information
oblerion committed Sep 30, 2024
1 parent e6c06f1 commit a9cdae8
Showing 1 changed file with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -27,86 +27,90 @@ 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);
//--------------------------------------------------------------------------------------

// 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
Expand Down

0 comments on commit a9cdae8

Please sign in to comment.