REXM: Update examples collection, some renames

This commit is contained in:
Ray
2025-10-17 17:01:38 +02:00
parent 4099218f1a
commit ed8c4c1b9b
17 changed files with 126 additions and 111 deletions

View File

@ -2,7 +2,7 @@
*
* raylib [shapes] example - pie chart
*
* Example complexity rating: [★☆☆☆] 1/4
* Example complexity rating: [★★★☆] 3/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.6
*

View File

@ -2,7 +2,7 @@
*
* raylib [shapes] example - simple particles
*
* Example complexity rating: [★☆☆] 1/4
* Example complexity rating: [★☆☆] 2/4
*
* Example originally created with raylib 5.6, last time updated with raylib 5.6
*
@ -20,7 +20,7 @@
#include <stdlib.h> // Required for: calloc(), free()
#include <math.h> // Required for: cosf(), sinf()
#define MAX_PARTICLES 3000 // Max number particles
#define MAX_PARTICLES 3000 // Max number of particles
//----------------------------------------------------------------------------------
// Types and Structures Definition
@ -100,6 +100,7 @@ int main(void)
// Update the parameters of each particle
UpdateParticles(&circularBuffer, screenWidth, screenHeight);
// Remove dead particles from the circular buffer
UpdateCircularBuffer(&circularBuffer);
@ -217,46 +218,53 @@ static void UpdateParticles(CircularBuffer *circularBuffer, int screenWidth, int
switch (circularBuffer->buffer[i].type)
{
case WATER:
{
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x;
circularBuffer->buffer[i].velocity.y += 0.2f; // Gravity
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
break;
} break;
case SMOKE:
{
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x;
circularBuffer->buffer[i].velocity.y -= 0.05f; // Upwards
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
circularBuffer->buffer[i].radius += 0.5f; // Increment radius: smoke expands
circularBuffer->buffer[i].color.a -= 4; // Decrement alpha: smoke fades
if (circularBuffer->buffer[i].color.a < 4) // If alpha transparent, particle dies
circularBuffer->buffer[i].alive = false;
break;
// If alpha transparent, particle dies
if (circularBuffer->buffer[i].color.a < 4) circularBuffer->buffer[i].alive = false;
} break;
case FIRE:
{
// Add a little horizontal oscillation to fire particles
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x + cosf(circularBuffer->buffer[i].lifeTime*215.0f);
circularBuffer->buffer[i].velocity.y -= 0.05f; // Upwards
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
circularBuffer->buffer[i].radius -= 0.15f; // Decrement radius: fire shrinks
circularBuffer->buffer[i].color.g -= 3; // Decrement green: fire turns reddish starting from yellow
if (circularBuffer->buffer[i].radius <= 0.02f) // If radius too small, particle dies
circularBuffer->buffer[i].alive = false;
break;
// If radius too small, particle dies
if (circularBuffer->buffer[i].radius <= 0.02f) circularBuffer->buffer[i].alive = false;
} break;
default: break;
}
// Disable particle when out of screen
Vector2 center = circularBuffer->buffer[i].position;
float radius = circularBuffer->buffer[i].radius;
if ((center.x < -radius) || (center.x > screenWidth + radius) ||
(center.y < -radius) || (center.y > screenHeight + radius))
if ((center.x < -radius) || (center.x > (screenWidth + radius)) ||
(center.y < -radius) || (center.y > (screenHeight + radius)))
{
circularBuffer->buffer[i].alive = false;
}
}
}
static void UpdateCircularBuffer(CircularBuffer *circularBuffer)
{
// Update circular buffer: advance tail over dead particles
while ((circularBuffer->tail != circularBuffer->head) &&
!circularBuffer->buffer[circularBuffer->tail].alive)
while ((circularBuffer->tail != circularBuffer->head) && !circularBuffer->buffer[circularBuffer->tail].alive)
{
circularBuffer->tail = (circularBuffer->tail + 1)%MAX_PARTICLES;
}

View File

@ -1,8 +1,8 @@
/*******************************************************************************************
*
* raylib [shapes] example - starfield
* raylib [shapes] example - starfield effect
*
* Example complexity rating: [] 1/4
* Example complexity rating: [] 2/4
*
* Example originally created with raylib 5.5, last time updated with raylib 5.6-dev
*
@ -31,7 +31,7 @@ int main(void)
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield");
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect");
Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f);
@ -45,9 +45,10 @@ int main(void)
Vector2 starsScreenPos[STAR_COUNT] = { 0 };
// Setup the stars with a random position
for (int i = 0; i < STAR_COUNT; i++) {
stars[i].x = GetRandomValue(-screenWidth*.5, screenWidth*.5);
stars[i].y = GetRandomValue(-screenHeight*.5, screenHeight*.5);
for (int i = 0; i < STAR_COUNT; i++)
{
stars[i].x = GetRandomValue(-screenWidth*0.5f, screenWidth*0.5f);
stars[i].y = GetRandomValue(-screenHeight*0.5f, screenHeight*0.5f);
stars[i].z = 1.0f;
}
@ -59,39 +60,36 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Change speed based on number keys
for (int i = 0; i <= 9; i++) {
if (IsKeyPressed(KEY_ZERO + i)) {
speed = 2.0f * (float)i / 9.0f;
}
}
// Change speed based on mouse
float mouseMove = GetMouseWheelMove();
if ((int)mouseMove != 0) speed += 2.0f*mouseMove/9.0f;
if (speed < 0.0f) speed = 0.1f;
else if (speed > 2.0f) speed = 2.0f;
// Toggle lines / points with space bar
if (IsKeyPressed(KEY_SPACE)) {
drawLines = !drawLines;
}
if (IsKeyPressed(KEY_SPACE)) drawLines = !drawLines;
float dt = GetFrameTime();
for (int i = 0; i < STAR_COUNT; i++) {
for (int i = 0; i < STAR_COUNT; i++)
{
// Update star's timer
stars[i].z -= dt * speed;
stars[i].z -= dt*speed;
// Calculate the screen position
starsScreenPos[i] = (Vector2) {
screenWidth*.5f + stars[i].x / stars[i].z,
screenHeight*.5f + stars[i].y / stars[i].z,
starsScreenPos[i] = (Vector2){
screenWidth*0.5f + stars[i].x/stars[i].z,
screenHeight*0.5f + stars[i].y/stars[i].z,
};
// If the star is too old, or offscreen, it dies and we make a new random one
if (stars[i].z < 0.0f
|| starsScreenPos[i].x < 0 || starsScreenPos[i].y < 0.0f
|| starsScreenPos[i].x > screenWidth || starsScreenPos[i].y > screenHeight) {
stars[i].x = GetRandomValue(-screenWidth*.5, screenWidth*.5);
stars[i].y = GetRandomValue(-screenHeight*.5, screenHeight*.5);
if ((stars[i].z < 0.0f) || (starsScreenPos[i].x < 0) || (starsScreenPos[i].y < 0.0f) ||
(starsScreenPos[i].x > screenWidth) || (starsScreenPos[i].y > screenHeight))
{
stars[i].x = GetRandomValue(-screenWidth*0.5f, screenWidth*0.5f);
stars[i].y = GetRandomValue(-screenHeight*0.5f, screenHeight*0.5f);
stars[i].z = 1.0f;
}
}
//----------------------------------------------------------------------------------
// Draw
@ -100,42 +98,47 @@ int main(void)
ClearBackground(bgColor);
for (int i = 0; i < STAR_COUNT; i++) {
if (drawLines) {
for (int i = 0; i < STAR_COUNT; i++)
{
if (drawLines)
{
// Get the time a little while ago for this star, but clamp it
float t = Clamp(stars[i].z + 1.0f/32.0f, 0.0f, 1.0f);
// If it's different enough from the current time, we proceed
if (t - stars[i].z > 1e-3) {
if ((t - stars[i].z) > 1e-3)
{
// Calculate the screen position of the old point
Vector2 startPos = (Vector2) {
screenWidth*.5f + stars[i].x / t,
screenHeight*.5f + stars[i].y / t,
Vector2 startPos = (Vector2){
screenWidth*0.5f + stars[i].x/t,
screenHeight*0.5f + stars[i].y/t,
};
// Draw a line connecting the old point to the current point
DrawLineV(startPos, starsScreenPos[i], RAYWHITE);
}
}
else {
else
{
// Make the radius grow as the star ages
float radius = Lerp(stars[i].z, 1.0f, 5.0f);
// Draw the circle
DrawCircleV(starsScreenPos[i], radius, RAYWHITE);
}
}
DrawText(TextFormat("[MOUSE WHEEL] Current Speed: %.0f", 9.0f*speed/2.0f), 10, 40, 20, RAYWHITE);
DrawText(TextFormat("[SPACE] Current draw mode: %s", drawLines ? "Lines" : "Circles"), 10, 70, 20, RAYWHITE);
DrawFPS(10, 10);
DrawText(TextFormat("Current Speed: %.0f [Number keys to change]", 9.0f * speed / 2.0f), 10, 30, 20, RAYWHITE);
DrawText(TextFormat("Drawing %s [Space to change]", drawLines ? "Lines" : "Circles"), 10, 50, 20, RAYWHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB