diff options
-rw-r--r-- | Cargo.lock | 94 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | resources/map.tmx | 6 | ||||
-rw-r--r-- | resources/tileset.tsx | 4 | ||||
-rw-r--r-- | src/main.rs | 93 |
5 files changed, 130 insertions, 68 deletions
@@ -122,6 +122,14 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "base64" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "bindgen" version = "0.32.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -142,6 +150,11 @@ dependencies = [ [[package]] name = "bitflags" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -151,6 +164,11 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "build_const" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "bumpalo" version = "2.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -362,6 +380,14 @@ dependencies = [ ] [[package]] +name = "crc" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "crc32fast" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -492,6 +518,17 @@ dependencies = [ ] [[package]] +name = "flate2" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz_oxide_c_api 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "fnv" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1038,6 +1075,34 @@ dependencies = [ ] [[package]] +name = "miniz-sys" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miniz_oxide" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miniz_oxide_c_api" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "mint" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1239,6 +1304,7 @@ version = "0.1.0" dependencies = [ "ggez 0.5.0-rc.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tiled 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1851,6 +1917,16 @@ dependencies = [ ] [[package]] +name = "tiled" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "toml" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2113,6 +2189,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "xml-rs" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "xml-rs" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2142,9 +2226,12 @@ dependencies = [ "checksum backtrace 0.3.30 (registry+https://github.com/rust-lang/crates.io-index)" = "ada4c783bb7e7443c14e0480f429ae2cc99da95065aeab7ee1b81ada0419404f" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" "checksum base-x 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "76f4eae81729e69bb1819a26c6caac956cc429238388091f98cb6cd858f16443" +"checksum base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "30e93c03064e7590d0466209155251b90c22e37fab1daf2771582598b5827557" "checksum bindgen 0.32.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b242e11a8f446f5fc7b76b37e81d737cabca562a927bd33766dac55b5f1177f" +"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +"checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" "checksum bumpalo 2.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "84dca3afd8e01b9526818b7963e5b4916063b3cdf9f10cf6b73ef0bd0ec37aa5" "checksum bytecount 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b92204551573580e078dc80017f36a213eb77a0450e4ddd8cfa0f3f2d1f0178f" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" @@ -2170,6 +2257,7 @@ dependencies = [ "checksum coreaudio-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f229761965dad3e9b11081668a6ea00f1def7aa46062321b5ec245b834f6e491" "checksum coreaudio-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "78fdbabf58d5b1f461e31b94a571c109284f384cec619a3d96e66ec55b4de82b" "checksum cpal 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58ae1ed6536b1b233f5e3aeb6997a046ddb4d05e3f61701b58a92eb254a829e" +"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" "checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4" @@ -2185,6 +2273,7 @@ dependencies = [ "checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" "checksum euclid 0.19.9 (registry+https://github.com/rust-lang/crates.io-index)" = "596b99621b9477e7a5f94d2d8dd13a9c5c302ac358b822c67a42b6f1054450e1" "checksum euclid_macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdcb84c18ea5037a1c5a23039b4ff29403abce2e0d6b1daa11cf0bde2b30be15" +"checksum flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f87e68aa82b2de08a6e037f1385455759df6e445a8df5e005b4297191dbf18aa" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" @@ -2246,6 +2335,9 @@ dependencies = [ "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum minimp3 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "542e9bed56860c5070a09939eee0e2df6f8f73f60304ddf56d620947e7017239" "checksum minimp3-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c109ae05c00ad6e3a53fab101e2f234545bdd010f0fffd399355efaf70817817" +"checksum miniz-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9e3ae51cea1576ceba0dde3d484d30e6e5b86dee0b2d412fe3a16a15c98202" +"checksum miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c468f2369f07d651a5d0bb2c9079f8488a66d5466efe42d0c5c6466edcb7f71e" +"checksum miniz_oxide_c_api 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b7fe927a42e3807ef71defb191dc87d4e24479b221e67015fe38ae2b7b447bab" "checksum mint 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c9e6c29b4bb0155117ea1a61520406c975673ee71b0287323f06d1a8d69c4a7c" "checksum nalgebra 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e12856109b5cb8e2934b5e45e4624839416e1c6c1f7d286711a7a66b79db29d" "checksum nix 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "becb657d662f1cd2ef38c7ad480ec6b8cf9e96b27adb543e594f9cf0f2e6065c" @@ -2338,6 +2430,7 @@ dependencies = [ "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum tiff 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4834f28a0330cb9f3f2c87d2649dca723cb33802e2bdcf18da32759fbec7ce" +"checksum tiled 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e43db82e3c62f96d84482c1b69e8a9b9ceb1c5cd80bd2ee1669caec58f5831d" "checksum toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b8c96d7873fa7ef8bdeb3a9cda3ac48389b4154f32b9803b4bc26220b677b039" "checksum twox-hash 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6c7bcecad121018bdcd6b709fa2325b004878fcb3d3067934ce90749f0faff9a" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" @@ -2371,5 +2464,6 @@ dependencies = [ "checksum x11-dl 2.18.3 (registry+https://github.com/rust-lang/crates.io-index)" = "940586acb859ea05c53971ac231685799a7ec1dee66ac0bccc0e6ad96e06b4e3" "checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" "checksum xi-unicode 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "12ea8eda4b1eb72f02d148402e23832d56a33f55d8c1b2d5bcdde91d79d47cb1" +"checksum xml-rs 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7ec6c39eaa68382c8e31e35239402c0a9489d4141a8ceb0c716099a0b515b562" "checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5" "checksum zip 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c18fc320faf909036e46ac785ea827f72e485304877faf1a3a39538d3714dbc3" @@ -7,3 +7,4 @@ edition = "2018" [dependencies] ggez = "0.5.0-rc.2" rand = "0.7.0-pre.1" +tiled = "0.8.0" diff --git a/resources/map.tmx b/resources/map.tmx index 19762e3..ac97e2c 100644 --- a/resources/map.tmx +++ b/resources/map.tmx @@ -1,6 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> -<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="20" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1"> - <tileset firstgid="1" source="tileset.tsx"/> +<map version="1.2" tiledversion="1.2.1" orientation="orthogonal" renderorder="right-down" width="20" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1"> + <tileset firstgid="1" name="tileset" tilewidth="16" tileheight="16" tilecount="3" columns="3"> + <image source="tileset.png" width="48" height="16"/> + </tileset> <layer id="1" name="Tile Layer 1" width="20" height="20"> <data encoding="csv"> 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, diff --git a/resources/tileset.tsx b/resources/tileset.tsx deleted file mode 100644 index b1a905e..0000000 --- a/resources/tileset.tsx +++ /dev/null @@ -1,4 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<tileset version="1.2" tiledversion="1.2.3" name="tileset" tilewidth="16" tileheight="16" tilecount="3" columns="3"> - <image source="tileset.png" width="48" height="16"/> -</tileset> diff --git a/src/main.rs b/src/main.rs index 7ce26c9..1d4c606 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,96 +1,65 @@ use ggez::conf::Conf; use ggez::event::{self, EventHandler}; use ggez::filesystem; -use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image}; +use ggez::graphics::{self, spritebatch::SpriteBatch, DrawParam, FilterMode, Image, Rect}; use ggez::nalgebra::{Point2, Vector2}; -use ggez::timer::delta; use ggez::{Context, ContextBuilder, GameResult}; -use std::collections::HashMap; -use std::io::Read; -use std::time::Duration; +use std::io::BufReader; +use tiled::{parse, Map}; struct State { - dt: Duration, - spritebatches: HashMap<Tiles, SpriteBatch>, - map: Vec<char>, -} - -#[derive(Debug, PartialEq, Eq, Hash)] -enum Tiles { - Grass, - Dirt, + spritebatch: SpriteBatch, + map: Map, } impl State { fn new(context: &mut Context) -> GameResult<State> { - let mut grass = Image::new(context, "/grass.png")?; - grass.set_filter(FilterMode::Nearest); - - let mut dirt = Image::new(context, "/dirt.png")?; - dirt.set_filter(FilterMode::Nearest); + let mut tileset = Image::new(context, "/tileset.png")?; + tileset.set_filter(FilterMode::Nearest); - let mut spritebatches = HashMap::new(); - spritebatches.insert(Tiles::Grass, SpriteBatch::new(grass)); - spritebatches.insert(Tiles::Dirt, SpriteBatch::new(dirt)); - - let mut map_str = String::new(); - filesystem::open(context, "/map.txt") - .unwrap() - .read_to_string(&mut map_str) - .unwrap(); + let reader = BufReader::new(filesystem::open(context, "/map.tmx")?); Ok(State { - dt: Duration::new(0, 0), - spritebatches, - map: map_str.replace("\n", "").chars().collect(), + spritebatch: SpriteBatch::new(tileset), + map: parse(reader).unwrap(), }) } } const TILE_SIZE: f32 = 16.0; -const TILE_SCALE: f32 = 4.0; +const TILE_SCALE: f32 = 3.0; impl EventHandler for State { - fn update(&mut self, context: &mut Context) -> GameResult { - self.dt = delta(context); + fn update(&mut self, _context: &mut Context) -> GameResult { Ok(()) } fn draw(&mut self, context: &mut Context) -> GameResult { graphics::clear(context, graphics::BLACK); - for x in 0..4 { - for y in 0..4 { - let draw_param = DrawParam::default() - .dest(Point2::new( - TILE_SIZE * TILE_SCALE * x as f32, - TILE_SIZE * TILE_SCALE * y as f32, - )) - .scale(Vector2::new(TILE_SCALE, TILE_SCALE)); - - let index = (x as usize) + (y as usize * 4); - match self.map[index] { - 'G' => { - self.spritebatches - .get_mut(&Tiles::Grass) - .unwrap() - .add(draw_param); - } - 'D' => { - self.spritebatches - .get_mut(&Tiles::Dirt) - .unwrap() - .add(draw_param); - } - _ => (), + for layer in self.map.layers.iter() { + for x in 0..self.map.width { + for y in 0..self.map.height { + let draw_param = DrawParam::default() + .dest(Point2::new( + TILE_SIZE * TILE_SCALE * x as f32, + TILE_SIZE * TILE_SCALE * y as f32, + )) + .scale(Vector2::new(TILE_SCALE, TILE_SCALE)) + .src(match layer.tiles[y as usize][x as usize] { + 1 => Rect::new(0.0, 0.0, 1.0 / 3.0, 1.0), + 2 => Rect::new(1.0 / 3.0, 0.0, 2.0 / 3.0, 1.0), + 3 => Rect::new(2.0 / 3.0, 0.0, 1.0, 1.0), + _ => Rect::zero(), + }); + + self.spritebatch.add(draw_param); } } } - for (_, spritebatch) in self.spritebatches.iter_mut() { - graphics::draw(context, spritebatch, DrawParam::default())?; - spritebatch.clear(); - } + graphics::draw(context, &self.spritebatch, DrawParam::default())?; + self.spritebatch.clear(); graphics::present(context)?; Ok(()) |