multi_window/
multi_window.rs1use astrelis_core::logging;
12use astrelis_render::{
13 GraphicsContext, RenderPassBuilder, RenderTarget, RenderableWindow, WindowContextDescriptor,
14};
15use astrelis_winit::{
16 WindowId,
17 app::run_app,
18 event::PhysicalSize,
19 window::{WindowBackend, WindowDescriptor},
20};
21use std::collections::HashMap;
22
23struct App {
24 context: &'static GraphicsContext,
25 windows: HashMap<WindowId, (RenderableWindow, wgpu::Color)>,
26}
27
28fn main() {
29 logging::init();
30
31 run_app(|ctx| {
32 let graphics_ctx = GraphicsContext::new_sync();
33
34 let mut windows = HashMap::new();
35
36 let colors = [
38 wgpu::Color {
39 r: 0.8,
40 g: 0.2,
41 b: 0.2,
42 a: 1.0,
43 },
44 wgpu::Color {
45 r: 0.2,
46 g: 0.8,
47 b: 0.2,
48 a: 1.0,
49 },
50 wgpu::Color {
51 r: 0.2,
52 g: 0.2,
53 b: 0.8,
54 a: 1.0,
55 },
56 ];
57
58 for (i, color) in colors.iter().enumerate() {
59 let window = ctx
60 .create_window(WindowDescriptor {
61 title: format!("Window {} - Multi-Window Example", i + 1),
62 size: Some(PhysicalSize::new(400.0, 300.0)),
63 ..Default::default()
64 })
65 .expect("Failed to create window");
66
67 let renderable_window = RenderableWindow::new_with_descriptor(
68 window,
69 graphics_ctx,
70 WindowContextDescriptor {
71 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
72 ..Default::default()
73 },
74 );
75
76 let window_id = renderable_window.id();
77 windows.insert(window_id, (renderable_window, *color));
78 }
79
80 Box::new(App {
81 context: graphics_ctx,
82 windows,
83 })
84 });
85}
86
87impl astrelis_winit::app::App for App {
88 fn update(&mut self, _ctx: &mut astrelis_winit::app::AppCtx) {
89 }
92
93 fn render(
94 &mut self,
95 _ctx: &mut astrelis_winit::app::AppCtx,
96 window_id: WindowId,
97 events: &mut astrelis_winit::event::EventBatch,
98 ) {
99 let Some((window, color)) = self.windows.get_mut(&window_id) else {
101 return;
102 };
103
104 events.dispatch(|event| {
106 if let astrelis_winit::event::Event::WindowResized(size) = event {
107 window.resized(*size);
108 astrelis_winit::event::HandleStatus::consumed()
109 } else {
110 astrelis_winit::event::HandleStatus::ignored()
111 }
112 });
113
114 let mut frame = window.begin_drawing();
116
117 {
118 let _render_pass = RenderPassBuilder::new()
119 .label("Multi-Window Render Pass")
120 .target(RenderTarget::Surface)
121 .clear_color(*color)
122 .build(&mut frame);
123 }
124
125 frame.finish();
126 }
127}