aspen_engine/application/mod.rs
1//#![allow(unused)]
2use winit::{
3 event::Event,
4 event_loop::{
5 ControlFlow,
6 EventLoop,
7 EventLoopBuilder
8 }
9};
10
11use crate::{
12 renderer::Renderer,
13 timing::TimingStruct,
14 interface::Client
15};
16
17#[derive(Debug)]
18enum GlobalEvent {
19 Update,
20 Shutdown,
21}
22
23pub struct Application<UD: Client> {
24 event_loop: EventLoop<GlobalEvent>,
25 user_data: UD,
26 timer: TimingStruct,
27 renderer: Option<Renderer>,
28}
29
30impl<UD: Client> Application<UD> {
31 pub fn new(user_data: UD, use_graphics: bool) -> Self {
32 let event_loop = EventLoopBuilder::<GlobalEvent>::with_user_event()
33 .build()
34 .expect("event loop creation failed");
35
36 let renderer = match use_graphics {
37 true => Some(Renderer::new()),
38 false => None
39 };
40
41 Self {
42 event_loop,
43 user_data,
44 timer: TimingStruct::new(),
45 renderer,
46 }
47 }
48
49 pub fn run(mut self) {
50 let proxy = self.event_loop.create_proxy();
51 self.event_loop.run(move |event, elwt| {
52 elwt.set_control_flow(ControlFlow::Poll);
53 match event {
54 //Event::WindowEvent { event, .. } => match event {
55 // WindowEvent::Resized(size) => {
56 // if size.width != 0 && size.height != 0 {
57 // // Some platforms like EGL require resizing GL surface to update the size
58 // // Notable platforms here are Wayland and macOS, other don't require it
59 // // and the function is no-op, but it's wise to resize it for portability
60 // // reasons.
61 // }
62 // },
63 // WindowEvent::CloseRequested
64 // | WindowEvent::KeyboardInput {
65 // event: KeyEvent { logical_key: Key::Named(NamedKey::Escape), .. },
66 // ..
67 // } => window_target.exit(),
68 // _ => (),
69 //},
70 Event::AboutToWait => {
71 proxy.send_event(GlobalEvent::Update).unwrap();
72 },
73 Event::UserEvent(global_event) => {
74 match global_event {
75 GlobalEvent::Update => {
76 let time_info = self.timer.update(100);
77
78 for _ in 0..time_info.fixed_steps {
79 self.user_data.fixed_update(time_info.fixed_delta);
80 }
81
82 self.user_data.update(time_info.delta);
83 },
84 GlobalEvent::Shutdown => {
85 elwt.exit()
86 }
87 }
88 },
89 _ => ()
90 }
91 }).unwrap()
92 }
93}