1+ /*******************************************************************************************
2+ *
3+ * raylib [shapes] example - dashed line drawing
4+ *
5+ * Example complexity rating: [★☆☆☆] 1/4
6+ *
7+ * Example originally created with raylib 2.5, last time updated with raylib 2.5
8+ *
9+ * Example contributed by Luís Almeida (@luis605)
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 Luís Almeida (@luis605)
15+ *
16+ ********************************************************************************************/
17+
18+ #include "raylib.h"
19+ #define RAYGUI_IMPLEMENTATION
20+ #include "raygui.h" // Required for GUI controls
21+
22+ //------------------------------------------------------------------------------------
23+ // Program main entry point
24+ //------------------------------------------------------------------------------------
25+ int main (void )
26+ {
27+ // Initialization
28+ //--------------------------------------------------------------------------------------
29+ const int screenWidth = 800 ;
30+ const int screenHeight = 450 ;
31+
32+ InitWindow (screenWidth , screenHeight , "raylib [shapes] example - interactive dashed line" );
33+
34+ // Line Properties
35+ Vector2 lineStartPosition = { 20.0f , 50.0f };
36+ Vector2 lineEndPosition = { 780.0f , 400.0f };
37+ float dashLength = 25.0f ;
38+ float blankLength = 15.0f ;
39+
40+ // Color selection
41+ Color lineColors [] = { RED , ORANGE , GOLD , GREEN , BLUE , VIOLET , PINK , BLACK };
42+ int colorIndex = 0 ;
43+
44+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
45+
46+ // Main game loop
47+ while (!WindowShouldClose ()) // Detect window close button or ESC key
48+ {
49+ // Update
50+ //----------------------------------------------------------------------------------
51+ lineEndPosition = GetMousePosition (); // Line endpoint follows the mouse
52+
53+ // --- Keyboard Controls ---
54+
55+ // Change Dash Length (UP/DOWN arrows)
56+ if (IsKeyDown (KEY_UP )) dashLength += 1.0f ;
57+ if (IsKeyDown (KEY_DOWN ) && dashLength > 1.0f ) dashLength -= 1.0f ;
58+
59+ // Change Space Length (LEFT/RIGHT arrows)
60+ if (IsKeyDown (KEY_RIGHT )) blankLength += 1.0f ;
61+ if (IsKeyDown (KEY_LEFT ) && blankLength > 1.0f ) blankLength -= 1.0f ;
62+
63+ // Cycle through colors ('C' key)
64+ if (IsKeyPressed (KEY_C ))
65+ {
66+ colorIndex = (colorIndex + 1 ) % (sizeof (lineColors )/sizeof (Color ));
67+ }
68+
69+ //----------------------------------------------------------------------------------
70+
71+ // Draw
72+ //----------------------------------------------------------------------------------
73+ BeginDrawing ();
74+
75+ ClearBackground (RAYWHITE );
76+
77+ // Draw the dashed line with the current properties
78+ DrawLineDashed (lineStartPosition , lineEndPosition , dashLength , blankLength , lineColors [colorIndex ]);
79+
80+ // Draw UI and Instructions
81+ DrawRectangle (5 , 5 , 265 , 95 , Fade (SKYBLUE , 0.5f ));
82+ DrawRectangleLines (5 , 5 , 265 , 95 , BLUE );
83+
84+ DrawText ("CONTROLS:" , 15 , 15 , 10 , BLACK );
85+ DrawText ("UP/DOWN: Change Dash Length" , 15 , 35 , 10 , BLACK );
86+ DrawText ("LEFT/RIGHT: Change Space Length" , 15 , 55 , 10 , BLACK );
87+ DrawText ("C: Cycle Color" , 15 , 75 , 10 , BLACK );
88+
89+ DrawText (TextFormat ("Dash: %.0f | Space: %.0f" , dashLength , blankLength ), 15 , 115 , 10 , DARKGRAY );
90+
91+ DrawFPS (screenWidth - 80 , 10 );
92+
93+ EndDrawing ();
94+ //----------------------------------------------------------------------------------
95+ }
96+
97+ // De-Initialization
98+ //--------------------------------------------------------------------------------------
99+ CloseWindow (); // Close window and OpenGL context
100+ //--------------------------------------------------------------------------------------
101+
102+ return 0 ;
103+ }
0 commit comments