Skip to content

Commit 098927c

Browse files
authored
Merge pull request #1 from Vrixyz/131-testbed-world-owner
131 testbed world owner
2 parents 73c93cf + 13bcce9 commit 098927c

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

examples2d/balls2.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use nphysics2d::object::{BodyHandle, Material};
99
use nphysics2d::volumetric::Volumetric;
1010
use nphysics2d::world::World;
1111
use nphysics_testbed2d::Testbed;
12-
use std::sync::Arc;
13-
use std::sync::Mutex;
12+
use nphysics_testbed2d::WorldOwnerExclusive;
1413

1514
const COLLIDER_MARGIN: f32 = 0.01;
1615

@@ -82,7 +81,7 @@ fn main() {
8281
/*
8382
* Set up the testbed.
8483
*/
85-
let mut testbed = Testbed::new(Arc::new(Mutex::new(world)));
84+
let mut testbed = Testbed::new(Box::new(WorldOwnerExclusive::new(world)));
8685
testbed.look_at(Point2::new(0.0, -2.5), 95.0);
8786
testbed.run();
8887
}

nphysics_testbed2d/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ extern crate time;
88

99
pub use engine::GraphicsManager;
1010
pub use testbed::Testbed;
11+
pub use testbed::WorldOwner;
12+
pub use testbed::WorldOwnerShared;
13+
pub use testbed::WorldOwnerExclusive;
1114

1215
mod engine;
1316
pub mod objects;

nphysics_testbed2d/src/testbed.rs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::path::Path;
1919
use std::rc::Rc;
2020
use std::sync::Arc;
2121
use std::sync::Mutex;
22+
use std::ops::DerefMut;
2223

2324
#[derive(PartialEq)]
2425
enum RunMode {
@@ -51,8 +52,45 @@ fn usage(exe_name: &str) {
5152
println!(" b - draw the bounding boxes.");
5253
}
5354

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 {
5562
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 {
5694
window: Option<Box<Window>>,
5795
graphics: GraphicsManager,
5896
nsteps: usize,
@@ -67,6 +105,7 @@ pub struct Testbed {
67105
cursor_pos: Point2<f32>,
68106
grabbed_object: Option<BodyHandle>,
69107
grabbed_object_constraint: Option<ConstraintHandle>,
108+
world: Box<WorldOwner>,
70109
}
71110

72111
impl Testbed {
@@ -79,7 +118,7 @@ impl Testbed {
79118
window.set_framerate_limit(Some(60));
80119

81120
Testbed {
82-
world: Arc::new(Mutex::new(world)),
121+
world: Box::new(WorldOwnerShared::new(world)),
83122
callbacks: Vec::new(),
84123
window: Some(window),
85124
graphics: graphics,
@@ -97,7 +136,7 @@ impl Testbed {
97136
}
98137
}
99138

100-
pub fn new(world: Arc<Mutex<World<f32>>>) -> Testbed {
139+
pub fn new(world: Box<WorldOwner>) -> Testbed {
101140
let mut res = Testbed::new_empty();
102141

103142
res.set_world(world);
@@ -117,9 +156,9 @@ impl Testbed {
117156
self.hide_counters = false;
118157
}
119158

120-
pub fn set_world(&mut self, world: Arc<Mutex<World<f32>>>) {
159+
pub fn set_world(&mut self, world: Box<WorldOwner>) {
121160
self.world = world;
122-
let mut world = self.world.lock().unwrap();
161+
let mut world = self.world.get_mut();
123162
world.enable_performance_counters();
124163

125164
self.graphics.clear(self.window.as_mut().unwrap());
@@ -142,9 +181,9 @@ impl Testbed {
142181
self.graphics.set_collider_color(collider, color);
143182
}
144183

145-
/*pub fn world(&self) -> &World<f32> {
146-
&self.world.borrow_mut()
147-
}*/
184+
pub fn world(&self) -> &Box<WorldOwner> {
185+
&self.world
186+
}
148187

149188
pub fn graphics_mut(&mut self) -> &mut GraphicsManager {
150189
&mut self.graphics
@@ -237,7 +276,7 @@ impl State for Testbed {
237276
// graphics.add(window, WorldObject::RigidBody(body));
238277
// },
239278
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();
241280
let mapped_point = self
242281
.graphics
243282
.camera()
@@ -310,7 +349,7 @@ impl State for Testbed {
310349
}
311350
}
312351
WindowEvent::MouseButton(_, Action::Release, _) => {
313-
let mut physics_world = &mut self.world.lock().unwrap();
352+
let mut physics_world = &mut self.world.get_mut();
314353
if let Some(body) = self.grabbed_object {
315354
for n in self
316355
.graphics
@@ -330,7 +369,7 @@ impl State for Testbed {
330369
self.grabbed_object_constraint = None;
331370
}
332371
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();
334373
self.cursor_pos.x = x as f32;
335374
self.cursor_pos.y = y as f32;
336375

@@ -371,7 +410,7 @@ impl State for Testbed {
371410
// // }
372411
// },
373412
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();
375414
self.draw_colls = !self.draw_colls;
376415
for co in physics_world.colliders() {
377416
// FIXME: ugly clone.
@@ -443,13 +482,13 @@ impl State for Testbed {
443482
for f in &self.callbacks {
444483
f(&mut self.graphics, self.time)
445484
}
446-
self.world.lock().unwrap().step();
485+
self.world.get_mut().step();
447486
if !self.hide_counters {
448-
println!("{}", self.world.lock().unwrap().performance_counters());
487+
println!("{}", self.world.get_mut().performance_counters());
449488
}
450-
self.time += self.world.lock().unwrap().timestep();
489+
self.time += self.world.get_mut().timestep();
451490
}
452-
let mut physics_world = &mut self.world.lock().unwrap();
491+
let physics_world = &self.world.get_mut();
453492

454493
for co in physics_world.colliders() {
455494
if self.graphics.body_nodes_mut(physics_world, co.data().body()).is_none() {
@@ -463,7 +502,7 @@ impl State for Testbed {
463502
if self.draw_colls {
464503
draw_collisions(
465504
window,
466-
&mut self.world.lock().unwrap(),
505+
&mut self.world.get_mut(),
467506
&mut self.persistant_contacts,
468507
self.running != RunMode::Stop,
469508
);
@@ -481,7 +520,7 @@ impl State for Testbed {
481520
&format!(
482521
"Simulation time: {:.*}sec.",
483522
4,
484-
self.world.lock().unwrap().performance_counters().step_time(),
523+
self.world.get_mut().performance_counters().step_time(),
485524
)[..],
486525
&Point2::origin(),
487526
60.0,

0 commit comments

Comments
 (0)