-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindow.rs
More file actions
126 lines (109 loc) · 3.11 KB
/
window.rs
File metadata and controls
126 lines (109 loc) · 3.11 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
//! Window construction and handle wrapper for rendering applications.
//!
//! This module wraps a `winit` window with sizing metadata and provides a
//! builder used by runtimes to create a window before constructing a
//! `RenderContext`.
use lambda_platform::winit::{
Loop,
WindowHandle,
WindowHandleBuilder,
WindowProperties,
};
use crate::events::Events;
/// Builder for windows that are used to render a frame within the
/// RenderContext.
pub struct WindowBuilder {
name: String,
dimensions: (u32, u32),
vsync: bool,
}
impl Default for WindowBuilder {
fn default() -> Self {
return Self::new();
}
}
impl WindowBuilder {
/// A new window builder will be 480x360 by default and have the name
/// "Window". After customizing the window with whatever properties your
/// application needs, you can supply it an event loop to process the
/// events that will be generated by the window.
pub fn new() -> Self {
return Self {
name: String::from("Window"),
dimensions: (480, 360),
vsync: true,
};
}
/// The name to be displayed in the title bar of the window.
pub fn with_name(mut self, name: &str) -> Self {
self.name = name.to_string();
return self;
}
/// Specify the dimensions for the window (Defaults to 480 x 360).
pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
self.dimensions = (width, height);
return self;
}
/// Request vertical sync behavior for the swapchain.
///
/// This value is consumed when building a `RenderContext` if no explicit
/// present mode is provided to `RenderContextBuilder`.
pub fn with_vsync(mut self, vsync: bool) -> Self {
self.vsync = vsync;
return self;
}
// TODO(vmarcella): Remove new call for window and construct the window directly.
pub fn build(self, event_loop: &mut Loop<Events>) -> Window {
return Window::new(
self.name.as_str(),
self.dimensions,
self.vsync,
event_loop,
);
}
}
/// Window implementation for rendering applications.
pub struct Window {
window_handle: WindowHandle,
vsync: bool,
}
impl Window {
fn new(
name: &str,
dimensions: (u32, u32),
vsync: bool,
event_loop: &mut Loop<Events>,
) -> Self {
let window_properties = WindowProperties {
name: name.to_string(),
dimensions,
};
let window_handle = WindowHandleBuilder::new()
.with_window_properties(window_properties, event_loop)
.build();
logging::debug!("Created window: {}", name);
return Self {
window_handle,
vsync,
};
}
/// Redraws the window.
pub fn redraw(&self) {
self.window_handle.window_handle.request_redraw();
}
/// Returns the window handle.
pub fn window_handle(&self) -> &WindowHandle {
return &self.window_handle;
}
/// Returns the dimensions of the current window. (width, height)
pub fn dimensions(&self) -> (u32, u32) {
return (
self.window_handle.size.width,
self.window_handle.size.height,
);
}
/// Returns the requested vertical sync preference for presentation.
pub fn vsync_requested(&self) -> bool {
return self.vsync;
}
}