@@ -1,27 +1,35 @@ import dfl.all; import util; import game; import output; +import driver; //import solver; +pragma(lib, "dfl.lib"); -class GUI : Form +class GUI : Form, GameObserver { + bool on_game_changed(char c, const(Game) g, bool finished) { + draw(gr, g); + invalidate(); + return false; + } + private { - Game g; int cell; int turn = 0; Font font; Color[char] colors; string[char] render; + void delegate(char c) fn; } - this(Game g) + this(const(Game) g) { noMessageFilter(); this.setStyle(ControlStyles.OPAQUE, true); - this.g = g; + this.fn = fn; this.paint ~= &my_paint; this.keyDown ~= &my_keydown; @@ -29,9 +37,12 @@ this.maximizeBox = false; this.minimizeBox = false; this.cell = min(1024/g.map.W, 640/g.map.H); this.clientSize = Size(g.map.W*cell, g.map.H*cell); - set_text(); + + const scrH = this.clientSize.height; + const scrW = this.clientSize.width; + this.gr = new MemoryGraphics(scrW, scrH); // Resources this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL); this.backColor = Color(255,255,255); @@ -52,21 +63,29 @@ this.render['R'] = "☃"; this.render['D'] = "☠"; this.render['L'] = "☒"; this.render['O'] = "☐"; + draw(gr, g); + } + + void set_fn(F)(F f) { this.fn = f; } + + void run() { + Application.run(this); } private: + Graphics gr; + void my_paint(Control, PaintEventArgs ev) { - const scrH = this.clientSize.height; - const scrW = this.clientSize.width; - Graphics gr = new MemoryGraphics(scrW, scrH, ev.graphics); - scope(exit) { - gr.copyTo(ev.graphics, Rect(0,0,scrW,scrH)); - gr.dispose(); - } + gr.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height)); + } + void draw(Graphics gr, const(Game) g) + { + int scrW = this.clientSize.width; + int scrH = this.clientSize.height; // Fill bg. gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH)); // Fill water. @@ -83,37 +102,35 @@ c = 'D'; gr.drawText(this.render[c], font, this.colors[c], r); } } + + set_text(g); } void my_keydown(Control c, KeyEventArgs ev) { switch(ev.keyCode) { - case Keys.DOWN: g.command('D'); break; - case Keys.UP: g.command('U'); break; - case Keys.LEFT: g.command('L'); break; - case Keys.RIGHT: g.command('R'); break; - case Keys.W: g.command('W'); break; - case Keys.A: g.command('A'); break; -// case Keys.G: solver.act(g); break; + case Keys.DOWN: fn('D'); break; + case Keys.UP: fn('U'); break; + case Keys.LEFT: fn('L'); break; + case Keys.RIGHT: fn('R'); break; + case Keys.W: fn('W'); break; + case Keys.A: fn('A'); break; default: break; } - if(g.cleared) - Application.exit(); - invalidate(); - set_text(); } - void set_text() { + void set_text(const(Game) g) { this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise); } } void main(string[] args) { - auto g = Game.load(File(args[1])); - g.set_output(new GuardedOutput(g)); - auto myForm = new GUI(g); - Application.run(myForm); + auto d = new Driver(File(args[1])); + d.addObserver!(GuardedOutput)(); + GUI g = d.addObserver!(GUI)(); + g.set_fn(&d.command); + g.run(); }