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.

88 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. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [core] example - input multitouch");
  21. Vector2 ballPosition = { -100.0f, -100.0f };
  22. Color ballColor = BEIGE;
  23. int touchCounter = 0;
  24. Vector2 touchPosition = { 0.0f };
  25. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  26. //---------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. ballPosition = GetMousePosition();
  33. ballColor = BEIGE;
  34. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) ballColor = MAROON;
  35. if (IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) ballColor = LIME;
  36. if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) ballColor = DARKBLUE;
  37. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) touchCounter = 10;
  38. if (IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)) touchCounter = 10;
  39. if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) touchCounter = 10;
  40. if (touchCounter > 0) touchCounter--;
  41. //----------------------------------------------------------------------------------
  42. // Draw
  43. //----------------------------------------------------------------------------------
  44. BeginDrawing();
  45. ClearBackground(RAYWHITE);
  46. // Multitouch
  47. for (int i = 0; i < MAX_TOUCH_POINTS; ++i)
  48. {
  49. touchPosition = GetTouchPosition(i); // Get the touch point
  50. if ((touchPosition.x >= 0) && (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it
  51. {
  52. // Draw circle and touch index number
  53. DrawCircleV(touchPosition, 34, ORANGE);
  54. DrawText(FormatText("%d", i), touchPosition.x - 10, touchPosition.y - 70, 40, BLACK);
  55. }
  56. }
  57. // Draw the normal mouse location
  58. DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor);
  59. DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
  60. DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, DARKGRAY);
  61. EndDrawing();
  62. //----------------------------------------------------------------------------------
  63. }
  64. // De-Initialization
  65. //--------------------------------------------------------------------------------------
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }