Mercurial > projects > myrrdin
annotate import/myrrdin/xmlmap.d @ 11:5866d9f2ca75 default tip
fixed director + tilemap stuff
author | fred@reichbier.de |
---|---|
date | Wed, 13 Aug 2008 18:31:10 +0200 |
parents | 79b534bbda65 |
children |
rev | line source |
---|---|
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
1 /* |
6 | 2 myrrdin, a 2d tile engine |
3 Copyright (c) 2008 Friedrich Weber | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
4 |
6 | 5 Permission is hereby granted, free of charge, to any person obtaining a copy |
6 of this software and associated documentation files (the Software), to deal | |
7 in the Software without restriction, including without limitation the rights | |
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 copies of the Software, and to permit persons to whom the Software is | |
10 furnished to do so, subject to the following conditions: | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
11 |
6 | 12 The above copyright notice and this permission notice shall be included in |
13 all copies or substantial portions of the Software. | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
14 |
6 | 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 THE SOFTWARE. | |
22 */ | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
23 |
7 | 24 module myrrdin.xmlmap; |
2 | 25 |
6 | 26 import myrrdin.tileset; |
27 import myrrdin.tilemap; | |
2 | 28 |
29 import tango.text.xml.Document; | |
30 import tango.text.convert.Integer; | |
31 import tango.io.Stdout; // TODO | |
6 | 32 import myrrdin.tools; |
3 | 33 import dsfml.system.all; |
34 import Text = tango.text.Util; | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
35 static import tango.core.Exception; |
2 | 36 |
37 alias XmlPath!(char).NodeSet NodeSet; | |
38 alias Document!(char).Node NodeImpl; | |
39 private char[] get_attribute(NodeSet node, char[] name) { | |
40 return node.attribute(name).nodes[0].value; | |
41 } | |
42 | |
10 | 43 Tileset parse_tileset(char[] content) { |
44 auto tileset = new Tileset; | |
2 | 45 |
46 auto doc = new Document!(char); | |
47 doc.parse(content); | |
48 auto root = doc.query["tileset"]; | |
49 auto nodes = root.child.nodes; | |
50 foreach(NodeImpl tiles_node; nodes) { | |
51 tileset.add_tile(Integer.parse(tiles_node.getAttribute("id").value), tiles_node.getAttribute("filename").value); | |
52 } | |
53 return tileset; | |
54 } | |
55 | |
10 | 56 Tilemap parse_map(char[] content) { |
2 | 57 auto doc = new Document!(char); |
58 doc.parse(content); | |
59 auto root = doc.query["map"]; | |
60 int width = Integer.parse(get_attribute(root, "width")); | |
61 int height = Integer.parse(get_attribute(root, "height")); | |
62 Stdout.formatln("Width: {}, Height: {}", width, height); | |
63 auto tiles_node = root.child.nodes[0]; | |
64 char[] tileset_file = tiles_node.getAttribute("tileset").value; | |
10 | 65 auto tilemap = new Tilemap(parse_tileset(read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size |
3 | 66 int layer_id=0; |
2 | 67 foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) { |
68 int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value); | |
69 int tile_height = Integer.parse(layer_node.getAttribute("tileheight").value); | |
3 | 70 tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height); |
71 char[][] content_ = Text.delimit(Text.trim(layer_node.value), ","); | |
72 int i=0; | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
73 bool running = true; |
3 | 74 for(int x=0; x < width; x++) { |
75 for(int y=0; y < height; y++) { | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
76 try { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
77 tilemap.map[layer_id][x][y] = Integer.parse(content_[i]); |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
78 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
79 catch (tango.core.Exception.ArrayBoundsException) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
80 // array out of bounds ... break |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
81 running = false; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
82 break; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
83 } |
3 | 84 i++; |
85 } | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
86 if(!running) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
87 break; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
88 } |
3 | 89 } |
90 layer_id++; | |
2 | 91 } |
92 return tilemap; | |
93 } |