Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_R
ifeq ($(BUILD_WEB_WEBGL2),TRUE)
LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
endif

# Add resources building if required
ifeq ($(BUILD_WEB_RESOURCES),TRUE)
LDFLAGS += --preload-file $(BUILD_WEB_RESOURCES_PATH)
Expand Down Expand Up @@ -559,7 +559,8 @@ SHAPES = \
shapes/shapes_ring_drawing \
shapes/shapes_rounded_rectangle_drawing \
shapes/shapes_splines_drawing \
shapes/shapes_top_down_lights
shapes/shapes_top_down_lights \
shapes/shapes_dashed_line

TEXTURES = \
textures/textures_background_scrolling \
Expand Down
5 changes: 3 additions & 2 deletions examples/Makefile.Web
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_R
ifeq ($(BUILD_WEB_WEBGL2),TRUE)
LDFLAGS += -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2
endif

# Add resources building if required
ifeq ($(BUILD_WEB_RESOURCES),TRUE)
LDFLAGS += --preload-file $(BUILD_WEB_RESOURCES_PATH)
Expand Down Expand Up @@ -559,7 +559,8 @@ SHAPES = \
shapes/shapes_ring_drawing \
shapes/shapes_rounded_rectangle_drawing \
shapes/shapes_splines_drawing \
shapes/shapes_top_down_lights
shapes/shapes_top_down_lights \
shapes/shapes_dashed_line

TEXTURES = \
textures/textures_background_scrolling \
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
| [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) |
| [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) |
| [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) |
| [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) |

### category: textures [26]

Expand Down
1 change: 1 addition & 0 deletions examples/examples_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ shapes;shapes_rectangle_advanced;★★★★;5.5;5.5;2024;2025;"Everton Jr.";@e
shapes;shapes_splines_drawing;★★★☆;5.0;5.0;2023;2025;"Ramon Santamaria";@raysan5
shapes;shapes_digital_clock;★★★★;5.5;5.6;2025;2025;"Hamza RAHAL";@hmz-rhl
shapes;shapes_double_pendulum;★★☆☆;5.5;5.5;2025;2025;"JoeCheong";@Joecheong2006
shapes;shapes_dashed_line;★☆☆☆;5.5;5.5;2025;2025;"Luís Almeida";@luis605
textures;textures_logo_raylib;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
textures;textures_srcrec_dstrec;★★★☆;1.3;1.3;2015;2025;"Ramon Santamaria";@raysan5
textures;textures_image_drawing;★★☆☆;1.4;1.4;2016;2025;"Ramon Santamaria";@raysan5
Expand Down
103 changes: 103 additions & 0 deletions examples/shapes/shapes_dashed_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*******************************************************************************************
*
* raylib [shapes] example - dashed line drawing
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 2.5, last time updated with raylib 2.5
*
* Example contributed by Luís Almeida (@luis605)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 Luís Almeida (@luis605)
*
********************************************************************************************/

#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h" // Required for GUI controls

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [shapes] example - interactive dashed line");

// Line Properties
Vector2 lineStartPosition = { 20.0f, 50.0f };
Vector2 lineEndPosition = { 780.0f, 400.0f };
float dashLength = 25.0f;
float blankLength = 15.0f;

// Color selection
Color lineColors[] = { RED, ORANGE, GOLD, GREEN, BLUE, VIOLET, PINK, BLACK };
int colorIndex = 0;

SetTargetFPS(60); // Set our game to run at 60 frames-per-second

// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
lineEndPosition = GetMousePosition(); // Line endpoint follows the mouse

// --- Keyboard Controls ---

// Change Dash Length (UP/DOWN arrows)
if (IsKeyDown(KEY_UP)) dashLength += 1.0f;
if (IsKeyDown(KEY_DOWN) && dashLength > 1.0f) dashLength -= 1.0f;

// Change Space Length (LEFT/RIGHT arrows)
if (IsKeyDown(KEY_RIGHT)) blankLength += 1.0f;
if (IsKeyDown(KEY_LEFT) && blankLength > 1.0f) blankLength -= 1.0f;

// Cycle through colors ('C' key)
if (IsKeyPressed(KEY_C))
{
colorIndex = (colorIndex + 1) % (sizeof(lineColors)/sizeof(Color));
}

//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

// Draw the dashed line with the current properties
DrawLineDashed(lineStartPosition, lineEndPosition, dashLength, blankLength, lineColors[colorIndex]);

// Draw UI and Instructions
DrawRectangle(5, 5, 265, 95, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(5, 5, 265, 95, BLUE);

DrawText("CONTROLS:", 15, 15, 10, BLACK);
DrawText("UP/DOWN: Change Dash Length", 15, 35, 10, BLACK);
DrawText("LEFT/RIGHT: Change Space Length", 15, 55, 10, BLACK);
DrawText("C: Cycle Color", 15, 75, 10, BLACK);

DrawText(TextFormat("Dash: %.0f | Space: %.0f", dashLength, blankLength), 15, 115, 10, DARKGRAY);

DrawFPS(screenWidth - 80, 10);

EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
Binary file added examples/shapes/shapes_dashed_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ RLAPI Rectangle GetShapesTextureRectangle(void); // Get texture source re
RLAPI void DrawPixel(int posX, int posY, Color color); // Draw a pixel using geometry [Can be slow, use with care]
RLAPI void DrawPixelV(Vector2 position, Color color); // Draw a pixel using geometry (Vector version) [Can be slow, use with care]
RLAPI void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); // Draw a line
RLAPI void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color); // Draw a dashed line
RLAPI void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); // Draw a line (using gl lines)
RLAPI void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); // Draw a line (using triangles/quads)
RLAPI void DrawLineStrip(const Vector2 *points, int pointCount, Color color); // Draw lines sequence (using gl lines)
Expand Down
49 changes: 49 additions & 0 deletions src/rshapes.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,55 @@ void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color colo
rlEnd();
}

void DrawLineDashed(Vector2 startPos, Vector2 endPos, int dashSize, int whiteSpaceSize, Color color)
{
// Calculate the vector and length of the line
float dx = endPos.x - startPos.x;
float dy = endPos.y - startPos.y;
float lineLength = sqrtf(dx*dx + dy*dy);

// If the line is too short for dashing or dash size is invalid, draw a solid thick line
if (lineLength < (dashSize + whiteSpaceSize) || dashSize <= 0)
{
DrawLineV(startPos, endPos, color);
return;
}

// Calculate the normalized direction vector of the line
float invLineLength = 1 / lineLength;
float dirX = dx * invLineLength;
float dirY = dy * invLineLength;

Vector2 currentPos = startPos;
float distanceTraveled = 0;

rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);

while (distanceTraveled < lineLength)
{
// Calculate the end of the current dash
float dashEndDist = distanceTraveled + dashSize;
if (dashEndDist > lineLength)
{
dashEndDist = lineLength;
}

Vector2 dashEndPos = { startPos.x + dashEndDist * dirX, startPos.y + dashEndDist * dirY };

// Draw the dash segment
rlVertex2f(currentPos.x, currentPos.y);
rlVertex2f(dashEndPos.x, dashEndPos.y);

// Update the distance traveled and move the current position for the next dash
distanceTraveled = dashEndDist + whiteSpaceSize;
currentPos.x = startPos.x + distanceTraveled * dirX;
currentPos.y = startPos.y + distanceTraveled * dirY;
}

rlEnd();
}

// Draw a line (using gl lines)
void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
{
Expand Down
Loading