]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/background.c
(#639) Make rigid bodies interact with platforms
[nothing.git] / src / game / level / background.c
index 2449375a399641f49d35860b7147b05b3380fe35..eaecf20dc00e53be6e1313f910806f99e55a387c 100644 (file)
@@ -1,63 +1,85 @@
-#include <assert.h>
+#include "system/stacktrace.h"
 
 #include "game/level/background.h"
 #include "math/rand.h"
 #include "math/rect.h"
+#include "system/line_stream.h"
 #include "system/lt.h"
+#include "system/nth_alloc.h"
+#include "system/log.h"
 
 #define BACKGROUND_CHUNK_COUNT 5
 #define BACKGROUND_CHUNK_WIDTH 250.0f
 #define BACKGROUND_CHUNK_HEIGHT 250.0f
 
-static void chunk_of_point(point_t p, int *x, int *y);
-int render_chunk(const background_t *background,
-                 const camera_t *camera,
+static void chunk_of_point(Point p, int *x, int *y);
+int render_chunk(const Background *background,
+                 Camera *camera,
                  int x, int y,
-                 color_t color,
-                 vec_t position,
+                 Color color,
+                 Vec position,
                  float parallax);
 
-struct background_t
+struct Background
 {
-    lt_t *lt;
-    color_t base_color;
-    vec_t position;
+    Lt *lt;
+    Color base_color;
+    Vec position;
+    int debug_mode;
 };
 
-background_t *create_background(color_t base_color)
+Background *create_background(Color base_color)
 {
-    lt_t *lt = create_lt();
+    Lt *lt = create_lt();
     if (lt == NULL) {
         return NULL;
     }
 
-    background_t *background = PUSH_LT(lt, malloc(sizeof(background_t)), free);
+    Background *background = PUSH_LT(lt, nth_alloc(sizeof(Background)), free);
     if (background == NULL) {
         RETURN_LT(lt, NULL);
     }
 
     background->base_color = base_color;
     background->position = vec(0.0f, 0.0f);
+    background->debug_mode = 0;
     background->lt = lt;
 
     return background;
 }
 
-void destroy_background(background_t *background)
+Background *create_background_from_line_stream(LineStream *line_stream)
 {
-    assert(background);
+    char color[7];
+    if (sscanf(line_stream_next(line_stream), "%6s", color) == EOF) {
+        log_fail("Could not read background's color\n");
+        return NULL;
+    }
+
+    return create_background(hexstr(color));
+}
+
+void destroy_background(Background *background)
+{
+    trace_assert(background);
     RETURN_LT0(background->lt);
 }
 
 /* TODO(#182): background chunks are randomly disappearing when the size of the window is less than size of the chunk  */
-int background_render(const background_t *background,
-                      const camera_t *camera)
+int background_render(const Background *background,
+                      Camera *camera)
 {
-    assert(background);
-    assert(camera);
+    trace_assert(background);
+    trace_assert(camera);
+
+    if (camera_clear_background(
+            camera,
+            background->base_color) < 0) {
+        return -1;
+    }
 
-    const rect_t view_port = camera_view_port(camera);
-    const vec_t position = vec(view_port.x, view_port.y);
+    const Rect view_port = camera_view_port(camera);
+    const Vec position = vec(view_port.x, view_port.y);
 
     for (int l = 0; l < 3; ++l) {
         const float parallax = 1.0f - 0.2f * (float)l;
@@ -92,30 +114,34 @@ int background_render(const background_t *background,
 
 /* Private Function */
 
-static void chunk_of_point(point_t p, int *x, int *y)
+static void chunk_of_point(Point p, int *x, int *y)
 {
-    assert(x);
-    assert(y);
+    trace_assert(x);
+    trace_assert(y);
     *x = (int) (p.x / BACKGROUND_CHUNK_WIDTH);
     *y = (int) (p.y / BACKGROUND_CHUNK_HEIGHT);
 }
 
-int render_chunk(const background_t *background,
-                 const camera_t *camera,
-                 int x, int y,
-                 color_t color,
-                 vec_t position,
+int render_chunk(const Background *background,
+                 Camera *camera,
+                 int chunk_x, int chunk_y,
+                 Color color,
+                 Vec position,
                  float parallax)
 {
     (void) background;
 
-    srand((unsigned int)(roundf((float)x + (float)y + parallax)));
+    if (background->debug_mode) {
+        return 0;
+    }
+
+    srand((unsigned int)(roundf((float)chunk_x + (float)chunk_y + parallax)));
 
     for (size_t i = 0; i < BACKGROUND_CHUNK_COUNT; ++i) {
-        const float rect_x = rand_float_range((float) x * BACKGROUND_CHUNK_WIDTH,
-                                              (float) (x + 1) * BACKGROUND_CHUNK_WIDTH);
-        const float rect_y = rand_float_range((float) y * BACKGROUND_CHUNK_HEIGHT,
-                                              (float) (y + 1) * BACKGROUND_CHUNK_HEIGHT);
+        const float rect_x = rand_float_range((float) chunk_x * BACKGROUND_CHUNK_WIDTH,
+                                              (float) (chunk_x + 1) * BACKGROUND_CHUNK_WIDTH);
+        const float rect_y = rand_float_range((float) chunk_y * BACKGROUND_CHUNK_HEIGHT,
+                                              (float) (chunk_y + 1) * BACKGROUND_CHUNK_HEIGHT);
         const float rect_w = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH * 0.5f);
         const float rect_h = rand_float_range(rect_w * 0.5f, rect_w * 1.5f);
 
@@ -132,3 +158,8 @@ int render_chunk(const background_t *background,
 
     return 0;
 }
+
+void background_toggle_debug_mode(Background *background)
+{
+    background->debug_mode = !background->debug_mode;
+}