changeset 0:a2d653eb9e99

first working version.
author fred@reichbier.de
date Thu, 17 Jul 2008 18:52:55 +0200
parents
children f193d0c14685
files .hgignore bin/gfx/grass-stone-water-east.png bin/gfx/grass-stone-water-l.png bin/gfx/grass-stone-water-north.png bin/gfx/grass.png dsss.conf src/consumer.d src/imagecache.d src/renderer.d src/test.d src/tileconsumer.d src/tilemap.d src/tileset.d
diffstat 13 files changed, 232 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,5 @@
+syntax: regexp
+^dsss_objs
+
+syntax: glob
+bin/test
Binary file bin/gfx/grass-stone-water-east.png has changed
Binary file bin/gfx/grass-stone-water-l.png has changed
Binary file bin/gfx/grass-stone-water-north.png has changed
Binary file bin/gfx/grass.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dsss.conf	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,4 @@
+[src/test.d]
+target = bin/test
+buildflags += -Isrc -I/usr/include/d/ -S/usr/lib -L-ldsfml-graphics -L-ldsfml-window -L-ldsfml-system
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/consumer.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,20 @@
+module consumer;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import renderer;
+
+class Consumer {
+    protected RenderWindow app;
+
+    this(Renderer renderer) {
+	this.app = renderer.app;
+    }
+
+    void draw() {
+	Shape shape = Shape.circle(300, 300, 100, Color.WHITE);
+	this.app.draw(shape);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/imagecache.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,28 @@
+module imagecache;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import tango.io.vfs.FileFolder;
+
+class Cache {
+    private FileFolder filefolder;
+    private Image[char[]] images;
+
+    this(char[] folder) {
+	this.filefolder = new FileFolder(folder, false);
+    }
+
+    void load(char[] real_filename, char[] seen_filename) {
+	this.images[seen_filename] = new Image();
+	this.images[seen_filename].loadFromFile(real_filename);
+    }
+
+    Image get_image(char[] filename) {
+	if (!(filename in this.images)) {
+	    this.load(this.filefolder.file(filename).toString(), filename);
+	}
+	return this.images[filename];
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/renderer.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,45 @@
+module renderer;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import consumer;
+import imagecache;
+
+class Renderer {
+    public RenderWindow app;
+    public Cache cache;
+    private Consumer[] consumers;
+    
+    this(char[] title, int width, int height, int depth=32) {
+	this.app = new RenderWindow(VideoMode(width, height, depth), title);
+	this.app.setFramerateLimit(40);
+
+	this.cache = new Cache("."); // TODO
+    }
+
+    void add_consumer(Consumer consumer) {
+	this.consumers ~= consumer;
+    }
+
+    /* start the mainloop */
+    void mainloop() {
+	Event evt;
+
+	while(this.app.isOpened()) {
+	    // handle all events
+	    while(this.app.getEvent(evt)) {
+		if (evt.Type == Event.EventType.CLOSED) {
+		    this.app.close();
+		}		    
+	    }
+	    // draw all
+	    foreach(Consumer consumer; this.consumers) {
+		consumer.draw();
+	    }
+	    // display all
+	    this.app.display();
+	}
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,29 @@
+module test;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import tileconsumer;
+import renderer;
+import consumer;
+import imagecache;
+import tilemap;
+import tileset;
+
+int main(char[][] args) {
+    Cache cache = new Cache("gfx");
+    Tileset tileset = new Tileset(cache);
+    Tilemap map = new Tilemap(tileset, 5, 5, 32, 32);
+    tileset.add_tile(0, "grass.png");
+    for(int x=0; x < 5; x++) {
+	map.map[x][x] = 0;
+    }
+
+    Renderer render = new Renderer("Blubb", 600, 480, 32);
+    TileConsumer consumer = new TileConsumer(render, map);
+    render.add_consumer(consumer);
+
+    render.mainloop();
+    return 0; 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tileconsumer.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,25 @@
+module tileconsumer;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import consumer;
+import renderer;
+import tileset;
+import tilemap;
+
+class TileConsumer : Consumer {
+    public Tilemap map;
+
+    this(Renderer renderer, Tilemap map) {
+	super(renderer);
+	this.map = map;
+    }
+
+    void draw() {
+	 foreach(Sprite sprite; this.map.get_sprites()) {
+	    this.app.draw(sprite);
+	 }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tilemap.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,56 @@
+module tilemap;
+
+import tango.math.Math;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import tileset;
+
+typedef Sprite[] SpriteArray;
+
+class Tilemap {
+    public int[int][int] map;
+    private Tileset tileset;
+    public int width, height, tilewidth, tileheight;
+
+    this(Tileset tileset, int width, int height, int tilewidth, int tileheight) {
+	this.tileset = tileset;
+	this.width = width;
+	this.height = height;
+	this.tilewidth = tilewidth;
+	this.tileheight = tileheight;
+    }
+
+    void real_to_tile(int real_x, int real_y, inout int tile_x, inout int tile_y) {
+	tile_x = rndint(floor(real_x / this.tilewidth));
+	tile_y = rndint(floor(real_y / this.tileheight));
+    }
+
+    void tile_to_real(int tile_x, int tile_y, inout int real_x, inout int real_y) {
+	real_x = tile_x*this.tilewidth;
+	real_y = tile_y*this.tileheight;
+    }
+
+    SpriteArray get_sprites() {
+	SpriteArray sprites;
+	Sprite sprite;
+	int real_x, real_y;
+	for(int x=0; x<width; x++) {
+	    if(x in this.map) {
+		for(int y=0; y<height; y++) {
+		    if(y in this.map[x]) {
+			sprite = new Sprite;
+			tile_to_real(x, y, real_x, real_y);
+			sprite.setX(real_x);
+			sprite.setY(real_y);
+			sprite.setImage(this.tileset.tiles[this.map[x][y]]);
+			sprites ~= sprite;
+		    }
+		}
+	    }
+	}
+	return sprites;	
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tileset.d	Thu Jul 17 18:52:55 2008 +0200
@@ -0,0 +1,20 @@
+module tileset;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import imagecache;
+
+class Tileset {
+    private Cache cache;
+    public Image[int] tiles;
+
+    this(Cache cache) {
+	this.cache = cache;	
+    }
+
+    void add_tile(int id, char[] filename) {
+	this.tiles[id] = this.cache.get_image(filename);
+    }
+}