Mercurial > projects > myrrdin
annotate import/myrrdin/xmlmap.d @ 6:510541745cd1
turned myrrdin into package.
author | fred@reichbier.de |
---|---|
date | Sat, 19 Jul 2008 15:10:56 +0200 |
parents | f4b89014ad39 |
children | 806b3781f4e4 |
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 |
2 | 24 module xmlmap; |
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; |
33 import myrrdin.imagecache; | |
3 | 34 import dsfml.system.all; |
35 import Text = tango.text.Util; | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
36 static import tango.core.Exception; |
2 | 37 |
38 alias XmlPath!(char).NodeSet NodeSet; | |
39 alias Document!(char).Node NodeImpl; | |
40 private char[] get_attribute(NodeSet node, char[] name) { | |
41 return node.attribute(name).nodes[0].value; | |
42 } | |
43 | |
44 Tileset parse_tileset(Cache cache, char[] content) { | |
45 auto tileset = new Tileset(cache); | |
46 | |
47 auto doc = new Document!(char); | |
48 doc.parse(content); | |
49 auto root = doc.query["tileset"]; | |
50 auto nodes = root.child.nodes; | |
51 foreach(NodeImpl tiles_node; nodes) { | |
52 tileset.add_tile(Integer.parse(tiles_node.getAttribute("id").value), tiles_node.getAttribute("filename").value); | |
53 } | |
54 return tileset; | |
55 } | |
56 | |
57 Tilemap parse_map(Cache cache, char[] content) { | |
58 auto doc = new Document!(char); | |
59 doc.parse(content); | |
60 auto root = doc.query["map"]; | |
61 int width = Integer.parse(get_attribute(root, "width")); | |
62 int height = Integer.parse(get_attribute(root, "height")); | |
63 Stdout.formatln("Width: {}, Height: {}", width, height); | |
64 auto tiles_node = root.child.nodes[0]; | |
65 char[] tileset_file = tiles_node.getAttribute("tileset").value; | |
66 auto tilemap = new Tilemap(parse_tileset(cache, read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size | |
3 | 67 int layer_id=0; |
2 | 68 foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) { |
69 int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value); | |
70 int tile_height = Integer.parse(layer_node.getAttribute("tileheight").value); | |
3 | 71 tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height); |
72 char[][] content_ = Text.delimit(Text.trim(layer_node.value), ","); | |
73 int i=0; | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
74 bool running = true; |
3 | 75 for(int x=0; x < width; x++) { |
76 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
|
77 try { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
78 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
|
79 } |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
80 catch (tango.core.Exception.ArrayBoundsException) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
81 // array out of bounds ... break |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
82 running = false; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
83 break; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
84 } |
3 | 85 i++; |
86 } | |
5
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
87 if(!running) { |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
88 break; |
f4b89014ad39
added moving figure stuff + animated sprites. not usable atm.
fred@reichbier.de
parents:
3
diff
changeset
|
89 } |
3 | 90 } |
91 layer_id++; | |
2 | 92 } |
93 return tilemap; | |
94 } |