Skip to content
Snippets Groups Projects
Unverified Commit e05eda43 authored by Anti-Alias's avatar Anti-Alias Committed by GitHub
Browse files

Changed Rc<Tileset> to Arc<Tileset> (#156)

* Changed Rc to Arc

* Trying to trigger a rebuild

* Trying to trigger a rebuild

* Testing
parent 3c2f804b
No related branches found
No related tags found
No related merge requests found
use std::rc::Rc; use std::sync::Arc;
use sfml::{ use sfml::{
graphics::{FloatRect, Texture}, graphics::{FloatRect, Texture},
...@@ -9,12 +9,12 @@ use tiled::Tileset; ...@@ -9,12 +9,12 @@ use tiled::Tileset;
/// A container for a tileset and the texture it references. /// A container for a tileset and the texture it references.
pub struct Tilesheet { pub struct Tilesheet {
texture: SfBox<Texture>, texture: SfBox<Texture>,
tileset: Rc<Tileset>, tileset: Arc<Tileset>,
} }
impl Tilesheet { impl Tilesheet {
/// Create a tilesheet from a Tiled tileset, loading its texture along the way. /// Create a tilesheet from a Tiled tileset, loading its texture along the way.
pub fn from_tileset<'p>(tileset: Rc<Tileset>) -> Self { pub fn from_tileset<'p>(tileset: Arc<Tileset>) -> Self {
let tileset_image = tileset.image.as_ref().unwrap(); let tileset_image = tileset.image.as_ref().unwrap();
let texture = { let texture = {
......
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc, sync::Arc,
}; };
use crate::Tileset; use crate::Tileset;
...@@ -10,19 +10,19 @@ pub type ResourcePath = Path; ...@@ -10,19 +10,19 @@ pub type ResourcePath = Path;
pub type ResourcePathBuf = PathBuf; pub type ResourcePathBuf = PathBuf;
pub trait ResourceCache { pub trait ResourceCache {
fn get_tileset(&self, path: impl AsRef<ResourcePath>) -> Option<Rc<Tileset>>; fn get_tileset(&self, path: impl AsRef<ResourcePath>) -> Option<Arc<Tileset>>;
fn get_or_try_insert_tileset_with<F, E>( fn get_or_try_insert_tileset_with<F, E>(
&mut self, &mut self,
path: ResourcePathBuf, path: ResourcePathBuf,
f: F, f: F,
) -> Result<Rc<Tileset>, E> ) -> Result<Arc<Tileset>, E>
where where
F: FnOnce() -> Result<Tileset, E>; F: FnOnce() -> Result<Tileset, E>;
} }
/// A cache that identifies resources by their path in the user's filesystem. /// A cache that identifies resources by their path in the user's filesystem.
pub struct FilesystemResourceCache { pub struct FilesystemResourceCache {
tilesets: HashMap<ResourcePathBuf, Rc<Tileset>>, tilesets: HashMap<ResourcePathBuf, Arc<Tileset>>,
} }
impl FilesystemResourceCache { impl FilesystemResourceCache {
...@@ -34,7 +34,7 @@ impl FilesystemResourceCache { ...@@ -34,7 +34,7 @@ impl FilesystemResourceCache {
} }
impl ResourceCache for FilesystemResourceCache { impl ResourceCache for FilesystemResourceCache {
fn get_tileset(&self, path: impl AsRef<ResourcePath>) -> Option<Rc<Tileset>> { fn get_tileset(&self, path: impl AsRef<ResourcePath>) -> Option<Arc<Tileset>> {
self.tilesets.get(path.as_ref()).map(Clone::clone) self.tilesets.get(path.as_ref()).map(Clone::clone)
} }
...@@ -42,13 +42,13 @@ impl ResourceCache for FilesystemResourceCache { ...@@ -42,13 +42,13 @@ impl ResourceCache for FilesystemResourceCache {
&mut self, &mut self,
path: ResourcePathBuf, path: ResourcePathBuf,
f: F, f: F,
) -> Result<Rc<Tileset>, E> ) -> Result<Arc<Tileset>, E>
where where
F: FnOnce() -> Result<Tileset, E>, F: FnOnce() -> Result<Tileset, E>,
{ {
Ok(match self.tilesets.entry(path) { Ok(match self.tilesets.entry(path) {
std::collections::hash_map::Entry::Occupied(o) => o.into_mut(), std::collections::hash_map::Entry::Occupied(o) => o.into_mut(),
std::collections::hash_map::Entry::Vacant(v) => v.insert(Rc::new(f()?)), std::collections::hash_map::Entry::Vacant(v) => v.insert(Arc::new(f()?)),
} }
.clone()) .clone())
} }
......
use std::{collections::HashMap, fmt, fs::File, io::Read, path::Path, rc::Rc, str::FromStr}; use std::{collections::HashMap, fmt, fs::File, io::Read, path::Path, sync::Arc, str::FromStr};
use xml::{attribute::OwnedAttribute, reader::XmlEvent, EventReader}; use xml::{attribute::OwnedAttribute, reader::XmlEvent, EventReader};
...@@ -13,7 +13,7 @@ use crate::{ ...@@ -13,7 +13,7 @@ use crate::{
pub(crate) struct MapTilesetGid { pub(crate) struct MapTilesetGid {
pub first_gid: Gid, pub first_gid: Gid,
pub tileset: Rc<Tileset>, pub tileset: Arc<Tileset>,
} }
/// All Tiled map files will be parsed into this. Holds all the layers and tilesets. /// All Tiled map files will be parsed into this. Holds all the layers and tilesets.
...@@ -31,7 +31,7 @@ pub struct Map { ...@@ -31,7 +31,7 @@ pub struct Map {
/// Tile height, in pixels. /// Tile height, in pixels.
pub tile_height: u32, pub tile_height: u32,
/// The tilesets present on this map. /// The tilesets present on this map.
tilesets: Vec<Rc<Tileset>>, tilesets: Vec<Arc<Tileset>>,
/// The layers present in this map. /// The layers present in this map.
layers: Vec<LayerData>, layers: Vec<LayerData>,
/// The custom properties of this map. /// The custom properties of this map.
...@@ -100,7 +100,7 @@ impl Map { ...@@ -100,7 +100,7 @@ impl Map {
impl Map { impl Map {
/// Get a reference to the map's tilesets. /// Get a reference to the map's tilesets.
pub fn tilesets(&self) -> &[Rc<Tileset>] { pub fn tilesets(&self) -> &[Arc<Tileset>] {
self.tilesets.as_ref() self.tilesets.as_ref()
} }
...@@ -186,7 +186,7 @@ impl Map { ...@@ -186,7 +186,7 @@ impl Map {
tilesets.push(MapTilesetGid{first_gid: res.first_gid, tileset}); tilesets.push(MapTilesetGid{first_gid: res.first_gid, tileset});
} }
EmbeddedParseResultType::Embedded { tileset } => { EmbeddedParseResultType::Embedded { tileset } => {
tilesets.push(MapTilesetGid{first_gid: res.first_gid, tileset: Rc::new(tileset)}); tilesets.push(MapTilesetGid{first_gid: res.first_gid, tileset: Arc::new(tileset)});
}, },
}; };
Ok(()) Ok(())
......
temp 0 → 100644
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment