]> git.lizzy.rs Git - nothing.git/blob - src/wavy_rect.c
c51b6c73a34decec81e668ea7a83317303052d12
[nothing.git] / src / wavy_rect.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <time.h>
5
6 #include <SDL2/SDL.h>
7
8 #include "./wavy_rect.h"
9 #include "./lt.h"
10 #include "./error.h"
11 #include "./pi.h"
12
13 #define WAVE_PILLAR_WIDTH 10.0f
14
15 struct wavy_rect_t
16 {
17     lt_t *lt;
18
19     rect_t rect;
20     color_t color;
21     float angle;
22 };
23
24 wavy_rect_t *create_wavy_rect(rect_t rect, color_t color)
25 {
26     lt_t *lt = create_lt();
27     if (lt == NULL) {
28         return NULL;
29     }
30
31     wavy_rect_t *wavy_rect = PUSH_LT(lt, malloc(sizeof(wavy_rect_t)), free);
32     if (wavy_rect == NULL) {
33         throw_error(ERROR_TYPE_SDL2);
34         RETURN_LT(lt, NULL);
35     }
36
37     wavy_rect->rect = rect;
38     wavy_rect->color = color;
39     wavy_rect->angle = 0.0f;
40     wavy_rect->lt = lt;
41
42     return wavy_rect;
43 }
44
45 wavy_rect_t *create_wavy_rect_from_stream(FILE *stream)
46 {
47     assert(stream);
48     char color_name[7];
49     rect_t rect;
50
51     if (fscanf(stream, "%f%f%f%f%6s\n",
52                &rect.x, &rect.y,
53                &rect.w, &rect.h,
54                color_name) < 0) {
55         throw_error(ERROR_TYPE_LIBC);
56         return NULL;
57     }
58
59     color_t color = color_from_hexstr(color_name);
60
61     return create_wavy_rect(rect, color);
62 }
63
64 void destroy_wavy_rect(wavy_rect_t *wavy_rect)
65 {
66     assert(wavy_rect);
67     RETURN_LT0(wavy_rect->lt);
68 }
69
70 int wavy_rect_render(const wavy_rect_t *wavy_rect,
71                      SDL_Renderer *renderer,
72                      const camera_t *camera)
73 {
74     assert(wavy_rect);
75     assert(renderer);
76     assert(camera);
77
78     srand(42);
79     for (float wave_scanner = 0;
80          wave_scanner < wavy_rect->rect.w;
81          wave_scanner += WAVE_PILLAR_WIDTH) {
82
83         const float s = (float) (rand() % 50) * 0.1f;
84         if (camera_fill_rect(
85                 camera,
86                 renderer,
87                 rect(
88                     wavy_rect->rect.x + wave_scanner,
89                     wavy_rect->rect.y + s * sinf(wavy_rect->angle + wave_scanner / WAVE_PILLAR_WIDTH),
90                     WAVE_PILLAR_WIDTH * 1.20f,
91                     wavy_rect->rect.h),
92                 wavy_rect->color) < 0) {
93             return -1;
94         }
95     }
96     srand((unsigned int) time(NULL));
97
98     return 0;
99 }
100
101 int wavy_rect_update(wavy_rect_t *wavy_rect,
102                      Uint32 delta_time)
103 {
104     assert(wavy_rect);
105
106     float d = (float) delta_time / 1000.0f;
107
108     wavy_rect->angle = fmodf(wavy_rect->angle + 2.0f * d, 2 * PI);
109
110     return 0;
111 }
112
113 int wavy_rect_overlaps(const wavy_rect_t *wavy_rect,
114                        rect_t rect)
115 {
116     assert(wavy_rect);
117     return rects_overlap(wavy_rect->rect, rect);
118 }