1
1
pub mod prelude {
2
- pub use crate :: { PixelsPlugin , PixelsResource , PixelsStage } ;
2
+ pub use crate :: { PixelsPlugin , PixelsResource , PixelsSet } ;
3
3
}
4
4
5
5
pub use pixels;
6
6
7
7
use bevy:: {
8
8
diagnostic:: { Diagnostic , DiagnosticId , Diagnostics } ,
9
9
prelude:: * ,
10
- window:: { WindowBackendScaleFactorChanged , WindowId , WindowResized } ,
10
+ window:: { WindowBackendScaleFactorChanged , WindowResized } ,
11
11
winit:: WinitWindows ,
12
12
} ;
13
13
use pixels:: { Pixels , SurfaceTexture } ;
@@ -16,8 +16,8 @@ use pollster::FutureExt as _;
16
16
#[ cfg( not( target_arch = "wasm32" ) ) ]
17
17
use std:: time:: Instant ;
18
18
19
- #[ derive( Debug , Hash , PartialEq , Eq , Clone , StageLabel ) ]
20
- pub enum PixelsStage {
19
+ #[ derive( SystemSet , Debug , Hash , PartialEq , Eq , Clone ) ]
20
+ pub enum PixelsSet {
21
21
Draw ,
22
22
Render ,
23
23
PostRender ,
@@ -26,7 +26,7 @@ pub enum PixelsStage {
26
26
#[ derive( Resource ) ]
27
27
pub struct PixelsResource {
28
28
pub pixels : Pixels ,
29
- pub window_id : WindowId ,
29
+ pub window : Entity ,
30
30
}
31
31
32
32
// Internal configuration resource for use in `setup` system. Users should set values on
@@ -60,25 +60,15 @@ impl Plugin for PixelsPlugin {
60
60
width : self . width ,
61
61
height : self . height ,
62
62
} )
63
- . add_stage_after (
64
- CoreStage :: PostUpdate ,
65
- PixelsStage :: Draw ,
66
- SystemStage :: parallel ( ) ,
63
+ . configure_set (
64
+ PixelsSet :: Draw
65
+ . before ( PixelsSet :: Render )
66
+ . before ( PixelsSet :: PostRender ) , // (PixelsSet::Draw, PixelsSet::Render, PixelsSet::PostRender).chain()
67
67
)
68
- . add_stage_after (
69
- PixelsStage :: Draw ,
70
- PixelsStage :: Render ,
71
- SystemStage :: parallel ( ) ,
72
- )
73
- . add_stage_after (
74
- PixelsStage :: Render ,
75
- PixelsStage :: PostRender ,
76
- SystemStage :: parallel ( ) ,
77
- )
78
- . add_startup_system_to_stage ( StartupStage :: PreStartup , Self :: setup)
68
+ . add_startup_system ( Self :: setup)
79
69
. add_system ( Self :: window_resize)
80
70
. add_system ( Self :: window_change)
81
- . add_system_to_stage ( PixelsStage :: Render , Self :: render) ;
71
+ . add_system ( Self :: render. in_set ( PixelsSet :: Render ) ) ;
82
72
}
83
73
}
84
74
@@ -90,18 +80,15 @@ impl PixelsPlugin {
90
80
mut commands : Commands ,
91
81
mut diagnostics : ResMut < Diagnostics > ,
92
82
options : Res < PixelsOptions > ,
93
- windows : Res < Windows > ,
83
+ windows : Query < ( Entity , & Window ) > ,
94
84
winit_windows : NonSend < WinitWindows > ,
95
85
) {
96
86
diagnostics. add ( Diagnostic :: new ( Self :: RENDER_TIME , "render_time" , 20 ) . with_suffix ( "s" ) ) ;
97
87
98
- let window_id = windows
99
- . get_primary ( )
100
- . expect ( "primary window not found" )
101
- . id ( ) ;
88
+ let ( window, _) = windows. get_single ( ) . expect ( "primary window not found" ) ;
102
89
103
90
let winit_window = winit_windows
104
- . get_window ( window_id )
91
+ . get_window ( window )
105
92
. expect ( "failed to get primary winit window" ) ;
106
93
107
94
let window_size = winit_window. inner_size ( ) ;
@@ -121,17 +108,19 @@ impl PixelsPlugin {
121
108
}
122
109
. expect ( "failed to create pixels" ) ;
123
110
124
- commands. insert_resource ( PixelsResource { pixels, window_id } ) ;
111
+ commands. insert_resource ( PixelsResource { pixels, window } ) ;
125
112
}
126
113
127
114
pub fn window_resize (
128
115
mut window_resized_events : EventReader < WindowResized > ,
129
116
mut resource : ResMut < PixelsResource > ,
130
- windows : Res < Windows > ,
117
+ windows : Query < & Window > ,
131
118
) {
132
119
for event in window_resized_events. iter ( ) {
133
- if event. id == resource. window_id {
134
- Self :: resize_surface_to_window ( & mut resource, & windows) ;
120
+ if event. window == resource. window {
121
+ if let Ok ( window) = windows. get ( event. window ) {
122
+ Self :: resize_surface_to_window ( & mut resource, window) ;
123
+ }
135
124
}
136
125
}
137
126
}
@@ -141,18 +130,18 @@ impl PixelsPlugin {
141
130
WindowBackendScaleFactorChanged ,
142
131
> ,
143
132
mut resource : ResMut < PixelsResource > ,
144
- windows : Res < Windows > ,
133
+ windows : Query < & Window > ,
145
134
) {
146
135
for event in window_backend_scale_factor_changed_events. iter ( ) {
147
- if event. id == resource. window_id {
148
- Self :: resize_surface_to_window ( & mut resource, & windows) ;
136
+ if event. window == resource. window {
137
+ if let Ok ( window) = windows. get ( event. window ) {
138
+ Self :: resize_surface_to_window ( & mut resource, window) ;
139
+ }
149
140
}
150
141
}
151
142
}
152
143
153
- fn resize_surface_to_window ( resource : & mut ResMut < PixelsResource > , windows : & Res < Windows > ) {
154
- let window = windows. get ( resource. window_id ) . unwrap ( ) ;
155
-
144
+ fn resize_surface_to_window ( resource : & mut ResMut < PixelsResource > , window : & Window ) {
156
145
let _ = resource
157
146
. pixels
158
147
. resize_surface ( window. physical_width ( ) , window. physical_height ( ) ) ;
0 commit comments