diff options
| author | Ray <[email protected]> | 2023-09-08 13:27:13 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2023-09-08 13:27:13 +0200 |
| commit | 1896268775e59d00b2b4f3fea0bc244eee1a1cb4 (patch) | |
| tree | dd695865a0545a29d08901563d1b96c578814564 /examples/shapes | |
| parent | 2d5d0c2999717ea28924d6cdd47bab014478cc32 (diff) | |
| download | raylib-1896268775e59d00b2b4f3fea0bc244eee1a1cb4.tar.gz raylib-1896268775e59d00b2b4f3fea0bc244eee1a1cb4.zip | |
Reviewed examples for consistency
Diffstat (limited to 'examples/shapes')
| -rw-r--r-- | examples/shapes/shapes_bouncing_ball.c | 6 | ||||
| -rw-r--r-- | examples/shapes/shapes_lines_bezier.c | 24 | ||||
| -rw-r--r-- | examples/shapes/shapes_lines_splines.c | 155 |
3 files changed, 181 insertions, 4 deletions
diff --git a/examples/shapes/shapes_bouncing_ball.c b/examples/shapes/shapes_bouncing_ball.c index 5e43b80d..203d88a5 100644 --- a/examples/shapes/shapes_bouncing_ball.c +++ b/examples/shapes/shapes_bouncing_ball.c @@ -23,6 +23,7 @@ int main(void) const int screenWidth = 800; const int screenHeight = 450; + SetConfigFlags(FLAG_MSAA_4X_HINT); InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball"); Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f }; @@ -61,11 +62,14 @@ int main(void) ClearBackground(RAYWHITE); DrawCircleV(ballPosition, (float)ballRadius, MAROON); - DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY); + //DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY); // On pause, we draw a blinking message if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY); + DrawCircle(400.5, 300.5, 50, BLACK); + DrawCircle(528.0, 172.0, 26, BLACK); + DrawFPS(10, 10); EndDrawing(); diff --git a/examples/shapes/shapes_lines_bezier.c b/examples/shapes/shapes_lines_bezier.c index 195281be..f0157685 100644 --- a/examples/shapes/shapes_lines_bezier.c +++ b/examples/shapes/shapes_lines_bezier.c @@ -28,6 +28,9 @@ int main(void) Vector2 start = { 0, 0 }; Vector2 end = { (float)screenWidth, (float)screenHeight }; + + Vector2 startControl = { 100, 0 }; + Vector2 endControl = { GetScreenWidth() - 100, GetScreenHeight() }; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -37,8 +40,16 @@ int main(void) { // Update //---------------------------------------------------------------------------------- - if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) start = GetMousePosition(); - else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) end = GetMousePosition(); + if (IsKeyDown(KEY_LEFT_CONTROL)) + { + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) startControl = GetMousePosition(); + else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) endControl = GetMousePosition(); + } + else + { + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) start = GetMousePosition(); + else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) end = GetMousePosition(); + } //---------------------------------------------------------------------------------- // Draw @@ -49,7 +60,14 @@ int main(void) DrawText("USE MOUSE LEFT-RIGHT CLICK to DEFINE LINE START and END POINTS", 15, 20, 20, GRAY); - DrawLineBezier(start, end, 2.0f, RED); + //DrawLineBezier(start, end, 2.0f, RED); + + DrawLineBezierCubic(start, end, startControl, endControl, 2.0f, RED); + + DrawLineEx(start, startControl, 1.0, LIGHTGRAY); + DrawLineEx(end, endControl, 1.0, LIGHTGRAY); + DrawCircleV(startControl, 10, RED); + DrawCircleV(endControl, 10, RED); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/shapes/shapes_lines_splines.c b/examples/shapes/shapes_lines_splines.c new file mode 100644 index 00000000..c020c60b --- /dev/null +++ b/examples/shapes/shapes_lines_splines.c @@ -0,0 +1,155 @@ +/******************************************************************************************* +* +* raylib [shapes] example - splines drawing +* +* Example originally created with raylib 4.6-dev, last time updated with raylib 4.6-dev +* +* 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) 2023 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#define MAX_CONTROL_POINTS 32 + +typedef struct { + Vector2 start; + Vector2 end; +} ControlPoint; + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - splines drawing"); + + Vector2 points[MAX_CONTROL_POINTS] = { + { 100.0f, 200.0f }, + { 300.0f, 400.0f }, + { 500.0f, 300.0f }, + { 700.0f, 100.0f }, + { 200.0f, 100.0f }, + }; + + int pointCount = 5; + int selectedPoint = -1; + + int splineType = 0; // 0-Linear, 1-BSpline, 2-CatmullRom, 3-Bezier + + // Cubic Bezier control points + ControlPoint control[MAX_CONTROL_POINTS] = { 0 }; + for (int i = 0; i < pointCount - 1; i++) + { + control[i].start = points[i]; + control[i].end = points[i + 1]; + } + + 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 + //---------------------------------------------------------------------------------- + // Points movement logic + if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON) && (pointCount < MAX_CONTROL_POINTS)) + { + points[pointCount] = GetMousePosition(); + pointCount++; + } + + for (int i = 0; i < pointCount; i++) + { + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointCircle(GetMousePosition(), points[i], 6.0f)) + { + selectedPoint = i; + break; + } + } + + if (selectedPoint >= 0) + { + points[selectedPoint] = GetMousePosition(); + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) selectedPoint = -1; + } + + // TODO: Cubic Bezier spline control points logic + + + // Spline selection logic + if (IsKeyPressed(KEY_ONE)) splineType = 0; + else if (IsKeyPressed(KEY_TWO)) splineType = 1; + else if (IsKeyPressed(KEY_THREE)) splineType = 2; + else if (IsKeyPressed(KEY_FOUR)) splineType = 3; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + if (splineType == 0) // Linear + { + // Draw linear spline + for (int i = 0; i < pointCount - 1; i++) + { + DrawLineEx(points[i], points[i + 1], 2.0f, RED); + } + } + else if (splineType == 1) // B-Spline + { + // Draw b-spline + DrawLineBSpline(points, pointCount, 2.0f, RED); + //for (int i = 0; i < (pointCount - 3); i++) DrawLineBSplineSegment(points[i], points[i + 1], points[i + 2], points[i + 3], 24.0f, BLUE); + } + else if (splineType == 2) // CatmullRom Spline + { + // Draw spline: catmull-rom + DrawLineCatmullRom(points, pointCount, 2.0f, RED); + //for (int i = 0; i < (pointCount - 3); i++) DrawLineCatmullRomSegment(points[i], points[i + 1], points[i + 2], points[i + 3], 24.0f, Fade(BLUE, 0.4f)); + } + else if (splineType == 3) // Cubic Bezier + { + // Draw line bezier cubic (with control points) + for (int i = 0; i < pointCount - 1; i++) + { + DrawLineBezierCubic(points[i], points[i + 1], control[i].start, control[i + 1].end, 2.0f, RED); + + // TODO: Every cubic bezier point should have two control points + DrawCircleV(control[i].start, 4, GOLD); + DrawCircleV(control[i].end, 4, GOLD); + DrawLineEx(points[i], control[i].start, 1.0, LIGHTGRAY); + DrawLineEx(points[i + 1], control[i].end, 1.0, LIGHTGRAY); + } + } + + // Draw control points + for (int i = 0; i < pointCount; i++) + { + DrawCircleV(points[i], 6.0f, RED); + if ((splineType != 0) && (i < pointCount - 1)) DrawLineV(points[i], points[i + 1], GRAY); + } + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}
\ No newline at end of file |
