3 #include "game/level/background.h"
6 #include "system/error.h"
8 #include "system/line_stream.h"
10 #define BACKGROUND_CHUNK_COUNT 5
11 #define BACKGROUND_CHUNK_WIDTH 250.0f
12 #define BACKGROUND_CHUNK_HEIGHT 250.0f
14 static void chunk_of_point(Point p, int *x, int *y);
15 int render_chunk(const Background *background,
30 Background *create_background(Color base_color)
37 Background *background = PUSH_LT(lt, malloc(sizeof(Background)), free);
38 if (background == NULL) {
42 background->base_color = base_color;
43 background->position = vec(0.0f, 0.0f);
44 background->debug_mode = 0;
50 Background *create_background_from_line_stream(LineStream *line_stream)
53 if (sscanf(line_stream_next(line_stream), "%6s", color) == EOF) {
54 throw_error(ERROR_TYPE_LIBC);
58 return create_background(color_from_hexstr(color));
61 void destroy_background(Background *background)
64 RETURN_LT0(background->lt);
67 /* TODO(#182): background chunks are randomly disappearing when the size of the window is less than size of the chunk */
68 int background_render(const Background *background,
74 if (camera_clear_background(
76 background->base_color) < 0) {
80 const Rect view_port = camera_view_port(camera);
81 const Vec position = vec(view_port.x, view_port.y);
83 for (int l = 0; l < 3; ++l) {
84 const float parallax = 1.0f - 0.2f * (float)l;
86 int min_x = 0, min_y = 0;
87 chunk_of_point(vec(view_port.x - position.x * parallax,
88 view_port.y - position.y * parallax),
91 int max_x = 0, max_y = 0;
92 chunk_of_point(vec(view_port.x - position.x * parallax + view_port.w,
93 view_port.y - position.y * parallax + view_port.h),
96 for (int x = min_x; x <= max_x; ++x) {
97 for (int y = min_y; y <= max_y; ++y) {
102 color_darker(background->base_color, 0.05f * (float)(l + 1)),
114 /* Private Function */
116 static void chunk_of_point(Point p, int *x, int *y)
120 *x = (int) (p.x / BACKGROUND_CHUNK_WIDTH);
121 *y = (int) (p.y / BACKGROUND_CHUNK_HEIGHT);
124 int render_chunk(const Background *background,
126 int chunk_x, int chunk_y,
133 if (background->debug_mode) {
137 srand((unsigned int)(roundf((float)chunk_x + (float)chunk_y + parallax)));
139 for (size_t i = 0; i < BACKGROUND_CHUNK_COUNT; ++i) {
140 const float rect_x = rand_float_range((float) chunk_x * BACKGROUND_CHUNK_WIDTH,
141 (float) (chunk_x + 1) * BACKGROUND_CHUNK_WIDTH);
142 const float rect_y = rand_float_range((float) chunk_y * BACKGROUND_CHUNK_HEIGHT,
143 (float) (chunk_y + 1) * BACKGROUND_CHUNK_HEIGHT);
144 const float rect_w = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH * 0.5f);
145 const float rect_h = rand_float_range(rect_w * 0.5f, rect_w * 1.5f);
147 if (camera_fill_rect(
149 rect(rect_x + position.x * parallax,
150 rect_y + position.y * parallax,
161 void background_toggle_debug_mode(Background *background)
163 background->debug_mode = !background->debug_mode;