17 define BOARD_WIDTH = SQUARE_W * 8;
18 define BOARD_HEIGHT = SQUARE_H * 8;
20 static char * names[12] =
22 "whitePawn", "whiteKnight", "whiteBishop", "whiteRook", "whiteQueen", "whiteKing",
23 "blackPawn", "blackKnight", "blackBishop", "blackRook", "blackQueen", "blackKing"
26 class Chess2D : Window
28 ChessState * chessState;
32 //BitmapResource boardBMP { ":board.pcx", window = this };
33 BitmapResource boardBMP { ":board.jpg", window = this };
35 isActiveClient = true, background = black, borderStyle = fixed, hasMinimize = true;
42 Bitmap chessPieces {};
43 chessPieces.Load(":pieces.pcx", null, null);
46 piecesBMP[c] = Bitmap {};
47 piecesBMP[c].Allocate(null, SQUARE_W, SQUARE_H, 0, chessPieces.format, true);
48 piecesBMP[c].Grab(chessPieces, (c % 6) * SQUARE_H,(c / 6) * SQUARE_H);
49 piecesBMP[c].transparent = true;
50 piecesBMP[c].MakeDD(displaySystem);
57 char fileName[MAX_FILENAME];
58 sprintf(fileName, ":%s.png", names[c]);
59 piecesBMP[c] = Bitmap{};
60 piecesBMP[c].LoadT(fileName, null, displaySystem);
65 bool OnResizing(int * w, int * h)
67 *w = Max(*w, BOARD_WIDTH + OFFSET_X * 2);
68 *h = Max(*h, BOARD_HEIGHT + OFFSET_Y * 2);
72 void OnUnloadGraphics()
82 void OnRedraw(Surface surface)
88 flip = chessState->turn == Black;
90 flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
93 surface.SetForeground(white);
94 surface.Blit(boardBMP.bitmap, 0,0, 0,0, boardBMP.bitmap.width, boardBMP.bitmap.height);
96 if(chessState->gameRunning)
103 if(!dragging || x != moving.x || y != moving.y)
105 Piece atBoard = chessState->board[y][x];
106 int piece = (int)atBoard.player * 6 + (int)atBoard.type;
109 rx = flip ? (7-x) : x;
110 ry = flip ? y : (7-y);
112 surface.Blit(piecesBMP[piece-1],
113 OFFSET_X + rx * SQUARE_W, OFFSET_Y + ry * SQUARE_H,
114 0,0, piecesBMP[piece-1].width, piecesBMP[piece-1].height);
122 Piece atBoard = chessState->board[moving.y][moving.x];
123 int piece = (int)atBoard.player * 6 + (int)atBoard.type;
125 rx = flip ? (7-drag.x) : drag.x;
126 ry = flip ? drag.y : (7-drag.y);
128 surface.Blit(piecesBMP[piece-1],
129 OFFSET_X + rx * SQUARE_W, OFFSET_Y + ry * SQUARE_H,
130 0,0, piecesBMP[piece-1].width, piecesBMP[piece-1].height);
135 bool OnLeftButtonDown(int x, int y, Modifiers mods)
139 flip = chessState->turn == Black;
141 flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
144 x = (x - OFFSET_X) / SQUARE_W;
145 y = (y - OFFSET_Y) / SQUARE_H;
147 if(!flip) y = 7-y; else x = 7-x;
149 if(chessState->gameRunning &&
150 x < 8 && y < 8 && x >= 0 && y >= 0 && chessState->board[y][x] &&
151 chessState->isLocalPlayer[chessState->turn] &&
152 chessState->board[y][x].player == chessState->turn)
155 drag.x = moving.x = x;
156 drag.y = moving.y = y;
158 SetMouseRange( Box { OFFSET_X, OFFSET_Y, OFFSET_X + BOARD_WIDTH - 1, OFFSET_Y + BOARD_HEIGHT - 1 } );
163 bool OnLeftButtonUp(int x, int y, Modifiers mods)
168 flip = chessState->turn == Black;
170 flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
173 if(chessState->gameRunning)
175 x = (x - OFFSET_X) / SQUARE_W;
176 y = (y - OFFSET_Y) / SQUARE_H;
178 if(!flip) y = 7-y; else x = 7-x;
180 if(x < 8 && y < 8 && x >= 0 && y >= 0 && dragging)
185 ((Chess)master).ProcessUserMove(moving.x, moving.y, x, y);
191 bool OnMouseMove(int x, int y, Modifiers mods)
196 flip = chessState->turn == Black;
198 flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
201 x = (x - OFFSET_X) / SQUARE_W;
202 y = (y - OFFSET_Y) / SQUARE_H;
204 if(!flip) y = 7-y; else x = 7-x;
206 if(x < 8 && y < 8 && x >= 0 && y >= 0 && dragging)
215 property ChessState * chessState { set { chessState = value; } }