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 wStone = stoneBmps[Stone::white].bitmap;
98 DrawBitmap(surface, boardBmp, 0,0, 1);
100 // Draw the stones in the drawers
101 for(c = black; c <= white; c++)
103 Bitmap stone = stoneBmps[c].bitmap;
104 int drawerX = (c == black) ? blackDrawer : whiteDrawer;
107 for(r = 0; r < game.numStones[c]; r++)
108 DrawBitmap(surface, stoneBmps[c],
109 drawerX - stone.width/2, topDrawer + (int)(r*wStone.height*stoneOverlap), 1);
115 for(y = 0; draw && y < 4; y++)
117 for(x = 0; draw && x < 4; x++)
119 if(!game.stones[y][x])
126 for(y = 0; y < 4; y++)
128 for(x = 0; x < 4; x++)
130 Stone stone = game.stones[y][x];
133 if(game.winner || draw) stone += 2;
134 DrawStone(surface, { x, y }, stone);
141 // Display winning stones only in color
143 for(i = 0; i < 4; i++)
147 for(j = 0; j < 4; j++)
148 if(game.stones[i][j] != game.winner)
151 for(j = 0; j < 4; j++)
152 DrawStone(surface, { j, i }, game.winner);
154 for(j = 0; j < 4; j++)
155 if(game.stones[j][i] != game.winner)
158 for(j = 0; j < 4; j++)
159 DrawStone(surface, { i, j }, game.winner);
161 for(i = 0; i < 4; i++)
162 if(game.stones[i][i] != game.winner)
165 for(i = 0; i < 4; i++)
166 DrawStone(surface, { i, i }, game.winner);
168 for(i = 0; i < 4; i++)
169 if(game.stones[3-i][i] != game.winner)
172 for(i = 0; i < 4; i++)
173 DrawStone(surface, { i, 3-i }, game.winner);
176 // Inform the player he can remove a stone
178 DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
180 if(!game.winner && !draw)
182 // Display the current turn
183 DrawBitmap(surface, arrowBmp,
184 ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
188 bool OnLeftButtonDown(int x, int y, Modifiers mods)
190 int w = (int)(boardBmp.bitmap.width * scale);
191 int bx = (clientSize.w - w) / 2;
192 int by = (clientSize.h - clientSize.h) / 2;
193 x = (int)((x - bx) / scale);
194 y = (int)((y - by) / scale);
195 if(x > upperLeftX && y > upperLeftY)
197 int sx = (x - upperLeftX) / spaceX;
198 int sy = (y - upperLeftY) / spaceY;
201 GameConnection player = (player1.color == game.turn) ? player1 : player2;
202 if(player.Click(sx, sy))
210 MainWindow mainWindow {};