You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Input multitouch
  4. *
  5. * This example has been created using raylib 2.1 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_TOUCH_POINTS 10
  15. int main(void)
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. const int screenWidth = 800;
  20. const int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib [core] example - input multitouch");
  22. Vector2 ballPosition = { -100.0f, -100.0f };
  23. Color ballColor = BEIGE;
  24. int touchCounter = 0;
  25. Vector2 touchPosition = { 0.0f };
  26. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  27. //---------------------------------------------------------------------------------------
  28. // Main game loop
  29. while (!WindowShouldClose()) // Detect window close button or ESC key
  30. {
  31. // Update
  32. //----------------------------------------------------------------------------------
  33. ballPosition = GetMousePosition();
  34. ballColor = BEIGE;
  35. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) ballColor = MAROON;
  36. if (IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) ballColor = LIME;
  37. if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) ballColor = DARKBLUE;
  38. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) touchCounter = 10;
  39. if (IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)) touchCounter = 10;
  40. if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) touchCounter = 10;
  41. if (touchCounter > 0) touchCounter--;
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. // Multitouch
  48. for (int i = 0; i < MAX_TOUCH_POINTS; ++i)
  49. {
  50. touchPosition = GetTouchPosition(i); // Get the touch point
  51. if ((touchPosition.x >= 0) && (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it
  52. {
  53. // Draw circle and touch index number
  54. DrawCircleV(touchPosition, 34, ORANGE);
  55. DrawText(TextFormat("%d", i), touchPosition.x - 10, touchPosition.y - 70, 40, BLACK);
  56. }
  57. }
  58. // Draw the normal mouse location
  59. DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor);
  60. DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
  61. DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, DARKGRAY);
  62. EndDrawing();
  63. //----------------------------------------------------------------------------------
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }