shapes_draw_rectangle_rounded.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - draw rectangle rounded (with gui options)
  4. *
  5. * Example complexity rating: [★★★☆] 3/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-2024 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include <raylib.h>
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "raygui.h" // Required for GUI controls
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - draw rectangle rounded");
  30. float roundness = 0.2f;
  31. float width = 200.0f;
  32. float height = 100.0f;
  33. float segments = 0.0f;
  34. float lineThick = 1.0f;
  35. bool drawRect = false;
  36. bool drawRoundedRect = true;
  37. bool drawRoundedLines = false;
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. Rectangle rec = { ((float)GetScreenWidth() - width - 250)/2, (GetScreenHeight() - height)/2.0f, (float)width, (float)height };
  46. //----------------------------------------------------------------------------------
  47. // Draw
  48. //----------------------------------------------------------------------------------
  49. BeginDrawing();
  50. ClearBackground(RAYWHITE);
  51. DrawLine(560, 0, 560, GetScreenHeight(), Fade(LIGHTGRAY, 0.6f));
  52. DrawRectangle(560, 0, GetScreenWidth() - 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.3f));
  53. if (drawRect) DrawRectangleRec(rec, Fade(GOLD, 0.6f));
  54. if (drawRoundedRect) DrawRectangleRounded(rec, roundness, (int)segments, Fade(MAROON, 0.2f));
  55. if (drawRoundedLines) DrawRectangleRoundedLinesEx(rec, roundness, (int)segments, lineThick, Fade(MAROON, 0.4f));
  56. // Draw GUI controls
  57. //------------------------------------------------------------------------------
  58. GuiSliderBar((Rectangle){ 640, 40, 105, 20 }, "Width", TextFormat("%.2f", width), &width, 0, (float)GetScreenWidth() - 300);
  59. GuiSliderBar((Rectangle){ 640, 70, 105, 20 }, "Height", TextFormat("%.2f", height), &height, 0, (float)GetScreenHeight() - 50);
  60. GuiSliderBar((Rectangle){ 640, 140, 105, 20 }, "Roundness", TextFormat("%.2f", roundness), &roundness, 0.0f, 1.0f);
  61. GuiSliderBar((Rectangle){ 640, 170, 105, 20 }, "Thickness", TextFormat("%.2f", lineThick), &lineThick, 0, 20);
  62. GuiSliderBar((Rectangle){ 640, 240, 105, 20}, "Segments", TextFormat("%.2f", segments), &segments, 0, 60);
  63. GuiCheckBox((Rectangle){ 640, 320, 20, 20 }, "DrawRoundedRect", &drawRoundedRect);
  64. GuiCheckBox((Rectangle){ 640, 350, 20, 20 }, "DrawRoundedLines", &drawRoundedLines);
  65. GuiCheckBox((Rectangle){ 640, 380, 20, 20}, "DrawRect", &drawRect);
  66. //------------------------------------------------------------------------------
  67. DrawText(TextFormat("MODE: %s", (segments >= 4)? "MANUAL" : "AUTO"), 640, 280, 10, (segments >= 4)? MAROON : DARKGRAY);
  68. DrawFPS(10, 10);
  69. EndDrawing();
  70. //----------------------------------------------------------------------------------
  71. }
  72. // De-Initialization
  73. //--------------------------------------------------------------------------------------
  74. CloseWindow(); // Close window and OpenGL context
  75. //--------------------------------------------------------------------------------------
  76. return 0;
  77. }