]> git.lizzy.rs Git - nothing.git/blob - src/game/level/background.c
Delete LineStream from the existance
[nothing.git] / src / game / level / background.c
1 #include <stdio.h>
2
3 #include "game/level/background.h"
4 #include "math/rand.h"
5 #include "math/rect.h"
6 #include "system/lt.h"
7 #include "system/nth_alloc.h"
8 #include "system/log.h"
9 #include "system/stacktrace.h"
10 #include "config.h"
11
12 static inline
13 Vec2i chunk_of_point(Vec2f p)
14 {
15     return vec2i(
16         (int) floorf(p.x / BACKGROUND_CHUNK_WIDTH),
17         (int) floorf(p.y / BACKGROUND_CHUNK_HEIGHT));
18 }
19
20 int render_chunk(const Camera *camera,
21                  Vec2i chunk,
22                  Color color);
23
24 int background_render(const Background *background,
25                       const Camera *camera0)
26 {
27     trace_assert(background);
28     trace_assert(camera0);
29
30     Camera camera = *camera0;
31
32     if (camera_clear_background(
33             &camera,
34             background->base_color) < 0) {
35         return -1;
36     }
37
38     camera.scale = 1.0f - BACKGROUND_LAYERS_STEP * BACKGROUND_LAYERS_COUNT;
39
40     for (int l = 0; l < BACKGROUND_LAYERS_COUNT; ++l) {
41         const Rect view_port = camera_view_port(&camera);
42         const Vec2f position = vec(view_port.x, view_port.y);
43
44         Vec2i min = chunk_of_point(position);
45         Vec2i max = chunk_of_point(vec_sum(position, vec(view_port.w, view_port.h)));
46
47         for (int x = min.x - 1; x <= max.x; ++x) {
48             for (int y = min.y - 1; y <= max.y; ++y) {
49                 if (render_chunk(
50                         &camera,
51                         vec2i(x, y),
52                         color_darker(background->base_color, 0.05f * (float)(l + 1))) < 0) {
53                     return -1;
54                 }
55             }
56         }
57
58         camera.scale += BACKGROUND_LAYERS_STEP;
59     }
60
61     return 0;
62 }
63
64 /* Private Function */
65
66 int render_chunk(const Camera *camera,
67                  Vec2i chunk,
68                  Color color)
69 {
70     trace_assert(camera);
71
72     if (camera->debug_mode) {
73         return 0;
74     }
75
76     srand((unsigned int)(roundf((float)chunk.x + (float)chunk.y + camera->scale * 10.0f)));
77
78     for (size_t i = 0; i < BACKGROUND_TURDS_PER_CHUNK; ++i) {
79         const float rect_x = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH);
80         const float rect_y = rand_float_range(0.0f, BACKGROUND_CHUNK_HEIGHT);
81
82         const float rect_w = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH * 0.5f);
83         const float rect_h = rand_float_range(rect_w * 0.5f, rect_w * 1.5f);
84
85         if (camera_fill_rect(
86                 camera,
87                 rect((float) chunk.x * BACKGROUND_CHUNK_WIDTH + rect_x,
88                      (float) chunk.y * BACKGROUND_CHUNK_HEIGHT + rect_y,
89                      rect_w,
90                      rect_h),
91                 color) < 0) {
92             return -1;
93         }
94     }
95
96     return 0;
97 }
98
99 Color background_base_color(const Background *background)
100 {
101     return background->base_color;
102 }