-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_thread.rs
248 lines (227 loc) · 8.22 KB
/
render_thread.rs
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
use crate::graph::{Coordinate, LinearGraph, Walls};
use crate::map_element::MapElement;
use mockall_double::double;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use std::{thread, thread::JoinHandle};
#[double]
use crate::graph::GraphMethods;
#[double]
use crate::graph::Rays;
#[double]
use crate::map::Map;
#[double]
use crate::player_utils::Player;
pub struct RenderThread {
pub map_elements: Arc<RwLock<Vec<Box<dyn MapElement>>>>,
pub player: Arc<RwLock<Player>>,
pub map: Arc<Map>,
pub rays: Arc<Rays>,
pub start_render_receiver: Receiver<bool>,
pub sender_walls: Sender<(Walls, usize)>,
pub thread_index: usize,
pub threads_amount: usize,
}
macro_rules! check_next_ray {
($next_ray:expr, $rays_iter:expr, $walls_in_sight:expr) => {
$next_ray = $rays_iter.next();
if $next_ray.is_none() {
return $walls_in_sight;
}
};
}
impl RenderThread {
pub fn start_thread(render_thread: RenderThread) -> JoinHandle<()> {
thread::spawn(move || {
render_thread.start();
})
}
fn start(&self) {
while let Ok(true) = self.start_render_receiver.recv() {
let map_elements = self.map_elements.read().unwrap();
let player = self.player.read().unwrap();
let walls = self.get_walls_in_sight(
player.position(),
self.rays
.iter(player.angle(), self.thread_index, self.threads_amount),
&map_elements,
);
self.sender_walls.send((walls, self.thread_index)).unwrap();
}
}
fn get_walls_in_sight<'a>(
&self,
position: &Coordinate,
mut rays_iter: impl Iterator<Item = &'a LinearGraph>,
map_elements: &Vec<Box<dyn MapElement>>,
) -> Walls {
let mut walls_in_sight = Walls(vec![]);
let mut next_ray: Option<&LinearGraph> = None;
let mut current_ray: LinearGraph;
loop {
if next_ray.is_none() {
check_next_ray!(next_ray, rays_iter, walls_in_sight);
}
current_ray = next_ray.unwrap().clone();
next_ray = None;
loop {
if let Some((wall, ray_ret)) =
self.map.cast_ray(position, ¤t_ray, map_elements)
{
if walls_in_sight.is_wall_in_object(&wall) {
break;
}
if !walls_in_sight.is_wall_connected(&wall) && next_ray.is_some() {
break;
} else {
walls_in_sight.try_extend_last_wall(wall);
}
if next_ray.is_none() {
check_next_ray!(next_ray, rays_iter, walls_in_sight);
}
while GraphMethods::less_than(next_ray.unwrap(), ¤t_ray) {
check_next_ray!(next_ray, rays_iter, walls_in_sight);
}
current_ray = ray_ret;
} else {
break;
}
}
}
}
}
#[cfg(test)]
mod tests {
#![allow(non_upper_case_globals)]
use super::*;
use crate::graph::MockGraphMethods;
use crate::graph::{MockRays, MockRaysIterator, Wall};
use crate::map::MockMap;
use crate::map_element::{Color, Point};
use crate::player_utils::{Angle, MockPlayer, Radians};
use mockall::*;
use std::sync::mpsc::channel;
#[test]
fn render_thread_start() {
let mut seq = Sequence::new();
let map_elements: Arc<RwLock<Vec<Box<dyn MapElement>>>> = Arc::new(RwLock::new(vec![]));
let player = Arc::new(RwLock::new(MockPlayer::default()));
let mut map = MockMap::default();
let mut rays = MockRays::new();
lazy_static! {
static ref ray_vec_iter: Vec<LinearGraph> = vec![
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
LinearGraph::default(),
];
}
let less_than_context = MockGraphMethods::less_than_context();
let (start_render_sender, start_render_receiver) = channel::<bool>();
let (sender_walls, receiver_walls) = channel::<(Walls, usize)>();
static thread_index: usize = 3;
static threads_amount: usize = 4;
static player_position: Coordinate = Coordinate { x: 10.0, y: 20.0 };
static player_angle: Angle = Angle {
start: Radians::PI,
end: Radians::PI_2,
};
let walls_rays = vec![
Some((
Wall::new(Point { x: 2, y: 4 }, Point { x: 3, y: 4 }, Color::Red),
LinearGraph::default(),
)),
Some((
Wall::new(Point { x: 3, y: 4 }, Point { x: 4, y: 4 }, Color::Red),
LinearGraph::default(),
)),
Some((
Wall::new(Point { x: 4, y: 2 }, Point { x: 5, y: 2 }, Color::Red),
LinearGraph::default(),
)),
Some((
Wall::new(Point { x: 3, y: 4 }, Point { x: 4, y: 4 }, Color::Red),
LinearGraph::default(),
)),
Some((
Wall::new(Point { x: 4, y: 2 }, Point { x: 5, y: 2 }, Color::Red),
LinearGraph::default(),
)),
Some((
Wall::new(Point { x: 5, y: 2 }, Point { x: 5, y: 1 }, Color::Green),
LinearGraph::default(),
)),
None,
Some((
Wall::new(Point { x: 5, y: 1 }, Point { x: 5, y: 0 }, Color::Green),
LinearGraph::default(),
)),
];
let walls_in_sight = Walls(vec![
Wall::new(Point { x: 2, y: 4 }, Point { x: 4, y: 4 }, Color::Red),
Wall::new(Point { x: 4, y: 2 }, Point { x: 5, y: 2 }, Color::Red),
Wall::new(Point { x: 5, y: 2 }, Point { x: 5, y: 0 }, Color::Green),
]);
{
let mut player_write = player.write().unwrap();
player_write
.expect_position()
.times(1)
.return_const(player_position.clone())
.in_sequence(&mut seq);
player_write
.expect_angle()
.times(1)
.return_const(player_angle.clone())
.in_sequence(&mut seq);
}
rays.expect_iter()
.times(1)
.withf(|angle, _thread_index, _threads_amount| {
*angle == player_angle
&& *_thread_index == thread_index
&& *_threads_amount == threads_amount
})
.returning(|_, _, _| {
let mut rays_iterator = Box::new(MockRaysIterator::default());
for ray_ in ray_vec_iter.iter() {
rays_iterator
.expect_next()
.once()
.returning(move || Some(&ray_));
}
rays_iterator.expect_next().once().return_const(None);
return rays_iterator;
});
for wall_ray in walls_rays.into_iter() {
map.expect_cast_ray().once().return_const(wall_ray);
}
for _ in 0..4 {
less_than_context.expect().once().return_const(true);
less_than_context.expect().once().return_const(false);
}
less_than_context.expect().once().return_const(true);
let render_thread = RenderThread {
map_elements,
player,
map: Arc::new(map),
rays: Arc::new(rays),
start_render_receiver,
sender_walls,
thread_index,
threads_amount,
};
start_render_sender.send(true).unwrap();
start_render_sender.send(false).unwrap();
render_thread.start();
assert_eq!(
receiver_walls.recv().unwrap(),
(walls_in_sight, thread_index)
);
}
}