Merge branch 'master' into more-spline-functions

This commit is contained in:
Ray 2025-07-30 11:43:02 +02:00 • committed by GitHub
commit b80920b1ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 20425 additions and 18426 deletions

View file

@ -52,6 +52,8 @@ int main(void)
DrawCircle(screenWidth/5, 120, 35, DARKBLUE);
DrawCircleGradient(screenWidth/5, 220, 60, GREEN, SKYBLUE);
DrawCircleLines(screenWidth/5, 340, 80, DARKBLUE);
DrawEllipse(screenWidth/5, 120, 25, 20, YELLOW);
DrawEllipseLines(screenWidth/5, 120, 30, 25, YELLOW);
// Rectangle shapes and lines
DrawRectangle(screenWidth/4*2 - 60, 100, 120, 60, RED);

View file

@ -0,0 +1,185 @@
/*******************************************************************************************
*
* raylib [shapes] example - fancy clock using basic shapes
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.5
*
* Example contributed by Hamza RAHAL (@hmz-rhl) and reviewed by Ramon Santamaria (@raysan5)
*
* 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) 2025 Hamza RAHAL (@hmz-rhl)
*
********************************************************************************************/
#include "raylib.h"
#include <math.h> // Required for: cosf(), sinf()
#include <time.h> // Required for: time(), localtime()
#define DIGIT_SIZE 30
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum {
MODE_NORMAL = 0,
MODE_HANDS_FREE,
} ClockMode;
typedef struct {
int value;
Vector2 origin;
float angle;
int length;
int thickness;
Color color;
} ClockHand;
typedef struct {
ClockMode mode;
ClockHand second;
ClockHand minute;
ClockHand hour;
} Clock;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void UpdateClock(Clock *clock); // Update clock time
static void DrawClock(Clock clock, Vector2 centerPos); // Draw clock at desired position
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock");
// Initialize clock
Clock myClock = {
.mode = MODE_NORMAL,
.second.angle = 45,
.second.length = 140,
.second.thickness = 3,
.second.color = BEIGE,
.minute.angle = 10,
.minute.length = 130,
.minute.thickness = 7,
.minute.color = DARKGRAY,
.hour.angle = 0,
.hour.length = 100,
.hour.thickness = 7,
.hour.color = BLACK,
};
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
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE))
{
if (myClock.mode == MODE_HANDS_FREE) myClock.mode = MODE_NORMAL;
else if (myClock.mode == MODE_NORMAL) myClock.mode = MODE_HANDS_FREE;
}
UpdateClock(&myClock);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircle(400, 225, 5, BLACK); // Clock center dot
DrawClock(myClock, (Vector2){ 400, 225 }); // Clock in selected mode
DrawText("Press [SPACE] to switch clock mode", 10, 10, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Update clock time
static void UpdateClock(Clock *clock)
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
// Updating time data
clock->second.value = timeinfo->tm_sec;
clock->minute.value = timeinfo->tm_min;
clock->hour.value = timeinfo->tm_hour;
clock->hour.angle = (timeinfo->tm_hour%12)*180.0/6.0f;
clock->hour.angle += (timeinfo->tm_min%60)*30/60.0f;
clock->hour.angle -= 90;
clock->minute.angle = (timeinfo->tm_min%60)*6.0f;
clock->minute.angle += (timeinfo->tm_sec%60)*6/60.0f;
clock->minute.angle -= 90;
clock->second.angle = (timeinfo->tm_sec%60)*6.0f;
clock->second.angle -= 90;
}
// Draw clock
static void DrawClock(Clock clock, Vector2 centerPosition)
{
if (clock.mode == MODE_HANDS_FREE)
{
DrawCircleLinesV(centerPosition, clock.minute.length, LIGHTGRAY);
DrawText(TextFormat("%i", clock.second.value), centerPosition.x + (clock.second.length - 10)*cosf(clock.second.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.second.length*sinf(clock.second.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GRAY);
DrawText(TextFormat("%i", clock.minute.value), clock.minute.origin.x + clock.minute.length*cosf(clock.minute.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.minute.length*sinf(clock.minute.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, RED);
DrawText(TextFormat("%i", clock.hour.value), centerPosition.x + clock.hour.length*cosf(clock.hour.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.hour.length*sinf(clock.hour.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GOLD);
}
else if (clock.mode == MODE_NORMAL)
{
// Draw hand seconds
DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.second.length, clock.second.thickness },
(Vector2){ 0.0f, clock.second.thickness/2.0f }, clock.second.angle, clock.second.color);
// Draw hand minutes
DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.minute.length, clock.minute.thickness },
(Vector2){ 0.0f, clock.minute.thickness/2.0f }, clock.minute.angle, clock.minute.color);
// Draw hand hours
DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.hour.length, clock.hour.thickness },
(Vector2){ 0.0f, clock.hour.thickness/2.0f }, clock.hour.angle, clock.hour.color);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View file

@ -0,0 +1,182 @@
/*******************************************************************************************
*
* raylib [shapes] example - Double Pendulum
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.5
*
* Example contributed by JoeCheong (@Joecheong2006) and reviewed by Ramon Santamaria (@raysan5)
*
* 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) 2025 JoeCheong (@Joecheong2006)
*
********************************************************************************************/
#include "raylib.h"
#include <math.h> // Required for: sin(), cos(), PI
//----------------------------------------------------------------------------------
// Macro Helpers
//----------------------------------------------------------------------------------
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 450
#define CENTER_X SCREEN_WIDTH * 0.5
#define CENTER_Y SCREEN_HEIGHT * 0.5 - 100
// Constant for Simulation
#define SIMULATION_STEPS 30
#define G 9.81
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static Vector2 CalculatePendulumEndPoint(float l, float theta);
static Vector2 CalculateDoublePendulumEndPoint(float l1, float theta1, float l2, float theta2);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - Double Pendulum");
// Simulation Paramters
//--------------------------------------------------------------------------------------
float l1 = 15, m1 = 0.2, theta1 = DEG2RAD * 170, w1 = 0;
float l2 = 15, m2 = 0.1, theta2 = DEG2RAD * 0, w2 = 0;
float lengthScaler = 0.1;
float totalM = m1 + m2;
Vector2 previousPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2);
previousPosition.x += CENTER_X;
previousPosition.y += CENTER_Y;
// Scale length
float L1 = l1 * lengthScaler;
float L2 = l2 * lengthScaler;
// Draw Parameters
//--------------------------------------------------------------------------------------
int lineThick = 20, trailThick = 2;
float fateAlpha = 0.01;
// Create Framebuffer
//--------------------------------------------------------------------------------------
RenderTexture2D target = LoadRenderTexture(SCREEN_WIDTH, SCREEN_HEIGHT);
SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
float dt = GetFrameTime();
float step = dt / SIMULATION_STEPS, step2 = step * step;
// Update Physics - larger steps = better approximation
//----------------------------------------------------------------------------------
for (int i = 0; i < SIMULATION_STEPS; ++i)
{
float delta = theta1 - theta2;
float sinD = sin(delta), cosD = cos(delta), cos2D = cos(2 * delta);
float ww1 = w1 * w1, ww2 = w2 * w2;
// Calculate a1
float a1 = (-G * (2 * m1 + m2) * sin(theta1)
- m2 * G * sin(theta1 - 2 * theta2)
- 2 * sinD * m2 * (ww2 * L2 + ww1 * L1 * cosD))
/ (L1 * (2 * m1 + m2 - m2 * cos2D));
// Calculate a2
float a2 = (2 * sinD * (ww1 * L1 * totalM
+ G * totalM * cos(theta1)
+ ww2 * L2 * m2 * cosD))
/ (L2 * (2 * m1 + m2 - m2 * cos2D));
// Update thetas
theta1 += w1 * step + 0.5 * a1 * step2;
theta2 += w2 * step + 0.5 * a2 * step2;
// Update omegas
w1 += a1 * step;
w2 += a2 * step;
}
//----------------------------------------------------------------------------------
// Calculate position
Vector2 currentPosition = CalculateDoublePendulumEndPoint(l1, theta1, l2, theta2);
currentPosition.x += CENTER_X;
currentPosition.y += CENTER_Y;
// Draw to framebuffer
//----------------------------------------------------------------------------------
BeginTextureMode(target);
// Draw a transparent rectangle - smaller alpha = longer trails
DrawRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Fade(BLACK, fateAlpha));
// Draw trail
DrawCircleV(previousPosition, trailThick, RED);
DrawLineEx(previousPosition, currentPosition, trailThick * 2, RED);
EndTextureMode();
//----------------------------------------------------------------------------------
// Update previous position
previousPosition = currentPosition;
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
// Draw Trails Texture
DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height },
(Vector2){ 0, 0 }, WHITE);
// Draw Double Pendulum
DrawRectanglePro((Rectangle){ CENTER_X, CENTER_Y, 10 * l1, lineThick },
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta1, RAYWHITE);
Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1);
DrawRectanglePro((Rectangle){ CENTER_X + endpoint1.x, CENTER_Y + endpoint1.y, 10 * l2, lineThick },
(Vector2){0, lineThick * 0.5}, 90 - RAD2DEG * theta2, RAYWHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(target);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Calculate Pendulum End Point
static Vector2 CalculatePendulumEndPoint(float l, float theta)
{
return (Vector2){ 10 * l * sin(theta), 10 * l * cos(theta) };
}
// Calculate Double Pendulum End Point
static Vector2 CalculateDoublePendulumEndPoint(float l1, float theta1, float l2, float theta2)
{
Vector2 endpoint1 = CalculatePendulumEndPoint(l1, theta1);
Vector2 endpoint2 = CalculatePendulumEndPoint(l2, theta2);
return (Vector2){ endpoint1.x + endpoint2.x, endpoint1.y + endpoint2.y };
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 KiB

View file

@ -15,7 +15,7 @@
*
********************************************************************************************/
#include <raylib.h>
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h" // Required for GUI controls

View file

@ -15,7 +15,7 @@
*
********************************************************************************************/
#include <raylib.h>
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h" // Required for GUI controls

View file

@ -15,7 +15,7 @@
*
********************************************************************************************/
#include <raylib.h>
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h" // Required for GUI controls

View file

@ -103,15 +103,18 @@ int main(void)
}
// Spline point focus and selection logic
for (int i = 0; i < pointCount; i++)
if ((selectedPoint == -1) && ((splineTypeActive != SPLINE_BEZIER) || (selectedControlPoint == NULL)))
{
if (CheckCollisionPointCircle(GetMousePosition(), points[i], 8.0f))
focusedPoint = -1;
for (int i = 0; i < pointCount; i++)
{
focusedPoint = i;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) selectedPoint = i;
break;
if (CheckCollisionPointCircle(GetMousePosition(), points[i], 8.0f))
{
focusedPoint = i;
break;
}
}
else focusedPoint = -1;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) selectedPoint = focusedPoint;
}
// Spline point movement logic
@ -125,21 +128,23 @@ int main(void)
if (((splineTypeActive == SPLINE_BEZIER) || (splineTypeActive == SPLINE_BEZIER_VAR)) && (focusedPoint == -1))
{
// Spline control point focus and selection logic
for (int i = 0; i < pointCount - 1; i++)
if (selectedControlPoint == NULL)
{
if (CheckCollisionPointCircle(GetMousePosition(), control[i].start, 6.0f))
focusedControlPoint = NULL;
for (int i = 0; i < pointCount - 1; i++)
{
focusedControlPoint = &control[i].start;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) selectedControlPoint = &control[i].start;
break;
if (CheckCollisionPointCircle(GetMousePosition(), control[i].start, 6.0f))
{
focusedControlPoint = &control[i].start;
break;
}
else if (CheckCollisionPointCircle(GetMousePosition(), control[i].end, 6.0f))
{
focusedControlPoint = &control[i].end;
break;
}
}
else if (CheckCollisionPointCircle(GetMousePosition(), control[i].end, 6.0f))
{
focusedControlPoint = &control[i].end;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) selectedControlPoint = &control[i].end;
break;
}
else focusedControlPoint = NULL;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) selectedControlPoint = focusedControlPoint;
}
// Spline control point movement logic
@ -157,6 +162,9 @@ int main(void)
else if (IsKeyPressed(KEY_FOUR)) splineTypeActive = 3;
else if (IsKeyPressed(KEY_FIVE)) splineTypeActive = 4;
else if (IsKeyPressed(KEY_SIX)) splineTypeActive = 5;
// Clear selection when changing to a spline without control points
if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE)) selectedControlPoint = NULL;
//----------------------------------------------------------------------------------
// Draw
@ -299,7 +307,7 @@ int main(void)
}
// Check all possible UI states that require controls lock
if (splineTypeEditMode) GuiLock();
if (splineTypeEditMode || (selectedPoint != -1) || (selectedControlPoint != NULL)) GuiLock();
// Draw spline config
GuiLabel((Rectangle){ 12, 62, 140, 24 }, TextFormat("Spline thickness: %i", (int)splineThickness));
@ -307,10 +315,11 @@ int main(void)
GuiCheckBox((Rectangle){ 12, 110, 20, 20 }, "Show point helpers", &splineHelpersActive);
GuiUnlock();
if (splineTypeEditMode) GuiUnlock();
GuiLabel((Rectangle){ 12, 10, 140, 24 }, "Spline type:");
if (GuiDropdownBox((Rectangle){ 12, 8 + 24, 140, 28 }, "LINEAR;BSPLINE;CATMULLROM;BEZIER;LINEAR VARIABLE;BEZIER VARIABLE", &splineTypeActive, splineTypeEditMode)) splineTypeEditMode = !splineTypeEditMode;
GuiUnlock();
EndDrawing();
//----------------------------------------------------------------------------------