-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathengine.rs
More file actions
143 lines (104 loc) · 3.46 KB
/
engine.rs
File metadata and controls
143 lines (104 loc) · 3.46 KB
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
use winit::{WindowBuilder, get_available_monitors, get_primary_monitor, Event, WindowEvent, EventsLoop};
use vulkano_win::Window;
use std::clone::Clone;
use std::sync::{Arc, Mutex};
use config::Config;
use config::WindowConfig;
use input::InputSystem;
use renderer::Renderer;
use core::Scene;
use renderer::GraphicsState;
/// The minimum window resolution the engine supports.
pub const MINIMUM_RESOLUTION: [u32; 2] = [640, 480];
/// Game Engine
pub struct Engine {
/// Engine Configuration
config: Arc<Config>,
/// Actor Scene Graph
scene: Scene,
/// Input Mappings
inputs: Arc<Mutex<InputSystem>>,
/// API Specific Renderer
renderer: Renderer,
/// Graphics Data Structures
gfx: Arc<Mutex<GraphicsState>>,
/// OS Window
window: Arc<Window>,
/// OS Events
events_loop: Arc<EventsLoop>
}
impl Engine {
/// Initialize Engine subsystems
pub fn new(config: Config, scene: Scene) -> Engine {
let cfg = Arc::new(config.clone());
let (renderer, window, events_loop) = Renderer::new(create_window(&config.window), cfg.clone());
let inputs = Arc::new(Mutex::new(InputSystem::new(cfg.clone())));
let gfx = Arc::new(Mutex::new(GraphicsState::new()));
Engine {
window,
events_loop,
renderer,
gfx,
config: cfg,
inputs,
scene,
}
}
/// Handles input/output events from the window and any input middleware.
pub fn io(&mut self) -> bool {
let mut running = true;
self.events_loop.clone().poll_events(|ev| {
// Pass &ev to Input System
let mut inputs = self.inputs.lock().unwrap();
inputs.poll(&ev);
// Core Events
match ev {
Event::WindowEvent { event: WindowEvent::Resized(w, h), .. } => {
let mut config_ref = Arc::get_mut(&mut self.config).unwrap();
config_ref.window.resolution = [w, h];
self.renderer.resize();
},
Event::WindowEvent { event: WindowEvent::Closed, .. } => running = false,
_ => (),
};
});
running
}
/// Updates engine subsystems.
pub fn update(&mut self) {
// Update actors
self.scene.update(&self.config, &self.gfx, &self.inputs);
// Grab lock for graphics state
let gfx = self.gfx.lock().unwrap();
// Render graphics state
self.renderer.render(&gfx);
}
}
/// Creates a window builder with a given window configuration.
fn create_window(config: &WindowConfig) -> WindowBuilder {
let mut w = get_primary_monitor();
let dimensions = w.get_dimensions();
// Set resolution
let resolution = if config.resolution[0] <= MINIMUM_RESOLUTION[0] ||
config.resolution[1] <= MINIMUM_RESOLUTION[1] {
[dimensions.0, dimensions.1]
} else {
config.resolution.clone()
};
// Set fullscreen
if config.fullscreen {
for (i, monitor) in get_available_monitors().enumerate() {
if i == config.display as usize {
w = monitor;
}
}
}
// Create manager
let window_manager = WindowBuilder::new()
.with_title("CodeVR")
.with_dimensions(resolution[0], resolution[1]);
if config.fullscreen {
return window_manager.with_fullscreen(w);
}
window_manager
}