Changes to follow the code conventions. Improved some things.
This commit is contained in:
parent
333eead97b
commit
9f4640dd97
|
|
@ -20,7 +20,7 @@
|
||||||
#include <stdlib.h> // Required for: calloc(), free()
|
#include <stdlib.h> // Required for: calloc(), free()
|
||||||
#include <math.h> // Required for: cosf(), sinf()
|
#include <math.h> // Required for: cosf(), sinf()
|
||||||
|
|
||||||
#define MAX_PARTICLES 300 // Max number particles
|
#define MAX_PARTICLES 3000 // Max number particles
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Types and Structures Definition
|
// Types and Structures Definition
|
||||||
|
|
@ -52,6 +52,7 @@ typedef struct CircularBuffer {
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Declaration
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
static void EmitParticle(CircularBuffer *circularBuffer, Vector2 emitterPosition, ParticleType type);
|
||||||
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer);
|
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer);
|
||||||
static void UpdateParticles(CircularBuffer *circularBuffer, int screenWidth, int screenHeight);
|
static void UpdateParticles(CircularBuffer *circularBuffer, int screenWidth, int screenHeight);
|
||||||
static void UpdateCircularBuffer(CircularBuffer *circularBuffer);
|
static void UpdateCircularBuffer(CircularBuffer *circularBuffer);
|
||||||
|
|
@ -74,7 +75,7 @@ int main(void)
|
||||||
CircularBuffer circularBuffer = { 0, 0, particles };
|
CircularBuffer circularBuffer = { 0, 0, particles };
|
||||||
|
|
||||||
// Particle emitter parameters
|
// Particle emitter parameters
|
||||||
int emissionRate = 2;
|
int emissionRate = -2; // Negative: on average every -X frames. Positive: particles per frame
|
||||||
ParticleType currentType = WATER;
|
ParticleType currentType = WATER;
|
||||||
Vector2 emitterPosition = { screenWidth/2.0f, screenHeight/2.0f };
|
Vector2 emitterPosition = { screenWidth/2.0f, screenHeight/2.0f };
|
||||||
|
|
||||||
|
|
@ -87,41 +88,15 @@ int main(void)
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Emit new particles: when emissionRate is 1, emit every frame
|
// Emit new particles: when emissionRate is 1, emit every frame
|
||||||
if ((rand() % emissionRate) == 0)
|
if (emissionRate < 0)
|
||||||
{
|
{
|
||||||
Particle* newParticle = AddToCircularBuffer(&circularBuffer);
|
if (rand()%(-emissionRate) == 0)
|
||||||
// If buffer is full, newParticle is NULL
|
EmitParticle(&circularBuffer, emitterPosition, currentType);
|
||||||
if (newParticle != NULL)
|
|
||||||
{
|
|
||||||
// Fill particle properties
|
|
||||||
newParticle->position = emitterPosition;
|
|
||||||
newParticle->alive = true;
|
|
||||||
newParticle->lifeTime = 0.0f;
|
|
||||||
newParticle->type = currentType;
|
|
||||||
float speed = (float)(rand() % 10) / 5.0f;
|
|
||||||
switch (currentType)
|
|
||||||
{
|
|
||||||
case WATER:
|
|
||||||
newParticle->radius = 5.0f;
|
|
||||||
newParticle->color = BLUE;
|
|
||||||
break;
|
|
||||||
case SMOKE:
|
|
||||||
newParticle->radius = 7.0f;
|
|
||||||
newParticle->color = GRAY;
|
|
||||||
break;
|
|
||||||
case FIRE:
|
|
||||||
newParticle->radius = 10.0f;
|
|
||||||
newParticle->color = YELLOW;
|
|
||||||
speed /= 10.0f;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
newParticle->radius = 5.0f;
|
|
||||||
newParticle->color = WHITE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
float direction = (float)(rand() % 360);
|
|
||||||
newParticle->velocity = (Vector2){ speed * cosf(direction * DEG2RAD), speed * sinf(direction * DEG2RAD) };
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i <= emissionRate; ++i)
|
||||||
|
EmitParticle(&circularBuffer, emitterPosition, currentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the parameters of each particle
|
// Update the parameters of each particle
|
||||||
|
|
@ -130,18 +105,19 @@ int main(void)
|
||||||
UpdateCircularBuffer(&circularBuffer);
|
UpdateCircularBuffer(&circularBuffer);
|
||||||
|
|
||||||
// Change Particle Emission Rate (UP/DOWN arrows)
|
// Change Particle Emission Rate (UP/DOWN arrows)
|
||||||
if (IsKeyPressed(KEY_UP) && (emissionRate > 1))
|
if (IsKeyPressed(KEY_UP))
|
||||||
--emissionRate;
|
|
||||||
if (IsKeyPressed(KEY_DOWN))
|
|
||||||
++emissionRate;
|
++emissionRate;
|
||||||
|
if (IsKeyPressed(KEY_DOWN))
|
||||||
|
--emissionRate;
|
||||||
|
|
||||||
// Change Particle Type (LEFT/RIGHT arrows)
|
// Change Particle Type (LEFT/RIGHT arrows)
|
||||||
if (IsKeyPressed(KEY_RIGHT))
|
if (IsKeyPressed(KEY_RIGHT))
|
||||||
(currentType == FIRE) ? currentType = WATER : ++currentType;
|
(currentType == FIRE) ? (currentType = WATER) : ++currentType;
|
||||||
if (IsKeyPressed(KEY_LEFT))
|
if (IsKeyPressed(KEY_LEFT))
|
||||||
(currentType == WATER) ? currentType = FIRE : --currentType;
|
(currentType == WATER) ? (currentType = FIRE) : --currentType;
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
||||||
|
emitterPosition = GetMousePosition();
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
@ -160,7 +136,10 @@ int main(void)
|
||||||
DrawText("UP/DOWN: Change Particle Emission Rate", 15, 35, 10, BLACK);
|
DrawText("UP/DOWN: Change Particle Emission Rate", 15, 35, 10, BLACK);
|
||||||
DrawText("LEFT/RIGHT: Change Particle Type (Water, Smoke, Fire)", 15, 55, 10, BLACK);
|
DrawText("LEFT/RIGHT: Change Particle Type (Water, Smoke, Fire)", 15, 55, 10, BLACK);
|
||||||
|
|
||||||
DrawText(TextFormat("Emission Rate: %d | Type: %s", emissionRate, particleTypesChar[currentType]), 15, 95, 10, DARKGRAY);
|
if (emissionRate < 0)
|
||||||
|
DrawText(TextFormat("Particles every %d frames | Type: %s", -emissionRate, particleTypesChar[currentType]), 15, 95, 10, DARKGRAY);
|
||||||
|
else
|
||||||
|
DrawText(TextFormat("%d Particles per frame | Type: %s", emissionRate + 1, particleTypesChar[currentType]), 15, 95, 10, DARKGRAY);
|
||||||
|
|
||||||
DrawFPS(screenWidth - 80, 10);
|
DrawFPS(screenWidth - 80, 10);
|
||||||
|
|
||||||
|
|
@ -181,6 +160,40 @@ int main(void)
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Definition
|
// Module Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
static void EmitParticle(CircularBuffer *circularBuffer, Vector2 emitterPosition, ParticleType type)
|
||||||
|
{
|
||||||
|
Particle *newParticle = AddToCircularBuffer(circularBuffer);
|
||||||
|
// If buffer is full, newParticle is NULL
|
||||||
|
if (newParticle != NULL)
|
||||||
|
{
|
||||||
|
// Fill particle properties
|
||||||
|
newParticle->position = emitterPosition;
|
||||||
|
newParticle->alive = true;
|
||||||
|
newParticle->lifeTime = 0.0f;
|
||||||
|
newParticle->type = type;
|
||||||
|
float speed = (float)(rand()%10)/5.0f;
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case WATER:
|
||||||
|
newParticle->radius = 5.0f;
|
||||||
|
newParticle->color = BLUE;
|
||||||
|
break;
|
||||||
|
case SMOKE:
|
||||||
|
newParticle->radius = 7.0f;
|
||||||
|
newParticle->color = GRAY;
|
||||||
|
break;
|
||||||
|
case FIRE:
|
||||||
|
newParticle->radius = 10.0f;
|
||||||
|
newParticle->color = YELLOW;
|
||||||
|
speed /= 10.0f;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
float direction = (float)(rand()%360);
|
||||||
|
newParticle->velocity = (Vector2){ speed*cosf(direction*DEG2RAD), speed*sinf(direction*DEG2RAD) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer)
|
static Particle *AddToCircularBuffer(CircularBuffer *circularBuffer)
|
||||||
{
|
{
|
||||||
Particle *particle = NULL;
|
Particle *particle = NULL;
|
||||||
|
|
@ -221,11 +234,11 @@ static void UpdateParticles(CircularBuffer* circularBuffer, int screenWidth, int
|
||||||
break;
|
break;
|
||||||
case FIRE:
|
case FIRE:
|
||||||
// Add a little horizontal oscillation to fire particles
|
// Add a little horizontal oscillation to fire particles
|
||||||
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x + cosf(circularBuffer->buffer[i].lifeTime * 10.0f);
|
circularBuffer->buffer[i].position.x += circularBuffer->buffer[i].velocity.x + cosf(circularBuffer->buffer[i].lifeTime*215.0f);
|
||||||
circularBuffer->buffer[i].velocity.y -= 0.005f; // Upwards
|
circularBuffer->buffer[i].velocity.y -= 0.05f; // Upwards
|
||||||
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
|
circularBuffer->buffer[i].position.y += circularBuffer->buffer[i].velocity.y;
|
||||||
circularBuffer->buffer[i].radius -= 0.05f; // Decrement radius: fire shrinks
|
circularBuffer->buffer[i].radius -= 0.15f; // Decrement radius: fire shrinks
|
||||||
circularBuffer->buffer[i].color.g -= 1; // Decrement green: fire turns reddish starting from yellow
|
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
|
if (circularBuffer->buffer[i].radius <= 0.02f) // If radius too small, particle dies
|
||||||
circularBuffer->buffer[i].alive = false;
|
circularBuffer->buffer[i].alive = false;
|
||||||
break;
|
break;
|
||||||
|
|
@ -233,17 +246,17 @@ static void UpdateParticles(CircularBuffer* circularBuffer, int screenWidth, int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable particle when out of screen
|
// Disable particle when out of screen
|
||||||
if ((circularBuffer->buffer[i].position.x < 0) ||
|
Vector2 center = circularBuffer->buffer[i].position;
|
||||||
(circularBuffer->buffer[i].position.x > screenWidth) ||
|
float radius = circularBuffer->buffer[i].radius;
|
||||||
(circularBuffer->buffer[i].position.y < 0) ||
|
if ((center.x < -radius) || (center.x > screenWidth + radius) ||
|
||||||
(circularBuffer->buffer[i].position.y > screenHeight))
|
(center.y < -radius) || (center.y > screenHeight + radius))
|
||||||
circularBuffer->buffer[i].alive = false;
|
circularBuffer->buffer[i].alive = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static void UpdateCircularBuffer(CircularBuffer *circularBuffer)
|
static void UpdateCircularBuffer(CircularBuffer *circularBuffer)
|
||||||
{
|
{
|
||||||
// Update circular buffer: advance tail over dead particles
|
// Update circular buffer: advance tail over dead particles
|
||||||
while ((circularBuffer->tail != circularBuffer->head) &&
|
while (circularBuffer->tail != circularBuffer->head &&
|
||||||
!circularBuffer->buffer[circularBuffer->tail].alive)
|
!circularBuffer->buffer[circularBuffer->tail].alive)
|
||||||
{
|
{
|
||||||
circularBuffer->tail = (circularBuffer->tail + 1)%MAX_PARTICLES;
|
circularBuffer->tail = (circularBuffer->tail + 1)%MAX_PARTICLES;
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 32 KiB |
Loading…
Reference in New Issue
Block a user