@@ -4,34 +4,43 @@ use std::sync::Mutex;
4
4
use winit:: application:: ApplicationHandler ;
5
5
use winit:: event:: WindowEvent ;
6
6
use winit:: event_loop:: ActiveEventLoop ;
7
+ use winit:: event_loop:: EventLoopProxy ;
7
8
use winit:: window:: Window ;
8
9
use winit:: window:: WindowId ;
9
10
10
- use crate :: gpu:: Gpu ;
11
- use crate :: gpu:: Instance ;
11
+ use crate :: graphics:: Gpu ;
12
+ use crate :: graphics:: Instance ;
13
+ use crate :: graphics:: Rect ;
12
14
13
- #[ derive( Debug , Clone , Copy ) ]
14
- pub enum JsEvents {
15
- AddRect ( u32 , u32 , u32 , u32 ) ,
15
+ #[ derive( Debug ) ]
16
+ pub enum Js {
17
+ RectsUpdated ,
16
18
}
17
19
18
- #[ derive( Copy , Clone , Debug ) ]
19
- pub struct Rect ( u32 , u32 , u32 , u32 ) ;
20
-
21
20
#[ derive( Debug , Clone ) ]
22
21
pub struct AppState {
23
- rects : Vec < Rect > ,
22
+ pub rects : Arc < Mutex < Vec < Arc < Mutex < Rect > > > > > ,
23
+ pub event_loop : Arc < Mutex < EventLoopProxy < Js > > > ,
24
+ }
25
+
26
+ impl AppState {
27
+ pub fn new ( event_loop : Arc < Mutex < EventLoopProxy < Js > > > ) -> Self {
28
+ Self {
29
+ rects : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
30
+ event_loop,
31
+ }
32
+ }
24
33
}
25
34
26
35
pub struct App < ' window > {
27
36
window : Option < Arc < Window > > ,
28
37
gpu : Option < Gpu < ' window > > ,
29
- state : Arc < Mutex < AppState > > ,
38
+ pub state : Arc < Mutex < AppState > > ,
30
39
}
31
40
32
41
impl App < ' _ > {
33
- pub fn new ( ) -> Self {
34
- let state = Arc :: new ( Mutex :: new ( AppState { rects : Vec :: new ( ) } ) ) ;
42
+ pub fn new ( event_loop : Arc < Mutex < EventLoopProxy < Js > > > ) -> Self {
43
+ let state = Arc :: new ( Mutex :: new ( AppState :: new ( event_loop ) ) ) ;
35
44
36
45
Self {
37
46
window : None ,
@@ -40,50 +49,52 @@ impl App<'_> {
40
49
}
41
50
}
42
51
43
- pub fn add_rect ( & mut self , x : u32 , y : u32 , w : u32 , h : u32 ) {
44
- self . state . lock ( ) . unwrap ( ) . rects . push ( Rect ( x, y, w, h) ) ;
45
- self . sync_gpu_instance_buffer ( ) ;
46
-
47
- if let Some ( window) = self . window . as_ref ( ) {
48
- window. request_redraw ( ) ;
49
- }
50
- }
51
-
52
52
fn rects_to_instances ( & self ) -> Vec < Instance > {
53
53
self . state
54
54
. lock ( )
55
55
. unwrap ( )
56
56
. rects
57
+ . lock ( )
58
+ . unwrap ( )
57
59
. iter ( )
58
- . map ( |r| Instance :: new ( r. 0 as f32 , r. 1 as f32 , r. 2 as f32 , r. 3 as f32 ) )
60
+ . map ( |r| {
61
+ let rect = r. lock ( ) . unwrap ( ) ;
62
+ Instance :: new ( rect. 0 as f32 , rect. 1 as f32 , rect. 2 as f32 , rect. 3 as f32 )
63
+ } )
59
64
. collect ( )
60
65
}
61
-
62
- fn sync_gpu_instance_buffer ( & mut self ) {
63
- let instances = self . rects_to_instances ( ) ;
64
- if let Some ( gpu) = self . gpu . as_mut ( ) {
65
- gpu. update_instance_buffer ( & instances) ;
66
- }
67
- }
68
66
}
69
67
70
- impl < ' window > ApplicationHandler < JsEvents > for App < ' window > {
68
+ impl < ' window > ApplicationHandler < Js > for App < ' window > {
71
69
fn resumed ( & mut self , event_loop : & ActiveEventLoop ) {
72
70
if self . window . is_none ( ) {
73
71
let window = Arc :: new (
74
72
event_loop
75
- . create_window ( Window :: default_attributes ( ) . with_title ( "wgpu winit example" ) )
73
+ . create_window (
74
+ Window :: default_attributes ( )
75
+ . with_position ( winit:: dpi:: PhysicalPosition :: new ( 100 , 200 ) )
76
+ . with_title ( "wgpu winit example" ) ,
77
+ )
76
78
. expect ( "create window err." ) ,
77
79
) ;
78
80
79
81
self . window = Some ( window. clone ( ) ) ;
80
- self . gpu = Some ( Gpu :: new ( window. clone ( ) , self . rects_to_instances ( ) ) ) ;
82
+ self . gpu = Some ( Gpu :: new ( window. clone ( ) ) ) ;
81
83
}
82
84
}
83
85
84
- fn user_event ( & mut self , _event_loop : & ActiveEventLoop , event : JsEvents ) {
85
- let JsEvents :: AddRect ( x, y, w, h) = event;
86
- self . add_rect ( x, y, w, h) ;
86
+ fn user_event ( & mut self , _event_loop : & ActiveEventLoop , event : Js ) {
87
+ match event {
88
+ Js :: RectsUpdated => {
89
+ let instances = self . rects_to_instances ( ) ;
90
+ if let Some ( gpu) = self . gpu . as_mut ( ) {
91
+ gpu. update_instance_buffer ( & instances) ;
92
+ }
93
+ if let Some ( window) = self . window . as_ref ( ) {
94
+ window. request_redraw ( ) ;
95
+ }
96
+ }
97
+ }
87
98
}
88
99
89
100
fn window_event ( & mut self , event_loop : & ActiveEventLoop , _id : WindowId , event : WindowEvent ) {
0 commit comments