RogueLife/src/particles.c

70 lines
1.6 KiB
C

#include "particles.h"
#include <gint/display.h>
#include <stdio.h>
static bool damage_update(particle_damage_t *p, GUNUSED fixed_t dt)
{
return p->particle.age >= 500;
}
static void damage_render(int x, int y, particle_damage_t *p)
{
extern bopti_image_t img_font_damage_white;
extern bopti_image_t img_font_damage_red;
bopti_image_t *img = (p->color == C_RED) ? &img_font_damage_red :
&img_font_damage_white;
int char_w = img->width / 10;
int char_h = img->height;
/* Determine number of characters */
char str[16];
int n = snprintf(str, 16, "%d", p->damage);
y -= char_h / 2;
x -= (char_w * n + 1) / 2;
for(int i = 0; i < n; i++) {
int offset = (char_w + 1) * (str[i] - '0');
dsubimage(x, y, img, offset, 0, char_w, char_h, DIMAGE_NONE);
x += char_w;
}
}
static bool dash_update(particle_dash_t *p, GUNUSED fixed_t dt)
{
return p->particle.age >= 500;
}
static void dash_render(int x, int y, particle_dash_t *p)
{
if(!p->frame) return;
anim_frame_render(x, y, p->frame);
}
//---
// Generic functions
//---
bool particle_update(particle_t *p, fixed_t dt)
{
p->age += fround(dt * 1000);
if(p->type == PARTICLE_DAMAGE)
return damage_update((void *)p, dt);
if(p->type == PARTICLE_DASH)
return dash_update((void *)p, dt);
return true;
}
void particle_render(int x, int y, particle_t const *p)
{
if(p->type == PARTICLE_DAMAGE)
return damage_render(x, y, (void *)p);
if(p->type == PARTICLE_DASH)
return dash_render(x, y, (void *)p);
}