ソースを参照

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.
pull/5365/head
Ray 2ヶ月前
コミット
8081d2bd07
1個のファイルの変更56行の追加21行の削除
  1. +56
    -21
      examples/shapes/shapes_kaleidoscope.c

+ 56
- 21
examples/shapes/shapes_kaleidoscope.c ファイルの表示

@ -11,13 +11,26 @@
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software * BSD-like license that allows static linking with closed source software
* *
* Copyright (c) 2025 Hugo ARNAL (@hugoarnal)
* Copyright (c) 2025 Hugo ARNAL (@hugoarnal) and Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#define MAX_DRAW_LINES 8192
// Line data type
typedef struct {
Vector2 start;
Vector2 end;
} Line;
// Lines array as a global static variable to be stored
// in heap and avoid potential stack overflow (on Web platform)
static Line lines[MAX_DRAW_LINES] = { 0 };
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -30,22 +43,24 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - kaleidoscope"); InitWindow(screenWidth, screenHeight, "raylib [shapes] example - kaleidoscope");
// Line drawing properties
int symmetry = 6; int symmetry = 6;
float angle = 360.0f/(float)symmetry; float angle = 360.0f/(float)symmetry;
float thickness = 3.0f; float thickness = 3.0f;
Vector2 mousePos = { 0 };
Vector2 prevMousePos = { 0 }; Vector2 prevMousePos = { 0 };
SetTargetFPS(60);
ClearBackground(BLACK);
Vector2 scaleVector = { 1.0f, -1.0f };
Vector2 offset = { (float)screenWidth/2.0f, (float)screenHeight/2.0f }; Vector2 offset = { (float)screenWidth/2.0f, (float)screenHeight/2.0f };
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.target = (Vector2){ 0 }; camera.target = (Vector2){ 0 };
camera.offset = offset; camera.offset = offset;
camera.rotation = 0.0f; camera.rotation = 0.0f;
camera.zoom = 1.0f; camera.zoom = 1.0f;
int lineCounter = 0;
Vector2 scaleVector = { 1.0f, -1.0f };
SetTargetFPS(20);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
@ -53,38 +68,58 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
Vector2 mousePos = GetMousePosition();
prevMousePos = mousePos;
mousePos = GetMousePosition();
Vector2 lineStart = Vector2Subtract(mousePos, offset); Vector2 lineStart = Vector2Subtract(mousePos, offset);
Vector2 lineEnd = Vector2Subtract(prevMousePos, offset); Vector2 lineEnd = Vector2Subtract(prevMousePos, offset);
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
for (int s = 0; (s < symmetry) && (lineCounter < (MAX_DRAW_LINES - 1)); s++)
{
lineStart = Vector2Rotate(lineStart, angle*DEG2RAD);
lineEnd = Vector2Rotate(lineEnd, angle*DEG2RAD);
// Store mouse line
lines[lineCounter].start = lineStart;
lines[lineCounter].end = lineEnd;
// Store reflective line
lines[lineCounter + 1].start = Vector2Multiply(lineStart, scaleVector);
lines[lineCounter + 1].end = Vector2Multiply(lineEnd, scaleVector);
lineCounter += 2;
}
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera); BeginMode2D(camera);
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
for (int i = 0; i < symmetry; i++) {
lineStart = Vector2Rotate(lineStart, angle*DEG2RAD);
lineEnd = Vector2Rotate(lineEnd, angle*DEG2RAD);
DrawLineEx(lineStart, lineEnd, thickness, WHITE);
Vector2 reflectLineStart = Vector2Multiply(lineStart, scaleVector);
Vector2 reflectLineEnd = Vector2Multiply(lineEnd, scaleVector);
DrawLineEx(reflectLineStart, reflectLineEnd, thickness, WHITE);
for (int s = 0; s < symmetry; s++)
{
for (int i = 0; i < lineCounter; i += 2)
{
DrawLineEx(lines[i].start, lines[i].end, thickness, BLACK);
DrawLineEx(lines[i + 1].start, lines[i + 1].end, thickness, BLACK);
} }
} }
prevMousePos = mousePos;
EndMode2D(); EndMode2D();
DrawText(TextFormat("LINES: %i/%i", lineCounter, MAX_DRAW_LINES), 10, screenHeight - 30, 20, MAROON);
DrawFPS(10, 10);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------

読み込み中…
キャンセル
保存