3 #include "game/level/background.h"
6 #include "system/line_stream.h"
8 #include "system/nth_alloc.h"
9 #include "system/log.h"
10 #include "system/stacktrace.h"
14 Vec2i chunk_of_point(Vec2f p)
17 (int) floorf(p.x / BACKGROUND_CHUNK_WIDTH),
18 (int) floorf(p.y / BACKGROUND_CHUNK_HEIGHT));
21 int render_chunk(const Camera *camera,
25 int background_render(const Background *background,
26 const Camera *camera0)
28 trace_assert(background);
29 trace_assert(camera0);
31 Camera camera = *camera0;
33 if (camera_clear_background(
35 background->base_color) < 0) {
39 camera.scale = 1.0f - BACKGROUND_LAYERS_STEP * BACKGROUND_LAYERS_COUNT;
41 for (int l = 0; l < BACKGROUND_LAYERS_COUNT; ++l) {
42 const Rect view_port = camera_view_port(&camera);
43 const Vec2f position = vec(view_port.x, view_port.y);
45 Vec2i min = chunk_of_point(position);
46 Vec2i max = chunk_of_point(vec_sum(position, vec(view_port.w, view_port.h)));
48 for (int x = min.x - 1; x <= max.x; ++x) {
49 for (int y = min.y - 1; y <= max.y; ++y) {
53 color_darker(background->base_color, 0.05f * (float)(l + 1))) < 0) {
59 camera.scale += BACKGROUND_LAYERS_STEP;
65 /* Private Function */
67 int render_chunk(const Camera *camera,
73 if (camera->debug_mode) {
77 srand((unsigned int)(roundf((float)chunk.x + (float)chunk.y + camera->scale * 10.0f)));
79 for (size_t i = 0; i < BACKGROUND_TURDS_PER_CHUNK; ++i) {
80 const float rect_x = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH);
81 const float rect_y = rand_float_range(0.0f, BACKGROUND_CHUNK_HEIGHT);
83 const float rect_w = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH * 0.5f);
84 const float rect_h = rand_float_range(rect_w * 0.5f, rect_w * 1.5f);
88 rect((float) chunk.x * BACKGROUND_CHUNK_WIDTH + rect_x,
89 (float) chunk.y * BACKGROUND_CHUNK_HEIGHT + rect_y,
100 Color background_base_color(const Background *background)
102 return background->base_color;