view import/myrrdin/xmlmap.d @ 5:f4b89014ad39

added moving figure stuff + animated sprites. not usable atm.
author fred@reichbier.de
date Sat, 19 Jul 2008 14:33:08 +0200
parents src/xmlmap.d@a9af6ec19195
children 510541745cd1
line wrap: on
line source

/*
    This file is part of myrrdin.

    myrrdin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as 
    published by the Free Software Foundation, either version 3 of 
    the License, or (at your option) any later version.

    myrrdin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with myrrdin. If not, see <http://www.gnu.org/licenses/>.
*/

module xmlmap;

import tileset;
import tilemap;

import tango.text.xml.Document;
import tango.text.convert.Integer;
import tango.io.Stdout; // TODO
import tools;
import imagecache;
import dsfml.system.all;
import Text = tango.text.Util;
static import tango.core.Exception;

alias XmlPath!(char).NodeSet NodeSet;
alias Document!(char).Node NodeImpl;
private char[] get_attribute(NodeSet node, char[] name) {
    return node.attribute(name).nodes[0].value;
}

Tileset parse_tileset(Cache cache, char[] content) {
    auto tileset = new Tileset(cache);

    auto doc = new Document!(char);
    doc.parse(content);
    auto root = doc.query["tileset"];   
    auto nodes = root.child.nodes;
    foreach(NodeImpl tiles_node; nodes) {
	tileset.add_tile(Integer.parse(tiles_node.getAttribute("id").value), tiles_node.getAttribute("filename").value);
    }
    return tileset;
}

Tilemap parse_map(Cache cache, char[] content) {
    auto doc = new Document!(char);
    doc.parse(content);
    auto root = doc.query["map"];
    int width = Integer.parse(get_attribute(root, "width"));
    int height = Integer.parse(get_attribute(root, "height"));
    Stdout.formatln("Width: {}, Height: {}", width, height);
    auto tiles_node = root.child.nodes[0];
    char[] tileset_file = tiles_node.getAttribute("tileset").value;
    auto tilemap = new Tilemap(parse_tileset(cache, read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size
    int layer_id=0;
    foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) {
	int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value);
	int tile_height = Integer.parse(layer_node.getAttribute("tileheight").value);
	tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height);
	char[][] content_ = Text.delimit(Text.trim(layer_node.value), ",");
	int i=0;
	bool running = true;
	for(int x=0; x < width; x++) {
	    for(int y=0; y < height; y++) {
		try {
		    tilemap.map[layer_id][x][y] = Integer.parse(content_[i]);
		}
		catch (tango.core.Exception.ArrayBoundsException) {
		    // array out of bounds ... break
		    running = false;
		    break;
		}
		i++;
	    }
	    if(!running) {
		break;
	    }
	}
	layer_id++;
    }
    return tilemap;
}