shapes_lines_drawing.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - lines drawing
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Robin (@RobinsAviary) 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) 2025 Robin (@RobinsAviary)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  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 - lines drawing");
  29. // Hint text that shows before you click the screen
  30. bool startText = true;
  31. // The mouse's position on the previous frame
  32. Vector2 mousePositionPrevious = GetMousePosition();
  33. // The canvas to draw lines on
  34. RenderTexture canvas = LoadRenderTexture(screenWidth, screenHeight);
  35. // The line's thickness
  36. float lineThickness = 8.0f;
  37. // The lines hue (in HSV, from 0-360)
  38. float lineHue = 0.0f;
  39. // Clear the canvas to the background color
  40. BeginTextureMode(canvas);
  41. ClearBackground(RAYWHITE);
  42. EndTextureMode();
  43. SetTargetFPS(60);
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. // Disable the hint text once the user clicks
  51. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && startText) startText = false;
  52. // Clear the canvas when the user middle-clicks
  53. if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE))
  54. {
  55. BeginTextureMode(canvas);
  56. ClearBackground(RAYWHITE);
  57. EndTextureMode();
  58. }
  59. // Store whether the left and right buttons are down
  60. bool leftButtonDown = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
  61. bool rightButtonDown = IsMouseButtonDown(MOUSE_BUTTON_RIGHT);
  62. if (leftButtonDown || rightButtonDown)
  63. {
  64. // The color for the line
  65. Color drawColor = WHITE;
  66. if (leftButtonDown)
  67. {
  68. // Increase the hue value by the distance our cursor has moved since the last frame (divided by 3)
  69. lineHue += Vector2Distance(mousePositionPrevious, GetMousePosition())/3.0f;
  70. // While the hue is >=360, subtract it to bring it down into the range 0-360
  71. // This is more visually accurate than resetting to zero
  72. while (lineHue >= 360.0f) lineHue -= 360.0f;
  73. // Create the final color
  74. drawColor = ColorFromHSV(lineHue, 1.0f, 1.0f);
  75. }
  76. else if (rightButtonDown) drawColor = RAYWHITE; // Use the background color as an "eraser"
  77. // Draw the line onto the canvas
  78. BeginTextureMode(canvas);
  79. // Circles act as "caps", smoothing corners
  80. DrawCircleV(mousePositionPrevious, lineThickness/2.0f, drawColor);
  81. DrawCircleV(GetMousePosition(), lineThickness/2.0f, drawColor);
  82. DrawLineEx(mousePositionPrevious, GetMousePosition(), lineThickness, drawColor);
  83. EndTextureMode();
  84. }
  85. // Update line thickness based on mousewheel
  86. lineThickness += GetMouseWheelMove();
  87. lineThickness = Clamp(lineThickness, 1.0, 500.0f);
  88. // Update mouse's previous position
  89. mousePositionPrevious = GetMousePosition();
  90. //----------------------------------------------------------------------------------
  91. // Draw
  92. //----------------------------------------------------------------------------------
  93. BeginDrawing();
  94. // Draw the render texture to the screen, flipped vertically to make it appear top-side up
  95. DrawTextureRec(canvas.texture, (Rectangle){ 0.0f, 0.0f, (float)canvas.texture.width,(float)-canvas.texture.height }, Vector2Zero(), WHITE);
  96. // Draw the preview circle
  97. if (!leftButtonDown) DrawCircleLinesV(GetMousePosition(), lineThickness/2.0f, (Color){ 127, 127, 127, 127 });
  98. // Draw the hint text
  99. if (startText) DrawText("try clicking and dragging!", 275, 215, 20, LIGHTGRAY);
  100. EndDrawing();
  101. //----------------------------------------------------------------------------------
  102. }
  103. // De-Initialization
  104. //--------------------------------------------------------------------------------------
  105. UnloadRenderTexture(canvas); // Unload the canvas render texture
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }