changeset 2:fc2f936a961c

basic loading, to be fixed
author fred@reichbier.de
date Thu, 17 Jul 2008 21:06:43 +0200
parents f193d0c14685
children a9af6ec19195
files bin/map-example.xml bin/tileset-example.xml src/test.d src/tileset.d src/tools.d src/xmlmap.d
diffstat 6 files changed, 72 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/map-example.xml	Thu Jul 17 21:06:43 2008 +0200
@@ -0,0 +1,7 @@
+<map version="1" width="3" height="3">
+  <tiles tileset="tileset-example.xml">
+    <layer tilewidth="32" tileheight="32">
+      1,1,1,0,0,0,1,1,1
+    </layer>
+  </tiles>
+</map>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/tileset-example.xml	Thu Jul 17 21:06:43 2008 +0200
@@ -0,0 +1,3 @@
+<tileset version="1">
+  <tile id="0" filename="grass.png" />
+</tileset>
--- a/src/test.d	Thu Jul 17 20:19:24 2008 +0200
+++ b/src/test.d	Thu Jul 17 21:06:43 2008 +0200
@@ -14,8 +14,9 @@
 import tools;
 
 int main(char[][] args) {
-//    Tilemap map = parse_map(read_file_contents("map-example.xml"));
     Cache cache = new Cache("gfx");
+    Tilemap map = parse_map(cache, read_file_contents("map-example.xml"));
+/*
     Tileset tileset = new Tileset(cache);
     Tilemap map = new Tilemap(tileset, 5, 5, 32, 32);
     tileset.add_tile(0, "grass.png");
@@ -31,6 +32,6 @@
     TileConsumer consumer = new TileConsumer(render, map);
     render.add_consumer(consumer);
 
-    render.mainloop();
+    render.mainloop();*/
     return 0; 
 }
--- a/src/tileset.d	Thu Jul 17 20:19:24 2008 +0200
+++ b/src/tileset.d	Thu Jul 17 21:06:43 2008 +0200
@@ -6,9 +6,11 @@
 
 import imagecache;
 
+alias Image[int] TileList;
+
 class Tileset {
     private Cache cache;
-    public Image[int] tiles;
+    public TileList tiles;
 
     this(Cache cache) {
 	this.cache = cache;	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tools.d	Thu Jul 17 21:06:43 2008 +0200
@@ -0,0 +1,10 @@
+module tools;
+
+import tango.io.File;
+
+char[] read_file_contents(char[] filename) {
+    auto file = new File (filename);
+    auto content = cast(char[]) file.read();
+
+    return content;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/xmlmap.d	Thu Jul 17 21:06:43 2008 +0200
@@ -0,0 +1,46 @@
+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;
+
+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
+    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);
+    }
+    return tilemap;
+}