core_input_multitouch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Input multitouch
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 2.1, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Berni (@Berni8k) and 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) 2019-2024 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define MAX_TOUCH_POINTS 10
  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 multitouch");
  29. Vector2 touchPositions[MAX_TOUCH_POINTS] = { 0 };
  30. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  31. //---------------------------------------------------------------------------------------
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. // Get the touch point count ( how many fingers are touching the screen )
  38. int tCount = GetTouchPointCount();
  39. // Clamp touch points available ( set the maximum touch points allowed )
  40. if (tCount > MAX_TOUCH_POINTS) tCount = MAX_TOUCH_POINTS;
  41. // Get touch points positions
  42. for (int i = 0; i < tCount; ++i) touchPositions[i] = GetTouchPosition(i);
  43. //----------------------------------------------------------------------------------
  44. // Draw
  45. //----------------------------------------------------------------------------------
  46. BeginDrawing();
  47. ClearBackground(RAYWHITE);
  48. for (int i = 0; i < tCount; ++i)
  49. {
  50. // Make sure point is not (0, 0) as this means there is no touch for it
  51. if ((touchPositions[i].x > 0) && (touchPositions[i].y > 0))
  52. {
  53. // Draw circle and touch index number
  54. DrawCircleV(touchPositions[i], 34, ORANGE);
  55. DrawText(TextFormat("%d", i), (int)touchPositions[i].x - 10, (int)touchPositions[i].y - 70, 40, BLACK);
  56. }
  57. }
  58. DrawText("touch the screen at multiple locations to get multiple balls", 10, 10, 20, DARKGRAY);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }