|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [shapes] example - kaleidoscope |
| 4 | +* |
| 5 | +* Example complexity rating: [★★☆☆] 2/4 |
| 6 | +* |
| 7 | +* Example originally created with raylib 5.5, last time updated with raylib 5.6 |
| 8 | +* |
| 9 | +* Example contributed by Hugo ARNAL (@hugoarnal) and reviewed by Ramon Santamaria (@raysan5) |
| 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 Hugo ARNAL (@hugoarnal) |
| 15 | +* |
| 16 | +********************************************************************************************/ |
| 17 | + |
| 18 | +#include "raylib.h" |
| 19 | +#include "raymath.h" |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------------ |
| 22 | +// Program main entry point |
| 23 | +//------------------------------------------------------------------------------------ |
| 24 | +int main(void) |
| 25 | +{ |
| 26 | + // Initialization |
| 27 | + //-------------------------------------------------------------------------------------- |
| 28 | + const int screenWidth = 800; |
| 29 | + const int screenHeight = 450; |
| 30 | + |
| 31 | + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - kaleidoscope"); |
| 32 | + |
| 33 | + int symmetry = 6; |
| 34 | + float angle = 360.0f/(float)symmetry; |
| 35 | + float thickness = 3.0f; |
| 36 | + Vector2 prevMousePos = { 0 }; |
| 37 | + |
| 38 | + SetTargetFPS(60); |
| 39 | + ClearBackground(BLACK); |
| 40 | + |
| 41 | + Vector2 offset = { (float)screenWidth/2.0f, (float)screenHeight/2.0f }; |
| 42 | + Camera2D camera = { 0 }; |
| 43 | + camera.target = (Vector2){ 0 }; |
| 44 | + camera.offset = offset; |
| 45 | + camera.rotation = 0.0f; |
| 46 | + camera.zoom = 1.0f; |
| 47 | + |
| 48 | + Vector2 scaleVector = { 1.0f, -1.0f }; |
| 49 | + //-------------------------------------------------------------------------------------- |
| 50 | + |
| 51 | + // Main game loop |
| 52 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 53 | + { |
| 54 | + // Update |
| 55 | + //---------------------------------------------------------------------------------- |
| 56 | + Vector2 mousePos = GetMousePosition(); |
| 57 | + Vector2 lineStart = Vector2Subtract(mousePos, offset); |
| 58 | + Vector2 lineEnd = Vector2Subtract(prevMousePos, offset); |
| 59 | + //---------------------------------------------------------------------------------- |
| 60 | + |
| 61 | + // Draw |
| 62 | + //---------------------------------------------------------------------------------- |
| 63 | + BeginDrawing(); |
| 64 | + 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); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + prevMousePos = mousePos; |
| 80 | + EndMode2D(); |
| 81 | + EndDrawing(); |
| 82 | + //---------------------------------------------------------------------------------- |
| 83 | + } |
| 84 | + |
| 85 | + // De-Initialization |
| 86 | + //-------------------------------------------------------------------------------------- |
| 87 | + |
| 88 | + CloseWindow(); // Close window and OpenGL context |
| 89 | + //-------------------------------------------------------------------------------------- |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments