]> git.lizzy.rs Git - mt_client.git/blob - src/gfx.rs
Mesh data queue
[mt_client.git] / src / gfx.rs
1 use crate::{GfxEvent::*, NetEvent};
2 use cgmath::Rad;
3 use std::time::Instant;
4 use tokio::sync::mpsc;
5 use winit::{
6     event::{DeviceEvent::*, Event::*, WindowEvent::*},
7     event_loop::ControlFlow::ExitWithCode,
8     platform::run_return::EventLoopExtRunReturn,
9 };
10
11 mod map;
12 mod media;
13 mod state;
14 mod util;
15
16 pub async fn run(
17     mut event_loop: winit::event_loop::EventLoop<crate::GfxEvent>,
18     net_events: mpsc::UnboundedSender<NetEvent>,
19 ) {
20     let window = winit::window::WindowBuilder::new()
21         .build(&event_loop)
22         .unwrap();
23
24     window.set_cursor_visible(false);
25
26     let mut state = state::State::new(&window).await;
27     let mut map: Option<map::MapRender> = None;
28     let mut media = media::MediaMgr::new();
29
30     let mut nodedefs = None;
31
32     let mut last_frame = Instant::now();
33
34     let mut game_paused = false;
35
36     event_loop.run_return(move |event, _, flow| match event {
37         MainEventsCleared => window.request_redraw(),
38         RedrawRequested(id) if id == window.id() => {
39             let now = Instant::now();
40             let dt = now - last_frame;
41             last_frame = now;
42
43             state.update(dt);
44             if let Some(map) = &mut map {
45                 map.update(&mut state);
46             }
47
48             net_events
49                 .send(NetEvent::PlayerPos(
50                     state.camera.position.into(),
51                     Rad(state.camera.yaw).into(),
52                     Rad(state.camera.pitch).into(),
53                 ))
54                 .ok();
55
56             use wgpu::SurfaceError::*;
57             match state.render(&map) {
58                 Ok(_) => {}
59                 Err(Lost) => state.configure_surface(),
60                 Err(OutOfMemory) => *flow = ExitWithCode(0),
61                 Err(err) => eprintln!("gfx error: {err:?}"),
62             }
63         }
64         WindowEvent {
65             ref event,
66             window_id: id,
67         } if id == window.id() => match event {
68             CloseRequested => *flow = ExitWithCode(0),
69             Resized(size) => state.resize(*size),
70             ScaleFactorChanged { new_inner_size, .. } => state.resize(**new_inner_size),
71             KeyboardInput {
72                 input:
73                     winit::event::KeyboardInput {
74                         virtual_keycode: Some(key),
75                         state: key_state,
76                         ..
77                     },
78                 ..
79             } => {
80                 use fps_camera::Actions;
81                 use winit::event::{ElementState, VirtualKeyCode as Key};
82
83                 if key == &Key::Escape && key_state == &ElementState::Pressed {
84                     game_paused = !game_paused;
85                     window.set_cursor_visible(game_paused);
86                 }
87
88                 if !game_paused {
89                     let actions = match key {
90                         Key::W => Actions::MOVE_FORWARD,
91                         Key::A => Actions::STRAFE_LEFT,
92                         Key::S => Actions::MOVE_BACKWARD,
93                         Key::D => Actions::STRAFE_RIGHT,
94                         Key::Space => Actions::FLY_UP,
95                         Key::LShift => Actions::FLY_DOWN,
96                         _ => Actions::empty(),
97                     };
98
99                     match key_state {
100                         ElementState::Pressed => state.camera.enable_actions(actions),
101                         ElementState::Released => state.camera.disable_action(actions),
102                     }
103                 }
104             }
105             _ => {}
106         },
107         DeviceEvent {
108             event: MouseMotion { delta },
109             ..
110         } => {
111             if !game_paused {
112                 state.camera.update_mouse(-delta.0 as f32, delta.1 as f32);
113                 window
114                     .set_cursor_position(winit::dpi::PhysicalPosition::new(
115                         state.config.width / 2,
116                         state.config.height / 2,
117                     ))
118                     .ok();
119             }
120         }
121         UserEvent(event) => match event {
122             Close => *flow = ExitWithCode(0),
123             NodeDefs(defs) => nodedefs = Some(defs),
124             MapBlock(pos, blk) => {
125                 if let Some(map) = &map {
126                     map.add_block(pos, blk);
127                 }
128             }
129             Media(files, finished) => {
130                 media.add_server_media(files);
131
132                 if finished {
133                     map = Some(map::MapRender::new(
134                         &mut state,
135                         &media,
136                         nodedefs.take().unwrap_or_default(),
137                     ));
138
139                     net_events.send(NetEvent::Ready).ok();
140                 }
141             }
142             PlayerPos(pos, pitch, yaw) => {
143                 state.camera.position = pos.into();
144                 state.camera.pitch = Rad::<f32>::from(pitch).0;
145                 state.camera.yaw = Rad::<f32>::from(yaw).0;
146             }
147         },
148         _ => {}
149     });
150 }