2 #include "system/stacktrace.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"
23 Platforms *create_platforms_from_rect_layer(const RectLayer *layer)
29 Platforms *platforms = PUSH_LT(
31 nth_calloc(1, sizeof(Platforms)),
33 if (platforms == NULL) {
38 platforms->rects_size = rect_layer_count(layer);
40 platforms->rects = PUSH_LT(lt, nth_calloc(1, sizeof(Rect) * platforms->rects_size), free);
41 if (platforms->rects == NULL) {
44 memcpy(platforms->rects, rect_layer_rects(layer), sizeof(Rect) * platforms->rects_size);
47 platforms->colors = PUSH_LT(lt, nth_calloc(1, sizeof(Color) * platforms->rects_size), free);
48 if (platforms->colors == NULL) {
51 memcpy(platforms->colors, rect_layer_colors(layer), sizeof(Color) * platforms->rects_size);
56 void destroy_platforms(Platforms *platforms)
58 trace_assert(platforms);
59 RETURN_LT0(platforms->lt);
62 /* TODO(#450): platforms do not render their ids in debug mode */
63 int platforms_render(const Platforms *platforms,
66 for (size_t i = 0; i < platforms->rects_size; ++i) {
70 platforms->colors[i]) < 0) {
78 void platforms_touches_rect_sides(const Platforms *platforms,
80 int sides[RECT_SIDE_N])
82 trace_assert(platforms);
84 for (size_t i = 0; i < platforms->rects_size; ++i) {
85 rect_object_impact(object, platforms->rects[i], sides);
89 Vec2f platforms_snap_rect(const Platforms *platforms,
92 trace_assert(platforms);
94 Vec2f result = vec(1.0f, 1.0f);
95 for (size_t i = 0; i < platforms->rects_size; ++i) {
96 if (rects_overlap(platforms->rects[i], *object)) {
97 // TODO(#1161): can we reuse the Level Editor snapping mechanism in physics snapping
98 result = vec_entry_mult(result, rect_snap(platforms->rects[i], object));