]> git.lizzy.rs Git - nothing.git/blob - src/game/level/lava/wavy_rect.c
Delete LineStream from the existance
[nothing.git] / src / game / level / lava / wavy_rect.c
1 #include <SDL.h>
2 #include "system/stacktrace.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include "math/pi.h"
8 #include "system/log.h"
9 #include "system/lt.h"
10 #include "system/nth_alloc.h"
11 #include "wavy_rect.h"
12
13 #define WAVE_PILLAR_WIDTH 10.0f
14
15 struct Wavy_rect
16 {
17     Lt *lt;
18
19     Rect rect;
20     Color color;
21     float angle;
22 };
23
24 Wavy_rect *create_wavy_rect(Rect rect, Color color)
25 {
26     Lt *lt = create_lt();
27
28     Wavy_rect *wavy_rect = PUSH_LT(lt, nth_calloc(1, sizeof(Wavy_rect)), free);
29     if (wavy_rect == NULL) {
30         RETURN_LT(lt, NULL);
31     }
32
33     wavy_rect->rect = rect;
34     wavy_rect->color = color;
35     wavy_rect->angle = 0.0f;
36     wavy_rect->lt = lt;
37
38     return wavy_rect;
39 }
40
41 void destroy_wavy_rect(Wavy_rect *wavy_rect)
42 {
43     trace_assert(wavy_rect);
44     RETURN_LT0(wavy_rect->lt);
45 }
46
47 int wavy_rect_render(const Wavy_rect *wavy_rect,
48                      const Camera *camera)
49 {
50     trace_assert(wavy_rect);
51     trace_assert(camera);
52
53     srand(42);
54     for (float wave_scanner = 0;
55          wave_scanner < wavy_rect->rect.w;
56          wave_scanner += WAVE_PILLAR_WIDTH) {
57
58         const float s = (float) (rand() % 50) * 0.1f;
59         if (camera_fill_rect(
60                 camera,
61                 rect(
62                     wavy_rect->rect.x + wave_scanner,
63                     wavy_rect->rect.y + s * sinf(wavy_rect->angle + wave_scanner / WAVE_PILLAR_WIDTH),
64                     WAVE_PILLAR_WIDTH * 1.20f,
65                     wavy_rect->rect.h),
66                 wavy_rect->color) < 0) {
67             return -1;
68         }
69     }
70     srand((unsigned int) time(NULL));
71
72     return 0;
73 }
74
75 int wavy_rect_update(Wavy_rect *wavy_rect,
76                      float delta_time)
77 {
78     trace_assert(wavy_rect);
79     wavy_rect->angle = fmodf(wavy_rect->angle + 2.0f * delta_time, 2 * PI);
80
81     return 0;
82 }
83
84 Rect wavy_rect_hitbox(const Wavy_rect *wavy_rect)
85 {
86     return wavy_rect->rect;
87 }