# HG changeset patch # User fred@reichbier.de # Date 1216470788 -7200 # Node ID f4b89014ad3966951b78c94bb05869176616ae26 # Parent 292df259cc859eab104249a6de33fbc9f16c8b05 added moving figure stuff + animated sprites. not usable atm. diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/gfx/README Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,3 @@ +Most of this graphics are from 'reiners Tileset': http://reinerstileset.4players.de/ +Many thanks, reiner :-) + diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-1.png Binary file bin/gfx/f-1.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-10.png Binary file bin/gfx/f-10.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-11.png Binary file bin/gfx/f-11.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-12.png Binary file bin/gfx/f-12.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-2.png Binary file bin/gfx/f-2.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-3.png Binary file bin/gfx/f-3.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-4.png Binary file bin/gfx/f-4.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-5.png Binary file bin/gfx/f-5.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-6.png Binary file bin/gfx/f-6.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-7.png Binary file bin/gfx/f-7.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-8.png Binary file bin/gfx/f-8.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/f-9.png Binary file bin/gfx/f-9.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/mountain.png Binary file bin/gfx/mountain.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/spacecar-normal.png Binary file bin/gfx/spacecar-normal.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/spacecar-thrust1.png Binary file bin/gfx/spacecar-thrust1.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/spacecar-thrust2.png Binary file bin/gfx/spacecar-thrust2.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/gfx/water.png Binary file bin/gfx/water.png has changed diff -r 292df259cc85 -r f4b89014ad39 bin/map-example.xml --- a/bin/map-example.xml Fri Jul 18 16:12:41 2008 +0200 +++ b/bin/map-example.xml Sat Jul 19 14:33:08 2008 +0200 @@ -1,7 +1,10 @@ - 1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1 + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 + + + 0,3 diff -r 292df259cc85 -r f4b89014ad39 bin/tileset-example.xml --- a/bin/tileset-example.xml Fri Jul 18 16:12:41 2008 +0200 +++ b/bin/tileset-example.xml Sat Jul 19 14:33:08 2008 +0200 @@ -1,4 +1,5 @@ - - + + + diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/animatedsprite.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/animatedsprite.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,104 @@ +/* + 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 . +*/ + +module animatedsprite; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +class Frame { + Image image; + int length; // length in frames + + this(Image image, int length) { + this.image = image; + this.length = length; + } +} + +class Animation { + public Frame[] frames; + private int current_frame_idx; + private Frame current_frame; + private int frame_counter; + + this() { + + } + + /* add a frame */ + void add_frame(Image image, int length) { + this.frames ~= new Frame(image, length); + } + + void add_frame(Frame frame) { + this.frames ~= frame; + } + + /* internal. you do not have to call it */ + void play() { + this.current_frame = this.frames[0]; + this.current_frame_idx = 0; + } + + /* return the current image or null if the animation ended */ + Image get_image() { + if(this.frame_counter >= this.current_frame.length) { + this.frame_counter = 0; + this.current_frame_idx++; + if (this.frames.length <= this.current_frame_idx) { + // animation stop + return null; + } + this.current_frame = this.frames[this.current_frame_idx]; + } + this.frame_counter++; + return this.current_frame.image; + } +} + +class AnimatedSprite : Sprite { + private Animation current_animation = null; + private bool is_loop = false; + + /* play an animation now. */ + void play_animation(Animation animation, bool loop=false) { + this.current_animation = animation; + this.is_loop = loop; + this.current_animation.play(); + } + + bool is_playing() { + return (this.current_animation !is null); + } + + /* update everything. Perfect for an animated sprite */ + void update() { + if(this.current_animation) { + Image img = this.current_animation.get_image(); + if(img) { + this.setImage(img); + } + else if (this.is_loop) { + this.current_animation.play(); // is a loop, play again + } else { + this.current_animation = null; + } + } + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/consumer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/consumer.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,48 @@ +/* + 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 . +*/ + +module consumer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import renderer; + +class Consumer { + protected Renderer renderer; + + this(Renderer renderer) { + this.renderer = renderer; + } + + /* handle the event `evt`. Return true if the event was handled and should not + be propagated any further, otherwise false */ + bool handle_event(Event evt) { + return false; + } + + /* draw all the content of this with this.app.draw(stuff) */ + void draw() { + + } + + /* called for each loop iteration */ + void loop_iteration() { + + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/imagecache.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/imagecache.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,45 @@ +/* + 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 . +*/ + +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]; + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/movingfigure.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/movingfigure.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,91 @@ +/* + 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 . +*/ + +module movingfigure; + +import Integer = tango.text.convert.Integer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import spriteconsumer; +import animatedsprite; +import renderer; +import 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); +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/renderer.d --- /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 . +*/ + +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(); + } + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/sprite.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/sprite.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,7 @@ +module sprite; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all : Sprite = SFSprite; + +import renderer; diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/spriteconsumer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/spriteconsumer.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,45 @@ +/* + 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 . +*/ + +module spriteconsumer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import animatedsprite; +import consumer; +import renderer; + +class SpriteConsumer : Consumer { + private Sprite[] sprites; + + this(Renderer renderer) { + super(renderer); + } + + void add_sprite(Sprite sprite) { + this.sprites ~= sprite; + } + + void draw() { + foreach(Sprite sprite; this.sprites) { + this.renderer.draw(sprite); + } + } +} + diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/test.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/test.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,60 @@ +/* + 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 . +*/ + +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; +import xmlmap; +import tools; +import viewconsumer; +import animatedsprite; +import spriteconsumer; +import movingfigure; + +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); + map.set_view(render.app.getView()); + render.add_consumer(consumer); + render.add_consumer(load_charset(render, cache, "f-", ".png", 10)); +/* SpriteConsumer c = new SpriteConsumer(render); + AnimatedSprite s = new AnimatedSprite; + s.setX(10); + s.setY(10); + Animation a = new Animation; + a.add_frame(map.tileset.tiles[0], 10); + a.add_frame(map.tileset.tiles[1], 10); +// s.setImage(map.tileset.tiles[0]); + s.play_animation(a, true); + c.add_sprite(s); + render.add_consumer(c);*/ + + render.mainloop(); + return 0; +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/tileconsumer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/tileconsumer.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,55 @@ +/* + 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 . +*/ + +module tileconsumer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; +import tango.io.Stdout; + +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.renderer.draw(sprite); + } + } + + bool handle_event(Event evt) { + if (evt.Type == Event.EventType.MOUSEBUTTONPRESSED) { + Input input = this.renderer.app.getInput(); + Vector2i tile = this.map.real_to_tile(0, input.getMouseX(), input.getMouseY()); + if(this.map.has_tile(0, tile.x, tile.y)) { + Stdout.formatln("You clicked on the tile {}, {}", tile.x, tile.y); + } + return true; + } + return false; + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/tilemap.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/tilemap.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,101 @@ +/* + 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 . +*/ + +module tilemap; + +import tango.math.Math; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import tileset; +import tango.io.Stdout; + +typedef Sprite[] SpriteArray; + +class Tilemap { + public int[int][int][int] map; // Layer: x: y: Tile-ID + public Vector2i[int] layer_tsizes; // Layer: Tile size + public Tileset tileset; + public int width, height, tilewidth, tileheight; + private View view; + + 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; + } + + /* set the view. you should do this if you use a sf::View */ + void set_view(View view) { + this.view = view; + } + + /* translate On-Screen -> With-View */ + Vector2i translate_with_view(int real_x, int real_y) { + Vector2i vec = Vector2i(real_x, real_y); + if(this.view) { + FloatRect rect = this.view.getRect(); + vec.x += rect.getLeft(); + vec.y += rect.getTop(); + } + return vec; + } + + Vector2i real_to_tile(int layer, int real_x, int real_y) { + Vector2i vec = this.translate_with_view(real_x, real_y); + return Vector2i(rndint(floor(vec.x / this.layer_tsizes[layer].x)), rndint(floor(vec.y / this.layer_tsizes[layer].y))); + } + + Vector2i tile_to_real(int layer, int tile_x, int tile_y) { + return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y); + } + + bool has_tile(int layer, int x, int y) { + return (layer in this.map && x in this.map[layer] && y in this.map[layer][x]); + } + + SpriteArray get_sprites() { + SpriteArray sprites; + Sprite sprite; + int layer_id=0; + Vector2i pos; + foreach(int[int][int] layer; map) { + for(int x=0; x. +*/ + +module tileset; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import imagecache; + +alias Image[int] TileList; + +class Tileset { + private Cache cache; + public TileList tiles; + + this(Cache cache) { + this.cache = cache; + } + + void add_tile(int id, char[] filename) { + this.tiles[id] = this.cache.get_image(filename); + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/tools.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/tools.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,27 @@ +/* + 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 . +*/ + +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; +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/viewconsumer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/viewconsumer.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,69 @@ +/* + 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 . +*/ + +module viewconsumer; + +import dsfml.window.all; +import dsfml.system.all; +import dsfml.graphics.all; + +import consumer; +import renderer; +import tango.io.Stdout; + +/* That is a consumer which sets and manipulates the render window view */ +class ViewConsumer : Consumer { + protected View view; + + this(Renderer renderer) { + super(renderer); + // set the initial view + this.view = renderer.app.getView(); + this.view.setFromRect(new FloatRect(0, 0, renderer.app.getWidth(), renderer.app.getHeight())); + this.update_view(); + } + + protected void update_view() { + this.renderer.app.setView(this.view); + } + + void move_view(float x=0, float y=0) { + this.view.move(x, y); + } +} + +class InteractiveViewConsumer : ViewConsumer { + this(Renderer renderer) { + super(renderer); + } + + bool handle_event(Event evt) { + if (evt.Type == Event.EventType.KEYPRESSED) { + float x=0, y=0; + if(evt.Key.Code == KeyCode.LEFT) x -= 2; + if(evt.Key.Code == KeyCode.RIGHT) x += 2; + if(evt.Key.Code == KeyCode.UP) y -= 2; + if(evt.Key.Code == KeyCode.DOWN) y += 2; + if(x != 0 || y != 0) { + Stdout.formatln("{} fps", 1.0 / this.renderer.app.getFrameTime()); + this.move_view(x, y); + return true; + } + } + return false; + } +} diff -r 292df259cc85 -r f4b89014ad39 import/myrrdin/xmlmap.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/import/myrrdin/xmlmap.d Sat Jul 19 14:33:08 2008 +0200 @@ -0,0 +1,88 @@ +/* + 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 . +*/ + +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; +import dsfml.system.all; +import Text = tango.text.Util; +static import tango.core.Exception; + +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 + int layer_id=0; + 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); + tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height); + char[][] content_ = Text.delimit(Text.trim(layer_node.value), ","); + int i=0; + bool running = true; + for(int x=0; x < width; x++) { + for(int y=0; y < height; y++) { + try { + tilemap.map[layer_id][x][y] = Integer.parse(content_[i]); + } + catch (tango.core.Exception.ArrayBoundsException) { + // array out of bounds ... break + running = false; + break; + } + i++; + } + if(!running) { + break; + } + } + layer_id++; + } + return tilemap; +} diff -r 292df259cc85 -r f4b89014ad39 src/animatedsprite.d --- a/src/animatedsprite.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -module animatedsprite; - -import dsfml.window.all; -import dsfml.system.all; -import dsfml.graphics.all; - -class Frame { - Image image; - int length; // length in frames - - this(Image image, int length) { - this.image = image; - this.length = length; - } -} - -class Animation { - private Frame[] frames; - private int current_frame_idx; - private Frame current_frame; - private int frame_counter; - - this() { - - } - - void add_frame(Image image, int length) { - this.frames ~= new Frame(image, length); - } - - void play() { - this.current_frame = this.frames[0]; - this.current_frame_idx = 0; - } - - Image get_image() { - if(this.frame_counter >= this.current_frame.length) { - this.frame_counter = 0; - this.current_frame_idx++; - if (this.frames.length <= this.current_frame_idx) { - // animation stop - return null; - } - this.current_frame = this.frames[this.current_frame_idx]; - } - this.frame_counter++; - return this.current_frame.image; - } -} - -class AnimatedSprite : Sprite { - private Animation current_animation = null; - private bool is_loop = false; - - /* play an animation now. */ - void play_animation(Animation animation, bool loop=false) { - this.current_animation = animation; - this.is_loop = loop; - this.current_animation.play(); - } - - /* update everything. Perfect for an animated sprite */ - void update() { - if(this.current_animation) { - Image img = this.current_animation.get_image(); - if(img) { - this.setImage(img); - } - else if (this.is_loop) { - this.current_animation.play(); // is a loop, play again - } else { - this.current_animation = null; - } - } - } -} diff -r 292df259cc85 -r f4b89014ad39 src/consumer.d --- a/src/consumer.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -module consumer; - -import dsfml.window.all; -import dsfml.system.all; -import dsfml.graphics.all; - -import renderer; - -class Consumer { - protected Renderer renderer; - - this(Renderer renderer) { - this.renderer = renderer; - } - - /* handle the event `evt`. Return true if the event was handled and should not - be propagated any further, otherwise false */ - bool handle_event(Event evt) { - return false; - } - - /* draw all the content of this with this.app.draw(stuff) */ - void draw() { - - } -} diff -r 292df259cc85 -r f4b89014ad39 src/imagecache.d --- a/src/imagecache.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -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]; - } -} diff -r 292df259cc85 -r f4b89014ad39 src/renderer.d --- a/src/renderer.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -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; - } - } - } - // draw all - foreach(Consumer consumer; this.draw_consumers) { - consumer.draw(); - } - // display all - this.app.display(); - } - } -} diff -r 292df259cc85 -r f4b89014ad39 src/sprite.d --- a/src/sprite.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -module sprite; - -import dsfml.window.all; -import dsfml.system.all; -import dsfml.graphics.all : Sprite = SFSprite; - -import renderer; diff -r 292df259cc85 -r f4b89014ad39 src/spriteconsumer.d --- a/src/spriteconsumer.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -module spriteconsumer; - -import dsfml.window.all; -import dsfml.system.all; -import dsfml.graphics.all; - -import animatedsprite; -import consumer; -import renderer; - -class SpriteConsumer : Consumer { - private Sprite[] sprites; - - this(Renderer renderer) { - super(renderer); - } - - void add_sprite(Sprite sprite) { - this.sprites ~= sprite; - } - - void draw() { - foreach(Sprite sprite; this.sprites) { - this.renderer.draw(sprite); - } - } -} - diff -r 292df259cc85 -r f4b89014ad39 src/test.d --- a/src/test.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -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; -import xmlmap; -import tools; -import viewconsumer; -import animatedsprite; -import spriteconsumer; - -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); -// render.add_consumer(consumer); - SpriteConsumer c = new SpriteConsumer(render); - AnimatedSprite s = new AnimatedSprite; - s.setX(10); - s.setY(10); - Animation a = new Animation; - a.add_frame(map.tileset.tiles[0], 10); - a.add_frame(map.tileset.tiles[1], 10); -// s.setImage(map.tileset.tiles[0]); - s.play_animation(a, true); - c.add_sprite(s); - render.add_consumer(c); - - render.mainloop(); - return 0; -} diff -r 292df259cc85 -r f4b89014ad39 src/tileconsumer.d --- a/src/tileconsumer.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -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.renderer.draw(sprite); - } - } -} diff -r 292df259cc85 -r f4b89014ad39 src/tilemap.d --- a/src/tilemap.d Fri Jul 18 16:12:41 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -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][int] map; // Layer: x: y: Tile-ID - public Vector2i[int] layer_tsizes; // Layer: Tile size - public 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; - } - - Vector2i real_to_tile(int layer, int real_x, int real_y) { - return Vector2i(rndint(floor(real_x / this.layer_tsizes[layer].x)), rndint(floor(real_y / this.layer_tsizes[layer].y))); - } - - Vector2i tile_to_real(int layer, int tile_x, int tile_y) { - return Vector2i(tile_x*this.layer_tsizes[layer].x, tile_y*this.layer_tsizes[layer].y); - } - - SpriteArray get_sprites() { - SpriteArray sprites; - Sprite sprite; - int layer_id=0; - Vector2i pos; - foreach(int[int][int] layer; map) { - for(int x=0; x