2
0

shapes_rectangle_scaling.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - rectangle scaling
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Vlad Adrian (@demizdor) 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) 2018-2025 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define MOUSE_SCALE_MARK_SIZE 12
  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 [shapes] example - rectangle scaling");
  29. Rectangle rec = { 100, 100, 200, 80 };
  30. Vector2 mousePosition = { 0 };
  31. bool mouseScaleReady = false;
  32. bool mouseScaleMode = false;
  33. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. mousePosition = GetMousePosition();
  41. if (CheckCollisionPointRec(mousePosition, (Rectangle){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE }))
  42. {
  43. mouseScaleReady = true;
  44. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) mouseScaleMode = true;
  45. }
  46. else mouseScaleReady = false;
  47. if (mouseScaleMode)
  48. {
  49. mouseScaleReady = true;
  50. rec.width = (mousePosition.x - rec.x);
  51. rec.height = (mousePosition.y - rec.y);
  52. // Check minimum rec size
  53. if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
  54. if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
  55. // Check maximum rec size
  56. if (rec.width > (GetScreenWidth() - rec.x)) rec.width = GetScreenWidth() - rec.x;
  57. if (rec.height > (GetScreenHeight() - rec.y)) rec.height = GetScreenHeight() - rec.y;
  58. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) mouseScaleMode = false;
  59. }
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
  66. DrawRectangleRec(rec, Fade(GREEN, 0.5f));
  67. if (mouseScaleReady)
  68. {
  69. DrawRectangleLinesEx(rec, 1, RED);
  70. DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
  71. (Vector2){ rec.x + rec.width, rec.y + rec.height },
  72. (Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
  73. }
  74. EndDrawing();
  75. //----------------------------------------------------------------------------------
  76. }
  77. // De-Initialization
  78. //--------------------------------------------------------------------------------------
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }