core_input_mouse.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - input mouse
  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 - input mouse");
  26. Vector2 ballPosition = { -100.0f, -100.0f };
  27. Color ballColor = DARKBLUE;
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //---------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. if (IsKeyPressed(KEY_H))
  36. {
  37. if (IsCursorHidden()) ShowCursor();
  38. else HideCursor();
  39. }
  40. ballPosition = GetMousePosition();
  41. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) ballColor = MAROON;
  42. else if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) ballColor = LIME;
  43. else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) ballColor = DARKBLUE;
  44. else if (IsMouseButtonPressed(MOUSE_BUTTON_SIDE)) ballColor = PURPLE;
  45. else if (IsMouseButtonPressed(MOUSE_BUTTON_EXTRA)) ballColor = YELLOW;
  46. else if (IsMouseButtonPressed(MOUSE_BUTTON_FORWARD)) ballColor = ORANGE;
  47. else if (IsMouseButtonPressed(MOUSE_BUTTON_BACK)) ballColor = BEIGE;
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. DrawCircleV(ballPosition, 40, ballColor);
  54. DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
  55. DrawText("Press 'H' to toggle cursor visibility", 10, 30, 20, DARKGRAY);
  56. if (IsCursorHidden()) DrawText("CURSOR HIDDEN", 20, 60, 20, RED);
  57. else DrawText("CURSOR VISIBLE", 20, 60, 20, LIME);
  58. EndDrawing();
  59. //----------------------------------------------------------------------------------
  60. }
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. CloseWindow(); // Close window and OpenGL context
  64. //--------------------------------------------------------------------------------------
  65. return 0;
  66. }