Skip to content

Commit 8081d2b

Browse files
committed
REDESIGNED: example: shapes_kaleidoscope, store lines #5361
This redesign stores lines in Update and draws stored lines in Draw, instead of previous approach of drawing directly to framebuffer with no cleaning. This approach allows some interesting features like line draw replay or reversing.
1 parent 2453977 commit 8081d2b

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

examples/shapes/shapes_kaleidoscope.c

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@
1111
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
1212
* BSD-like license that allows static linking with closed source software
1313
*
14-
* Copyright (c) 2025 Hugo ARNAL (@hugoarnal)
14+
* Copyright (c) 2025 Hugo ARNAL (@hugoarnal) and Ramon Santamaria (@raysan5)
1515
*
1616
********************************************************************************************/
1717

1818
#include "raylib.h"
19+
1920
#include "raymath.h"
2021

22+
#define MAX_DRAW_LINES 8192
23+
24+
// Line data type
25+
typedef struct {
26+
Vector2 start;
27+
Vector2 end;
28+
} Line;
29+
30+
// Lines array as a global static variable to be stored
31+
// in heap and avoid potential stack overflow (on Web platform)
32+
static Line lines[MAX_DRAW_LINES] = { 0 };
33+
2134
//------------------------------------------------------------------------------------
2235
// Program main entry point
2336
//------------------------------------------------------------------------------------
@@ -30,61 +43,83 @@ int main(void)
3043

3144
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - kaleidoscope");
3245

46+
// Line drawing properties
3347
int symmetry = 6;
3448
float angle = 360.0f/(float)symmetry;
3549
float thickness = 3.0f;
50+
Vector2 mousePos = { 0 };
3651
Vector2 prevMousePos = { 0 };
37-
38-
SetTargetFPS(60);
39-
ClearBackground(BLACK);
40-
52+
Vector2 scaleVector = { 1.0f, -1.0f };
4153
Vector2 offset = { (float)screenWidth/2.0f, (float)screenHeight/2.0f };
54+
4255
Camera2D camera = { 0 };
4356
camera.target = (Vector2){ 0 };
4457
camera.offset = offset;
4558
camera.rotation = 0.0f;
4659
camera.zoom = 1.0f;
60+
61+
int lineCounter = 0;
4762

48-
Vector2 scaleVector = { 1.0f, -1.0f };
63+
SetTargetFPS(20);
4964
//--------------------------------------------------------------------------------------
5065

5166
// Main game loop
5267
while (!WindowShouldClose()) // Detect window close button or ESC key
5368
{
5469
// Update
5570
//----------------------------------------------------------------------------------
56-
Vector2 mousePos = GetMousePosition();
71+
prevMousePos = mousePos;
72+
mousePos = GetMousePosition();
73+
5774
Vector2 lineStart = Vector2Subtract(mousePos, offset);
5875
Vector2 lineEnd = Vector2Subtract(prevMousePos, offset);
76+
77+
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
78+
{
79+
for (int s = 0; (s < symmetry) && (lineCounter < (MAX_DRAW_LINES - 1)); s++)
80+
{
81+
lineStart = Vector2Rotate(lineStart, angle*DEG2RAD);
82+
lineEnd = Vector2Rotate(lineEnd, angle*DEG2RAD);
83+
84+
// Store mouse line
85+
lines[lineCounter].start = lineStart;
86+
lines[lineCounter].end = lineEnd;
87+
88+
// Store reflective line
89+
lines[lineCounter + 1].start = Vector2Multiply(lineStart, scaleVector);
90+
lines[lineCounter + 1].end = Vector2Multiply(lineEnd, scaleVector);
91+
92+
lineCounter += 2;
93+
}
94+
}
5995
//----------------------------------------------------------------------------------
6096

6197
// Draw
6298
//----------------------------------------------------------------------------------
6399
BeginDrawing();
100+
101+
ClearBackground(RAYWHITE);
102+
64103
BeginMode2D(camera);
65-
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
66-
for (int i = 0; i < symmetry; i++) {
67-
lineStart = Vector2Rotate(lineStart, angle*DEG2RAD);
68-
lineEnd = Vector2Rotate(lineEnd, angle*DEG2RAD);
69-
70-
DrawLineEx(lineStart, lineEnd, thickness, WHITE);
71-
72-
Vector2 reflectLineStart = Vector2Multiply(lineStart, scaleVector);
73-
Vector2 reflectLineEnd = Vector2Multiply(lineEnd, scaleVector);
74-
75-
DrawLineEx(reflectLineStart, reflectLineEnd, thickness, WHITE);
104+
for (int s = 0; s < symmetry; s++)
105+
{
106+
for (int i = 0; i < lineCounter; i += 2)
107+
{
108+
DrawLineEx(lines[i].start, lines[i].end, thickness, BLACK);
109+
DrawLineEx(lines[i + 1].start, lines[i + 1].end, thickness, BLACK);
76110
}
77111
}
78-
79-
prevMousePos = mousePos;
80112
EndMode2D();
113+
114+
DrawText(TextFormat("LINES: %i/%i", lineCounter, MAX_DRAW_LINES), 10, screenHeight - 30, 20, MAROON);
115+
DrawFPS(10, 10);
116+
81117
EndDrawing();
82118
//----------------------------------------------------------------------------------
83119
}
84120

85121
// De-Initialization
86122
//--------------------------------------------------------------------------------------
87-
88123
CloseWindow(); // Close window and OpenGL context
89124
//--------------------------------------------------------------------------------------
90125

0 commit comments

Comments
 (0)