]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/platforms.c
(#639) Introduce rigid_bodies_collide_with_itself
[nothing.git] / src / game / level / platforms.c
index e0ae6495615cf884fe42e31aa3fcbcdd6d924f72..3fd99661889a5cede28ef3b373f5112c4c05e977 100644 (file)
@@ -1,13 +1,16 @@
 #include <SDL2/SDL.h>
-#include <assert.h>
+#include "system/stacktrace.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include "platforms.h"
-#include "system/error.h"
 #include "system/lt.h"
 #include "system/lt/lt_adapters.h"
+#include "system/line_stream.h"
+#include "system/nth_alloc.h"
+#include "system/log.h"
 
 struct Platforms {
     Lt *lt;
@@ -17,49 +20,50 @@ struct Platforms {
     size_t rects_size;
 };
 
-Platforms *create_platforms_from_stream(FILE *stream)
+Platforms *create_platforms_from_line_stream(LineStream *line_stream)
 {
-    assert(stream);
+    trace_assert(line_stream);
 
     Lt *const lt = create_lt();
     if (lt == NULL) {
         return NULL;
     }
 
-    Platforms *platforms = PUSH_LT(lt, malloc(sizeof(Platforms)), free);
+    Platforms *platforms = PUSH_LT(lt, nth_alloc(sizeof(Platforms)), free);
     if (platforms == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     platforms->rects_size = 0;
-    if (fscanf(stream, "%lu", &platforms->rects_size) == EOF) {
-        throw_error(ERROR_TYPE_LIBC);
+    if (sscanf(
+            line_stream_next(line_stream),
+            "%lu",
+            &platforms->rects_size) == EOF) {
+        log_fail("Could not read amount of platforms\n");
         RETURN_LT(lt, NULL);
     }
 
-    platforms->rects = PUSH_LT(lt, malloc(sizeof(Rect) * platforms->rects_size), free);
+    platforms->rects = PUSH_LT(lt, nth_alloc(sizeof(Rect) * platforms->rects_size), free);
     if (platforms->rects == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
-    platforms->colors = PUSH_LT(lt, malloc(sizeof(Color) * platforms->rects_size), free);
+    platforms->colors = PUSH_LT(lt, nth_alloc(sizeof(Color) * platforms->rects_size), free);
     if (platforms->colors == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
 
     char color[7];
     for (size_t i = 0; i < platforms->rects_size; ++i) {
-        if (fscanf(stream, "%f%f%f%f%6s\n",
+        if (sscanf(line_stream_next(line_stream),
+                   "%f%f%f%f%6s\n",
                    &platforms->rects[i].x, &platforms->rects[i].y,
                    &platforms->rects[i].w, &platforms->rects[i].h,
                    color) < 0) {
-            throw_error(ERROR_TYPE_LIBC);
+            log_fail("Could not read %dth platform\n", i);
             RETURN_LT(lt, NULL);
         }
-        platforms->colors[i] = color_from_hexstr(color);
+        platforms->colors[i] = hexstr(color);
     }
 
     platforms->lt = lt;
@@ -67,37 +71,17 @@ Platforms *create_platforms_from_stream(FILE *stream)
     return platforms;
 }
 
-Platforms *create_platforms_from_file(const char *filename)
-{
-    assert(filename);
-
-    FILE *platforms_file = fopen(filename, "r");
-    if (platforms_file == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
-        return NULL;
-    }
-
-    Platforms *platforms = create_platforms_from_stream(platforms_file);
-    if (platforms != NULL) {
-        fclose(platforms_file);
-        return NULL;
-    }
-
-    fclose(platforms_file);
-    return platforms;
-}
-
 void destroy_platforms(Platforms *platforms)
 {
-    assert(platforms);
+    trace_assert(platforms);
     RETURN_LT0(platforms->lt);
 }
 
 int platforms_save_to_file(const Platforms *platforms,
                            const char *filename)
 {
-    assert(platforms);
-    assert(filename);
+    trace_assert(platforms);
+    trace_assert(filename);
 
     Lt *const lt = create_lt();
     if (lt == NULL) {
@@ -107,7 +91,7 @@ int platforms_save_to_file(const Platforms *platforms,
     FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
 
     if (platforms_file == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
+        log_fail("Could not open file '%s': %s\n", filename, strerror(errno));
         RETURN_LT(lt, -1);
     }
 
@@ -115,7 +99,7 @@ int platforms_save_to_file(const Platforms *platforms,
         if (fprintf(platforms_file, "%f %f %f %f\n",
                     platforms->rects[i].x, platforms->rects[i].y,
                     platforms->rects[i].w, platforms->rects[i].h) < 0) {
-            throw_error(ERROR_TYPE_LIBC);
+            log_fail("Could not read %dth platform\n", i);
             RETURN_LT(lt, -1);
         }
     }
@@ -142,7 +126,6 @@ int platforms_render(const Platforms *platforms,
                 camera,
                 platforms->rects[i],
                 platforms->colors[i]) < 0) {
-            throw_error(ERROR_TYPE_SDL2);
             return -1;
         }
     }
@@ -154,7 +137,7 @@ void platforms_touches_rect_sides(const Platforms *platforms,
                                   Rect object,
                                   int sides[RECT_SIDE_N])
 {
-    assert(platforms);
+    trace_assert(platforms);
 
     for (size_t i = 0; i < platforms->rects_size; ++i) {
         rect_object_impact(object, platforms->rects[i], sides);