From 2bb1fa7a668a0a661b38b5b88320b33ac262cd6b Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 2 Apr 2022 13:23:58 -0400 Subject: [PATCH] wip tile caching --- game.tiled-session | 1 + game/dist/map_gameMap.tmx | 8 +++ game/game_logic/Cargo.toml | 2 +- .../src/rendering/utilities/map_render.rs | 51 +++++++++++++++---- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/game.tiled-session b/game.tiled-session index 776e14b2..9095aa56 100644 --- a/game.tiled-session +++ b/game.tiled-session @@ -52,6 +52,7 @@ "openFiles": [ ], "project": "game.tiled-project", + "property.type": "float", "recentFiles": [ "game/dist/assets/env/env_beachTile/beachTile.tsx", "game/dist/map_gameMap.tmx", diff --git a/game/dist/map_gameMap.tmx b/game/dist/map_gameMap.tmx index af2da828..f3384c58 100644 --- a/game/dist/map_gameMap.tmx +++ b/game/dist/map_gameMap.tmx @@ -3,9 +3,17 @@ + + + + + + + + diff --git a/game/game_logic/Cargo.toml b/game/game_logic/Cargo.toml index 01481368..e285ec99 100644 --- a/game/game_logic/Cargo.toml +++ b/game/game_logic/Cargo.toml @@ -26,4 +26,4 @@ approx = "0.5.1" poll-promise = { version = "0.1.0", features = ["tokio"] } tempfile = "3.3.0" nalgebra = "0.30.1" -tiled = "0.10.1" \ No newline at end of file +tiled = { version = "0.10.1", path = "../../third_party/rs-tiled" } diff --git a/game/game_logic/src/rendering/utilities/map_render.rs b/game/game_logic/src/rendering/utilities/map_render.rs index a94e79e3..8a105177 100644 --- a/game/game_logic/src/rendering/utilities/map_render.rs +++ b/game/game_logic/src/rendering/utilities/map_render.rs @@ -1,8 +1,11 @@ use std::{collections::HashMap, sync::Arc}; -use crate::asset_manager::InternalData; +use crate::asset_manager::{load_texture_from_internal_data, InternalData}; use nalgebra as na; -use tiled::{Loader, ResourceCache, ResourcePath, ResourcePathBuf, Tileset}; +use raylib::{ + camera::Camera2D, prelude::RaylibDrawHandle, texture::Texture2D, RaylibHandle, RaylibThread, +}; +use tiled::{Loader, Map, ResourceCache, ResourcePath, ResourcePathBuf, Tileset}; /// Possible errors generated by the map loading process #[derive(Debug, thiserror::Error)] @@ -44,10 +47,6 @@ impl ResourceCache for ProgramDataTileCache { .unwrap(), ) }) - - // .ok_or(MapRenderError::AssetNotFound(path.to_string()))? - // .data - // .into_owned(); } } @@ -68,11 +67,18 @@ impl ResourceCache for ProgramDataTileCache { } #[derive(Debug)] -pub struct MapRenderer {} +pub struct MapRenderer { + map: Map, + tile_textures: HashMap>, +} impl MapRenderer { /// Construct a new MapRenderer. - pub fn new(tmx_path: &str) -> Result { + pub fn new( + tmx_path: &str, + raylib: &mut RaylibHandle, + raylib_thread: &RaylibThread, + ) -> Result { // Pull the TMX from storage let data = InternalData::get(tmx_path) .ok_or(MapRenderError::AssetNotFound(tmx_path.to_string()))? @@ -83,7 +89,32 @@ impl MapRenderer { let mut loader = Loader::with_cache(ProgramDataTileCache::new()); let map = loader.load_tmx_map_from(data.as_slice(), tmx_path)?; - Ok(Self {}) + // Iterate over all images in the map + let mut tile_textures = HashMap::new(); + for tileset in map.tilesets() { + for (idx, tile) in tileset.tiles() { + if let Some(image) = tile.data.image { + // We now have a path to an image + let image_path = image.source; + + // Load the texture + let texture = load_texture_from_internal_data( + raylib, + raylib_thread, + image_path.to_str().unwrap(), + ) + .unwrap(); + + // Store the texture in the cache + tile_textures + .entry(tileset.name) + .or_insert_with(HashMap::new) + .insert(idx, texture); + } + } + } + + Ok(Self { map, tile_textures }) } pub fn sample_friction_at(&self, position: na::Vector2) -> f32 { @@ -93,4 +124,6 @@ impl MapRenderer { pub fn sample_temperature_at(&self, position: na::Vector2) -> f32 { todo!() } + + pub fn render_map(&self, draw_handle: &RaylibDrawHandle, camera: &Camera2D) {} }