3 define scale = (float)clientSize.h / boardBmp.bitmap.height;
4 define upperLeftX = 334;
5 define upperLeftY = 147;
6 define spaceX = (676 - upperLeftX);
7 define spaceY = (485 - upperLeftY);
8 define whiteDrawer = 145;
9 define blackDrawer = 1885;
10 define topDrawer = 126;
11 define stoneOverlap = 0.75;
13 class MainWindow : Window
15 GameConnection player1 { };
16 GameConnection player2 { };
20 borderStyle = sizable;
24 clientSize = { 1019, 824 };
28 BitmapResource boardBmp { ":board.jpg", window = this };
29 BitmapResource arrowBmp { ":arrow.png", alphaBlend = true, window = this };
30 BitmapResource removeBmp { ":remove.png", alphaBlend = true, window = this };
31 Array<BitmapResource> stoneBmps
34 BitmapResource { ":black.png", alphaBlend = true, window = this },
35 BitmapResource { ":white.png", alphaBlend = true, window = this },
36 BitmapResource { ":blackGray.png", alphaBlend = true, window = this },
37 BitmapResource { ":whiteGray.png", alphaBlend = true, window = this }
41 Menu fileMenu { menu, "File", f };
44 fileMenu, "New Game", n, ctrlN;
46 bool NotifySelect(MenuItem selection, Modifiers mods)
53 MenuDivider { fileMenu };
54 MenuItem exit { fileMenu, "Exit", x, altF4, NotifySelect = MenuFileExit };
65 void DrawBitmap(Surface surface, BitmapResource res, int x, int y, float s)
67 Bitmap board = boardBmp.bitmap;
68 Bitmap bmp = res.bitmap;
69 int bw = (int)(board.width * scale);
70 int bh = clientSize.h;
71 int bx = (clientSize.w - bw) / 2;
72 int by = (clientSize.h - bh) / 2;
74 surface.Filter(res.bitmap,
75 bx + (int)(x * scale), by + (int)(y * scale), 0,0,
76 (int)(s*bmp.width * scale), (int)(s*bmp.height * scale),
77 bmp.width, bmp.height);
80 void DrawStone(Surface surface, Point where, Stone color)
82 Bitmap stone = stoneBmps[color].bitmap;
83 float x = upperLeftX + (where.x + .5f) * spaceX - stone.width / 2;
84 float y = upperLeftY + (where.y + .5f) * spaceY - stone.height / 2;
85 DrawBitmap(surface, stoneBmps[color], (int)x, (int)y, 1);
88 void OnRedraw(Surface surface)
90 Bitmap board = boardBmp.bitmap;
91 Bitmap remove = removeBmp.bitmap;
92 Bitmap wStone = stoneBmps[Stone::white].bitmap;
99 DrawBitmap(surface, boardBmp, 0,0, 1);
101 // Draw the stones in the drawers
102 for(c = black; c <= white; c++)
104 Bitmap stone = stoneBmps[c].bitmap;
105 int bx = (clientSize.w - board.width * scale)/2;
106 int drawerX = (c == black) ? blackDrawer : whiteDrawer;
109 for(r = 0; r < game.numStones[c]; r++)
110 DrawBitmap(surface, stoneBmps[c],
111 drawerX - stone.width/2, topDrawer + (int)(r*wStone.height*stoneOverlap), 1);
117 for(y = 0; draw && y < 4; y++)
119 for(x = 0; draw && x < 4; x++)
121 if(!game.stones[y][x])
128 for(y = 0; y < 4; y++)
130 for(x = 0; x < 4; x++)
132 Stone stone = game.stones[y][x];
135 if(game.winner || draw) stone += 2;
136 DrawStone(surface, { x, y }, stone);
143 // Display winning stones only in color
145 for(i = 0; i < 4; i++)
149 for(j = 0; j < 4; j++)
150 if(game.stones[i][j] != game.winner)
153 for(j = 0; j < 4; j++)
154 DrawStone(surface, { j, i }, game.winner);
156 for(j = 0; j < 4; j++)
157 if(game.stones[j][i] != game.winner)
160 for(j = 0; j < 4; j++)
161 DrawStone(surface, { i, j }, game.winner);
163 for(i = 0; i < 4; i++)
164 if(game.stones[i][i] != game.winner)
167 for(i = 0; i < 4; i++)
168 DrawStone(surface, { i, i }, game.winner);
170 for(i = 0; i < 4; i++)
171 if(game.stones[3-i][i] != game.winner)
174 for(i = 0; i < 4; i++)
175 DrawStone(surface, { i, 3-i }, game.winner);
178 // Inform the player he can remove a stone
180 DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
182 if(!game.winner && !draw)
184 // Display the current turn
185 DrawBitmap(surface, arrowBmp,
186 ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
190 bool OnLeftButtonDown(int x, int y, Modifiers mods)
192 int w = (int)(boardBmp.bitmap.width * scale);
193 int bx = (clientSize.w - w) / 2;
194 int by = (clientSize.h - clientSize.h) / 2;
195 x = (int)((x - bx) / scale);
196 y = (int)((y - by) / scale);
197 if(x > upperLeftX && y > upperLeftY)
199 int sx = (x - upperLeftX) / spaceX;
200 int sy = (y - upperLeftY) / spaceY;
203 GameConnection player = (player1.color == game.turn) ? player1 : player2;
204 if(player.Click(sx, sy))
212 MainWindow mainWindow {};