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-2025 Robin (@RobinsAviary)
15+ *
16+ ********************************************************************************************/
17+
18+ #include "raylib.h"
19+ #include "raymath.h"
20+
21+ //------------------------------------------------------------------------------------
22+ // Program main entry point
23+ //------------------------------------------------------------------------------------
24+ int main (void )
25+ {
26+ // Initialization
27+ //--------------------------------------------------------------------------------------
28+ const int screenWidth = 800 ;
29+ const int screenHeight = 450 ;
30+
31+ InitWindow (screenWidth , screenHeight , "raylib [shapes] example - lines drawing" );
32+
33+ // Hint text that shows before you click the screen
34+ bool startText = true;
35+
36+ // The mouse's position on the previous frame
37+ Vector2 mousePositionPrevious = GetMousePosition ();
38+
39+ // The canvas to draw lines on
40+ RenderTexture canvas = LoadRenderTexture (screenWidth , screenHeight );
41+
42+ // The background color of the canvas
43+ const Color backgroundColor = RAYWHITE ;
44+
45+ // The line's thickness
46+ float lineThickness = 8.0f ;
47+ // The lines hue (in HSV, from 0-360)
48+ float lineHue = 0.0f ;
49+
50+ // Clear the canvas to the background color
51+ BeginTextureMode (canvas );
52+ ClearBackground (backgroundColor );
53+ EndTextureMode ();
54+ //--------------------------------------------------------------------------------------
55+
56+ // Main game loop
57+ while (!WindowShouldClose ()) // Detect window close button or ESC key
58+ {
59+ // Update
60+ //----------------------------------------------------------------------------------
61+ // Disable the hint text once the user clicks
62+ if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT ) && startText )
63+ {
64+ startText = false;
65+ }
66+
67+ // Clear the canvas when the user middle-clicks
68+ if (IsMouseButtonPressed (MOUSE_BUTTON_MIDDLE ))
69+ {
70+ BeginTextureMode (canvas );
71+ ClearBackground (backgroundColor );
72+ EndTextureMode ();
73+ }
74+
75+ // Store whether the left and right buttons are down
76+ bool leftButtonDown = IsMouseButtonDown (MOUSE_BUTTON_LEFT );
77+ bool rightButtonDown = IsMouseButtonDown (MOUSE_BUTTON_RIGHT );
78+
79+ if (leftButtonDown || rightButtonDown )
80+ {
81+ // The color for the line
82+ Color drawColor ;
83+
84+ if (leftButtonDown )
85+ {
86+ // Increase the hue value by the distance our cursor has moved since the last frame (divided by 3)
87+ lineHue += Vector2Distance (mousePositionPrevious , GetMousePosition ())/3.0f ;
88+
89+ // While the hue is >=360, subtract it to bring it down into the range 0-360
90+ // This is more visually accurate than resetting to zero
91+ while (lineHue >= 360.0f )
92+ {
93+ lineHue -= 360.0f ;
94+ }
95+
96+ // Create the final color
97+ drawColor = ColorFromHSV (lineHue , 1.0f , 1.0f );
98+ }
99+ else if (rightButtonDown )
100+ {
101+ // Use the background color as an "eraser"
102+ drawColor = backgroundColor ;
103+ }
104+
105+ // Draw the line onto the canvas
106+ BeginTextureMode (canvas );
107+ // Circles act as "caps", smoothing corners
108+ DrawCircleV (mousePositionPrevious , lineThickness /2.0f , drawColor );
109+ DrawCircleV (GetMousePosition (), lineThickness /2.0f , drawColor );
110+ DrawLineEx (mousePositionPrevious , GetMousePosition (), lineThickness , drawColor );
111+ EndTextureMode ();
112+ }
113+
114+ // Update line thickness based on mousewheel
115+ lineThickness += GetMouseWheelMove ();
116+ lineThickness = Clamp (lineThickness , 1.0 , 500.0f );
117+
118+ // Update mouse's previous position
119+ mousePositionPrevious = GetMousePosition ();
120+ //----------------------------------------------------------------------------------
121+
122+ // Draw
123+ //----------------------------------------------------------------------------------
124+ BeginDrawing ();
125+ // Draw the render texture to the screen, flipped vertically to make it appear top-side up
126+ DrawTextureRec (canvas .texture , (Rectangle ){ 0.0f , 0.0f , (float )canvas .texture .width ,(float )- canvas .texture .height }, Vector2Zero (), WHITE );
127+
128+ // Draw the preview circle
129+ if (!leftButtonDown ) DrawCircleLinesV (GetMousePosition (), lineThickness /2.0f , (Color ){ 127 , 127 , 127 , 127 });
130+
131+ // Draw the hint text
132+ if (startText ) DrawText ("try clicking and dragging!" , 275 , 215 , 20 , LIGHTGRAY );
133+ EndDrawing ();
134+ //----------------------------------------------------------------------------------
135+ }
136+
137+ // De-Initialization
138+ //--------------------------------------------------------------------------------------
139+ // Unload the canvas render texture
140+ UnloadRenderTexture (canvas );
141+
142+ CloseWindow (); // Close window and OpenGL context
143+ //--------------------------------------------------------------------------------------
144+
145+ return 0 ;
146+ }
0 commit comments