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
mod vehicle;
use agents::app::{App, State};
use vehicle::{Bounds, Vehicle};
use anyhow::Result;
use draw2d::{
camera::{default_camera_controls, OrthoCamera},
graphics::{
ext::TextureLoader,
layer::{Batch, LayerHandle},
texture_atlas::TextureAtlas,
vertex::Vertex2d,
Graphics,
},
};
use std::time::Duration;
struct Demo {
background_layer: LayerHandle,
foreground_layer: LayerHandle,
vehicles: Vec<Vehicle>,
camera: OrthoCamera,
}
impl Demo {
fn new(window: &mut glfw::Window, graphics: &mut Graphics) -> Result<Self> {
let (w, h) = window.get_size();
let mut vehicles = vec![];
let max = 10000;
for i in 0..max {
let norm = i as f32 / max as f32;
let angle = norm * std::f32::consts::TAU;
vehicles.push(Vehicle::new(
[angle.cos() * 10.0, angle.sin() * 10.0],
[angle.cos() * 2.0, angle.sin() * 2.0],
));
}
Ok(Self {
background_layer: graphics.add_layer_to_bottom(),
foreground_layer: graphics.add_layer_to_top(),
vehicles,
camera: OrthoCamera::with_viewport(20.0, w as f32 / h as f32),
})
}
fn update_projection(&self, graphics: &mut Graphics) {
graphics
.get_layer_mut(&self.background_layer)
.set_projection(self.camera.as_matrix());
graphics
.get_layer_mut(&self.foreground_layer)
.set_projection(self.camera.as_matrix());
}
}
impl State for Demo {
fn init(
&mut self,
_window: &mut glfw::Window,
graphics: &mut Graphics,
) -> Result<()> {
self.update_projection(graphics);
let mut background = Batch::empty();
let grid_cell_texture =
graphics.read_texture_file("./assets/GridCell.png")?;
background.texture_handle = graphics.add_texture(grid_cell_texture)?;
let size = 20.0;
let grid_spacing = 2.0;
let grid_size = (size * 2.0) / grid_spacing;
background.vertices.extend_from_slice(&[
Vertex2d {
pos: [-size, size],
uv: [0.0, 0.0],
rgba: [0.2, 0.2, 0.4, 1.0],
},
Vertex2d {
pos: [size, size],
uv: [grid_size, 0.0],
rgba: [0.2, 0.2, 0.4, 1.0],
},
Vertex2d {
pos: [size, -size],
uv: [grid_size, grid_size],
rgba: [0.2, 0.2, 0.4, 1.0],
},
Vertex2d {
pos: [-size, size],
uv: [0.0, 0.0],
rgba: [0.2, 0.2, 0.4, 1.0],
},
Vertex2d {
pos: [size, -size],
uv: [grid_size, grid_size],
rgba: [0.2, 0.2, 0.4, 1.0],
},
Vertex2d {
pos: [-size, -size],
uv: [0.0, grid_size],
rgba: [0.2, 0.2, 0.4, 1.0],
},
]);
graphics
.get_layer_mut(&self.background_layer)
.push_batch(background);
Ok(())
}
fn update(
&mut self,
window: &mut glfw::Window,
graphics: &mut Graphics,
update_duration: Duration,
) -> Result<()> {
let mut vehicle_batch = Batch::empty();
let bounds = Bounds::create(window);
let dt = update_duration.as_secs_f32();
for vehicle in &mut self.vehicles {
vehicle.enforce_bounds(&bounds);
vehicle.integrate(dt);
vehicle.draw(&mut vehicle_batch);
}
let layer = graphics.get_layer_mut(&self.foreground_layer);
layer.clear();
layer.push_batch(vehicle_batch);
Ok(())
}
fn handle_event(
&mut self,
window_event: &glfw::WindowEvent,
_window: &mut glfw::Window,
graphics: &mut Graphics,
) -> Result<()> {
if default_camera_controls(&mut self.camera, &window_event) {
self.update_projection(graphics);
}
Ok(())
}
}
fn main() -> Result<()> {
App::new(Demo::new)?.main_loop()
}