Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Sync/Send for Map #265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::sync::{Arc, Mutex};
use bindings::ffi::{self, TCOD_fov_algorithm_t};
use bindings::{AsNative, c_bool};

unsafe impl Sync for Map {}
unsafe impl Send for Map {}

pub struct Map {
tcod_map: ffi::TCOD_map_t,
tcod_map: Arc<Mutex<ffi::TCOD_map_t>>,
}

impl AsNative<ffi::TCOD_map_t> for Map {
unsafe fn as_native(&self) -> &ffi::TCOD_map_t {

impl AsNative<Arc<Mutex<ffi::TCOD_map_t>>> for Map {
unsafe fn as_native(&self) -> &Arc<Mutex<ffi::TCOD_map_t>> {
&self.tcod_map
}
}
Expand All @@ -15,14 +20,15 @@ impl Map {
pub fn new(width: i32, height: i32) -> Map {
assert!(width > 0 && height > 0);
unsafe {
Map{tcod_map: ffi::TCOD_map_new(width, height)}
Map{tcod_map: Arc::new(Mutex::new(ffi::TCOD_map_new(width, height)))}
}
}

pub fn size(&self) -> (i32, i32) {
unsafe {
(ffi::TCOD_map_get_width(self.tcod_map),
ffi::TCOD_map_get_height(self.tcod_map))
let tcod_map = *self.tcod_map.lock().unwrap();
(ffi::TCOD_map_get_width(tcod_map),
ffi::TCOD_map_get_height(tcod_map))
}
}

Expand All @@ -31,7 +37,8 @@ impl Map {
let (width, height) = self.size();
assert!(x < width && y < height);
unsafe {
ffi::TCOD_map_set_properties(self.tcod_map, x, y,
let tcod_map = *self.tcod_map.lock().unwrap();
ffi::TCOD_map_set_properties(tcod_map, x, y,
transparent as c_bool,
walkable as c_bool);
}
Expand All @@ -41,7 +48,8 @@ impl Map {
light_walls: bool, algo: FovAlgorithm) {
assert!(origin_x >= 0 && origin_y >= 0);
unsafe {
ffi::TCOD_map_compute_fov(self.tcod_map, origin_x, origin_y, max_radius,
let tcod_map = *self.tcod_map.lock().unwrap();
ffi::TCOD_map_compute_fov(tcod_map, origin_x, origin_y, max_radius,
light_walls as c_bool,
algo.into());
}
Expand All @@ -52,30 +60,34 @@ impl Map {
let (width, height) = self.size();
assert!(x < width && y < height);
unsafe {
ffi::TCOD_map_is_in_fov(self.tcod_map, x, y) != 0
let tcod_map = *self.tcod_map.lock().unwrap();
ffi::TCOD_map_is_in_fov(tcod_map, x, y) != 0
}
}

pub fn is_walkable(&self, x: i32, y: i32) -> bool {
assert!(x >= 0 && y >= 0);
let (width, height) = self.size();
assert!(x < width && y < height);
let tcod_map = *self.tcod_map.lock().unwrap();
unsafe {
ffi::TCOD_map_is_walkable(self.tcod_map, x, y) != 0
ffi::TCOD_map_is_walkable(tcod_map, x, y) != 0
}
}

pub fn clear(&mut self, transparent: bool, walkable: bool) {
unsafe {
ffi::TCOD_map_clear(self.tcod_map, transparent as c_bool, walkable as c_bool);
let tcod_map = *self.tcod_map.lock().unwrap();
ffi::TCOD_map_clear(tcod_map, transparent as c_bool, walkable as c_bool);
}
}
}

impl Drop for Map {
fn drop(&mut self) {
let tcod_map = *self.tcod_map.lock().unwrap();
unsafe {
ffi::TCOD_map_delete(self.tcod_map)
ffi::TCOD_map_delete(tcod_map)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ impl<'a> AStar<'a> {

pub fn new_from_map(map: Map, diagonal_cost: f32) -> AStar<'static> {
let tcod_path = unsafe {
ffi::TCOD_path_new_using_map(*map.as_native(), diagonal_cost)
let tcod_map = *map.as_native().lock().unwrap();
ffi::TCOD_path_new_using_map(tcod_map, diagonal_cost)
};
let (w, h) = map.size();
AStar {
Expand Down Expand Up @@ -207,7 +208,8 @@ impl<'a> Dijkstra<'a> {

pub fn new_from_map(map: Map, diagonal_cost: f32) -> Dijkstra<'static> {
let tcod_path = unsafe {
ffi::TCOD_dijkstra_new(*map.as_native(), diagonal_cost)
let tcod_map = *map.as_native().lock().unwrap();
ffi::TCOD_dijkstra_new(tcod_map, diagonal_cost)
};
let (w, h) = map.size();
Dijkstra {
Expand Down