-
Notifications
You must be signed in to change notification settings - Fork 0
/
debris.h
146 lines (130 loc) · 4.82 KB
/
debris.h
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
#define NUM_DEBRIS 48
static fx3_t debris_positions[NUM_DEBRIS];
static fx3_t debris_rotation_axes[NUM_DEBRIS];
static fx_t debris_rotations[NUM_DEBRIS];
static fx_t debris_rotation_speeds[NUM_DEBRIS];
static fx3_t debris_speeds[NUM_DEBRIS];
static inline void randomize_debris_properties(uint16_t i) {
fx3_t r;
r.x = fx_random_signed_one();
r.y = fx_random_signed_one();
r.z = fx_random_signed_one();
fx3_normalize_ip(&r);
debris_rotation_axes[i] = r;
debris_rotations[i] = fx_random_one();
switch (i & 3) {
case 0:
case 1:
case 2:
debris_rotation_speeds[i] = (fx_random_one() >> 7) + 256;
debris_speeds[i].x = fx_random_one() >> 12;
debris_speeds[i].y = fx_random_one() >> 12;
debris_speeds[i].z = fx_random_one() >> 12;
break;
case 3:
debris_rotation_speeds[i] = (fx_random_one() >> 8) + 256;
debris_speeds[i].x = fx_random_one() >> 13;
debris_speeds[i].y = fx_random_one() >> 13;
debris_speeds[i].z = fx_random_one() >> 13;
break;
}
}
static void init_debris() {
uint16_t i;
for (i = 0; i < NUM_DEBRIS; ++i) {
fx3_t r;
r.x = random_debris_x();
r.y = random_debris_x();
r.z = random_debris_x();
debris_positions[i] = r;
randomize_debris_properties(i);
}
}
#define DEBRIS_BOX_SIZE 65536L
#define DEBRIS_BOX_HALF_SIZE (DEBRIS_BOX_SIZE >> 1)
static void update_debris(const fx3_t* center, const fx3_t* movement) {
uint16_t i;
fx_t x_low, x_high, y_low, y_high, z_low, z_high;
x_low = center->x - DEBRIS_BOX_HALF_SIZE;
x_high = center->x + DEBRIS_BOX_HALF_SIZE;
y_low = center->y - DEBRIS_BOX_HALF_SIZE;
y_high = center->y + DEBRIS_BOX_HALF_SIZE;
z_low = center->z - DEBRIS_BOX_HALF_SIZE;
z_high = center->z + DEBRIS_BOX_HALF_SIZE;
for (i = 0; i < NUM_DEBRIS; ++i) {
fx3_t* p = &debris_positions[i];
fx3_add_ip(p, &debris_speeds[i]);
fx3_add_ip(p, movement);
if (p->x <= x_low) {
p->x += DEBRIS_BOX_SIZE;
p->y = random_debris_x();
p->z = random_debris_x();
} else if (p->x >= x_high) {
p->x -= DEBRIS_BOX_SIZE;
p->y = random_debris_x();
p->z = random_debris_x();
} else if (p->y <= y_low) {
p->y += DEBRIS_BOX_SIZE;
p->x = random_debris_x();
p->z = random_debris_x();
} else if (p->y >= y_high) {
p->y -= DEBRIS_BOX_SIZE;
p->x = random_debris_x();
p->z = random_debris_x();
} else if (p->z <= z_low) {
p->z += DEBRIS_BOX_SIZE;
p->x = random_debris_x();
p->y = random_debris_x();
} else if (p->z >= z_high) {
p->z -= DEBRIS_BOX_SIZE;
p->x = random_debris_x();
p->y = random_debris_x();
}
debris_rotations[i] += debris_rotation_speeds[i];
if (debris_rotations[i] >= FX_ONE) {
debris_rotations[i] -= FX_ONE;
}
}
}
#define DEBRIS_CLIP_BORDER 32
#define DEBRIS_LEFT_CLIP (-DEBRIS_CLIP_BORDER << RASTER_SUBPIXEL_BITS)
#define DEBRIS_RIGHT_CLIP ((SCREEN_X_MAX + DEBRIS_CLIP_BORDER) << RASTER_SUBPIXEL_BITS)
#define DEBRIS_TOP_CLIP (-DEBRIS_CLIP_BORDER << RASTER_SUBPIXEL_BITS)
#define DEBRIS_BOTTOM_CLIP ((SCREEN_Y_MAX + DEBRIS_CLIP_BORDER) << RASTER_SUBPIXEL_BITS)
static void draw_debris(const fx4x3_t* view_matrix) {
fx4_t rotation;
fx_t rotation_angle;
fx3_t transformed_position;
const fx3_t* position_iter = debris_positions;
const fx_t* rotation_iter = debris_rotations;
uint16_t i;
for (i = 0; i < NUM_DEBRIS; ++i) {
const fx3_t* position = position_iter++;
rotation_angle = *rotation_iter++;
fx_transform_point(&transformed_position, view_matrix, position);
if (transformed_position.z < NEAR_CLIP)
continue;
project_to_screen(&transformed_position,
SCREEN_SUBPIXEL_CENTER_X, SCREEN_SUBPIXEL_CENTER_Y);
if (transformed_position.x < DEBRIS_LEFT_CLIP ||
transformed_position.x > DEBRIS_RIGHT_CLIP ||
transformed_position.y < DEBRIS_TOP_CLIP ||
transformed_position.y > DEBRIS_BOTTOM_CLIP)
continue;
fx_quat_rotation_axis_angle(&rotation, &debris_rotation_axes[i], rotation_angle);
switch (i & 3) {
case 0:
draw_scrap(view_matrix, &rotation, position);
break;
case 1:
draw_scrap2(view_matrix, &rotation, position);
break;
case 2:
draw_scrap3(view_matrix, &rotation, position);
break;
case 3:
draw_asteroid(view_matrix, &rotation, position);
break;
}
}
}