8 #include "system/error.h"
10 #include "system/lt/lt_adapters.h"
11 #include "system/line_stream.h"
12 #include "system/nth_alloc.h"
22 Platforms *create_platforms_from_line_stream(LineStream *line_stream)
26 Lt *const lt = create_lt();
31 Platforms *platforms = PUSH_LT(lt, nth_alloc(sizeof(Platforms)), free);
32 if (platforms == NULL) {
33 throw_error(ERROR_TYPE_LIBC);
37 platforms->rects_size = 0;
39 line_stream_next(line_stream),
41 &platforms->rects_size) == EOF) {
42 throw_error(ERROR_TYPE_LIBC);
46 platforms->rects = PUSH_LT(lt, nth_alloc(sizeof(Rect) * platforms->rects_size), free);
47 if (platforms->rects == NULL) {
48 throw_error(ERROR_TYPE_LIBC);
52 platforms->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * platforms->rects_size), free);
53 if (platforms->colors == NULL) {
54 throw_error(ERROR_TYPE_LIBC);
59 for (size_t i = 0; i < platforms->rects_size; ++i) {
60 if (sscanf(line_stream_next(line_stream),
62 &platforms->rects[i].x, &platforms->rects[i].y,
63 &platforms->rects[i].w, &platforms->rects[i].h,
65 throw_error(ERROR_TYPE_LIBC);
68 platforms->colors[i] = color_from_hexstr(color);
76 void destroy_platforms(Platforms *platforms)
79 RETURN_LT0(platforms->lt);
82 int platforms_save_to_file(const Platforms *platforms,
88 Lt *const lt = create_lt();
93 FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
95 if (platforms_file == NULL) {
96 throw_error(ERROR_TYPE_LIBC);
100 for (size_t i = 0; i < platforms->rects_size; ++i) {
101 if (fprintf(platforms_file, "%f %f %f %f\n",
102 platforms->rects[i].x, platforms->rects[i].y,
103 platforms->rects[i].w, platforms->rects[i].h) < 0) {
104 throw_error(ERROR_TYPE_LIBC);
112 Solid_ref platforms_as_solid(Platforms *platforms)
115 .tag = SOLID_PLATFORMS,
116 .ptr = (void*)platforms
122 /* TODO(#450): platforms do not render their ids in debug mode */
123 int platforms_render(const Platforms *platforms,
126 for (size_t i = 0; i < platforms->rects_size; ++i) {
127 if (camera_fill_rect(
130 platforms->colors[i]) < 0) {
131 throw_error(ERROR_TYPE_SDL2);
139 void platforms_touches_rect_sides(const Platforms *platforms,
141 int sides[RECT_SIDE_N])
145 for (size_t i = 0; i < platforms->rects_size; ++i) {
146 rect_object_impact(object, platforms->rects[i], sides);