2
0

shapes_dashed_line.c 3.8 KB

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