Mercurial > projects > myrrdin
changeset 10:79b534bbda65
new director model
author | fred@reichbier.de |
---|---|
date | Sat, 19 Jul 2008 19:29:00 +0200 |
parents | adc5e1eedb8a |
children | 5866d9f2ca75 |
files | import/myrrdin/consumers/consumer.d import/myrrdin/consumers/movingfigure.d import/myrrdin/consumers/sprite.d import/myrrdin/consumers/tile.d import/myrrdin/consumers/view.d import/myrrdin/director.d import/myrrdin/movingfigure.d import/myrrdin/renderer.d import/myrrdin/scenes/scene.d import/myrrdin/tileset.d import/myrrdin/tools.d import/myrrdin/xmlmap.d test/director/testdirector.d test/movingfigure/movingfigure.d test/view/viewconsumer.d |
diffstat | 15 files changed, 295 insertions(+), 129 deletions(-) [+] |
line wrap: on
line diff
--- a/import/myrrdin/consumers/consumer.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/consumers/consumer.d Sat Jul 19 19:29:00 2008 +0200 @@ -28,12 +28,13 @@ import dsfml.graphics.all; import myrrdin.renderer; +import myrrdin.director; class Consumer { protected Renderer renderer; - this(Renderer renderer) { - this.renderer = renderer; + this() { + this.renderer = Director.renderer; } /* handle the event `evt`. Return true if the event was handled and should not
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/consumers/movingfigure.d Sat Jul 19 19:29:00 2008 +0200 @@ -0,0 +1,98 @@ +/* + myrrdin, a 2d tile engine + Copyright (c) 2008 Friedrich Weber + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the Software), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +module myrrdin.movingfigure; + +import Integer = tango.text.convert.Integer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import myrrdin.consumers.sprite; +import myrrdin.animatedsprite; +import myrrdin.renderer; +import myrrdin.imagecache; +import myrrdin.director; + +class MovingAnimatedFigureConsumer : SpriteConsumer { + private Animation ani_left, ani_right, ani_up, ani_down; + public AnimatedSprite sprite; + private Input input; + + this(Animation go_left, Animation go_right, Animation go_up, Animation go_down) { + super(); + this.ani_left = go_left; + this.ani_right = go_right; + this.ani_up = go_up; + this.ani_down = go_down; + this.sprite = new AnimatedSprite; + this.add_sprite(sprite); + this.sprite.setImage(this.ani_down.frames[0].image); + this.input = this.renderer.app.getInput(); + } + + void draw() { + super.draw(); + } + + void loop_iteration() { + float x=0, y=0; + if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1; + if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1; + if(this.input.isKeyDown(KeyCode.UP)) y -= 1; + if(this.input.isKeyDown(KeyCode.DOWN)) y += 1; + if(x != 0 || y != 0) { + this.sprite.move(x, y); + if (!this.sprite.is_playing()) { + if(x > 0) this.sprite.play_animation(this.ani_right); + if(x < 0) this.sprite.play_animation(this.ani_left); + if(y > 0) this.sprite.play_animation(this.ani_down); + if(y < 0) this.sprite.play_animation(this.ani_up); + } + } + } +} + +MovingAnimatedFigureConsumer load_charset(char[] prefix, char[] suffix, uint frame_duration=0) { + Image get_f(int id) { + return Director.cache.get_image(prefix~Integer.toString(id)~suffix); + } + Animation up = new Animation; + up.add_frame(get_f(1), frame_duration); + up.add_frame(get_f(2), frame_duration); + up.add_frame(get_f(3), frame_duration); + Animation right = new Animation; + right.add_frame(get_f(4), frame_duration); + right.add_frame(get_f(5), frame_duration); + right.add_frame(get_f(6), frame_duration); + Animation down = new Animation; + down.add_frame(get_f(7), frame_duration); + down.add_frame(get_f(8), frame_duration); + down.add_frame(get_f(9), frame_duration); + Animation left = new Animation; + left.add_frame(get_f(10), frame_duration); + left.add_frame(get_f(11), frame_duration); + left.add_frame(get_f(12), frame_duration); + return new MovingAnimatedFigureConsumer(left, right, up, down); +}
--- a/import/myrrdin/consumers/sprite.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/consumers/sprite.d Sat Jul 19 19:29:00 2008 +0200 @@ -34,8 +34,8 @@ class SpriteConsumer : Consumer { private Sprite[] sprites; - this(Renderer renderer) { - super(renderer); + this() { + super(); } void add_sprite(Sprite sprite) {
--- a/import/myrrdin/consumers/tile.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/consumers/tile.d Sat Jul 19 19:29:00 2008 +0200 @@ -36,8 +36,7 @@ class TileConsumer : Consumer { public Tilemap map; - this(Renderer renderer, Tilemap map) { - super(renderer); + this(Tilemap map) { this.map = map; }
--- a/import/myrrdin/consumers/view.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/consumers/view.d Sat Jul 19 19:29:00 2008 +0200 @@ -35,8 +35,8 @@ class ViewConsumer : Consumer { protected View view; - this(Renderer renderer) { - super(renderer); + this() { + super(); // set the initial view this.view = renderer.app.getView(); this.view.setFromRect(new FloatRect(0, 0, renderer.app.getWidth(), renderer.app.getHeight())); @@ -53,8 +53,8 @@ } class InteractiveViewConsumer : ViewConsumer { - this(Renderer renderer) { - super(renderer); + this() { + super(); } bool handle_event(Event evt) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/director.d Sat Jul 19 19:29:00 2008 +0200 @@ -0,0 +1,69 @@ +/* + myrrdin, a 2d tile engine + Copyright (c) 2008 Friedrich Weber + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +module myrrdin.director; + +import myrrdin.renderer; +import myrrdin.scenes.scene; +import myrrdin.consumers.consumer; +import myrrdin.imagecache; + +class DirectorClass { + public Renderer renderer; + private Scene current_scene = null; + public Cache cache; + + this(char[] gfx_dir, char[] title, int width, int height, int depth=32) { + this.renderer = new Renderer(title, width, height, depth); + this.cache = new Cache(gfx_dir); + } + + void run() { + this.renderer.mainloop(); + } + + void set_scene(Scene scene) { + if(this.current_scene) { + this.unset_scene(); + } + this.current_scene = scene; + this.renderer.draw_consumers ~= scene.draw_consumers; + this.renderer.event_consumers ~= scene.event_consumers; + } + + protected void unset_scene() { + foreach(Consumer c; this.renderer.draw_consumers) { + this.renderer.remove_consumer(c); + } + foreach(Consumer c; this.renderer.event_consumers) { + this.renderer.remove_consumer(c); + } + } +} + +void CreateDirector(char[] gfx_dir, char[] title, int width, int height, int depth=32) { + assert(Director is null, "You can set only one director."); + Director = new DirectorClass(gfx_dir, title, width, height, depth); +} + +DirectorClass Director = null;
--- a/import/myrrdin/movingfigure.d Sat Jul 19 17:27:34 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - myrrdin, a 2d tile engine - Copyright (c) 2008 Friedrich Weber - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the Software), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - */ - -module myrrdin.movingfigure; - -import Integer = tango.text.convert.Integer; - -import dsfml.window.all; -import dsfml.system.all; -import dsfml.graphics.all; - -import myrrdin.consumers.sprite; -import myrrdin.animatedsprite; -import myrrdin.renderer; -import myrrdin.imagecache; - -class MovingAnimatedFigureConsumer : SpriteConsumer { - private Animation ani_left, ani_right, ani_up, ani_down; - public AnimatedSprite sprite; - private Input input; - - this(Renderer renderer, Animation go_left, Animation go_right, Animation go_up, Animation go_down) { - super(renderer); - this.ani_left = go_left; - this.ani_right = go_right; - this.ani_up = go_up; - this.ani_down = go_down; - this.sprite = new AnimatedSprite; - this.add_sprite(sprite); - this.sprite.setImage(this.ani_down.frames[0].image); - this.input = this.renderer.app.getInput(); - } - - void draw() { - super.draw(); - } - - void loop_iteration() { - float x=0, y=0; - if(this.input.isKeyDown(KeyCode.LEFT)) x -= 1; - if(this.input.isKeyDown(KeyCode.RIGHT)) x += 1; - if(this.input.isKeyDown(KeyCode.UP)) y -= 1; - if(this.input.isKeyDown(KeyCode.DOWN)) y += 1; - if(x != 0 || y != 0) { - this.sprite.move(x, y); - if (!this.sprite.is_playing()) { - if(x > 0) this.sprite.play_animation(this.ani_right); - if(x < 0) this.sprite.play_animation(this.ani_left); - if(y > 0) this.sprite.play_animation(this.ani_down); - if(y < 0) this.sprite.play_animation(this.ani_up); - } - } - } -} - -MovingAnimatedFigureConsumer load_charset(Renderer renderer, Cache cache, char[] prefix, char[] suffix, uint frame_duration=0) { - Image get_f(int id) { - return cache.get_image(prefix~Integer.toString(id)~suffix); - } - Animation up = new Animation; - up.add_frame(get_f(1), frame_duration); - up.add_frame(get_f(2), frame_duration); - up.add_frame(get_f(3), frame_duration); - Animation right = new Animation; - right.add_frame(get_f(4), frame_duration); - right.add_frame(get_f(5), frame_duration); - right.add_frame(get_f(6), frame_duration); - Animation down = new Animation; - down.add_frame(get_f(7), frame_duration); - down.add_frame(get_f(8), frame_duration); - down.add_frame(get_f(9), frame_duration); - Animation left = new Animation; - left.add_frame(get_f(10), frame_duration); - left.add_frame(get_f(11), frame_duration); - left.add_frame(get_f(12), frame_duration); - return new MovingAnimatedFigureConsumer(renderer, left, right, up, down); -}
--- a/import/myrrdin/renderer.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/renderer.d Sat Jul 19 19:29:00 2008 +0200 @@ -33,8 +33,8 @@ class Renderer { public RenderWindow app; - private Consumer[] draw_consumers; - private Consumer[] event_consumers; + public Consumer[] draw_consumers; + public Consumer[] event_consumers; this(char[] title, int width, int height, int depth=32) { this.app = new RenderWindow(VideoMode(width, height, depth), title); @@ -57,6 +57,20 @@ this.draw_consumers ~= consumer; } + /* remove consumer from both lists */ + void remove_consumer(Consumer consumer) { + void del_c(Consumer[] carr, Consumer c) { + for(int i=0; i<carr.length; i++) { + if(carr[i] == c) { + carr = carr[0..i] ~ carr[i..$]; + break; // we hope there are no duplicates + } + } + } + del_c(this.draw_consumers, consumer); + del_c(this.event_consumers, consumer); + } + /* use this instead of this.app.draw - it contains a hook for animated sprites */ void draw(AnimatedSprite obj) { obj.update();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/scenes/scene.d Sat Jul 19 19:29:00 2008 +0200 @@ -0,0 +1,51 @@ +/* + myrrdin, a 2d tile engine + Copyright (c) 2008 Friedrich Weber + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +module myrrdin.scenes.scene; + +import myrrdin.consumers.consumer; + +class Scene { + public Consumer[] draw_consumers; + public Consumer[] event_consumers; + + this() { + } + + /* 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; + } + +}
--- a/import/myrrdin/tileset.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/tileset.d Sat Jul 19 19:29:00 2008 +0200 @@ -28,6 +28,7 @@ import dsfml.graphics.all; import myrrdin.imagecache; +import myrrdin.director; alias Image[int] TileList; @@ -35,8 +36,8 @@ private Cache cache; public TileList tiles; - this(Cache cache) { - this.cache = cache; + this() { + this.cache = Director.cache; } void add_tile(int id, char[] filename) {
--- a/import/myrrdin/tools.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/tools.d Sat Jul 19 19:29:00 2008 +0200 @@ -31,3 +31,4 @@ return content; } +
--- a/import/myrrdin/xmlmap.d Sat Jul 19 17:27:34 2008 +0200 +++ b/import/myrrdin/xmlmap.d Sat Jul 19 19:29:00 2008 +0200 @@ -30,7 +30,6 @@ import tango.text.convert.Integer; import tango.io.Stdout; // TODO import myrrdin.tools; -import myrrdin.imagecache; import dsfml.system.all; import Text = tango.text.Util; static import tango.core.Exception; @@ -41,8 +40,8 @@ return node.attribute(name).nodes[0].value; } -Tileset parse_tileset(Cache cache, char[] content) { - auto tileset = new Tileset(cache); +Tileset parse_tileset(char[] content) { + auto tileset = new Tileset; auto doc = new Document!(char); doc.parse(content); @@ -54,7 +53,7 @@ return tileset; } -Tilemap parse_map(Cache cache, char[] content) { +Tilemap parse_map(char[] content) { auto doc = new Document!(char); doc.parse(content); auto root = doc.query["map"]; @@ -63,7 +62,7 @@ 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 + auto tilemap = new Tilemap(parse_tileset(read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size int layer_id=0; foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) { int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/director/testdirector.d Sat Jul 19 19:29:00 2008 +0200 @@ -0,0 +1,29 @@ +/* + myrrdin, a 2d tile engine + Copyright (c) 2008 Friedrich Weber + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import myrrdin.director; + +void main() { + CreateDirector("gfx", 600, 480, 32); + +}
--- a/test/movingfigure/movingfigure.d Sat Jul 19 17:27:34 2008 +0200 +++ b/test/movingfigure/movingfigure.d Sat Jul 19 19:29:00 2008 +0200 @@ -25,23 +25,23 @@ import myrrdin.consumers.tile; import myrrdin.renderer; -import myrrdin.imagecache; import myrrdin.tilemap; import myrrdin.tileset; import myrrdin.xmlmap; import myrrdin.tools; import myrrdin.animatedsprite; import myrrdin.consumers.sprite; -import myrrdin.movingfigure; +import myrrdin.consumers.movingfigure; +import myrrdin.director; int main(char[][] args) { - Cache cache = new Cache("gfx"); - Tilemap map = parse_map(cache, read_file_contents("map-example.xml")); - Renderer render = new Renderer("Blubb", 600, 480, 32); - TileConsumer consumer = new TileConsumer(render, map); - map.set_view(render.app.getView()); + CreateDirector("gfx", "Blubb", 600, 480, 32); + Tilemap map = parse_map(read_file_contents("map-example.xml")); + TileConsumer consumer = new TileConsumer(map); + map.set_view(Director.renderer.app.getView()); + auto render = Director.renderer; render.add_consumer(consumer); - render.add_consumer(load_charset(render, cache, "f-", ".png", 10)); + render.add_consumer(load_charset("f-", ".png", 10)); render.mainloop(); return 0;
--- a/test/view/viewconsumer.d Sat Jul 19 17:27:34 2008 +0200 +++ b/test/view/viewconsumer.d Sat Jul 19 19:29:00 2008 +0200 @@ -36,13 +36,14 @@ import myrrdin.tools; import myrrdin.consumers.view; import myrrdin.animatedsprite; +import myrrdin.director; int main(char[][] args) { - Cache cache = new Cache("gfx"); - Tilemap map = parse_map(cache, read_file_contents("map-example.xml")); - Renderer render = new Renderer("Blubb", 600, 480, 32); - render.add_consumer(new InteractiveViewConsumer(render)); - TileConsumer consumer = new TileConsumer(render, map); + CreateDirector("gfx", "Blubb", 600, 480, 32); + Tilemap map = parse_map(read_file_contents("map-example.xml")); + TileConsumer consumer = new TileConsumer(map); + auto render = Director.renderer; + render.add_consumer(new InteractiveViewConsumer); map.set_view(render.app.getView()); render.add_consumer(consumer); render.mainloop();