]> git.lizzy.rs Git - nothing.git/blob - src/game/level/platforms.c
6a670db9d60138254806bbd3baebf6b84bf30b4f
[nothing.git] / src / game / level / platforms.c
1 #include <SDL.h>
2 #include "system/stacktrace.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7
8 #include "platforms.h"
9 #include "system/lt.h"
10 #include "system/line_stream.h"
11 #include "system/nth_alloc.h"
12 #include "system/log.h"
13 #include "game/level/level_editor/rect_layer.h"
14 #include "math/extrema.h"
15
16 struct Platforms {
17     Lt *lt;
18
19     Rect *rects;
20     Color *colors;
21     size_t rects_size;
22 };
23
24 Platforms *create_platforms_from_rect_layer(const RectLayer *layer)
25 {
26     trace_assert(layer);
27
28     Lt *lt = create_lt();
29
30     Platforms *platforms = PUSH_LT(
31         lt,
32         nth_calloc(1, sizeof(Platforms)),
33         free);
34     if (platforms == NULL) {
35         RETURN_LT(lt, NULL);
36     }
37     platforms->lt = lt;
38
39     platforms->rects_size = rect_layer_count(layer);
40
41     platforms->rects = PUSH_LT(lt, nth_calloc(1, sizeof(Rect) * platforms->rects_size), free);
42     if (platforms->rects == NULL) {
43         RETURN_LT(lt, NULL);
44     }
45     memcpy(platforms->rects, rect_layer_rects(layer), sizeof(Rect) * platforms->rects_size);
46
47
48     platforms->colors = PUSH_LT(lt, nth_calloc(1, sizeof(Color) * platforms->rects_size), free);
49     if (platforms->colors == NULL) {
50         RETURN_LT(lt, NULL);
51     }
52     memcpy(platforms->colors, rect_layer_colors(layer), sizeof(Color) * platforms->rects_size);
53
54     return platforms;
55 }
56
57 void destroy_platforms(Platforms *platforms)
58 {
59     trace_assert(platforms);
60     RETURN_LT0(platforms->lt);
61 }
62
63 int platforms_render(const Platforms *platforms,
64                      const Camera *camera)
65 {
66     for (size_t i = 0; i < platforms->rects_size; ++i) {
67         Rect platform_rect = platforms->rects[i];
68         if (camera_fill_rect(
69                 camera,
70                 platform_rect,
71                 platforms->colors[i]) < 0) {
72             return -1;
73         }
74
75         char debug_text[256];
76         snprintf(debug_text, 256,
77             "id:%zd\n"
78             "x:%.2f\n"
79             "y:%.2f\n"
80             "w:%.2f\n"
81             "h:%.2f\n",
82             i, platform_rect.x, platform_rect.y, platform_rect.w, platform_rect.h);
83
84         Vec2f text_pos = (Vec2f){.x = platform_rect.x, .y = platform_rect.y};
85         Rect text_rect = sprite_font_boundary_box(text_pos, vec(2.0f, 2.0f), debug_text);
86
87         Rect world_viewport = camera_view_port(camera);
88         Rect viewport = camera_view_port_screen(camera);
89
90         if (rects_overlap(
91                 camera_rect(
92                     camera,
93                     platform_rect),
94                 viewport) &&
95             camera_is_point_visible(
96                 camera,
97                 text_pos) == false) {
98             if (platform_rect.w > text_rect.w){
99                 text_pos.x = fmaxf(fminf(world_viewport.x, platform_rect.x + platform_rect.w - text_rect.w),
100                                    platform_rect.x);
101             }
102             if (platform_rect.h > text_rect.h){
103                 text_pos.y = fmaxf(fminf(world_viewport.y, platform_rect.y + platform_rect.h - text_rect.h),
104                                    platform_rect.y);
105             }
106         }
107
108         if (camera_render_debug_text(
109                 camera,
110                 debug_text,
111                 text_pos) < 0) {
112             return -1;
113         }
114     }
115
116     return 0;
117 }
118
119 void platforms_touches_rect_sides(const Platforms *platforms,
120                                   Rect object,
121                                   int sides[RECT_SIDE_N])
122 {
123     trace_assert(platforms);
124
125     for (size_t i = 0; i < platforms->rects_size; ++i) {
126         rect_object_impact(object, platforms->rects[i], sides);
127     }
128 }
129
130 Vec2f platforms_snap_rect(const Platforms *platforms,
131                          Rect *object)
132 {
133     trace_assert(platforms);
134
135     Vec2f result = vec(1.0f, 1.0f);
136     for (size_t i = 0; i < platforms->rects_size; ++i) {
137         if (rects_overlap(platforms->rects[i], *object)) {
138             // TODO(#1161): can we reuse the Level Editor snapping mechanism in physics snapping
139             result = vec_entry_mult(result, rect_snap(platforms->rects[i], object));
140         }
141     }
142
143     return result;
144 }