Prettify particles & put them on different layers

Also hazards now emit black particles
This commit is contained in:
Edgaru089 2024-04-16 21:08:21 +08:00
parent e655723786
commit e613fe3bbd
11 changed files with 119 additions and 61 deletions

View File

@ -46,7 +46,7 @@ void app_DebugText(App *app, vector_Vector *vec_string) {
PUSH_STRING(buf); PUSH_STRING(buf);
snprintf( snprintf(
buf, sizeof(buf) - 1, buf, sizeof(buf) - 1,
"Particle count: %d\n", tree_Count(app->particle->parts)); "Particle count[0]: %d\n", tree_Count(app->particle->parts[0]));
PUSH_STRING(buf); PUSH_STRING(buf);
char zero = '\0'; char zero = '\0';

View File

@ -6,6 +6,7 @@
#include "player.h" #include "player.h"
#include "types.h" #include "types.h"
#include "util/assert.h" #include "util/assert.h"
#include "util/rand.h"
#include <stdio.h> #include <stdio.h>
@ -52,6 +53,40 @@ void misc_thinker_Hazard(App *app, Entity *e, Duration deltaTime) {
e->thinker = NULL; e->thinker = NULL;
return; return;
} }
// Emit some black particles
// BTW, very bad (but reliable) way to store a number here!
static const FillMode mode = {
.rop2 = 13, // R2_COPYPEN
.style = 0, // BS_SOLID
.hatch = 0,
.rotate = {.microseconds = 0},
.dissolve = {.microseconds = 0},
.fadein = false,
.bg = 0xffffff,
.fg = 0};
uint64_t emitCooldown = 400.0 * 40000.0 / e->misc->trigger.size.x; // 40 msec across 400px
TimePoint lastEmit = {.microseconds = (uint64_t)e->thinkerData};
if (time_Since(lastEmit).microseconds > emitCooldown) {
lastEmit = time_Now();
lastEmit.microseconds -= 10000 * rand_Double01();
Box2 worldbox = ABSOLUTE_BOX(e, e->misc->trigger);
particle_Emit(
app->particle,
0,
vec2_Random(
worldbox.lefttop.x + 20,
worldbox.lefttop.x + worldbox.size.x - 20,
worldbox.lefttop.y + 20.0,
worldbox.lefttop.y + 40.0),
vec2(0.0, rand_DoubleRange(-200.0, -280.0)),
rand_DoubleRange(1, 1.5),
rand_DoubleRange(18, 30),
20, duration_FromSeconds(0), &mode);
e->thinkerData = (void *)lastEmit.microseconds;
}
if (app->player->player == NULL) // No player if (app->player->player == NULL) // No player
return; return;
Component_Player *p = app->player->player; Component_Player *p = app->player->player;

View File

