Skip to content

Commit 0658b1c

Browse files
committed
Change window creation api
1 parent 15c5c8a commit 0658b1c

File tree

14 files changed

+140
-135
lines changed

14 files changed

+140
-135
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ Example:
1515

1616
```rust
1717
fn main() {
18-
Glass::run(GlassConfig::default(), |_| Box::new(YourApp))
18+
Glass::run(GlassConfig::default(), |context| {
19+
// Create window if relevant
20+
// context.create_window(WindowConfig {
21+
// width: 1920,
22+
// height: 1080,
23+
// exit_on_esc: true,
24+
// ..WindowConfig::default()
25+
// });
26+
Box::new(YourApp)
27+
})
1928
}
2029

2130
// Organize your app in anyway you like

examples/egui_gui.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ use egui_demo_lib::DemoWindows;
33
use egui_wgpu::ScreenDescriptor;
44
use egui_winit::EventResponse;
55
use glass::{
6-
window::GlassWindow, Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
6+
window::{GlassWindow, WindowConfig},
7+
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
78
};
89
use wgpu::{CommandBuffer, CommandEncoder, StoreOp, TextureView};
910
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::WindowId};
1011

1112
fn main() -> Result<(), GlassError> {
12-
Glass::run(GlassConfig::default(), |_| {
13+
Glass::run(GlassConfig::default(), |context| {
14+
context.create_window(WindowConfig {
15+
width: 1920,
16+
height: 1080,
17+
exit_on_esc: true,
18+
..WindowConfig::default()
19+
});
1320
Box::new(GuiApp {
1421
gui: None,
1522
})

examples/game_of_life/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ fn config() -> GlassConfig {
4747
instance_flags: InstanceFlags::from_build_config(),
4848
trace_path: None,
4949
},
50-
window_configs: vec![WindowConfig {
50+
}
51+
}
52+
53+
fn main() -> Result<(), GlassError> {
54+
Glass::run(config(), |context| {
55+
context.create_window(WindowConfig {
5156
width: WIDTH,
5257
height: HEIGHT,
5358
exit_on_esc: true,
5459
present_mode: PresentMode::AutoNoVsync,
5560
..WindowConfig::default()
56-
}],
57-
}
58-
}
59-
60-
fn main() -> Result<(), GlassError> {
61-
Glass::run(config(), |_| Box::new(GameOfLifeApp::default()))
61+
});
62+
Box::new(GameOfLifeApp::default())
63+
})
6264
}
6365

6466
// Think of this like reading a "table of contents".

examples/hello_world.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
use glass::{Glass, GlassApp, GlassConfig, GlassError};
1+
use glass::{window::WindowConfig, Glass, GlassApp, GlassConfig, GlassError};
22

33
fn main() -> Result<(), GlassError> {
4-
Glass::run(GlassConfig::default(), |_| Box::new(HelloWorld))
4+
Glass::run(GlassConfig::default(), |context| {
5+
context.create_window(WindowConfig {
6+
width: 1920,
7+
height: 1080,
8+
exit_on_esc: true,
9+
..WindowConfig::default()
10+
});
11+
Box::new(HelloWorld)
12+
})
513
}
614

715
struct HelloWorld;

examples/lines.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use glass::{
66
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
77
};
88
use rapier2d::prelude::*;
9-
use wgpu::{util::DeviceExt, Buffer, CommandBuffer, Features, Limits, StoreOp};
9+
use wgpu::{util::DeviceExt, Buffer, CommandBuffer, Features, Limits, PresentMode, StoreOp};
1010
use winit::event_loop::ActiveEventLoop;
1111

1212
const WIDTH: u32 = 1920;
@@ -25,17 +25,20 @@ fn config() -> GlassConfig {
2525
features: Features::POLYGON_MODE_LINE,
2626
..DeviceConfig::default()
2727
},
28-
window_configs: vec![WindowConfig {
29-
width: WIDTH,
30-
height: HEIGHT,
31-
exit_on_esc: true,
32-
..WindowConfig::default()
33-
}],
3428
}
3529
}
3630

3731
fn main() -> Result<(), GlassError> {
38-
Glass::run(config(), |context| Box::new(LineApp::new(context)))
32+
Glass::run(config(), |context| {
33+
context.create_window(WindowConfig {
34+
width: WIDTH,
35+
height: HEIGHT,
36+
present_mode: PresentMode::AutoVsync,
37+
exit_on_esc: true,
38+
..WindowConfig::default()
39+
});
40+
Box::new(LineApp::new(context))
41+
})
3942
}
4043

4144
struct LineApp {

examples/multiple_windows/main.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const WIDTH: u32 = 256;
1313
const HEIGHT: u32 = 256;
1414

1515
fn main() -> Result<(), GlassError> {
16-
Glass::run(GlassConfig::windowless(), |_| {
16+
Glass::run(GlassConfig::default(), |_| {
1717
Box::new(MultiWindowApp::default())
1818
})
1919
}
@@ -28,29 +28,23 @@ const CLEAR_COLORS: [Color; 5] = [
2828

2929
/// Example buffer data etc.
3030
#[derive(Default)]
31-
struct MultiWindowApp {
32-
pub window_ids: Vec<WindowId>,
33-
}
31+
struct MultiWindowApp;
3432

3533
impl GlassApp for MultiWindowApp {
36-
fn start(&mut self, event_loop: &ActiveEventLoop, context: &mut GlassContext) {
34+
fn start(&mut self, _event_loop: &ActiveEventLoop, context: &mut GlassContext) {
3735
println!("Press space to create windows, esc to close all but last");
38-
self.window_ids.push(
39-
context
40-
.create_window(event_loop, WindowConfig {
41-
width: WIDTH,
42-
height: HEIGHT,
43-
exit_on_esc: true,
44-
..WindowConfig::default()
45-
})
46-
.unwrap(),
47-
);
36+
context.create_window(WindowConfig {
37+
width: WIDTH,
38+
height: HEIGHT,
39+
exit_on_esc: true,
40+
..WindowConfig::default()
41+
});
4842
}
4943

5044
fn window_input(
5145
&mut self,
5246
context: &mut GlassContext,
53-
event_loop: &ActiveEventLoop,
47+
_event_loop: &ActiveEventLoop,
5448
_window_id: WindowId,
5549
event: &WindowEvent,
5650
) {
@@ -64,19 +58,15 @@ impl GlassApp for MultiWindowApp {
6458
{
6559
println!("Key: {:?}", event);
6660
if event.physical_key == PhysicalKey::Code(KeyCode::Space)
67-
&& event.state == ElementState::Pressed
61+
&& event.state == ElementState::Released
6862
{
6963
// Create window - this will work when your window has focus
70-
self.window_ids.push(
71-
context
72-
.create_window(event_loop, WindowConfig {
73-
width: WIDTH,
74-
height: HEIGHT,
75-
exit_on_esc: true,
76-
..WindowConfig::default()
77-
})
78-
.unwrap(),
79-
);
64+
context.create_window(WindowConfig {
65+
width: WIDTH,
66+
height: HEIGHT,
67+
exit_on_esc: true,
68+
..WindowConfig::default()
69+
});
8070
}
8171
}
8272
}

examples/quad/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ const OPENGL_TO_WGPU: glam::Mat4 = glam::Mat4::from_cols_array(&[
2020
]);
2121

2222
fn main() -> Result<(), GlassError> {
23-
Glass::run(config(), |_| Box::new(TreeApp::default()))
23+
Glass::run(config(), |context| {
24+
context.create_window(WindowConfig {
25+
width: WIDTH,
26+
height: HEIGHT,
27+
exit_on_esc: true,
28+
..WindowConfig::default()
29+
});
30+
Box::new(TreeApp::default())
31+
})
2432
}
2533

2634
fn config() -> GlassConfig {
@@ -33,12 +41,6 @@ fn config() -> GlassConfig {
3341
},
3442
..DeviceConfig::performance()
3543
},
36-
window_configs: vec![WindowConfig {
37-
width: WIDTH,
38-
height: HEIGHT,
39-
exit_on_esc: true,
40-
..WindowConfig::default()
41-
}],
4244
}
4345
}
4446

examples/sand/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ const CANVAS_SCALE: u32 = 2;
2626

2727
fn main() -> Result<(), GlassError> {
2828
Glass::run(config(), |context| {
29+
context.create_window(WindowConfig {
30+
width: CANVAS_SIZE * CANVAS_SCALE,
31+
height: CANVAS_SIZE * CANVAS_SCALE,
32+
present_mode: PresentMode::AutoNoVsync,
33+
exit_on_esc: true,
34+
..WindowConfig::default()
35+
});
2936
Box::new(SandSim::new(context)) as Box<dyn GlassApp>
3037
})
3138
}
@@ -238,12 +245,5 @@ fn config() -> GlassConfig {
238245
},
239246
..DeviceConfig::default()
240247
},
241-
window_configs: vec![WindowConfig {
242-
width: CANVAS_SIZE * CANVAS_SCALE,
243-
height: CANVAS_SIZE * CANVAS_SCALE,
244-
present_mode: PresentMode::AutoNoVsync,
245-
exit_on_esc: true,
246-
..WindowConfig::default()
247-
}],
248248
}
249249
}

examples/shader_with_includes/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{borrow::Cow, path::PathBuf};
22

33
use glass::{
44
utils::{ShaderModule, WatchedShaderModule},
5-
window::GlassWindow,
5+
window::{GlassWindow, WindowConfig},
66
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
77
};
88
use wgpu::{
@@ -12,7 +12,15 @@ use wgpu::{
1212
use winit::event_loop::ActiveEventLoop;
1313

1414
fn main() -> Result<(), GlassError> {
15-
Glass::run(GlassConfig::default(), |_| Box::new(TriangleApp::default()))
15+
Glass::run(GlassConfig::default(), |context| {
16+
context.create_window(WindowConfig {
17+
width: 1920,
18+
height: 1080,
19+
exit_on_esc: true,
20+
..WindowConfig::default()
21+
});
22+
Box::new(TriangleApp::default())
23+
})
1624
}
1725

1826
#[derive(Default)]

examples/triangle/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::borrow::Cow;
22

33
use glass::{
4-
window::GlassWindow, Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
4+
window::{GlassWindow, WindowConfig},
5+
Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
56
};
67
use wgpu::{
78
CommandBuffer, MultisampleState, PipelineLayoutDescriptor, PrimitiveState, RenderPipeline,
@@ -10,7 +11,15 @@ use wgpu::{
1011
use winit::event_loop::ActiveEventLoop;
1112

1213
fn main() -> Result<(), GlassError> {
13-
Glass::run(GlassConfig::default(), |_| Box::new(TriangleApp::default()))
14+
Glass::run(GlassConfig::default(), |context| {
15+
context.create_window(WindowConfig {
16+
width: 1920,
17+
height: 1080,
18+
exit_on_esc: true,
19+
..WindowConfig::default()
20+
});
21+
Box::new(TriangleApp::default())
22+
})
1423
}
1524

1625
#[derive(Default)]

0 commit comments

Comments
 (0)