6 #include "./dying_rect.h"
9 #define DYING_RECT_PIECE_COUNT 20
10 #define DYING_RECT_PIECE_SIZE 20.0f
12 typedef struct piece_t {
32 dying_rect_t *create_dying_rect(rect_t rect,
36 lt_t *lt = create_lt();
41 dying_rect_t *dying_rect = PUSH_LT(lt, malloc(sizeof(dying_rect_t)), free);
42 if (dying_rect == NULL) {
43 throw_error(ERROR_TYPE_LIBC);
48 dying_rect->position = vec(rect.x, rect.y);
49 dying_rect->size = vec(rect.w, rect.h);
50 dying_rect->color = color;
51 dying_rect->duration = duration;
52 dying_rect->time_passed = 0;
54 dying_rect->pieces = PUSH_LT(lt, malloc(sizeof(piece_t) * DYING_RECT_PIECE_COUNT), free);
55 if (dying_rect->pieces == NULL) {
56 throw_error(ERROR_TYPE_LIBC);
60 for (size_t i = 0; i < DYING_RECT_PIECE_COUNT; ++i) {
61 dying_rect->pieces[i].position = dying_rect->position;
62 dying_rect->pieces[i].angle = rand_float(2 * PI);
63 dying_rect->pieces[i].angle_velocity = rand_float(8.0f);
64 dying_rect->pieces[i].body = random_triangle(DYING_RECT_PIECE_SIZE);
65 dying_rect->pieces[i].direction = vec_from_polar(
66 rand_float_range(-PI, 0.0f),
67 rand_float_range(100.0f, 300.0f));
73 void destroy_dying_rect(dying_rect_t *dying_rect)
76 RETURN_LT0(dying_rect->lt);
79 /* TODO(#109): Dying Rect animation is too boring */
80 int dying_rect_render(const dying_rect_t *dying_rect,
81 SDL_Renderer *renderer,
82 const camera_t *camera)
88 for (size_t i = 0; i < DYING_RECT_PIECE_COUNT; ++i) {
89 color_t color = dying_rect->color;
90 color.a = fminf(1.0f, 4.0f - (float) dying_rect->time_passed / (float) dying_rect->duration * 4.0f);
92 if (camera_fill_triangle(
95 triangle_mat3x3_product(
96 dying_rect->pieces[i].body,
98 trans_mat(dying_rect->pieces[i].position.x,
99 dying_rect->pieces[i].position.y),
100 rot_mat(dying_rect->pieces[i].angle))),
109 int dying_rect_update(dying_rect_t *dying_rect,
113 assert(delta_time > 0.0f);
115 if (dying_rect_is_dead(dying_rect)) {
119 dying_rect->time_passed = dying_rect->time_passed + delta_time;
121 for (size_t i = 0; i < DYING_RECT_PIECE_COUNT; ++i) {
123 &dying_rect->pieces[i].position,
125 dying_rect->pieces[i].direction,
127 dying_rect->pieces[i].angle = fmodf(
128 dying_rect->pieces[i].angle + dying_rect->pieces[i].angle_velocity * delta_time,
135 int dying_rect_is_dead(const dying_rect_t *dying_rect)
138 return dying_rect->time_passed >= dying_rect->duration;