8 #include "system/error.h"
19 platforms_t *create_platforms_from_stream(FILE *stream)
23 lt_t *const lt = create_lt();
28 platforms_t *platforms = PUSH_LT(lt, malloc(sizeof(platforms_t)), free);
29 if (platforms == NULL) {
30 throw_error(ERROR_TYPE_LIBC);
34 platforms->rects_size = 0;
35 if (fscanf(stream, "%lu", &platforms->rects_size) == EOF) {
36 throw_error(ERROR_TYPE_LIBC);
40 platforms->rects = PUSH_LT(lt, malloc(sizeof(rect_t) * platforms->rects_size), free);
41 if (platforms->rects == NULL) {
42 throw_error(ERROR_TYPE_LIBC);
46 platforms->colors = PUSH_LT(lt, malloc(sizeof(color_t) * platforms->rects_size), free);
47 if (platforms->colors == NULL) {
48 throw_error(ERROR_TYPE_LIBC);
53 for (size_t i = 0; i < platforms->rects_size; ++i) {
54 if (fscanf(stream, "%f%f%f%f%6s\n",
55 &platforms->rects[i].x, &platforms->rects[i].y,
56 &platforms->rects[i].w, &platforms->rects[i].h,
58 throw_error(ERROR_TYPE_LIBC);
61 platforms->colors[i] = color_from_hexstr(color);
69 platforms_t *create_platforms_from_file(const char *filename)
73 FILE *platforms_file = fopen(filename, "r");
74 if (platforms_file == NULL) {
75 throw_error(ERROR_TYPE_LIBC);
79 platforms_t *platforms = create_platforms_from_stream(platforms_file);
80 if (platforms != NULL) {
81 fclose(platforms_file);
85 fclose(platforms_file);
89 void destroy_platforms(platforms_t *platforms)
92 RETURN_LT0(platforms->lt);
95 int platforms_save_to_file(const platforms_t *platforms,
101 lt_t *const lt = create_lt();
106 FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose);
108 if (platforms_file == NULL) {
109 throw_error(ERROR_TYPE_LIBC);
113 for (size_t i = 0; i < platforms->rects_size; ++i) {
114 if (fprintf(platforms_file, "%f %f %f %f\n",
115 platforms->rects[i].x, platforms->rects[i].y,
116 platforms->rects[i].w, platforms->rects[i].h) < 0) {
117 throw_error(ERROR_TYPE_LIBC);
125 solid_ref_t platforms_as_solid(platforms_t *platforms)
128 .tag = SOLID_PLATFORMS,
129 .ptr = (void*)platforms
135 int platforms_render(const platforms_t *platforms,
136 const camera_t *camera)
138 for (size_t i = 0; i < platforms->rects_size; ++i) {
139 if (camera_fill_rect(
142 platforms->colors[i]) < 0) {
143 throw_error(ERROR_TYPE_SDL2);
151 void platforms_rect_object_collide(const platforms_t *platforms,
153 int sides[RECT_SIDE_N])
157 memset(sides, 0, sizeof(int) * RECT_SIDE_N);
159 for (size_t i = 0; i < platforms->rects_size; ++i) {
160 rect_object_impact(&object, &platforms->rects[i], sides);