8 #include "system/error.h"
10 #include "system/lt/lt_adapters.h"
20 Platforms *create_platforms_from_stream(FILE *stream)
24 Lt *const lt = create_lt();
29 Platforms *platforms = PUSH_LT(lt, malloc(sizeof(Platforms)), free);
30 if (platforms == NULL) {
31 throw_error(ERROR_TYPE_LIBC);
35 platforms->rects_size = 0;
36 if (fscanf(stream, "%lu", &platforms->rects_size) == EOF) {
37 throw_error(ERROR_TYPE_LIBC);
41 platforms->rects = PUSH_LT(lt, malloc(sizeof(Rect) * platforms->rects_size), free);
42 if (platforms->rects == NULL) {
43 throw_error(ERROR_TYPE_LIBC);
47 platforms->colors = PUSH_LT(lt, malloc(sizeof(Color) * platforms->rects_size), free);
48 if (platforms->colors == NULL) {
49 throw_error(ERROR_TYPE_LIBC);
54 for (size_t i = 0; i < platforms->rects_size; ++i) {
55 if (fscanf(stream, "%f%f%f%f%6s\n",
56 &platforms->rects[i].x, &platforms->rects[i].y,
57 &platforms->rects[i].w, &platforms->rects[i].h,
59 throw_error(ERROR_TYPE_LIBC);
62 platforms->colors[i] = color_from_hexstr(color);
70 Platforms *create_platforms_from_file(const char *filename)
74 FILE *platforms_file = fopen(filename, "r");
75 if (platforms_file == NULL) {
76 throw_error(ERROR_TYPE_LIBC);
80 Platforms *platforms = create_platforms_from_stream(platforms_file);
81 if (platforms != NULL) {
82 fclose(platforms_file);
86 fclose(platforms_file);
90 void destroy_platforms(Platforms *platforms)
93 RETURN_LT0(platforms->lt);
96 int platforms_save_to_file(const Platforms *platforms,
102 Lt *const lt = create_lt();
107 FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
109 if (platforms_file == NULL) {
110 throw_error(ERROR_TYPE_LIBC);
114 for (size_t i = 0; i < platforms->rects_size; ++i) {
115 if (fprintf(platforms_file, "%f %f %f %f\n",
116 platforms->rects[i].x, platforms->rects[i].y,
117 platforms->rects[i].w, platforms->rects[i].h) < 0) {
118 throw_error(ERROR_TYPE_LIBC);
126 Solid_ref platforms_as_solid(Platforms *platforms)
129 .tag = SOLID_PLATFORMS,
130 .ptr = (void*)platforms
136 /* TODO: platforms do not render their ids in debug mode */
137 int platforms_render(const Platforms *platforms,
140 for (size_t i = 0; i < platforms->rects_size; ++i) {
141 if (camera_fill_rect(
144 platforms->colors[i]) < 0) {
145 throw_error(ERROR_TYPE_SDL2);
153 void platforms_touches_rect_sides(const Platforms *platforms,
155 int sides[RECT_SIDE_N])
159 for (size_t i = 0; i < platforms->rects_size; ++i) {
160 rect_object_impact(object, platforms->rects[i], sides);