summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/graphics.rs104
1 files changed, 56 insertions, 48 deletions
diff --git a/src/graphics.rs b/src/graphics.rs
index 3b08c77..fb38b96 100644
--- a/src/graphics.rs
+++ b/src/graphics.rs
@@ -138,6 +138,61 @@ impl MapScene {
}
}
+impl MapScene {
+ fn load_tilset(self: &mut Self, tmx_tileset: &tiled::Tileset) {
+ // load tileset if not loaded yet
+ if self.tilesets.contains_key(&tmx_tileset.first_gid) {
+ return;
+ }
+
+ // TODO: replace with iter or error message
+ assert_eq!(tmx_tileset.images.len(), 1);
+
+ // load tileset image
+ let tmx_image = &tmx_tileset.images[0];
+ let asset = match MapAssets::get(&tmx_image.source) {
+ Some(asset) => asset,
+ None => {
+ panic!("Failed to get asset: {}", &tmx_image.source);
+ }
+ };
+
+
+ let image = match Image::from_memory(asset.as_ref()) {
+ Some(image) => image,
+ None => {
+ panic!("Failed load image from asset");
+ }
+ };
+
+ // load tiles (textures)
+ let tileset_width = tmx_image.width as u32 / tmx_tileset.tile_width;
+ let tileset_height = tmx_image.height as u32 / tmx_tileset.tile_height;
+
+ for ty in 0 .. tileset_height {
+ for tx in 0 .. tileset_width {
+ let texture = Texture::from_image_with_rect(
+ &image, &Rect::new(
+ (tx * tmx_tileset.tile_width) as i32,
+ (ty * tmx_tileset.tile_height) as i32,
+ tmx_tileset.tile_width as i32,
+ tmx_tileset.tile_height as i32
+ )
+ ).unwrap();
+
+ // save tile texture
+ self.textures.insert(
+ tmx_tileset.first_gid + ty * tileset_width + tx,
+ texture
+ );
+ }
+ }
+
+ // save tileset image
+ self.tilesets.insert(tmx_tileset.first_gid, image);
+ }
+}
+
impl Scene for MapScene {
fn render(self: &mut Self, window: &mut RenderWindow, state: &crate::game::State) {
for y in 0 .. state.map.height {
@@ -160,55 +215,8 @@ impl Scene for MapScene {
}
};
- // load tileset if not loaded yet
// TODO: load in a separate thread?
- if !self.tilesets.contains_key(&tmx_tileset.first_gid) {
- // TODO: replace with iter or error message
- assert_eq!(tmx_tileset.images.len(), 1);
-
- // load tileset image
- let tmx_image = &tmx_tileset.images[0];
- let asset = match MapAssets::get(&tmx_image.source) {
- Some(asset) => asset,
- None => {
- panic!("Failed to get asset: {}", &tmx_image.source);
- }
- };
-
-
- let image = match Image::from_memory(asset.as_ref()) {
- Some(image) => image,
- None => {
- panic!("Failed load image from asset");
- }
- };
-
- // load tiles (textures)
- let tileset_width = tmx_image.width as u32 / tmx_tileset.tile_width;
- let tileset_height = tmx_image.height as u32 / tmx_tileset.tile_height;
-
- for ty in 0 .. tileset_height {
- for tx in 0 .. tileset_width {
- let texture = Texture::from_image_with_rect(
- &image, &Rect::new(
- (tx * tmx_tileset.tile_width) as i32,
- (ty * tmx_tileset.tile_height) as i32,
- tmx_tileset.tile_width as i32,
- tmx_tileset.tile_height as i32
- )
- ).unwrap();
-
- // save tile texture
- self.textures.insert(
- tmx_tileset.first_gid + ty * tileset_width + tx,
- texture
- );
- }
- }
-
- // save tileset image
- self.tilesets.insert(tmx_tileset.first_gid, image);
- }
+ self.load_tilset(tmx_tileset);
// draw tiles
// TODO: check map render order