diff import/myrrdin/renderer.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/renderer.d@292df259cc85
children 510541745cd1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/import/myrrdin/renderer.d	Sat Jul 19 14:33:08 2008 +0200
@@ -0,0 +1,103 @@
+/*
+    This file is part of myrrdin.
+
+    myrrdin is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Lesser General Public License as 
+    published by the Free Software Foundation, either version 3 of 
+    the License, or (at your option) any later version.
+
+    myrrdin is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public 
+    License along with myrrdin. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+module renderer;
+
+import dsfml.window.all;
+import dsfml.system.all;
+import dsfml.graphics.all;
+
+import consumer;
+import imagecache;
+import animatedsprite;
+
+class Renderer {
+    public RenderWindow app;
+    public Cache cache;
+    private Consumer[] draw_consumers;
+    private Consumer[] event_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
+    }
+
+    /* add a consumer for event handling and drawing */
+    void add_consumer(Consumer consumer) {
+	this.add_event_consumer(consumer);
+	this.add_draw_consumer(consumer);
+    }
+
+    /* add a consumer only for event handling */
+    void add_event_consumer(Consumer consumer) {
+	this.event_consumers ~= consumer;
+    }
+
+    /* add a consumer only for drawing */
+    void add_draw_consumer(Consumer consumer) {
+	this.draw_consumers ~= consumer;
+    }
+
+    /* use this instead of this.app.draw - it contains a hook for animated sprites */
+    void draw(AnimatedSprite obj) {
+	obj.update();
+	this.app.draw(obj);
+    }
+
+    void draw(IDrawable obj) {
+	this.app.draw(obj);
+    }
+
+    void draw(Sprite obj) {
+	if(cast(AnimatedSprite)obj !is null) {
+	    this.draw(cast(AnimatedSprite)obj);
+	}
+	else {
+	    this.app.draw(obj);
+	}
+    }
+
+    /* 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();
+		} else {
+		    foreach(Consumer consumer; this.event_consumers) {
+			if(consumer.handle_event(evt)) break;
+		    }
+		}		    
+	    }
+	    // loop iteration
+	    foreach(Consumer consumer; this.draw_consumers) {
+		consumer.loop_iteration();
+	    }
+	    // draw all
+	    foreach(Consumer consumer; this.draw_consumers) {
+		consumer.draw();
+	    }
+	    // display all
+	    this.app.display();
+	}
+    }
+}