From 394d4b37ff50c0d92f20b090c6f71c87459cb342 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 14 Jan 2019 16:15:08 +0100 Subject: Separate tileset loading from MapScene::render to MapScene::load_tilset --- src/graphics.rs | 104 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 48 deletions(-) (limited to 'src') 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 -- cgit v1.2.1