core_input_mouse.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Mouse input
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.0, last time updated with raylib 5.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
  26. Vector2 ballPosition = { -100.0f, -100.0f };
  27. Color ballColor = DARKBLUE;
  28. int isCursorHidden = 0;
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //---------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. if (IsKeyPressed(KEY_H))
  37. {
  38. if (isCursorHidden == 0)
  39. {
  40. HideCursor();
  41. isCursorHidden = 1;
  42. }
  43. else
  44. {
  45. ShowCursor();
  46. isCursorHidden = 0;
  47. }
  48. }
  49. ballPosition = GetMousePosition();
  50. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) ballColor = MAROON;
  51. else if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) ballColor = LIME;
  52. else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) ballColor = DARKBLUE;
  53. else if (IsMouseButtonPressed(MOUSE_BUTTON_SIDE)) ballColor = PURPLE;
  54. else if (IsMouseButtonPressed(MOUSE_BUTTON_EXTRA)) ballColor = YELLOW;
  55. else if (IsMouseButtonPressed(MOUSE_BUTTON_FORWARD)) ballColor = ORANGE;
  56. else if (IsMouseButtonPressed(MOUSE_BUTTON_BACK)) ballColor = BEIGE;
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawCircleV(ballPosition, 40, ballColor);
  63. DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
  64. DrawText("Press 'H' to toggle cursor visibility", 10, 30, 20, DARKGRAY);
  65. if (isCursorHidden == 1) DrawText("CURSOR HIDDEN", 20, 60, 20, RED);
  66. else DrawText("CURSOR VISIBLE", 20, 60, 20, LIME);
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }