Adds explosions of different kinds.

This commit is contained in:
Linus_Probert 2018-02-19 15:55:37 +01:00
parent 46e03af0b8
commit 0adcbc1b11
3 changed files with 69 additions and 1 deletions

View File

@ -17,6 +17,7 @@
*/
#include <stdlib.h>
#include <stdarg.h>
#include "particle_engine.h"
#include "linkedlist.h"
#include "util.h"
@ -92,6 +93,61 @@ particle_engine_bloodspray(Position pos, Dimension dim, unsigned int count)
}
}
static void
create_explosion(Position pos, Dimension dim, size_t c_count, ...)
{
SDL_Color *colors = ec_malloc(c_count * sizeof(SDL_Color));
va_list(args);
va_start(args, c_count);
for (size_t i = 0; i < c_count; ++i) {
colors[i] = va_arg(args, SDL_Color);
}
va_end(args);
for (unsigned int i = 0; i < 150; ++i) {
int x, y, xv, yv;
unsigned int lt;
Particle *p;
x = get_random(dim.width) + pos.x;
y = get_random(dim.height) + pos.y;
xv = get_random(600) - 300;
yv = get_random(600) - 300;
lt = get_random(10);
p = ec_malloc(sizeof(Particle));
p->pos = (Position) { x, y };
p->velocity = (Vector2d) { xv, yv };
p->movetime = lt;
p->lifetime = lt;
p->dim = (Dimension) { 2, 2 };
p->color = colors[get_random(c_count-1)];
linkedlist_append(&engine->particles, p);
}
}
void
particle_engine_fire_explosion(Position pos, Dimension dim)
{
static SDL_Color red = { 255, 0, 0, 255 };
static SDL_Color yellow = { 255, 255, 0, 255 };
check_engine();
create_explosion(pos, dim, 3, yellow, yellow, red);
}
void
particle_engine_eldritch_explosion(Position pos, Dimension dim)
{
static SDL_Color green = { 0, 255, 0, 255 };
check_engine();
create_explosion(pos, dim, 1, green);
}
static void
move_particle(Particle *particle, float deltaTime)
{
@ -145,8 +201,8 @@ render_particle(Particle *p, Camera *cam)
SDL_Rect box = { pos.x, pos.y, p->dim.width, p->dim.height };
SDL_SetRenderDrawColor(cam->renderer,
p->color.r,
p->color.b,
p->color.g,
p->color.b,
p->color.a);
SDL_RenderFillRect(cam->renderer, &box);
}

View File

@ -30,6 +30,12 @@ particle_engine_init(void);
void
particle_engine_bloodspray(Position, Dimension, unsigned int count);
void
particle_engine_fire_explosion(Position, Dimension);
void
particle_engine_eldritch_explosion(Position, Dimension);
void
particle_engine_update(float deltatime);

View File

@ -47,6 +47,12 @@ pointer_handle_event(Pointer *p, SDL_Event *event)
p->sprite->pos.y = event->motion.y - 6;
//debug("Pointer pos: %dx%d", p->sprite->pos.x, p->sprite->pos.y);
}
#ifdef DEBUG
if (event->type == SDL_MOUSEBUTTONDOWN) {
Dimension dim = { 10, 10 };
particle_engine_eldritch_explosion(p->sprite->pos, dim);
}
#endif // DEBUG
}
void