Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Celeste: particles

Mason Chen4b9b9025ba94
diff --git a/games/celeste/src/celeste.h b/games/celeste/src/celeste.h
index 22d6088a..27a43147 100644
--- a/games/celeste/src/celeste.h
+++ b/games/celeste/src/celeste.h
@@ -214,6 +214,7 @@ typedef void (*StripFn)(uint16_t *strip, int y0, int y1, void *ctx);
void gfx_custom(StripFn fn, void *ctx, int y0, int y1);
void gfx_keep(bool on); /* the layers that must be drawn (tiles, stylegrounds in front): commands kept for them */
void blit_line(uint16_t *strip, int sy0, int sy1, float x0, float y0, float x1, float y1, uint16_t col, uint8_t alpha);
+void blit_rect(uint16_t *strip, int sy0, int sy1, float x, float y, float w, float h, uint16_t col, uint8_t alpha);
/* gfx_tex_ex drawn into the strip by a StripFn layer (world coordinates; the texture got when the layer was recorded) */
void blit_tex_ex(uint16_t *strip, int sy0, int sy1, uint16_t tex, float x, float y, float ox, float oy, float sx, float sy,
float rot, uint16_t tint, uint8_t alpha, uint8_t flags);
diff --git a/games/celeste/src/entities.c b/games/celeste/src/entities.c
index 24c7ef60..0f976611 100644
--- a/games/celeste/src/entities.c
+++ b/games/celeste/src/entities.c
@@ -469,6 +469,9 @@ static void fx_entity_update(Ent *e) {
fx_update();
}
static const EntClass FX = {.name = "fx", .update = fx_entity_update};
+/* the level's ParticlesBG, Particles and ParticlesFG (drawn at their depths) */
+static void parts_render(Ent *e) { particles_render(e->depth == D_BGPARTICLES ? PL_BG : e->depth == D_PARTICLES ? PL_MID : PL_FG); }
+static const EntClass PARTS = {.name = "particles", .render = parts_render};
/* FormationBackdrop: darkens the level behind hearts, cassettes, seeds being put together */
static void formation_update(Ent *e) {
@@ -1439,6 +1442,9 @@ void entities_load(Room *rm, const uint8_t *blob, int slot) {
if (!have) {
Ent *e = ent_new(&FX, 0, 0);
if (e) e->tags = TAG_GLOBAL | TAG_TRANSITION_UPDATE, e->collidable = 0, e->visible = 0, e->dead = 0;
+ static const int32_t depths[3] = {D_BGPARTICLES, D_PARTICLES, D_FGPARTICLES};
+ for (int k = 0; k < 3; k++)
+ if ((e = ent_new(&PARTS, 0, 0)) != NULL) e->tags = TAG_GLOBAL, e->depth = depths[k], e->collidable = 0, e->dead = 0;
e = ent_new(&FORMATION, 0, 0);
if (e) e->tags = TAG_GLOBAL | TAG_FROZEN_UPDATE, e->depth = -1999900, e->collidable = 0, e->dead = 0;
}
diff --git a/games/celeste/src/fx.c b/games/celeste/src/fx.c
index 06b4fb38..4556a711 100644
--- a/games/celeste/src/fx.c
+++ b/games/celeste/src/fx.c
@@ -87,23 +87,37 @@ void particles_update(void) {
}
}
-void particles_render(int layer) {
+/* ParticleSystem.Render: a layer's particles drawn into the strips by one command (as pictures they were up to 128
+ * commands, most of them scaled or turned: the frame has 48 of those) */
+static void particles_strip(uint16_t *strip, int sy0, int sy1, void *ctx) {
+ int layer = (int)(intptr_t)ctx;
for (int i = 0; i < MAX_PARTICLES; i++) {
Particle *p = &parts[i];
uint8_t a = (uint8_t)(p->color >> 24);
if (!p->active || p->layer != layer || !a) continue;
- float x = (float)(int)p->pos.x, y = (float)(int)p->pos.y;
+ float x = (float)(int)p->pos.x, y = (float)(int)p->pos.y; /* (Particle.Render: the position truncated) */
if (p->src == 0xFFFF) {
int s = (int)(p->size + 0.5f);
- if (s < 1) continue;
- gfx_rect(x - s / 2, y - s / 2, (float)s, (float)s, rgb(p->color & 0xFFFFFF), a);
+ if (s >= 1) blit_rect(strip, sy0, sy1, x - s / 2, y - s / 2, (float)s, (float)s, rgb(p->color & 0xFFFFFF), a);
} else {
Tex t;
- if (!tex_get(p->src, &t)) continue;
- gfx_tex_ex(p->src, x, y, t.fw / 2.f, t.fh / 2.f, p->size, p->size, p->rot, rgb(p->color & 0xFFFFFF), a, 0);
+ if (tex_get(p->src, &t))
+ blit_tex_ex(strip, sy0, sy1, p->src, x, y, t.fw * t.scale / 2.f, t.fh * t.scale / 2.f, p->size, p->size, p->rot,
+ rgb(p->color & 0xFFFFFF), a, 0);
}
}
}
+void particles_render(int layer) {
+ float y0 = 1e9f, y1 = -1e9f;
+ for (int i = 0; i < MAX_PARTICLES; i++) {
+ Particle *p = &parts[i];
+ if (!p->active || p->layer != layer || !(p->color >> 24)) continue;
+ Tex t;
+ if (p->src != 0xFFFF) tex_get(p->src, &t); /* (loaded now: nothing loads while the strips are drawn) */
+ y0 = fminf(y0, p->pos.y), y1 = fmaxf(y1, p->pos.y);
+ }
+ if (y0 <= y1) gfx_custom(particles_strip, (void *)(intptr_t)layer, (int)floorf(y0) - 24 - g_camy, (int)ceilf(y1) + 25 - g_camy);
+}
/* Celeste's Dust.Burst and BurstFG */
void dust_burst(V2 pos, float dir, int n) {
diff --git a/games/celeste/src/gfx.c b/games/celeste/src/gfx.c
index e0a53bc2..33c42513 100644
--- a/games/celeste/src/gfx.c
+++ b/games/celeste/src/gfx.c
@@ -952,6 +952,11 @@ void blit_tex_ex(uint16_t *strip, int sy0, int sy1, uint16_t tex, float x, float
blit_affine(strip, sy0, sy1, &t, &a, tint, alpha, flags);
}
+/* gfx_rect (world coordinates) drawn straight into a strip, for StripFn layers */
+void blit_rect(uint16_t *strip, int sy0, int sy1, float x, float y, float w, float h, uint16_t col, uint8_t alpha) {
+ fill(strip, sy0, sy1, (int)floorf(x + 0.5f) - g_camx, (int)floorf(y + 0.5f) - g_camy, (int)floorf(w + 0.5f),
+ (int)floorf(h + 0.5f), col, alpha);
+}
void gfx_rect(float x, float y, float w, float h, uint16_t col, uint8_t alpha) {
if (!alpha) return;
int X = sx_(x), Y = sy_(y), W_ = (int)floorf(w + 0.5f), H_ = (int)floorf(h + 0.5f);