public ChessServer(IPEndPoint localEP) : base(localEP) { board = new ChessBoard(); }
/// <summary> /// Helper function to render the Chessboard as an image. Eventually it would be neat to only render it when needed /// aka if it needs a redraw or whenever a piece was moved. /// </summary> private void ChessBoardRenderer() { Bitmap canvas = new Bitmap(64 * 8, 64 * 8); Graphics graphics = Graphics.FromImage(canvas); SolidBrush blackBrush = new SolidBrush(Color.Black); SolidBrush whiteBrush = new SolidBrush(Color.White); SolidBrush selectedBrush = new SolidBrush(Color.FromArgb(128, 128, 128, 128)); Pen selectedPen = new Pen(Color.LightGreen, 3); while (isRunning) { // Paint all the squares. This could eventually be merged with the piece renderer below, but for current // development and debugging it is easier to just keep them seperate. for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if ((x + y) % 2 == 0) { graphics.FillRectangle(blackBrush, x * 64, y * 64, 64, 64); } else { graphics.FillRectangle(whiteBrush, x * 64, y * 64, 64, 64); } } } if (!chessClient.Connected) { connectionStatusLabel.Text = "Not connected"; } else { ChessBoard board = chessClient.GetBoard(); connectionStatusLabel.Text = chessClient.Client.RemoteEndPoint.ToString(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Piece p = board.GetPieceAt(x, y); if (p == null) { continue; } graphics.DrawImage(p.GetImage(), x * 64, y * 64, 64, 64); } } // Draw the selection if there is one (else it is just rendered outside of the board, which we cannot see) graphics.FillRectangle(selectedBrush, mouseClickX * 64, mouseClickY * 64, 64, 64); graphics.DrawRectangle(selectedPen, mouseClickX * 64, mouseClickY * 64, 64, 64); } chessImageHolder.Image = canvas; Thread.Sleep(250); } }