comparison 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
comparison
equal deleted inserted replaced
4:292df259cc85 5:f4b89014ad39
1 /*
2 This file is part of myrrdin.
3
4 myrrdin is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of
7 the License, or (at your option) any later version.
8
9 myrrdin is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with myrrdin. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 module renderer;
19
20 import dsfml.window.all;
21 import dsfml.system.all;
22 import dsfml.graphics.all;
23
24 import consumer;
25 import imagecache;
26 import animatedsprite;
27
28 class Renderer {
29 public RenderWindow app;
30 public Cache cache;
31 private Consumer[] draw_consumers;
32 private Consumer[] event_consumers;
33
34 this(char[] title, int width, int height, int depth=32) {
35 this.app = new RenderWindow(VideoMode(width, height, depth), title);
36 this.app.setFramerateLimit(40);
37
38 this.cache = new Cache("."); // TODO
39 }
40
41 /* add a consumer for event handling and drawing */
42 void add_consumer(Consumer consumer) {
43 this.add_event_consumer(consumer);
44 this.add_draw_consumer(consumer);
45 }
46
47 /* add a consumer only for event handling */
48 void add_event_consumer(Consumer consumer) {
49 this.event_consumers ~= consumer;
50 }
51
52 /* add a consumer only for drawing */
53 void add_draw_consumer(Consumer consumer) {
54 this.draw_consumers ~= consumer;
55 }
56
57 /* use this instead of this.app.draw - it contains a hook for animated sprites */
58 void draw(AnimatedSprite obj) {
59 obj.update();
60 this.app.draw(obj);
61 }
62
63 void draw(IDrawable obj) {
64 this.app.draw(obj);
65 }
66
67 void draw(Sprite obj) {
68 if(cast(AnimatedSprite)obj !is null) {
69 this.draw(cast(AnimatedSprite)obj);
70 }
71 else {
72 this.app.draw(obj);
73 }
74 }
75
76 /* start the mainloop */
77 void mainloop() {
78 Event evt;
79
80 while(this.app.isOpened()) {
81 // handle all events
82 while(this.app.getEvent(evt)) {
83 if (evt.Type == Event.EventType.CLOSED) {
84 this.app.close();
85 } else {
86 foreach(Consumer consumer; this.event_consumers) {
87 if(consumer.handle_event(evt)) break;
88 }
89 }
90 }
91 // loop iteration
92 foreach(Consumer consumer; this.draw_consumers) {
93 consumer.loop_iteration();
94 }
95 // draw all
96 foreach(Consumer consumer; this.draw_consumers) {
97 consumer.draw();
98 }
99 // display all
100 this.app.display();
101 }
102 }
103 }