comparison 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
comparison
equal deleted inserted replaced
4:292df259cc85 5:f4b89014ad39
1 /*
2 This file is part of myrrdin.
3
4 myrrdin is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of
7 the License, or (at your option) any later version.
8
9 myrrdin is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with myrrdin. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 module xmlmap;
19
20 import tileset;
21 import tilemap;
22
23 import tango.text.xml.Document;
24 import tango.text.convert.Integer;
25 import tango.io.Stdout; // TODO
26 import tools;
27 import imagecache;
28 import dsfml.system.all;
29 import Text = tango.text.Util;
30 static import tango.core.Exception;
31
32 alias XmlPath!(char).NodeSet NodeSet;
33 alias Document!(char).Node NodeImpl;
34 private char[] get_attribute(NodeSet node, char[] name) {
35 return node.attribute(name).nodes[0].value;
36 }
37
38 Tileset parse_tileset(Cache cache, char[] content) {
39 auto tileset = new Tileset(cache);
40
41 auto doc = new Document!(char);
42 doc.parse(content);
43 auto root = doc.query["tileset"];
44 auto nodes = root.child.nodes;
45 foreach(NodeImpl tiles_node; nodes) {
46 tileset.add_tile(Integer.parse(tiles_node.getAttribute("id").value), tiles_node.getAttribute("filename").value);
47 }
48 return tileset;
49 }
50
51 Tilemap parse_map(Cache cache, char[] content) {
52 auto doc = new Document!(char);
53 doc.parse(content);
54 auto root = doc.query["map"];
55 int width = Integer.parse(get_attribute(root, "width"));
56 int height = Integer.parse(get_attribute(root, "height"));
57 Stdout.formatln("Width: {}, Height: {}", width, height);
58 auto tiles_node = root.child.nodes[0];
59 char[] tileset_file = tiles_node.getAttribute("tileset").value;
60 auto tilemap = new Tilemap(parse_tileset(cache, read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size
61 int layer_id=0;
62 foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) {
63 int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value);
64 int tile_height = Integer.parse(layer_node.getAttribute("tileheight").value);
65 tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height);
66 char[][] content_ = Text.delimit(Text.trim(layer_node.value), ",");
67 int i=0;
68 bool running = true;
69 for(int x=0; x < width; x++) {
70 for(int y=0; y < height; y++) {
71 try {
72 tilemap.map[layer_id][x][y] = Integer.parse(content_[i]);
73 }
74 catch (tango.core.Exception.ArrayBoundsException) {
75 // array out of bounds ... break
76 running = false;
77 break;
78 }
79 i++;
80 }
81 if(!running) {
82 break;
83 }
84 }
85 layer_id++;
86 }
87 return tilemap;
88 }