@ -7,28 +7,40 @@
#include <stdio.h> #include <stdio.h>
typedef struct {
uint64_t layer;
uint64_t id;
} _particle_DeleteRecord;
static inline void particle_Delete(System_Particle *sys, int layer, uint64_t id) {
uint64_t a[2] = {layer, id};
vector_Push(sys->deleted_ids, a);
}
System_Particle *particle_NewSystem(App *super) { System_Particle *particle_NewSystem(App *super) {
System_Particle *sys = zero_malloc(sizeof(System_Particle)); System_Particle *sys = zero_malloc(sizeof(System_Particle));
sys->super = super; sys->super = super;
sys->parts = tree_Create(sizeof(Particle)); sys->deleted_ids = vector_Create(sizeof(_particle_DeleteRecord));
sys->deleted_ids = vector_Create(sizeof(uintptr_t));
sys->maxID = 0; sys->maxID = 0;
for (int i = 0; i < PARTICLE_LAYER_COUNT; i++)
sys->parts[i] = tree_Create(sizeof(Particle));
return sys; return sys;
} }
void particle_DeleteSystem(System_Particle *sys) { void particle_DeleteSystem(System_Particle *sys) {
tree_Destroy(sys->parts); for (int i = 0; i < PARTICLE_LAYER_COUNT; i++)
tree_Destroy(sys->parts[i]);
vector_Destroy(sys->deleted_ids); vector_Destroy(sys->deleted_ids);
free(sys); free(sys);
} }
static void _particle_AdvancePart(System_Particle *sys, Particle *p, Duration deltaTime) { static void _particle_AdvancePart(System_Particle *sys, int layer, Particle *p, Duration deltaTime) {
p->size -= p->sizedec * duration_Seconds(deltaTime); p->size -= p->sizedec * duration_Seconds(deltaTime);
if ((p->tolive.microseconds != 0 && if ((p->tolive.microseconds != 0 &&
time_Since(p->addtime).microseconds > p->tolive.microseconds) || time_Since(p->addtime).microseconds > p->tolive.microseconds) ||
p->size < EPS) { // vanished p->size < EPS) { // vanished
particle_Delete(sys, p->id); particle_Delete(sys, layer, p->id);
return; return;
} }
@ -41,33 +53,33 @@ static void _particle_AdvancePart(System_Particle *sys, Particle *p, Duration de
} }
void particle_Advance(System_Particle *sys, Duration deltaTime) { void particle_Advance(System_Particle *sys, Duration deltaTime) {
// Clear particles marked as deleted // Delete particles
for (int i = 0; i < vector_Size(sys->deleted_ids); i++) { int len = vector_Size(sys->deleted_ids);
uintptr_t id = *(uintptr_t *)vector_At(sys->deleted_ids, i); for (int i = 0; i < len; i++) {
tree_Node *n = tree_FindNode(sys->parts, id); _particle_DeleteRecord rec = *(_particle_DeleteRecord *)vector_At(sys->deleted_ids, i);
if (n != NULL) tree_Node *node = tree_FindNode(sys->parts[rec.layer], rec.id);
tree_Delete(sys->parts, n); if (!node)
WARN("particle id %llu in layer %llu not found", rec.id, rec.layer);
else else
fprintf(stderr, "[particle_Delete][WARN] Missing particle ID [%d]\n", id); tree_Delete(sys->parts[rec.layer], node);
} }
vector_Clear(sys->deleted_ids); vector_Clear(sys->deleted_ids);
for (tree_Node *i = tree_FirstNode(sys->parts); for (int layer = 0; layer < PARTICLE_LAYER_COUNT; layer++) {
for (tree_Node *i = tree_FirstNode(sys->parts[layer]);
i != NULL; i != NULL;
i = tree_Node_Next(i)) { i = tree_Node_Next(i)) {
Particle *p = (Particle *)i->data; Particle *p = (Particle *)i->data;
_particle_AdvancePart(sys, p, deltaTime); _particle_AdvancePart(sys, layer, p, deltaTime);
}
} }
} }
void particle_Delete(System_Particle *sys, uintptr_t id) {
vector_Push(sys->deleted_ids, &id);
}
void particle_Emit(System_Particle *sys, int layer, Vec2 pos, Vec2 vec, double vec_friction, double size, double sizedec, Duration tolive, const FillMode *fill) {
void particle_Emit(System_Particle *sys, Vec2 pos, Vec2 vec, double vec_friction, double size, double sizedec, Duration tolive, const FillMode *fill) { ASSERT(layer >= 0 && layer <= (PARTICLE_LAYER_COUNT - 1));
uintptr_t id = ++sys->maxID; uintptr_t id = ++sys->maxID;
Particle *p = tree_Insert(sys->parts, id, NULL); Particle *p = tree_Insert(sys->parts[layer], id, NULL);
memset(p, 0, sizeof(Particle)); memset(p, 0, sizeof(Particle));
p->id = id; p->id = id;
p->addtime = time_Now(); p->addtime = time_Now();
@ -83,10 +95,11 @@ void particle_Emit(System_Particle *sys, Vec2 pos, Vec2 vec, double vec_friction
void particle_Clear(System_Particle *sys) { void particle_Clear(System_Particle *sys) {
vector_Clear(sys->deleted_ids); vector_Clear(sys->deleted_ids);
for (int i = 0; i < PARTICLE_LAYER_COUNT; i++) {
// Delete every first node // Delete every first node
int count = tree_Count(sys->parts); int count = tree_Count(sys->parts[i]);
while (count--) while (count--)
tree_Delete(sys->parts, tree_FirstNode(sys->parts)); tree_Delete(sys->parts[i], tree_FirstNode(sys->parts[i]));
ASSERT(tree_Count(sys->parts[i]) == 0);
ASSERT(tree_Count(sys->parts) == 0); }
} }

View File

@ -11,6 +11,9 @@ extern "C" {
#endif #endif
#define PARTICLE_LAYER_COUNT 4
// A particle instance. // A particle instance.
typedef struct { typedef struct {
uintptr_t id; // ID. uintptr_t id; // ID.
@ -30,7 +33,7 @@ typedef struct _App App;
// Particle system. // Particle system.
typedef struct { typedef struct {
App *super; App *super;
tree_Tree *parts; // uintptr_t -> struct Particle tree_Tree *parts[PARTICLE_LAYER_COUNT]; // uintptr_t -> struct Particle
vector_Vector *deleted_ids; vector_Vector *deleted_ids;
uintptr_t maxID; uintptr_t maxID;
} System_Particle; } System_Particle;
@ -43,8 +46,7 @@ void particle_Render(System_Particle *sys);
// Emit a particle here. // Emit a particle here.
void particle_Emit(System_Particle *sys, Vec2 pos, Vec2 vec, double vec_friction, double size, double sizedec, Duration tolive, const FillMode *fill); void particle_Emit(System_Particle *sys, int layer, Vec2 pos, Vec2 vec, double vec_friction, double size, double sizedec, Duration tolive, const FillMode *fill);
void particle_Delete(System_Particle *sys, uintptr_t id);
// Clear all particles. // Clear all particles.
void particle_Clear(System_Particle *sys); void particle_Clear(System_Particle *sys);

View File

@ -10,10 +10,8 @@
extern "C" { extern "C" {
void particle_Render(System_Particle *sys) { void particle_Render(System_Particle *sys) {
ASSERT(sys->super->camera && "particle_Render called without a Camera system"); for (int l = 0; l < PARTICLE_LAYER_COUNT; l++)
System_Camera *cam = sys->super->camera; for (tree_Node *i = tree_FirstNode(sys->parts[l]);
for (tree_Node *i = tree_FirstNode(sys->parts);
i != NULL; i != NULL;
i = tree_Node_Next(i)) { i = tree_Node_Next(i)) {
Particle *p = (Particle *)i->data; Particle *p = (Particle *)i->data;

View File

@ -83,6 +83,7 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y / 2.0)); Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y / 2.0));
particle_Emit( particle_Emit(
sys->super->particle, sys->super->particle,
3,
vec2_Add(vec2_Random(-5, 5, -30, 30), to_pos), vec2_Add(vec2_Random(-5, 5, -30, 30), to_pos),
vec2(rand_DoubleRange(650, 700) * -p->faceDirection, rand_DoubleRange(-100, 100)), vec2(rand_DoubleRange(650, 700) * -p->faceDirection, rand_DoubleRange(-100, 100)),
7, rand_DoubleRange(14, 20), rand_DoubleRange(32, 40), 7, rand_DoubleRange(14, 20), rand_DoubleRange(32, 40),
@ -98,6 +99,7 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y)); Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y));
particle_Emit( particle_Emit(
sys->super->particle, sys->super->particle,
3,
vec2_Add(vec2_Random(-10, 10, -10, 10), to_pos), vec2_Add(vec2_Random(-10, 10, -10, 10), to_pos),
vec2_Add( vec2_Add(
vec2(0, rand_DoubleRange(-200, -240)), vec2(0, rand_DoubleRange(-200, -240)),
@ -134,6 +136,7 @@ void player_Advance(System_Player *sys, Duration deltaTime) {
for (int i = 0; i < airjumpParticleCount; i++) for (int i = 0; i < airjumpParticleCount; i++)
particle_Emit( particle_Emit(
sys->super->particle, sys->super->particle,
3,
vec2_Add(vec2_Random(-20, 20, -5, 5), p->super->position->position), vec2_Add(vec2_Random(-20, 20, -5, 5), p->super->position->position),
(p->onGround) ? vec2(rand_DoubleRange(-50, 50), -100) : vec2(0, 200), 2, rand_DoubleRange(8, 12), 20, (p->onGround) ? vec2(rand_DoubleRange(-50, 50), -100) : vec2(0, 200), 2, rand_DoubleRange(8, 12), 20,
duration_FromSeconds(0), &render_ModeRotate); duration_FromSeconds(0), &render_ModeRotate);
@ -188,6 +191,7 @@ void player_HazardHarm(System_Player *sys) {
double angle = rand_Double01() * 2.0 * M_PI; double angle = rand_Double01() * 2.0 * M_PI;
particle_Emit( particle_Emit(
sys->super->particle, sys->super->particle,
3,
sys->player->super->position->position, sys->player->super->position->position,
vec2(speed_linear * cos(angle), speed_linear * sin(angle)), vec2(speed_linear * cos(angle), speed_linear * sin(angle)),
rand_DoubleRange(2, 3), rand_DoubleRange(2, 3),

View File

@ -19,7 +19,6 @@ typedef enum {
// render_Primitive describes one render operation. // render_Primitive describes one render operation.
typedef struct { typedef struct {
FillMode mode; // fill mode FillMode mode; // fill mode
uint32_t fg, bg; // foreground & background colors
render_PrimitiveType type; render_PrimitiveType type;
vector_Vector *points; // Vector of Vec2 vector_Vector *points; // Vector of Vec2

View File

@ -47,9 +47,6 @@ extern "C" void render_DrawPrimitiveW(App *app, render_Primitive *p, Vec2 offset
// See if any of the points are in the camera box // See if any of the points are in the camera box
if (needDraw) { if (needDraw) {
// Set the colors // Set the colors
setlinecolor(p->fg);
setfillcolor(p->fg);
setbkcolor(p->bg);
render_SetModes(p->mode, time_Now()); render_SetModes(p->mode, time_Now());
// Draw the converted primitive // Draw the converted primitive

View File

@ -51,8 +51,6 @@ static void _render_BundleCommand(char *cmd) {
} }
_tmpp = malloc(sizeof(render_Primitive)); _tmpp = malloc(sizeof(render_Primitive));
_tmpp->points = vector_Create(sizeof(Vec2)); _tmpp->points = vector_Create(sizeof(Vec2));
_tmpp->fg = 0xffffff;
_tmpp->bg = 0;
_tmpp->mode = render_ModeDefault; _tmpp->mode = render_ModeDefault;
// parse the type // parse the type
@ -91,7 +89,7 @@ static void _render_BundleCommand(char *cmd) {
int r = atoi(strtok(NULL, " ")); int r = atoi(strtok(NULL, " "));
int g = atoi(strtok(NULL, " ")); int g = atoi(strtok(NULL, " "));
int b = atoi(strtok(NULL, " ")); int b = atoi(strtok(NULL, " "));
_tmpp->fg = RGB(r, g, b); _tmpp->mode.fg = RGB(r, g, b);
} else } else
WARN("FG without PRIM first", 0); WARN("FG without PRIM first", 0);
} else if (CMD("BG")) { } else if (CMD("BG")) {
@ -100,7 +98,7 @@ static void _render_BundleCommand(char *cmd) {
int r = atoi(strtok(NULL, " ")); int r = atoi(strtok(NULL, " "));
int g = atoi(strtok(NULL, " ")); int g = atoi(strtok(NULL, " "));
int b = atoi(strtok(NULL, " ")); int b = atoi(strtok(NULL, " "));
_tmpp->bg = RGB(r, g, b); _tmpp->mode.bg = RGB(r, g, b);
} else } else
WARN("BG without PRIM first", 0); WARN("BG without PRIM first", 0);
} else { } else {

View File

@ -61,21 +61,27 @@ const FillMode render_ModeDefault = {
.hatch = 0, .hatch = 0,
.rotate = {.microseconds = 0}, .rotate = {.microseconds = 0},
.dissolve = {.microseconds = 0}, .dissolve = {.microseconds = 0},
.fadein = false}; .fadein = false,
.bg = 0,
.fg = 0xffffff};
const FillMode render_ModeInverse = { const FillMode render_ModeInverse = {
.rop2 = R2_NOT, .rop2 = R2_NOT,
.style = BS_SOLID, .style = BS_SOLID,
.hatch = 0, .hatch = 0,
.rotate = {.microseconds = 0}, .rotate = {.microseconds = 0},
.dissolve = {.microseconds = 0}, .dissolve = {.microseconds = 0},
.fadein = false}; .fadein = false,
.bg = 0,
.fg = 0xffffff};
extern const FillMode render_ModeRotate = { extern const FillMode render_ModeRotate = {
.rop2 = R2_COPYPEN, .rop2 = R2_COPYPEN,
.style = BS_SOLID, .style = BS_SOLID,
.hatch = 0, .hatch = 0,
.rotate = {.microseconds = 100000}, .rotate = {.microseconds = 100000},
.dissolve = {.microseconds = 0}, .dissolve = {.microseconds = 0},
.fadein = false}; .fadein = false,
.bg = 0,
.fg = 0xffffff};
void render_SetModes(FillMode mode, TimePoint since) { void render_SetModes(FillMode mode, TimePoint since) {
@ -97,12 +103,17 @@ void render_SetModes(FillMode mode, TimePoint since) {
static const long hatches[] = {HS_HORIZONTAL, HS_FDIAGONAL, HS_VERTICAL, HS_BDIAGONAL}; static const long hatches[] = {HS_HORIZONTAL, HS_FDIAGONAL, HS_VERTICAL, HS_BDIAGONAL};
setfillstyle(BS_HATCHED, hatches[steps % 4], NULL); setfillstyle(BS_HATCHED, hatches[steps % 4], NULL);
setrop2(R2_COPYPEN); setrop2(R2_COPYPEN);
setfillcolor(0xffffff);
setbkcolor(0);
return; return;
} }
// Normal mode // Normal mode
setfillstyle(mode.style, mode.hatch, NULL); setfillstyle(mode.style, mode.hatch, NULL);
setrop2(mode.rop2); setrop2(mode.rop2);
setlinecolor(mode.fg);
setfillcolor(mode.fg);
setbkcolor(mode.bg);
} }
void render_FillScreen() { void render_FillScreen() {

View File

@ -43,6 +43,7 @@ typedef struct {
// amount of time. Overrides rotate, style & hatch // amount of time. Overrides rotate, style & hatch
Duration dissolve; Duration dissolve;
bool fadein; // Fade Into being full for true, fade out of otherwise bool fadein; // Fade Into being full for true, fade out of otherwise
uint32_t bg, fg; // Background & foreground colors
} FillMode; } FillMode;
// Default fill mode. // Default fill mode.