]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/platforms.c
Merge pull request #1209 from isidentical/console-shortcuts
[nothing.git] / src / game / level / platforms.c
index f45efcd084ca559ff53dc9f328b69596f4867b8b..55b98682b6f4b191e78a448f9a1f5257adadeb11 100644 (file)
@@ -1,13 +1,16 @@
-#include <SDL2/SDL.h>
-#include <assert.h>
+#include <SDL.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"
+#include "game/level/level_editor/rect_layer.h"
 
 struct Platforms {
     Lt *lt;
@@ -17,132 +20,54 @@ struct Platforms {
     size_t rects_size;
 };
 
-Platforms *create_platforms_from_stream(FILE *stream)
+Platforms *create_platforms_from_rect_layer(const RectLayer *layer)
 {
-    assert(stream);
+    trace_assert(layer);
 
-    Lt *const lt = create_lt();
-    if (lt == NULL) {
-        return NULL;
-    }
+    Lt *lt = create_lt();
 
-    Platforms *platforms = PUSH_LT(lt, malloc(sizeof(Platforms)), free);
+    Platforms *platforms = PUSH_LT(
+        lt,
+        nth_calloc(1, sizeof(Platforms)),
+        free);
     if (platforms == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
+    platforms->lt = lt;
 
-    platforms->rects_size = 0;
-    if (fscanf(stream, "%lu", &platforms->rects_size) == EOF) {
-        throw_error(ERROR_TYPE_LIBC);
-        RETURN_LT(lt, NULL);
-    }
+    platforms->rects_size = rect_layer_count(layer);
 
-    platforms->rects = PUSH_LT(lt, malloc(sizeof(Rect) * platforms->rects_size), free);
+    platforms->rects = PUSH_LT(lt, nth_calloc(1, sizeof(Rect) * platforms->rects_size), free);
     if (platforms->rects == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
+    memcpy(platforms->rects, rect_layer_rects(layer), sizeof(Rect) * platforms->rects_size);
+
 
-    platforms->colors = PUSH_LT(lt, malloc(sizeof(Color) * platforms->rects_size), free);
+    platforms->colors = PUSH_LT(lt, nth_calloc(1, 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",
-                   &platforms->rects[i].x, &platforms->rects[i].y,
-                   &platforms->rects[i].w, &platforms->rects[i].h,
-                   color) < 0) {
-            throw_error(ERROR_TYPE_LIBC);
-            RETURN_LT(lt, NULL);
-        }
-        platforms->colors[i] = color_from_hexstr(color);
-    }
-
-    platforms->lt = lt;
+    memcpy(platforms->colors, rect_layer_colors(layer), sizeof(Color) * platforms->rects_size);
 
     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);
-
-    Lt *const lt = create_lt();
-    if (lt == NULL) {
-        return -1;
-    }
-
-    FILE *platforms_file = PUSH_LT(lt, fopen(filename, "w"), fclose_lt);
-
-    if (platforms_file == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
-        RETURN_LT(lt, -1);
-    }
-
-    for (size_t i = 0; i < platforms->rects_size; ++i) {
-        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);
-            RETURN_LT(lt, -1);
-        }
-    }
-
-    RETURN_LT(lt, 0);
-}
-
-Solid_ref platforms_as_solid(Platforms *platforms)
-{
-    Solid_ref ref = {
-        .tag = SOLID_PLATFORMS,
-        .ptr = (void*)platforms
-    };
-
-    return ref;
-}
-
-/* TODO: platforms do not render their ids in debug mode */
+/* TODO(#450): platforms do not render their ids in debug mode */
 int platforms_render(const Platforms *platforms,
-                     Camera *camera)
+                     const Camera *camera)
 {
     for (size_t i = 0; i < platforms->rects_size; ++i) {
         if (camera_fill_rect(
                 camera,
                 platforms->rects[i],
                 platforms->colors[i]) < 0) {
-            throw_error(ERROR_TYPE_SDL2);
             return -1;
         }
     }
@@ -154,9 +79,25 @@ 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);
     }
 }
+
+Vec2f platforms_snap_rect(const Platforms *platforms,
+                         Rect *object)
+{
+    trace_assert(platforms);
+
+    Vec2f result = vec(1.0f, 1.0f);
+    for (size_t i = 0; i < platforms->rects_size; ++i) {
+        if (rects_overlap(platforms->rects[i], *object)) {
+            // TODO(#1161): can we reuse the Level Editor snapping mechanism in physics snapping
+            result = vec_entry_mult(result, rect_snap(platforms->rects[i], object));
+        }
+    }
+
+    return result;
+}