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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
use std::collections::HashMap;
use bimap::BiHashMap;
use enum_map::{enum_map, Enum, EnumMap};
use crate::{context, framebuffer, shader, audio};
const DELTA_TIME: f64 = 1.0 / 60.0;
pub trait Game {
fn initialize_audio(&self, ctx: &context::Context, st: &State, actx: &audio::Context) ->
HashMap<String, audio::Audio>;
fn finish_title(&mut self);
fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>;
fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>;
}
#[derive(Debug, Enum, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Key {
Up, Down, Left, Right,
A, B, L, R,
Start, Select,
}
pub const KEYS: [Key; 10] = [
Key::Up, Key::Down, Key::Left, Key::Right,
Key::A, Key::B, Key::L, Key::R,
Key::Start, Key::Select,
];
pub struct Keys {
pub pressed: EnumMap<Key, bool>,
pub new: EnumMap<Key, bool>,
}
impl Keys {
pub fn new() -> Self {
Self {
pressed: enum_map! {
Key::Up => false, Key::Down => false, Key::Left => false, Key::Right => false,
Key::A => false, Key::B => false, Key::L => false, Key::R => false,
Key::Start => false, Key::Select => false,
},
new: enum_map! {
Key::Up => false, Key::Down => false, Key::Left => false, Key::Right => false,
Key::A => false, Key::B => false, Key::L => false, Key::R => false,
Key::Start => false, Key::Select => false,
},
}
}
pub fn up(&self) -> bool { self.pressed[Key::Up] }
pub fn down(&self) -> bool { self.pressed[Key::Down] }
pub fn left(&self) -> bool { self.pressed[Key::Left] }
pub fn right(&self) -> bool { self.pressed[Key::Right] }
pub fn a(&self) -> bool { self.pressed[Key::A] }
pub fn b(&self) -> bool { self.pressed[Key::B] }
pub fn l(&self) -> bool { self.pressed[Key::L] }
pub fn r(&self) -> bool { self.pressed[Key::R] }
pub fn start(&self) -> bool { self.pressed[Key::Start] }
pub fn select(&self) -> bool { self.pressed[Key::Select] }
pub fn new_up(&mut self) -> bool { let ret = self.new[Key::Up]; self.new[Key::Up] = false; ret }
pub fn new_down(&mut self) -> bool { let ret = self.new[Key::Down]; self.new[Key::Down] = false; ret }
pub fn new_left(&mut self) -> bool { let ret = self.new[Key::Left]; self.new[Key::Left] = false; ret }
pub fn new_right(&mut self) -> bool { let ret = self.new[Key::Right]; self.new[Key::Right] = false; ret }
pub fn new_a(&mut self) -> bool { let ret = self.new[Key::A]; self.new[Key::A] = false; ret }
pub fn new_b(&mut self) -> bool { let ret = self.new[Key::B]; self.new[Key::B] = false; ret }
pub fn new_l(&mut self) -> bool { let ret = self.new[Key::L]; self.new[Key::L] = false; ret }
pub fn new_r(&mut self) -> bool { let ret = self.new[Key::R]; self.new[Key::R] = false; ret }
pub fn new_start(&mut self) -> bool { let ret = self.new[Key::Start]; self.new[Key::Start] = false; ret }
pub fn new_select(&mut self) -> bool { let ret = self.new[Key::Select]; self.new[Key::Select] = false; ret }
}
pub struct PointLight {
pub pos: glam::Vec3,
pub color: glam::Vec3,
pub attenuation: glam::Vec2,
}
pub struct State {
pub acc: f64,
pub last: f64,
pub tick: u64,
pub rebinding: Option<Key>,
pub keybindings: BiHashMap<winit::keyboard::KeyCode, Key>,
pub keys: Keys,
pub screen: framebuffer::Framebuffer,
pub render_framebuffer: framebuffer::Framebuffer,
pub shader_upscale: shader::Shader,
pub audio: Option<audio::Assets>,
pub projection: glam::Mat4,
pub camera: (glam::Vec3, glam::Vec3, glam::Vec3),
pub lighting: (glam::Vec3, glam::Vec3, glam::Vec3),
pub point_lights: Vec<PointLight>,
pub log: Vec<(u64, String)>,
}
pub fn now(ctx: &context::Context) -> f64 {
ctx.performance.now() / 1000.0
}
pub fn default_keybindings() -> BiHashMap<winit::keyboard::KeyCode, Key> {
BiHashMap::from_iter(vec![
(winit::keyboard::KeyCode::KeyW, Key::Up),
(winit::keyboard::KeyCode::KeyS, Key::Down),
(winit::keyboard::KeyCode::KeyA, Key::Left),
(winit::keyboard::KeyCode::KeyD, Key::Right),
(winit::keyboard::KeyCode::Digit1, Key::A),
(winit::keyboard::KeyCode::Digit2, Key::B),
(winit::keyboard::KeyCode::KeyQ, Key::L),
(winit::keyboard::KeyCode::KeyE, Key::R),
(winit::keyboard::KeyCode::Tab, Key::Start),
(winit::keyboard::KeyCode::Space, Key::Select),
])
}
impl State {
pub fn new(ctx: &context::Context) -> Self {
let screen = framebuffer::Framebuffer::screen(ctx);
let render_framebuffer = framebuffer::Framebuffer::new(
ctx,
&glam::Vec2::new(context::RENDER_WIDTH, context::RENDER_HEIGHT),
&glam::Vec2::new(0.0, 0.0),
);
let shader_upscale = shader::Shader::new_nolib(
ctx,
include_str!("assets/shaders/scale/vert.glsl"),
include_str!("assets/shaders/scale/frag.glsl"),
);
Self {
acc: 0.0,
last: now(ctx),
tick: 0,
rebinding: None,
keybindings: default_keybindings(),
keys: Keys::new(),
screen,
render_framebuffer,
shader_upscale,
audio: None,
projection: glam::Mat4::perspective_lh(
std::f32::consts::PI / 4.0,
context::RENDER_WIDTH / context::RENDER_HEIGHT,
// 0.1,
0.5,
50.0,
),
camera: (glam::Vec3::new(0.0, 0.0, 0.0), glam::Vec3::new(0.0, 0.0, 1.0), glam::Vec3::new(0.0, 1.0, 0.0)),
lighting: (
glam::Vec3::new(1.0, 1.0, 1.0),
glam::Vec3::new(1.0, 1.0, 1.0),
glam::Vec3::new(1.0, -1.0, 1.0),
),
point_lights: Vec::new(),
log: Vec::new(),
}
}
pub fn write_log(&mut self, e: &str) {
log::info!("log: {}", e.to_owned());
self.log.push((self.tick, e.to_owned()));
}
pub fn handle_resize(&mut self, ctx: &context::Context) {
self.screen = framebuffer::Framebuffer::screen(ctx);
}
pub fn move_camera(
&mut self,
_ctx: &context::Context,
pos: &glam::Vec3,
dir: &glam::Vec3,
up: &glam::Vec3,
) {
self.camera = (pos.clone(), dir.clone(), up.clone());
}
pub fn set_lighting(
&mut self,
_ctx: &context::Context,
ambient: &glam::Vec3,
color: &glam::Vec3,
dir: &glam::Vec3,
) {
self.lighting = (ambient.clone(), color.clone(), dir.clone());
}
pub fn add_point_light(
&mut self,
_ctx: &context::Context,
pos: &glam::Vec3,
color: &glam::Vec3,
attenuation: &glam::Vec2,
) {
self.point_lights.push(
PointLight {
pos: pos.clone(),
color: color.clone(),
attenuation: attenuation.clone(),
},
);
}
pub fn clear_point_lights(&mut self, _ctx: &context::Context) {
self.point_lights.clear();
}
pub fn view(&self) -> glam::Mat4 {
glam::Mat4::look_to_lh(
self.camera.0,
self.camera.1,
self.camera.2,
)
}
pub fn bind_3d_helper(&mut self, ctx: &context::Context, shader: &shader::Shader, plc: usize) {
shader.bind(ctx);
shader.set_mat4(ctx, "projection", &self.projection);
shader.set_mat4(ctx, "view", &self.view());
shader.set_vec3(
ctx, "light_ambient_color",
&self.lighting.0,
);
shader.set_vec3(
ctx, "light_dir_color",
&self.lighting.1,
);
shader.set_vec3(
ctx, "light_dir",
&self.lighting.2.normalize(),
);
shader.set_i32(
ctx, &format!("light_count"),
plc as _,
);
}
pub fn bind_3d_no_point_lights(&mut self, ctx: &context::Context, shader: &shader::Shader) {
self.bind_3d_helper(ctx, shader, 0);
}
pub fn bind_3d(&mut self, ctx: &context::Context, shader: &shader::Shader) {
let plc = self.point_lights.len().min(5);
self.bind_3d_helper(ctx, shader, plc);
if plc > 0 {
let lpos: Vec<_> = self.point_lights.iter().take(plc).map(|l| l.pos).collect();
shader.set_vec3_array(
ctx, &format!("light_pos[0]"),
&lpos,
);
let lcolor: Vec<_> = self.point_lights.iter().take(plc).map(|l| l.color).collect();
shader.set_vec3_array(
ctx, &format!("light_color[0]"),
&lcolor,
);
let lattenuation: Vec<_> = self.point_lights.iter().take(plc).map(|l| l.attenuation).collect();
shader.set_vec2_array(
ctx, &format!("light_attenuation[0]"),
&lattenuation,
);
}
}
pub fn bind_2d(&mut self, ctx: &context::Context, shader: &shader::Shader) {
shader.bind(ctx);
shader.set_mat4(&ctx, "projection", &glam::Mat4::IDENTITY);
shader.set_mat4(
ctx, "view",
&glam::Mat4::from_scale(
glam::Vec3::new(
2.0 / context::RENDER_WIDTH,
2.0 / context::RENDER_HEIGHT,
1.0,
),
),
);
}
pub fn mouse_pressed<G>(
&mut self,
ctx: &context::Context,
_button: winit::event::MouseButton,
game: &mut G
) where G: Game {
log::info!("click");
if self.audio.is_none() {
self.audio = Some(audio::Assets::new(|actx| {
game.initialize_audio(ctx, &self, actx)
}));
game.finish_title();
}
}
pub fn mouse_released(
&mut self,
_ctx: &context::Context,
_button: winit::event::MouseButton,
) {
}
pub fn key_pressed(
&mut self,
_ctx: &context::Context,
key: winit::keyboard::KeyCode,
) {
if let Some(k) = self.rebinding {
self.keybindings.insert(key, k);
self.rebinding = None;
} else if let Some(k) = self.keybindings.get_by_left(&key) {
self.keys.pressed[*k] = true;
self.keys.new[*k] = true;
}
}
pub fn key_released(
&mut self,
_ctx: &context::Context,
key: winit::keyboard::KeyCode,
) {
if let Some(k) = self.keybindings.get_by_left(&key) {
self.keys.pressed[*k] = false;
}
}
/// Return the first keybinding for the given virtual key
pub fn keybinding_for(&self, k: &Key) -> Option<String> {
if let Some(kc) = self.keybindings.get_by_right(k) {
Some(format!("{:?}", kc))
} else {
None
}
}
pub fn rebind_key(&mut self, k: &Key) {
self.rebinding = Some(*k);
}
pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game {
let now = now(ctx);
let diff = now - self.last;
self.acc += diff;
self.last = now;
// update, if enough time has accumulated since last update
if self.acc >= DELTA_TIME {
self.acc -= DELTA_TIME;
self.tick += 1;
game.update(ctx, self);
// if a lot of time has elapsed (e.g. if window is unfocused and not
// running update loop), prevent "death spiral"
if self.acc >= DELTA_TIME { self.acc = 0.0 }
}
}
pub fn run_render<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game {
self.render_framebuffer.bind(&ctx);
ctx.clear_color(glam::Vec4::new(0.1, 0.1, 0.1, 1.0));
ctx.clear();
game.render(ctx, self);
self.screen.bind(&ctx);
ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 1.0));
ctx.clear();
self.shader_upscale.bind(&ctx);
self.render_framebuffer.bind_texture(&ctx);
ctx.render_no_geometry();
}
}
|