#include #include #include #include "pxl8_macros.h" #include "pxl8_vfx.h" struct pxl8_particles { pxl8_particle* particles; u32 alive_count; u32 count; u32 max_count; f32 x, y; f32 spread_x, spread_y; f32 drag; f32 gravity_x, gravity_y; f32 turbulence; f32 spawn_rate; f32 spawn_timer; void (*render_fn)(pxl8_gfx* gfx, pxl8_particle* p, void* userdata); void (*spawn_fn)(pxl8_particle* p, void* userdata); void (*update_fn)(pxl8_particle* p, f32 dt, void* userdata); void* userdata; }; void pxl8_vfx_plasma(pxl8_gfx* ctx, f32 time, f32 scale1, f32 scale2, u8 palette_offset) { if (!ctx || !pxl8_gfx_get_framebuffer(ctx)) return; for (i32 y = 0; y < pxl8_gfx_get_height(ctx); y++) { for (i32 x = 0; x < pxl8_gfx_get_width(ctx); x++) { f32 v1 = sinf(x * scale1 + time); f32 v2 = sinf(y * scale1 + time * 0.7f); f32 v3 = sinf((x + y) * scale2 + time * 1.3f); f32 v4 = sinf(sqrtf(x * x + y * y) * 0.05f + time * 0.5f); f32 v = v1 + v2 + v3 + v4; f32 normalized = (1.0f + v / 4.0f) * 0.5f; if (normalized < 0.0f) normalized = 0.0f; if (normalized > 1.0f) normalized = 1.0f; u8 color = palette_offset + (u8)(15.0f * normalized); pxl8_pixel(ctx, x, y, color); } } } void pxl8_vfx_raster_bars(pxl8_gfx* ctx, pxl8_raster_bar* bars, u32 bar_count, f32 time) { if (!ctx || !bars) return; for (u32 i = 0; i < bar_count; i++) { pxl8_raster_bar* bar = &bars[i]; f32 y = bar->base_y + bar->amplitude * sinf(time * bar->speed + bar->phase); i32 y_int = (i32)y; for (i32 dy = 0; dy < bar->height; dy++) { f32 position = (f32)dy / (f32)(bar->height - 1); f32 distance_from_center = fabsf(position - 0.5f) * 2.0f; u8 color_idx; if (distance_from_center < 0.3f) { color_idx = bar->fade_color; } else if (distance_from_center < 0.6f) { color_idx = bar->fade_color - 1; } else if (distance_from_center < 0.8f) { color_idx = bar->color; } else { color_idx = bar->color - 1; } pxl8_rect_fill(ctx, 0, y_int + dy, pxl8_gfx_get_width(ctx), 1, color_idx); } } } void pxl8_vfx_rotozoom(pxl8_gfx* ctx, f32 angle, f32 zoom, i32 cx, i32 cy) { if (!ctx || !pxl8_gfx_get_framebuffer(ctx)) return; f32 cos_a = cosf(angle); f32 sin_a = sinf(angle); u8* temp_buffer = (u8*)SDL_malloc(pxl8_gfx_get_width(ctx) * pxl8_gfx_get_height(ctx)); if (!temp_buffer) return; SDL_memcpy(temp_buffer, pxl8_gfx_get_framebuffer(ctx), pxl8_gfx_get_width(ctx) * pxl8_gfx_get_height(ctx)); for (i32 y = 0; y < pxl8_gfx_get_height(ctx); y++) { for (i32 x = 0; x < pxl8_gfx_get_width(ctx); x++) { f32 dx = x - cx; f32 dy = y - cy; f32 src_x = cx + (dx * cos_a - dy * sin_a) / zoom; f32 src_y = cy + (dx * sin_a + dy * cos_a) / zoom; i32 sx = (i32)src_x; i32 sy = (i32)src_y; if (sx >= 0 && sx < pxl8_gfx_get_width(ctx) && sy >= 0 && sy < pxl8_gfx_get_height(ctx)) { pxl8_gfx_get_framebuffer(ctx)[y * pxl8_gfx_get_width(ctx) + x] = temp_buffer[sy * pxl8_gfx_get_width(ctx) + sx]; } } } SDL_free(temp_buffer); } void pxl8_vfx_tunnel(pxl8_gfx* ctx, f32 time, f32 speed, f32 twist) { if (!ctx || !pxl8_gfx_get_framebuffer(ctx)) return; f32 cx = pxl8_gfx_get_width(ctx) / 2.0f; f32 cy = pxl8_gfx_get_height(ctx) / 2.0f; for (i32 y = 0; y < pxl8_gfx_get_height(ctx); y++) { for (i32 x = 0; x < pxl8_gfx_get_width(ctx); x++) { f32 dx = x - cx; f32 dy = y - cy; f32 dist = sqrtf(dx * dx + dy * dy); if (dist > 1.0f) { f32 angle = atan2f(dy, dx); f32 u = angle * 0.159f + twist * sinf(time * 0.5f); f32 v = 256.0f / dist + time * speed; u8 tx = (u8)((i32)(u * 256.0f) & 0xFF); u8 ty = (u8)((i32)(v * 256.0f) & 0xFF); u8 color = tx ^ ty; pxl8_pixel(ctx, x, y, color); } } } } void pxl8_vfx_water_ripple(pxl8_gfx* ctx, f32* height_map, i32 drop_x, i32 drop_y, f32 damping) { if (!ctx || !height_map) return; i32 w = pxl8_gfx_get_width(ctx); i32 h = pxl8_gfx_get_height(ctx); static f32* prev_height = NULL; if (!prev_height) { prev_height = (f32*)SDL_calloc(w * h, sizeof(f32)); if (!prev_height) return; } if (drop_x >= 0 && drop_x < w && drop_y >= 0 && drop_y < h) { height_map[drop_y * w + drop_x] = 255.0f; } for (i32 y = 1; y < h - 1; y++) { for (i32 x = 1; x < w - 1; x++) { i32 idx = y * w + x; f32 sum = height_map[idx - w] + height_map[idx + w] + height_map[idx - 1] + height_map[idx + 1]; f32 avg = sum / 2.0f; prev_height[idx] = (avg - prev_height[idx]) * damping; } } f32* temp = height_map; height_map = prev_height; prev_height = temp; } pxl8_particles* pxl8_particles_create(u32 max_count) { pxl8_particles* particles = SDL_calloc(1, sizeof(pxl8_particles)); if (!particles) return NULL; particles->particles = SDL_calloc(max_count, sizeof(pxl8_particle)); if (!particles->particles) { SDL_free(particles); return NULL; } particles->max_count = max_count; particles->drag = 0.98f; particles->gravity_y = 100.0f; particles->spawn_rate = 10.0f; return particles; } void pxl8_particles_destroy(pxl8_particles* particles) { if (!particles) return; SDL_free(particles->particles); SDL_free(particles); } void pxl8_particles_clear(pxl8_particles* particles) { if (!particles || !particles->particles) return; for (u32 i = 0; i < particles->max_count; i++) { particles->particles[i].life = 0; particles->particles[i].flags = 0; } particles->alive_count = 0; particles->spawn_timer = 0; } void pxl8_particles_emit(pxl8_particles* particles, u32 count) { if (!particles || !particles->particles) return; for (u32 i = 0; i < count && particles->alive_count < particles->max_count; i++) { for (u32 j = 0; j < particles->max_count; j++) { if (particles->particles[j].life <= 0) { pxl8_particle* p = &particles->particles[j]; p->life = 1.0f; p->max_life = 1.0f; p->x = particles->x + (((f32)rand() / RAND_MAX) - 0.5f) * particles->spread_x; p->y = particles->y + (((f32)rand() / RAND_MAX) - 0.5f) * particles->spread_y; p->z = 0; p->vx = p->vy = p->vz = 0; p->ax = particles->gravity_x; p->ay = particles->gravity_y; p->az = 0; p->color = p->start_color = p->end_color = 15; p->size = 1.0f; p->angle = 0; p->spin = 0; p->flags = 1; if (particles->spawn_fn) { particles->spawn_fn(p, particles->userdata); } particles->alive_count++; break; } } } } void pxl8_particles_render(pxl8_particles* particles, pxl8_gfx* gfx) { if (!particles || !particles->particles || !gfx) return; for (u32 i = 0; i < particles->max_count; i++) { pxl8_particle* p = &particles->particles[i]; if (p->life > 0 && p->flags) { if (particles->render_fn) { particles->render_fn(gfx, p, particles->userdata); } else { i32 x = (i32)p->x; i32 y = (i32)p->y; if (x >= 0 && x < pxl8_gfx_get_width(gfx) && y >= 0 && y < pxl8_gfx_get_height(gfx)) { pxl8_pixel(gfx, x, y, p->color); } } } } } void pxl8_particles_update(pxl8_particles* particles, f32 dt) { if (!particles || !particles->particles) return; if (particles->spawn_rate > 0.0f) { particles->spawn_timer += dt; f32 spawn_interval = 1.0f / particles->spawn_rate; u32 max_spawns_per_frame = particles->max_count / 10; if (max_spawns_per_frame < 1) max_spawns_per_frame = 1; u32 spawn_count = 0; while (particles->spawn_timer >= spawn_interval && spawn_count < max_spawns_per_frame) { pxl8_particles_emit(particles, 1); particles->spawn_timer -= spawn_interval; spawn_count++; } } for (u32 i = 0; i < particles->max_count; i++) { pxl8_particle* p = &particles->particles[i]; if (p->life > 0) { if (particles->update_fn) { particles->update_fn(p, dt, particles->userdata); } else { p->vx += p->ax * dt; p->vy += p->ay * dt; p->vz += p->az * dt; p->vx *= particles->drag; p->vy *= particles->drag; p->vz *= particles->drag; p->x += p->vx * dt; p->y += p->vy * dt; p->z += p->vz * dt; p->angle += p->spin * dt; } p->life -= dt / p->max_life; if (p->life <= 0) { p->flags = 0; particles->alive_count--; } } } } void pxl8_vfx_explosion(pxl8_particles* particles, i32 x, i32 y, u32 color, f32 force) { if (!particles) return; particles->x = x; particles->y = y; particles->spread_x = particles->spread_y = 2.0f; particles->gravity_x = 0; particles->gravity_y = 200.0f; particles->drag = 0.95f; particles->update_fn = NULL; for (u32 i = 0; i < 50 && i < particles->max_count; i++) { pxl8_particle* p = &particles->particles[i]; f32 angle = ((f32)rand() / RAND_MAX) * 6.28f; f32 speed = force * (0.5f + ((f32)rand() / RAND_MAX) * 0.5f); p->x = x; p->y = y; p->vx = cosf(angle) * speed; p->vy = sinf(angle) * speed; p->life = 1.0f; p->max_life = 1.0f + ((f32)rand() / RAND_MAX); p->color = color; p->flags = 1; } } static void fire_spawn(pxl8_particle* p, void* userdata) { uintptr_t palette_start = (uintptr_t)userdata; p->start_color = palette_start + 6 + (rand() % 3); p->end_color = palette_start; p->color = p->start_color; p->max_life = 1.5f + ((f32)rand() / RAND_MAX) * 1.5f; p->vy = -80.0f - ((f32)rand() / RAND_MAX) * 120.0f; p->vx = (((f32)rand() / RAND_MAX) - 0.5f) * 40.0f; } static void fire_update(pxl8_particle* p, f32 dt, void* userdata) { (void)userdata; p->vx += p->ax * dt; p->vy += p->ay * dt; p->vz += p->az * dt; p->x += p->vx * dt; p->y += p->vy * dt; p->z += p->vz * dt; f32 life_ratio = 1.0f - (p->life / p->max_life); if (p->start_color >= p->end_color) { u8 color_range = p->start_color - p->end_color; p->color = p->start_color - (u8)(life_ratio * color_range); } else { u8 color_range = p->end_color - p->start_color; p->color = p->start_color + (u8)(life_ratio * color_range); } } void pxl8_vfx_fire(pxl8_particles* particles, i32 x, i32 y, i32 width, u8 palette_start) { if (!particles) return; particles->x = x; particles->y = y; particles->spread_x = width; particles->spread_y = 4.0f; particles->gravity_x = 0; particles->gravity_y = -100.0f; particles->drag = 0.97f; particles->spawn_rate = 120.0f; particles->spawn_fn = fire_spawn; particles->update_fn = fire_update; particles->userdata = (void*)(uintptr_t)palette_start; } static void rain_spawn(pxl8_particle* p, void* userdata) { (void)userdata; p->start_color = 27 + (rand() % 3); p->end_color = 29; p->color = p->start_color; p->max_life = 2.0f; p->vy = 200.0f + ((f32)rand() / RAND_MAX) * 100.0f; } void pxl8_vfx_rain(pxl8_particles* particles, i32 width, f32 wind) { if (!particles) return; particles->x = width / 2.0f; particles->y = -10; particles->spread_x = width; particles->spread_y = 0; particles->gravity_x = wind; particles->gravity_y = 300.0f; particles->drag = 1.0f; particles->spawn_rate = 100.0f; particles->spawn_fn = rain_spawn; particles->update_fn = NULL; } static void smoke_spawn(pxl8_particle* p, void* userdata) { uintptr_t base_color = (uintptr_t)userdata; p->start_color = base_color; p->end_color = base_color + 4; p->color = p->start_color; p->max_life = 3.0f + ((f32)rand() / RAND_MAX) * 2.0f; p->vy = -20.0f - ((f32)rand() / RAND_MAX) * 30.0f; p->vx = (((f32)rand() / RAND_MAX) - 0.5f) * 20.0f; p->size = 1.0f + ((f32)rand() / RAND_MAX) * 2.0f; } void pxl8_vfx_smoke(pxl8_particles* particles, i32 x, i32 y, u8 color) { if (!particles) return; particles->x = x; particles->y = y; particles->spread_x = 5.0f; particles->spread_y = 5.0f; particles->gravity_x = 0; particles->gravity_y = -50.0f; particles->drag = 0.96f; particles->spawn_rate = 20.0f; particles->spawn_fn = smoke_spawn; particles->update_fn = NULL; particles->userdata = (void*)(uintptr_t)color; } static void snow_spawn(pxl8_particle* p, void* userdata) { (void)userdata; p->start_color = 8 + (rand() % 3); p->end_color = 10; p->color = p->start_color; p->max_life = 4.0f; p->vx = (((f32)rand() / RAND_MAX) - 0.5f) * 20.0f; p->vy = 30.0f + ((f32)rand() / RAND_MAX) * 20.0f; } void pxl8_vfx_snow(pxl8_particles* particles, i32 width, f32 wind) { if (!particles) return; particles->x = width / 2.0f; particles->y = -10; particles->spread_x = width; particles->spread_y = 0; particles->gravity_x = wind; particles->gravity_y = 30.0f; particles->drag = 1.0f; particles->spawn_rate = 30.0f; particles->spawn_fn = snow_spawn; particles->update_fn = NULL; } static void sparks_spawn(pxl8_particle* p, void* userdata) { uintptr_t base_color = (uintptr_t)userdata; p->start_color = base_color; p->end_color = base_color > 2 ? base_color - 2 : 0; p->color = p->start_color; p->max_life = 0.5f + ((f32)rand() / RAND_MAX) * 1.0f; f32 angle = ((f32)rand() / RAND_MAX) * 6.28f; f32 speed = 100.0f + ((f32)rand() / RAND_MAX) * 200.0f; p->vx = cosf(angle) * speed; p->vy = sinf(angle) * speed - 50.0f; } void pxl8_vfx_sparks(pxl8_particles* particles, i32 x, i32 y, u32 color) { if (!particles) return; particles->x = x; particles->y = y; particles->spread_x = 2.0f; particles->spread_y = 2.0f; particles->gravity_x = 0; particles->gravity_y = 100.0f; particles->drag = 0.97f; particles->spawn_rate = 40.0f; particles->spawn_fn = sparks_spawn; particles->update_fn = NULL; particles->userdata = (void*)(uintptr_t)color; } void pxl8_vfx_starfield(pxl8_particles* particles, f32 speed, f32 spread) { if (!particles) return; particles->spread_x = particles->spread_y = spread; particles->gravity_x = particles->gravity_y = 0; particles->drag = 1.0f; particles->spawn_rate = 0; particles->update_fn = NULL; for (u32 i = 0; i < particles->max_count; i++) { pxl8_particle* p = &particles->particles[i]; p->x = ((f32)rand() / RAND_MAX) * spread * 2.0f - spread; p->y = ((f32)rand() / RAND_MAX) * spread * 2.0f - spread; p->z = ((f32)rand() / RAND_MAX) * spread; p->vz = -speed; p->life = 1000.0f; p->max_life = 1000.0f; p->color = 8 + (rand() % 8); p->flags = 1; } }