Skip to content
Snippets Groups Projects
Commit f7ace239 authored by Matthew Hall's avatar Matthew Hall
Browse files

Initial commit. Can parse map and tileset tags

parents
No related branches found
No related tags found
No related merge requests found
tags
target
Cargo.lock
*.swp
[package]
name = "tiled"
version = "0.0.1"
authors = ["Matthew Hall <matthew@quickbeam.me.uk>"]
[dependencies.rust-xml]
git = "https://github.com/mattyhall/rust-xml.git"
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
<tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32">
<image source="tilesheet.png" width="448" height="192"/>
</tileset>
<layer name="Tile Layer 1" width="100" height="100">
<data encoding="base64" compression="zlib">
eJzt1zEKwzAMBVBdIScIJXPS+9+uDY1BuHbpULvLeyDIkEkfIXmLiK2qW6cYb7lqv+p41r1TcpmjZFLPyamVEXOcGSzpu66SixkZb41Xz9dO7fE+O4yR+703KmdyhExGK32v+1zv+rxLZDJO7vO5G3KvSyY1eYxT31Dlrm3t9AhZzNC6a+v3RsmhNzP83qdc8ttdHuO07thWLlt4E87w7S6wM+bo7e3ef8z1KRt5/N+388N88gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIHsArPIXTA==
</data>
</layer>
</map>
This diff is collapsed.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
#![feature(globs, macro_rules)]
extern crate xml;
use std::io::File;
use std::io::BufferedReader;
use xml::reader::EventReader;
use xml::common::Attribute;
use xml::reader::events::*;
macro_rules! get_attrs {
($attrs:expr, optionals: [$(($oName:pat, $oVar:ident, $oT:ty, $oMethod:expr)),*],
required: [$(($name:pat, $var:ident, $t:ty, $method:expr)),*], $msg:expr) => {
{
$(let mut $oVar: Option<$oT> = None;)*
$(let mut $var: Option<$t> = None;)*
for attr in $attrs.iter() {
match attr.name.local_name[] {
$($oName => $oVar = $oMethod(attr.value.clone()),)*
$($name => $var = $method(attr.value.clone()),)*
_ => {}
}
}
if !(true $(&& $oVar.is_some())*) {
return Err($msg);
}
(($($oVar),*), ($($var.unwrap()),*))
}
}
}
#[deriving(Show)]
pub struct Map {
version: String,
width: int,
height: int,
tile_width: int,
tile_height: int
}
impl Map {
pub fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Map, String> {
let ((), (v, w, h, tw, th)) = get_attrs!(
attrs,
optionals: [],
required: [("version", version, String, |v| Some(v)),
("width", width, int, |v:String| from_str(v[])),
("height", height, int, |v:String| from_str(v[])),
("tilewidth", tile_width, int, |v:String| from_str(v[])),
("tileheight", tile_height, int, |v:String| from_str(v[]))],
"map must have a version, width and height with correct types".to_string());
while true {
match parser.next() {
StartElement {name, attributes, ..} => {
if name.local_name[] == "tileset" {
let t = try!(Tileset::new(parser, attributes));
println!("{}", t);
}
}
EndElement {name, ..} => {
if name.local_name[] == "map" {
return Ok(Map {version: v, width: w, height: h, tile_width: tw, tile_height: th});
}
}
_ => {}
}
}
Err("This should never happen".to_string())
}
}
#[deriving(Show)]
pub struct Tileset {
first_gid: int,
name: String,
}
impl Tileset {
pub fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<Attribute>) -> Result<Tileset, String> {
let ((), (g, n)) = get_attrs!(
attrs,
optionals: [],
required: [("firstgid", first_gid, int, |v:String| from_str(v[])),
("name", name, String, |v| Some(v))],
"tileset must have a firstgid and name with correct types".to_string());
Ok(Tileset {first_gid: g, name: n})
}
}
pub fn parse<B: Buffer>(parser: &mut EventReader<B>) -> Result<(), String>{
while true {
match parser.next() {
StartElement {name, attributes, ..} => {
if name.local_name[] == "map" {
let m = try!(Map::new(parser, attributes));
println!("{}", m);
return Ok(());
}
}
_ => {}
}
}
Ok(())
}
#![feature(globs)]
extern crate serialize;
extern crate xml;
extern crate tiled;
use serialize::base64::{FromBase64};
use std::io::File;
use std::io::BufferedReader;
use xml::reader::EventReader;
use xml::reader::events::*;
use tiled::parse;
fn main() {
let file = File::open(&Path::new("assets/tiled_base64_zlib.tmx")).unwrap();
let reader = BufferedReader::new(file);
let mut parser = EventReader::new(reader);
parse(&mut parser);
}
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