Update and rename core_input_gamepad_virtual.c to core_virtual_Dpad.c
This commit is contained in:
parent
e6c06f1f38
commit
a9cdae8dba
|
|
@ -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
|
* 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 screenWidth = 800;
|
||||||
const int screenHeight = 450;
|
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 dpadX = 90;
|
||||||
const int dpad_y = 300;
|
const int dpadY = 300;
|
||||||
const int dpad_rad = 25;//radius of each pad
|
const int dpadRad = 25;//radius of each pad
|
||||||
Color dpad_color = BLUE;
|
Color dpadColor = BLUE;
|
||||||
int dpad_keydown = -1;//-1 if not down, else 0,1,2,3
|
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
|
{dpadX,dpadY-dpadRad*1.5},//up
|
||||||
{dpad_x-dpad_rad*1.5,dpad_y},//left
|
{dpadX-dpadRad*1.5,dpadY},//left
|
||||||
{dpad_x+dpad_rad*1.5,dpad_y},//right
|
{dpadX+dpadRad*1.5,dpadY},//right
|
||||||
{dpad_x,dpad_y+dpad_rad*1.5}//down
|
{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 playerX=100;
|
||||||
float player_y=100;
|
float playerY=100;
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
@ -54,59 +54,63 @@ int main(void)
|
||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
// update --------------------------------------------------------------------------
|
// Update
|
||||||
dpad_keydown = -1; //reset
|
//--------------------------------------------------------------------------
|
||||||
float input_x=0;
|
dpadKeydown = -1; //reset
|
||||||
float input_y=0;
|
float inputX=0;
|
||||||
|
float inputY=0;
|
||||||
if(GetTouchPointCount()>0)
|
if(GetTouchPointCount()>0)
|
||||||
{//use touch pos
|
{//use touch pos
|
||||||
input_x = GetTouchX();
|
inputX = GetTouchX();
|
||||||
input_y = GetTouchY();
|
inputY = GetTouchY();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{//use mouse pos
|
{//use mouse pos
|
||||||
input_x = GetMouseX();
|
inputX = GetMouseX();
|
||||||
input_y = GetMouseY();
|
inputY = GetMouseY();
|
||||||
}
|
}
|
||||||
for(int i=0;i<4;i++)
|
for(int i=0;i<4;i++)
|
||||||
{
|
{
|
||||||
//test distance each collider and input < radius
|
//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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// move player
|
// move player
|
||||||
switch(dpad_keydown){
|
switch(dpadKeydown){
|
||||||
case 0: player_y -= 50*GetFrameTime();
|
case 0: playerY -= 50*GetFrameTime();
|
||||||
break;
|
break;
|
||||||
case 1: player_x -= 50*GetFrameTime();
|
case 1: playerX -= 50*GetFrameTime();
|
||||||
break;
|
break;
|
||||||
case 2: player_x += 50*GetFrameTime();
|
case 2: playerX += 50*GetFrameTime();
|
||||||
break;
|
break;
|
||||||
case 3: player_y += 50*GetFrameTime();
|
case 3: playerY += 50*GetFrameTime();
|
||||||
default:;
|
default:;
|
||||||
};
|
};
|
||||||
// draw ----------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
for(int i=0;i<4;i++)
|
for(int i=0;i<4;i++)
|
||||||
{
|
{
|
||||||
//draw all pad
|
//draw all pad
|
||||||
DrawCircle(dpad_collider[i][0],dpad_collider[i][1],dpad_rad,dpad_color);
|
DrawCircle(dpadCollider[i][0],dpadCollider[i][1],dpadRad,dpadColor);
|
||||||
if(i!=dpad_keydown)
|
if(i!=dpadKeydown)
|
||||||
{
|
{
|
||||||
//draw label
|
//draw label
|
||||||
DrawText(TextSubtext(dpad_label,i,1),
|
DrawText(TextSubtext(dpadLabel,i,1),
|
||||||
dpad_collider[i][0]-5,
|
dpadCollider[i][0]-5,
|
||||||
dpad_collider[i][1]-5,16,BLACK);
|
dpadCollider[i][1]-5,16,BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
DrawText("Player",player_x,player_y,16,BLACK);
|
DrawText("Player",playerX,playerY,16,BLACK);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
Loading…
Reference in New Issue
Block a user