-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.cpp
More file actions
257 lines (218 loc) · 8.63 KB
/
background.cpp
File metadata and controls
257 lines (218 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "background.h"
#include "random.h"
const f32 MOTE_AREA_RADIUS_Y = 8.0f;
const f32 MOTE_AREA_RADIUS_X = 8.0f;
const f32 FLOATY_AREA_RADIUS_Y = 12.0f;
const f32 FLOATY_AREA_RADIUS_X = 12.0f;
const f32 MIN_MOTE_Z = 0.94f;
const f32 MAX_MOTE_Z = 0.96f;
const f32 MIN_FLOATY_Z = 0.90f;
const f32 MAX_FLOATY_Z = 0.94f;
const f32 MIN_DISTANCE_EFFECT = 0.4f;
const f32 MAX_DISTANCE_EFFECT = 0.6f;
const i32 MOTE_COUNT = 10000;
const i32 ATTRACTOR_COUNT = 0;
global u32 palette[] = {
0xffd7eaf1, // 0: sky blue
0xffd7eaf1, // 0: sky blue
0xffd7eaf1, // 0: sky blue
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xffbcc4a1, // 2: dark grass green
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffc3b9a3, // 4: brown
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd7eaf1, // 0: sky blue
0xffd7eaf1, // 0: sky blue
0xffd7eaf1, // 0: sky blue
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xfff8fde3, // 1: cloud white
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffefdfb8, // 3: orange-ish
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
0xffd4e3ec, // 5: light blue
};
global char* texture_paths[] = {
"assets/cloud.png",
"assets/cloud_3.png",
"assets/cloud_4.png",
"assets/cloud_5.png",
"assets/cloud_6.png",
"assets/cloud_7.png",
"assets/tower.png",
"assets/castle.png"
};
void create_background(game_state_* game_state,
background_* background) {
background->background_color = to_rgb(0xffc8dfec);
background->motes.count = MOTE_COUNT;
background->motes.values = PUSH_ARRAY(&game_state->render_arena,
MOTE_COUNT,
mote_);
background->attractors.count = ATTRACTOR_COUNT;
background->attractors.values = PUSH_ARRAY(&game_state->render_arena,
ATTRACTOR_COUNT,
mote_attractor_);
const i32 floaty_count = 70;
background->floaties.count = floaty_count;
background->floaties.values = PUSH_ARRAY(&game_state->render_arena,
floaty_count,
floaty_);
const i32 texture_count = ARRAY_SIZE(texture_paths);
background->textures.count = texture_count;
background->textures.values = PUSH_ARRAY(&game_state->render_arena,
texture_count,
tex2);
for (int i = 0; i < texture_count; ++i) {
tex2 texture = load_image(texture_paths[i]);
background->textures.set(i, texture);
}
for (int i = 0; i < floaty_count; ++i) {
floaty_* floaty = background->floaties.at(i);
i32 texture_index = random_i32(0, texture_count);
floaty->texture = background->textures[texture_index];
floaty->source_rect = rect_i {0,0,floaty->texture.width, floaty->texture.height};
floaty->z = random_f32(0.0f, 1.0f);
// f32 z = scale(floaty->z, MIN_FLOATY_Z, MAX_FLOATY_Z);
// f32 parallax = 1.0f - z;
f32 min_x = -FLOATY_AREA_RADIUS_X;
f32 max_x = FLOATY_AREA_RADIUS_X;
f32 min_y = -FLOATY_AREA_RADIUS_Y;
f32 max_y = FLOATY_AREA_RADIUS_Y;
floaty->center = v2 {
random_f32(min_x, max_x),
random_f32(min_y, max_y)
};
floaty->velocity = vpixels_to_meters(random_f32(0.0f, 0.5f));
}
for (i32 i = 0; i < MOTE_COUNT; ++i) {
mote_* mote = background->motes.at(i);
mote->width = 1;
mote->color = palette[random_i32(0, ARRAY_SIZE(palette))];
mote->z = random_f32(0.0f, 1.0f);
// f32 z = scale(mote->z, MIN_MOTE_Z, MAX_MOTE_Z);
// f32 parallax = 1.0f - z;
f32 min_x = -MOTE_AREA_RADIUS_X;
f32 max_x = MOTE_AREA_RADIUS_X;
f32 min_y = -MOTE_AREA_RADIUS_Y;
f32 max_y = MOTE_AREA_RADIUS_Y;
mote->center = v2 {
random_f32(min_x, max_x),
random_f32(min_y, max_y)
};
mote->velocity = v2 {
vpixels_to_meters(random_f32(-2.0f, 2.0f)),
0.0f
};
}
for (i32 i = 0; i < ATTRACTOR_COUNT; ++i) {
mote_attractor_* attractor = background->attractors.at(i);
attractor->position = v2 {
random_f32(-1.0f, 1.0f),
random_f32(-1.0f, 1.0f)
};
}
}
void update_background(background_* background,
render_group_* render_group,
camera_ camera,
f32 dt) {
TIMED_FUNC();
// u32 u32_background_color = from_rgb(background->background_color);
m3x3 inverse_view = get_inverse_view_transform_3x3(camera);
for (i32 i = 0; i < background->floaties.count; ++i) {
floaty_* floaty = background->floaties.at(i);
floaty->center.x += floaty->velocity * dt;
f32 z = scale(floaty->z, MIN_FLOATY_Z, MAX_FLOATY_Z);
f32 x = floaty->center.x;
f32 y = floaty->center.y;
f32 parallax = 1.0f - z;
f32 min_x = (camera.center.x * parallax - FLOATY_AREA_RADIUS_X);
f32 max_x = (camera.center.x * parallax + FLOATY_AREA_RADIUS_X);
f32 min_y = (camera.center.y * parallax - FLOATY_AREA_RADIUS_Y);
f32 max_y = (camera.center.y * parallax + FLOATY_AREA_RADIUS_Y);
if (x < min_x || x > max_x) {
x = wrap(x, min_x, max_x);
y = random_f32(min_y, max_y);
floaty->texture = background->textures[random_i32(0, background->textures.count)];
floaty->source_rect = rect_i {0,0,floaty->texture.width, floaty->texture.height};
}
y = snap_to(wrap(y, min_y, max_y), VIRTUAL_PIXEL_SIZE);
floaty->center.x = x;
floaty->center.y = y;
push_texture(render_group,
v2 { snap_to(floaty->center.x, VIRTUAL_PIXEL_SIZE), floaty->center.y },
v2 { floaty->texture.width / 2.0f, floaty->texture.height / 2.0f },
VIRTUAL_PIXEL_SIZE,
floaty->texture,
floaty->source_rect,
rgba_{0.7f, 0.7f, 0.75f,
scale(floaty->z, MIN_DISTANCE_EFFECT, MAX_DISTANCE_EFFECT)},
0.0f,
z,
true);
}
for (i32 i = 0; i < background->motes.count; ++i) {
mote_* mote = background->motes.at(i);
f32 z = scale(mote->z, MIN_MOTE_Z, MAX_MOTE_Z);
f32 parallax = 1.0f - z;
mote->center += mote->velocity * dt;
f32 x = mote->center.x;
f32 y = mote->center.y;
f32 min_x = (camera.center.x * parallax - MOTE_AREA_RADIUS_X);
f32 max_x = (camera.center.x * parallax + MOTE_AREA_RADIUS_X);
f32 min_y = (camera.center.y * parallax - MOTE_AREA_RADIUS_Y);
f32 max_y = (camera.center.y * parallax + MOTE_AREA_RADIUS_Y);
x = wrap(x, min_x, max_x);
y = wrap(y, min_y, max_y);
mote->center.x = x;
mote->center.y = y;
push_rect(render_group,
to_rgb(mote->color),
v2 { snap_to(mote->center.x, VIRTUAL_PIXEL_SIZE),
snap_to(mote->center.y, VIRTUAL_PIXEL_SIZE) },
v2 { (f32)mote->width, 1.0f } * VIRTUAL_PIXEL_SIZE,
0.0f,
z,
true);
}
for (int i = 0; i < background->attractors.count; ++i) {
mote_attractor_* attractor = background->attractors.at(i);
v2 position = inverse_view * v2 { attractor->position.x, attractor->position.y };
push_rect(render_group,
to_rgb(0xffff0000),
position,
v2 { 2.0f, 2.0f } * VIRTUAL_PIXEL_SIZE,
0.0f,
0.0f,
false);
}
}