Skip to content

Commit f680776

Browse files
authored
feat:Added DrawLineDashed() and a new example explaining how to use this new function. (#5222)
1 parent e5f809f commit f680776

File tree

8 files changed

+161
-4
lines changed

8 files changed

+161
-4
lines changed

examples/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_R
355355
ifeq ($(BUILD_WEB_WEBGL2),TRUE)
356356
LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
357357
endif
358-
358+
359359
# Add resources building if required
360360
ifeq ($(BUILD_WEB_RESOURCES),TRUE)
361361
LDFLAGS += --preload-file $(BUILD_WEB_RESOURCES_PATH)
@@ -559,7 +559,8 @@ SHAPES = \
559559
shapes/shapes_ring_drawing \
560560
shapes/shapes_rounded_rectangle_drawing \
561561
shapes/shapes_splines_drawing \
562-
shapes/shapes_top_down_lights
562+
shapes/shapes_top_down_lights \
563+
shapes/shapes_dashed_line
563564

564565
TEXTURES = \
565566
textures/textures_background_scrolling \

examples/Makefile.Web

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_R
355355
ifeq ($(BUILD_WEB_WEBGL2),TRUE)
356356
LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
357357
endif
358-
358+
359359
# Add resources building if required
360360
ifeq ($(BUILD_WEB_RESOURCES),TRUE)
361361
LDFLAGS += --preload-file $(BUILD_WEB_RESOURCES_PATH)
@@ -559,7 +559,8 @@ SHAPES = \
559559
shapes/shapes_ring_drawing \
560560
shapes/shapes_rounded_rectangle_drawing \
561561
shapes/shapes_splines_drawing \
562-
shapes/shapes_top_down_lights
562+
shapes/shapes_top_down_lights \
563+
shapes/shapes_dashed_line
563564

564565
TEXTURES = \
565566
textures/textures_background_scrolling \

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
9191
| [shapes_splines_drawing](shapes/shapes_splines_drawing.c) | <img src="shapes/shapes_splines_drawing.png" alt="shapes_splines_drawing" width="80"> | ⭐⭐⭐☆ | 5.0 | 5.0 | [Ramon Santamaria](https://github.com/raysan5) |
9292
| [shapes_digital_clock](shapes/shapes_digital_clock.c) | <img src="shapes/shapes_digital_clock.png" alt="shapes_digital_clock" width="80"> | ⭐⭐⭐⭐️ | 5.5 | 5.6 | [Hamza RAHAL](https://github.com/hmz-rhl) |
9393
| [shapes_double_pendulum](shapes/shapes_double_pendulum.c) | <img src="shapes/shapes_double_pendulum.png" alt="shapes_double_pendulum" width="80"> | ⭐⭐☆☆ | 5.5 | 5.5 | [JoeCheong](https://github.com/Joecheong2006) |
94+
| [shapes_dashed_line](shapes/shapes_dashed_line.c) | <img src="shapes/shapes_dashed_line.png" alt="shapes_dashed_line" width="80"> | ⭐☆☆☆ | 5.5 | 5.5 | [Luís Almeida](https://github.com/luis605) |
9495

9596
### category: textures [26]
9697

examples/examples_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ shapes;shapes_rectangle_advanced;★★★★;5.5;5.5;2024;2025;"Everton Jr.";@e
6666
shapes;shapes_splines_drawing;★★★☆;5.0;5.0;2023;2025;"Ramon Santamaria";@raysan5
6767
shapes;shapes_digital_clock;★★★★;5.5;5.6;2025;2025;"Hamza RAHAL";@hmz-rhl
6868
shapes;shapes_double_pendulum;★★☆☆;5.5;5.5;2025;2025;"JoeCheong";@Joecheong2006
69+
shapes;shapes_dashed_line;★☆☆☆;5.5;5.5;2025;2025;"Luís Almeida";@luis605
6970
textures;textures_logo_raylib;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
7071
textures;textures_srcrec_dstrec;★★★☆;1.3;1.3;2015;2025;"Ramon Santamaria";@raysan5
7172
textures;textures_image_drawing;★★☆☆;1.4;1.4;2016;2025;"Ramon Santamaria";@raysan5
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
16.5 KB
Loading

src/raylib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ RLAPI Rectangle GetShapesTextureRectangle(void); // Get texture source re
12581258
RLAPI void DrawPixel(int posX, int posY, Color color); // Draw a pixel using geometry [Can be slow, use with care]
12591259
RLAPI void DrawPixelV(Vector2 position, Color color); // Draw a pixel using geometry (Vector version) [Can be slow, use with care]
12601260
RLAPI void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line
1261+
RLAPI void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color); // Draw a dashed line
12611262
RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (using gl lines)
12621263
RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line (using triangles/quads)
12631264
RLAPI void DrawLineStrip(const Vector2 *points, int pointCount, Color color); // Draw lines sequence (using gl lines)

src/rshapes.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,55 @@ void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color colo
182182
rlEnd();
183183
}
184184

185+
void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color)
186+
{
187+
// Calculate the vector and length of the line
188+
float dx = endPos.x - startPos.x;
189+
float dy = endPos.y - startPos.y;
190+
float lineLength = sqrtf(dx*dx + dy*dy);
191+
192+
// If the line is too short for dashing or dash size is invalid, draw a solid thick line
193+
if (lineLength < (dashSize + whiteSpaceSize) || dashSize <= 0)
194+
{
195+
DrawLineV(startPos, endPos, color);
196+
return;
197+
}
198+
199+
// Calculate the normalized direction vector of the line
200+
float invLineLength = 1 / lineLength;
201+
float dirX = dx * invLineLength;
202+
float dirY = dy * invLineLength;
203+
204+
Vector2 currentPos = startPos;
205+
float distanceTraveled = 0;
206+
207+
rlBegin(RL_LINES);
208+
rlColor4ub(color.r, color.g, color.b, color.a);
209+
210+
while (distanceTraveled < lineLength)
211+
{
212+
// Calculate the end of the current dash
213+
float dashEndDist = distanceTraveled + dashSize;
214+
if (dashEndDist > lineLength)
215+
{
216+
dashEndDist = lineLength;
217+
}
218+
219+
Vector2 dashEndPos = { startPos.x + dashEndDist * dirX, startPos.y + dashEndDist * dirY };
220+
221+
// Draw the dash segment
222+
rlVertex2f(currentPos.x, currentPos.y);
223+
rlVertex2f(dashEndPos.x, dashEndPos.y);
224+
225+
// Update the distance traveled and move the current position for the next dash
226+
distanceTraveled = dashEndDist + whiteSpaceSize;
227+
currentPos.x = startPos.x + distanceTraveled * dirX;
228+
currentPos.y = startPos.y + distanceTraveled * dirY;
229+
}
230+
231+
rlEnd();
232+
}
233+
185234
// Draw a line (using gl lines)
186235
void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
187236
{

0 commit comments

Comments
 (0)