@@ -4,24 +4,27 @@ use crate::error::Result;
44use bevy:: app:: { App , AppExit } ;
55use bevy:: log:: tracing_subscriber;
66use bevy:: prelude:: * ;
7- use bevy:: window:: {
8- RawHandleWrapper , Window , WindowResolution , WindowWrapper ,
9- } ;
7+ use bevy:: window:: { RawHandleWrapper , Window , WindowRef , WindowResolution , WindowWrapper } ;
108use raw_window_handle:: {
119 DisplayHandle , HandleError , HasDisplayHandle , HasWindowHandle , RawDisplayHandle ,
1210 RawWindowHandle , WindowHandle ,
1311} ;
1412use std:: cell:: RefCell ;
1513use std:: num:: NonZero ;
14+ use std:: sync:: atomic:: AtomicU32 ;
1615use std:: sync:: OnceLock ;
16+ use bevy:: camera:: RenderTarget ;
17+ use bevy:: camera:: visibility:: RenderLayers ;
1718use tracing:: debug;
1819
1920static IS_INIT : OnceLock < ( ) > = OnceLock :: new ( ) ;
21+ static WINDOW_COUNT : AtomicU32 = AtomicU32 :: new ( 0 ) ;
2022
2123thread_local ! {
2224 static APP : OnceLock <RefCell <App >> = OnceLock :: default ( ) ;
2325}
2426
27+
2528fn app < T > ( cb : impl FnOnce ( & App ) -> Result < T > ) -> Result < T > {
2629 let res = APP . with ( |app_lock| {
2730 let app = app_lock
@@ -144,7 +147,7 @@ pub fn create_surface(
144147 let handle_wrapper = RawHandleWrapper :: new ( & window_wrapper) ?;
145148
146149 let entity_id = app_mut ( |app| {
147- let entity = app
150+ let mut window = app
148151 . world_mut ( )
149152 . spawn ( (
150153 Window {
@@ -153,22 +156,44 @@ pub fn create_surface(
153156 ..default ( )
154157 } ,
155158 handle_wrapper,
156- ) )
157- . id ( ) ;
159+ ) ) ;
160+
161+ let count = WINDOW_COUNT . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: SeqCst ) ;
162+ let render_layer = RenderLayers :: none ( ) . with ( count as usize ) ;
158163
159- // TODO: spawn a camera for this window with a render target of this window
164+ let window_entity = window. id ( ) ;
165+ window. with_children ( |parent| {
166+ parent. spawn ( (
167+ Camera3d :: default ( ) ,
168+ Camera {
169+ target : RenderTarget :: Window ( WindowRef :: Entity ( window_entity) ) ,
170+ ..default ( )
171+ } ,
172+ Projection :: Orthographic ( OrthographicProjection :: default_3d ( ) ) ,
173+ render_layer,
174+ ) ) ;
175+ } ) ;
160176
161- Ok ( entity . to_bits ( ) )
177+ Ok ( window_entity . to_bits ( ) )
162178 } ) ?;
163179
164180 Ok ( entity_id)
165181}
166182
183+ pub fn destroy_surface ( window_entity : Entity ) -> Result < ( ) > {
184+ app_mut ( |app| {
185+ if app. world_mut ( ) . get :: < Window > ( window_entity) . is_some ( ) {
186+ app. world_mut ( ) . despawn ( window_entity) ;
187+ WINDOW_COUNT . fetch_sub ( 1 , std:: sync:: atomic:: Ordering :: SeqCst ) ;
188+ }
189+ Ok ( ( ) )
190+ } )
191+ }
192+
167193/// Update window size when resized.
168- pub fn window_resized ( window_id : u64 , width : u32 , height : u32 ) -> Result < ( ) > {
194+ pub fn resize_surface ( window_entity : Entity , width : u32 , height : u32 ) -> Result < ( ) > {
169195 app_mut ( |app| {
170- let entity = Entity :: from_bits ( window_id) ;
171- if let Some ( mut window) = app. world_mut ( ) . get_mut :: < Window > ( entity) {
196+ if let Some ( mut window) = app. world_mut ( ) . get_mut :: < Window > ( window_entity) {
172197 window. resolution . set_physical_resolution ( width, height) ;
173198 Ok ( ( ) )
174199 } else {
@@ -240,6 +265,18 @@ pub fn exit(exit_code: u8) -> Result<()> {
240265 } )
241266}
242267
268+ pub fn background_color ( window_entity : Entity , color : Color ) -> Result < ( ) > {
269+ app_mut ( |app| {
270+ let mut camera_query = app. world_mut ( ) . query :: < ( & mut Camera , & ChildOf ) > ( ) ;
271+ for ( mut camera, parent) in camera_query. iter_mut ( & mut app. world_mut ( ) ) {
272+ if parent. parent ( ) == window_entity {
273+ camera. clear_color = ClearColorConfig :: Custom ( color) ;
274+ }
275+ }
276+ Ok ( ( ) )
277+ } )
278+ }
279+
243280fn setup_tracing ( ) -> Result < ( ) > {
244281 let subscriber = tracing_subscriber:: FmtSubscriber :: new ( ) ;
245282 tracing:: subscriber:: set_global_default ( subscriber) ?;
0 commit comments