8 #include "system/error.h"
10 #include "system/lt/lt_adapters.h"
11 #include "system/line_stream.h"
21 Platforms *create_platforms_from_line_stream(LineStream *line_stream)
25 Lt *const lt = create_lt();
30 Platforms *platforms = PUSH_LT(lt, malloc(sizeof(Platforms)), free);
31 if (platforms == NULL) {
32 throw_error(ERROR_TYPE_LIBC);
36 platforms->rects_size = 0;
38 line_stream_next(line_stream),
40 &platforms->rects_size) == EOF) {
41 throw_error(ERROR_TYPE_LIBC);
45 platforms->rects = PUSH_LT(lt, malloc(sizeof(Rect) * platforms->rects_size), free);
46 if (platforms->rects == NULL) {
47 throw_error(ERROR_TYPE_LIBC);
51 platforms->colors = PUSH_LT(lt, malloc(sizeof(Color) * platforms->rects_size), free);
52 if (platforms->colors == NULL) {
53 throw_error(ERROR_TYPE_LIBC);
58 for (size_t i = 0; i < platforms->rects_size; ++i) {
59 if (sscanf(line_stream_next(line_stream),
61 &platforms->rects[i].x, &platforms->rects[i].y,
62 &platforms->rects[i].w, &platforms->rects[i].h,
64 throw_error(ERROR_TYPE_LIBC);
67 platforms->colors[i] = color_from_hexstr(color);
75 void destroy_platforms(Platforms *platforms)
78 RETURN_LT0(platforms->lt);
81 int platforms_save_to_file(const Platforms *platforms,
87 Lt *const lt = create_lt();
92 FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
94 if (platforms_file == NULL) {
95 throw_error(ERROR_TYPE_LIBC);
99 for (size_t i = 0; i < platforms->rects_size; ++i) {
100 if (fprintf(platforms_file, "%f %f %f %f\n",
101 platforms->rects[i].x, platforms->rects[i].y,
102 platforms->rects[i].w, platforms->rects[i].h) < 0) {
103 throw_error(ERROR_TYPE_LIBC);
111 Solid_ref platforms_as_solid(Platforms *platforms)
114 .tag = SOLID_PLATFORMS,
115 .ptr = (void*)platforms
121 /* TODO(#450): platforms do not render their ids in debug mode */
122 int platforms_render(const Platforms *platforms,
125 for (size_t i = 0; i < platforms->rects_size; ++i) {
126 if (camera_fill_rect(
129 platforms->colors[i]) < 0) {
130 throw_error(ERROR_TYPE_SDL2);
138 void platforms_touches_rect_sides(const Platforms *platforms,
140 int sides[RECT_SIDE_N])
144 for (size_t i = 0; i < platforms->rects_size; ++i) {
145 rect_object_impact(object, platforms->rects[i], sides);