3 #include "game/level/background.h"
8 #define BACKGROUND_CHUNK_COUNT 10
9 #define BACKGROUND_CHUNK_WIDTH 500.0f
10 #define BACKGROUND_CHUNK_HEIGHT 500.0f
12 static void chunk_of_point(point_t p, int *x, int *y);
13 int render_chunk(const background_t *background,
14 const camera_t *camera,
27 background_t *create_background(color_t base_color)
29 lt_t *lt = create_lt();
34 background_t *background = PUSH_LT(lt, malloc(sizeof(background_t)), free);
35 if (background == NULL) {
39 background->base_color = base_color;
40 background->position = vec(0.0f, 0.0f);
46 void destroy_background(background_t *background)
49 RETURN_LT0(background->lt);
52 int background_render(const background_t *background,
53 const camera_t *camera)
58 const rect_t view_port = camera_view_port(camera);
59 const vec_t position = vec(view_port.x, view_port.y);
61 for (int l = 0; l < 3; ++l) {
62 const float parallax = 1.0f - 0.2f * (float)l;
64 int min_x = 0, min_y = 0;
65 chunk_of_point(vec(view_port.x - position.x * parallax,
66 view_port.y - position.y * parallax),
69 int max_x = 0, max_y = 0;
70 chunk_of_point(vec(view_port.x - position.x * parallax + view_port.w,
71 view_port.y - position.y * parallax + view_port.h),
74 for (int x = min_x; x <= max_x; ++x) {
75 for (int y = min_y; y <= max_y; ++y) {
80 color_darker(background->base_color, 0.2f * (float)l),
92 /* Private Function */
94 static void chunk_of_point(point_t p, int *x, int *y)
98 *x = (int) (p.x / BACKGROUND_CHUNK_WIDTH);
99 *y = (int) (p.y / BACKGROUND_CHUNK_HEIGHT);
102 int render_chunk(const background_t *background,
103 const camera_t *camera,
111 srand((unsigned int)(roundf((float)x + (float)y + parallax)));
113 for (size_t i = 0; i < BACKGROUND_CHUNK_COUNT; ++i) {
114 const float rect_x = rand_float_range((float) x * BACKGROUND_CHUNK_WIDTH,
115 (float) (x + 1) * BACKGROUND_CHUNK_WIDTH);
116 const float rect_y = rand_float_range((float) y * BACKGROUND_CHUNK_HEIGHT,
117 (float) (y + 1) * BACKGROUND_CHUNK_HEIGHT);
118 const float rect_w = rand_float_range(0.0f, BACKGROUND_CHUNK_WIDTH * 0.5f);
119 const float rect_h = rand_float_range(0.0f, BACKGROUND_CHUNK_HEIGHT * 0.5f);
121 if (camera_fill_rect(
123 rect(rect_x + position.x * parallax,
124 rect_y + position.y * parallax,