core_input_virtual_controls.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - input virtual controls
  4. *
  5. * Example originally created with raylib 5.0, last time updated with raylib 5.0
  6. *
  7. * Example create by GreenSnakeLinux (@GreenSnakeLinux),
  8. * lighter by oblerion (@oblerion) and
  9. * reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2024 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h>
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [core] example - input virtual controls");
  29. const int dpadX = 90;
  30. const int dpadY = 300;
  31. const int dpadRad = 25;//radius of each pad
  32. Color dpadColor = BLUE;
  33. int dpadKeydown = -1;//-1 if not down, else 0,1,2,3
  34. const float dpadCollider[4][2]= // collider array with x,y position
  35. {
  36. {dpadX,dpadY-dpadRad*1.5},//up
  37. {dpadX-dpadRad*1.5,dpadY},//left
  38. {dpadX+dpadRad*1.5,dpadY},//right
  39. {dpadX,dpadY+dpadRad*1.5}//down
  40. };
  41. const char dpadLabel[4]="XYBA";//label of Dpad
  42. float playerX=100;
  43. float playerY=100;
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //--------------------------------------------------------------------------
  51. dpadKeydown = -1; //reset
  52. float inputX=0;
  53. float inputY=0;
  54. if(GetTouchPointCount()>0)
  55. {//use touch pos
  56. inputX = GetTouchX();
  57. inputY = GetTouchY();
  58. }
  59. else
  60. {//use mouse pos
  61. inputX = GetMouseX();
  62. inputY = GetMouseY();
  63. }
  64. for(int i=0;i<4;i++)
  65. {
  66. //test distance each collider and input < radius
  67. if( fabsf(dpadCollider[i][1]-inputY) + fabsf(dpadCollider[i][0]-inputX) < dpadRad)
  68. {
  69. dpadKeydown = i;
  70. break;
  71. }
  72. }
  73. // move player
  74. switch(dpadKeydown){
  75. case 0: playerY -= 50*GetFrameTime();
  76. break;
  77. case 1: playerX -= 50*GetFrameTime();
  78. break;
  79. case 2: playerX += 50*GetFrameTime();
  80. break;
  81. case 3: playerY += 50*GetFrameTime();
  82. default:;
  83. };
  84. //--------------------------------------------------------------------------
  85. // Draw
  86. //--------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. for(int i=0;i<4;i++)
  90. {
  91. //draw all pad
  92. DrawCircle(dpadCollider[i][0],dpadCollider[i][1],dpadRad,dpadColor);
  93. if(i!=dpadKeydown)
  94. {
  95. //draw label
  96. DrawText(TextSubtext(dpadLabel,i,1),
  97. dpadCollider[i][0]-7,
  98. dpadCollider[i][1]-8,20,BLACK);
  99. }
  100. }
  101. DrawRectangle(playerX-4,playerY-4,75,28,RED);
  102. DrawText("Player",playerX,playerY,20,WHITE);
  103. EndDrawing();
  104. //--------------------------------------------------------------------------
  105. }
  106. // De-Initialization
  107. //--------------------------------------------------------------------------------------
  108. CloseWindow(); // Close window and OpenGL context
  109. //--------------------------------------------------------------------------------------
  110. return 0;
  111. }