using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WorldOfPeacecraft { class Gui : Form, IGui { private const int tileSize = 16; private const int entitySize = 10; private IBackend Backend; private Panel Board = new Panel(); public Gui () { AllocConsole(); InitializeComponents(); } public void SetBackend (IBackend backend) { Backend = backend; } public void InitializeComponents () { this.SuspendLayout(); this.Size = new Size(400, 400); Board.Location = new Point(0,0); Board.Size = new Size(400, 400); Board.Paint += DoPaint; this.DoubleBuffered = true; this.MaximizeBox = false; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "WorldOfPeacecraft"; this.ShowIcon = false; this.Controls.Add(Board); this.ResumeLayout(); this.PerformLayout(); } public void DoPaint (object source, PaintEventArgs args) { BufferedGraphics buffer = BufferedGraphicsManager.Current.Allocate (Board.CreateGraphics (), Board.DisplayRectangle); Graphics g = buffer.Graphics; PaintMap(g); buffer.Render(); } public void PaintMap (Graphics g) { ITile[,] Map = Backend.GetMap (); if (Map != null) { for (int y = 0; y < Map.GetLength(1); y++) { for (int x = 0; x < Map.GetLength(0); x++) { PaintTile (g, Map [x, y], x, y); } } } else { g.FillRectangle(new SolidBrush(Color.White), Board.DisplayRectangle); } } public void PaintTile (Graphics g, ITile tile, int x, int y) { int posx = x * tileSize; int posy = y * tileSize; Color color; if (tile.IsForest ()) { color = Color.Green; } // Stupid parenthesis else if (tile.IsHuntable ()) { color = Color.BurlyWood; } else if (tile.IsWalkable ()) { color = Color.Yellow; } else if (tile.IsWall ()) { color = Color.DarkGray; } else if (tile.IsWater ()) { color = Color.Blue; } else { color = Color.Black; } g.FillRectangle(new SolidBrush(color), posx, posy, tileSize, tileSize); } public void PerformRefresh () { this.BeginInvoke(new MethodInvoker(delegate { ITile[,] map = Backend.GetMap(); int mapWidth = (map.GetLength(0)) * tileSize; int mapHeight = (map.GetLength(1)) * tileSize; this.SuspendLayout(); this.SetClientSizeCore(mapWidth, mapHeight); Board.Size = new Size(mapWidth, mapHeight); this.ResumeLayout(); this.PerformLayout(); this.Refresh(); })); } [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AllocConsole(); } }