@@ -19,6 +19,7 @@ use std::path::Path;
19
19
use std:: rc:: Rc ;
20
20
use std:: sync:: Arc ;
21
21
use std:: sync:: Mutex ;
22
+ use std:: ops:: DerefMut ;
22
23
23
24
#[ derive( PartialEq ) ]
24
25
enum RunMode {
@@ -51,8 +52,45 @@ fn usage(exe_name: &str) {
51
52
println ! ( " b - draw the bounding boxes." ) ;
52
53
}
53
54
54
- pub struct Testbed {
55
+ /// This trait is designed to allow choosing implementation of underlying storing of World: shared between threads or owned only by WorldOwner.
56
+ pub trait WorldOwner {
57
+ fn get_mut < ' a : ' b , ' b > ( & ' a mut self ) -> Box < DerefMut < Target = World < f32 > > + ' b > ;
58
+ }
59
+
60
+ #[ derive( Clone ) ]
61
+ pub struct WorldOwnerShared {
55
62
world : Arc < Mutex < World < f32 > > > ,
63
+ }
64
+
65
+ impl WorldOwnerShared {
66
+ pub fn new ( w : World < f32 > ) -> WorldOwnerShared {
67
+ WorldOwnerShared { world : Arc :: new ( Mutex :: new ( w) ) }
68
+ }
69
+ }
70
+
71
+ impl WorldOwner for WorldOwnerShared {
72
+ fn get_mut < ' a : ' b , ' b > ( & ' a mut self ) -> Box < DerefMut < Target = World < f32 > > + ' b > {
73
+ Box :: new ( self . world . lock ( ) . unwrap ( ) )
74
+ }
75
+ }
76
+
77
+ pub struct WorldOwnerExclusive {
78
+ world : World < f32 >
79
+ }
80
+
81
+ impl WorldOwnerExclusive {
82
+ pub fn new ( world : World < f32 > ) -> WorldOwnerExclusive {
83
+ WorldOwnerExclusive { world}
84
+ }
85
+ }
86
+
87
+ impl WorldOwner for WorldOwnerExclusive {
88
+ fn get_mut < ' a : ' b , ' b > ( & ' a mut self ) -> Box < DerefMut < Target = World < f32 > > + ' b > {
89
+ Box :: new ( & mut self . world )
90
+ }
91
+ }
92
+
93
+ pub struct Testbed {
56
94
window : Option < Box < Window > > ,
57
95
graphics : GraphicsManager ,
58
96
nsteps : usize ,
@@ -67,6 +105,7 @@ pub struct Testbed {
67
105
cursor_pos : Point2 < f32 > ,
68
106
grabbed_object : Option < BodyHandle > ,
69
107
grabbed_object_constraint : Option < ConstraintHandle > ,
108
+ world : Box < WorldOwner > ,
70
109
}
71
110
72
111
impl Testbed {
@@ -79,7 +118,7 @@ impl Testbed {
79
118
window. set_framerate_limit ( Some ( 60 ) ) ;
80
119
81
120
Testbed {
82
- world : Arc :: new ( Mutex :: new ( world) ) ,
121
+ world : Box :: new ( WorldOwnerShared :: new ( world) ) ,
83
122
callbacks : Vec :: new ( ) ,
84
123
window : Some ( window) ,
85
124
graphics : graphics,
@@ -97,7 +136,7 @@ impl Testbed {
97
136
}
98
137
}
99
138
100
- pub fn new ( world : Arc < Mutex < World < f32 > > > ) -> Testbed {
139
+ pub fn new ( world : Box < WorldOwner > ) -> Testbed {
101
140
let mut res = Testbed :: new_empty ( ) ;
102
141
103
142
res. set_world ( world) ;
@@ -117,9 +156,9 @@ impl Testbed {
117
156
self . hide_counters = false ;
118
157
}
119
158
120
- pub fn set_world ( & mut self , world : Arc < Mutex < World < f32 > > > ) {
159
+ pub fn set_world ( & mut self , world : Box < WorldOwner > ) {
121
160
self . world = world;
122
- let mut world = self . world . lock ( ) . unwrap ( ) ;
161
+ let mut world = self . world . get_mut ( ) ;
123
162
world. enable_performance_counters ( ) ;
124
163
125
164
self . graphics . clear ( self . window . as_mut ( ) . unwrap ( ) ) ;
@@ -142,9 +181,9 @@ impl Testbed {
142
181
self . graphics . set_collider_color ( collider, color) ;
143
182
}
144
183
145
- /* pub fn world(&self) -> &World<f32 > {
146
- &self.world.borrow_mut()
147
- }*/
184
+ pub fn world ( & self ) -> & Box < WorldOwner > {
185
+ & self . world
186
+ }
148
187
149
188
pub fn graphics_mut ( & mut self ) -> & mut GraphicsManager {
150
189
& mut self . graphics
@@ -237,7 +276,7 @@ impl State for Testbed {
237
276
// graphics.add(window, WorldObject::RigidBody(body));
238
277
// },
239
278
WindowEvent :: MouseButton ( _, Action :: Press , modifier) => {
240
- let mut physics_world = & mut self . world . lock ( ) . unwrap ( ) ;
279
+ let mut physics_world = & mut self . world . get_mut ( ) ;
241
280
let mapped_point = self
242
281
. graphics
243
282
. camera ( )
@@ -310,7 +349,7 @@ impl State for Testbed {
310
349
}
311
350
}
312
351
WindowEvent :: MouseButton ( _, Action :: Release , _) => {
313
- let mut physics_world = & mut self . world . lock ( ) . unwrap ( ) ;
352
+ let mut physics_world = & mut self . world . get_mut ( ) ;
314
353
if let Some ( body) = self . grabbed_object {
315
354
for n in self
316
355
. graphics
@@ -330,7 +369,7 @@ impl State for Testbed {
330
369
self . grabbed_object_constraint = None ;
331
370
}
332
371
WindowEvent :: CursorPos ( x, y, modifiers) => {
333
- let mut physics_world = & mut self . world . lock ( ) . unwrap ( ) ;
372
+ let mut physics_world = & mut self . world . get_mut ( ) ;
334
373
self . cursor_pos . x = x as f32 ;
335
374
self . cursor_pos . y = y as f32 ;
336
375
@@ -371,7 +410,7 @@ impl State for Testbed {
371
410
// // }
372
411
// },
373
412
WindowEvent :: Key ( Key :: Space , Action :: Release , _) => {
374
- let mut physics_world = & mut self . world . lock ( ) . unwrap ( ) ;
413
+ let mut physics_world = & mut self . world . get_mut ( ) ;
375
414
self . draw_colls = !self . draw_colls ;
376
415
for co in physics_world. colliders ( ) {
377
416
// FIXME: ugly clone.
@@ -443,13 +482,13 @@ impl State for Testbed {
443
482
for f in & self . callbacks {
444
483
f ( & mut self . graphics , self . time )
445
484
}
446
- self . world . lock ( ) . unwrap ( ) . step ( ) ;
485
+ self . world . get_mut ( ) . step ( ) ;
447
486
if !self . hide_counters {
448
- println ! ( "{}" , self . world. lock ( ) . unwrap ( ) . performance_counters( ) ) ;
487
+ println ! ( "{}" , self . world. get_mut ( ) . performance_counters( ) ) ;
449
488
}
450
- self . time += self . world . lock ( ) . unwrap ( ) . timestep ( ) ;
489
+ self . time += self . world . get_mut ( ) . timestep ( ) ;
451
490
}
452
- let mut physics_world = & mut self . world . lock ( ) . unwrap ( ) ;
491
+ let physics_world = & self . world . get_mut ( ) ;
453
492
454
493
for co in physics_world. colliders ( ) {
455
494
if self . graphics . body_nodes_mut ( physics_world, co. data ( ) . body ( ) ) . is_none ( ) {
@@ -463,7 +502,7 @@ impl State for Testbed {
463
502
if self . draw_colls {
464
503
draw_collisions (
465
504
window,
466
- & mut self . world . lock ( ) . unwrap ( ) ,
505
+ & mut self . world . get_mut ( ) ,
467
506
& mut self . persistant_contacts ,
468
507
self . running != RunMode :: Stop ,
469
508
) ;
@@ -481,7 +520,7 @@ impl State for Testbed {
481
520
& format ! (
482
521
"Simulation time: {:.*}sec." ,
483
522
4 ,
484
- self . world. lock ( ) . unwrap ( ) . performance_counters( ) . step_time( ) ,
523
+ self . world. get_mut ( ) . performance_counters( ) . step_time( ) ,
485
524
) [ ..] ,
486
525
& Point2 :: origin ( ) ,
487
526
60.0 ,
0 commit comments