aboutsummaryrefslogtreecommitdiff
path: root/raylib/examples/shapes/shapes_lines_splines.c
blob: c020c60b6f28014f04ee4e7020e7d8219ec3d77b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
}