4 #include "dying_rect.h"
6 #include "system/error.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 const camera_t *camera)
86 for (size_t i = 0; i < DYING_RECT_PIECE_COUNT; ++i) {
87 color_t color = dying_rect->color;
88 color.a = fminf(1.0f, 4.0f - (float) dying_rect->time_passed / (float) dying_rect->duration * 4.0f);
90 if (camera_fill_triangle(
92 triangle_mat3x3_product(
93 dying_rect->pieces[i].body,
95 trans_mat(dying_rect->pieces[i].position.x,
96 dying_rect->pieces[i].position.y),
97 rot_mat(dying_rect->pieces[i].angle))),
106 int dying_rect_update(dying_rect_t *dying_rect,
110 assert(delta_time > 0.0f);
112 if (dying_rect_is_dead(dying_rect)) {
116 dying_rect->time_passed = dying_rect->time_passed + delta_time;
118 for (size_t i = 0; i < DYING_RECT_PIECE_COUNT; ++i) {
120 &dying_rect->pieces[i].position,
122 dying_rect->pieces[i].direction,
124 dying_rect->pieces[i].angle = fmodf(
125 dying_rect->pieces[i].angle + dying_rect->pieces[i].angle_velocity * delta_time,
132 int dying_rect_is_dead(const dying_rect_t *dying_rect)
135 return dying_rect->time_passed >= dying_rect->duration;