2 #include "system/stacktrace.h"
10 #include "system/lt/lt_adapters.h"
11 #include "system/line_stream.h"
12 #include "system/nth_alloc.h"
13 #include "system/log.h"
23 Platforms *create_platforms_from_line_stream(LineStream *line_stream)
25 trace_assert(line_stream);
27 Lt *const lt = create_lt();
32 Platforms *platforms = PUSH_LT(lt, nth_alloc(sizeof(Platforms)), free);
33 if (platforms == NULL) {
37 platforms->rects_size = 0;
39 line_stream_next(line_stream),
41 &platforms->rects_size) == EOF) {
42 log_fail("Could not read amount of platforms\n");
46 platforms->rects = PUSH_LT(lt, nth_alloc(sizeof(Rect) * platforms->rects_size), free);
47 if (platforms->rects == NULL) {
51 platforms->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * platforms->rects_size), free);
52 if (platforms->colors == NULL) {
57 for (size_t i = 0; i < platforms->rects_size; ++i) {
58 if (sscanf(line_stream_next(line_stream),
60 &platforms->rects[i].x, &platforms->rects[i].y,
61 &platforms->rects[i].w, &platforms->rects[i].h,
63 log_fail("Could not read %dth platform\n", i);
66 platforms->colors[i] = hexstr(color);
74 void destroy_platforms(Platforms *platforms)
76 trace_assert(platforms);
77 RETURN_LT0(platforms->lt);
80 int platforms_save_to_file(const Platforms *platforms,
83 trace_assert(platforms);
84 trace_assert(filename);
86 Lt *const lt = create_lt();
91 FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
93 if (platforms_file == NULL) {
94 log_fail("Could not open file '%s': %s\n", filename, strerror(errno));
98 for (size_t i = 0; i < platforms->rects_size; ++i) {
99 if (fprintf(platforms_file, "%f %f %f %f\n",
100 platforms->rects[i].x, platforms->rects[i].y,
101 platforms->rects[i].w, platforms->rects[i].h) < 0) {
102 log_fail("Could not read %dth platform\n", i);
110 Solid_ref platforms_as_solid(Platforms *platforms)
113 .tag = SOLID_PLATFORMS,
114 .ptr = (void*)platforms
120 /* TODO(#450): platforms do not render their ids in debug mode */
121 int platforms_render(const Platforms *platforms,
124 for (size_t i = 0; i < platforms->rects_size; ++i) {
125 if (camera_fill_rect(
128 platforms->colors[i]) < 0) {
136 void platforms_touches_rect_sides(const Platforms *platforms,
138 int sides[RECT_SIDE_N])
140 trace_assert(platforms);
142 for (size_t i = 0; i < platforms->rects_size; ++i) {
143 rect_object_impact(object, platforms->rects[i], sides);