3 #include "game/level/background.h"
7 #include "system/nth_alloc.h"
8 #include "system/log.h"
9 #include "system/stacktrace.h"
13 Vec2i chunk_of_point(Vec2f p)
16 (int) floorf(p.x / BACKGROUND_CHUNK_WIDTH),
17 (int) floorf(p.y / BACKGROUND_CHUNK_HEIGHT));
20 int render_chunk(const Camera *camera,
24 int background_render(const Background *background,
25 const Camera *camera0)
27 trace_assert(background);
28 trace_assert(camera0);
30 Camera camera = *camera0;
32 if (camera_clear_background(
34 background->base_color) < 0) {
38 camera.scale = 1.0f - BACKGROUND_LAYERS_STEP * BACKGROUND_LAYERS_COUNT;
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);
44 Vec2i min = chunk_of_point(position);
45 Vec2i max = chunk_of_point(vec_sum(position, vec(view_port.w, view_port.h)));
47 for (int x = min.x - 1; x <= max.x; ++x) {
48 for (int y = min.y - 1; y <= max.y; ++y) {
52 color_darker(background->base_color, 0.05f * (float)(l + 1))) < 0) {
58 camera.scale += BACKGROUND_LAYERS_STEP;
64 /* Private Function */
66 int render_chunk(const Camera *camera,
72 if (camera->debug_mode) {
76 srand((unsigned int)(roundf((float)chunk.x + (float)chunk.y + camera->scale * 10.0f)));
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);
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);
87 rect((float) chunk.x * BACKGROUND_CHUNK_WIDTH + rect_x,
88 (float) chunk.y * BACKGROUND_CHUNK_HEIGHT + rect_y,
99 Color background_base_color(const Background *background)
101 return background->base_color;