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 = 150;
9 define blackDrawer = 1888;
10 define topDrawer = 126;
12 class MainWindow : Window
14 GameConnection player1 { };
15 GameConnection player2 { };
19 borderStyle = sizable;
24 size = { w = parent.clientSize.h * 2038/1647, h = parent.clientSize.h };
27 BitmapResource boardBmp { ":board.jpg", window = this };
28 BitmapResource arrowBmp { ":arrow.png", alphaBlend = true, window = this };
29 BitmapResource removeBmp { ":remove.png", alphaBlend = true, window = this };
30 BitmapResource smileBmp { ":smile.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 }
39 Menu fileMenu { menu, "File", f };
42 fileMenu, "New Game", n, ctrlN;
44 bool NotifySelect(MenuItem selection, Modifiers mods)
51 MenuDivider { fileMenu };
52 MenuItem exit { fileMenu, "Exit", x, altF4, NotifySelect = MenuFileExit };
62 void DrawBitmap(Surface surface, BitmapResource res, int x, int y, float s)
64 Bitmap board = boardBmp.bitmap;
65 Bitmap bmp = res.bitmap;
66 int bw = (int)(board.width * scale;
67 int bh = clientSize.h;
68 int bx = (clientSize.w - bw) / 2;
69 int by = (clientSize.h - bh) / 2;
71 surface.Filter(res.bitmap,
72 bx + (int)(x * scale), by + (int)(y * scale), 0,0,
73 (int)(s*bmp.width * scale), (int)(s*bmp.height * scale),
74 bmp.width, bmp.height);
77 void DrawWin(Surface surface, Point p1, Point p2)
80 for(c = 0; c < 13; c++)
82 int x = (int)(upperLeftX + (p1.x+0.5)*spaceX + (p2.x - p1.x)/3 * c * spaceX / 4 - (smileBmp.bitmap.width*3)/2);
83 int y = (int)(upperLeftY + (p1.y+0.5)*spaceY + (p2.y - p1.y)/3 * c * spaceY / 4 - (smileBmp.bitmap.height*3)/2);
84 DrawBitmap(surface, smileBmp, x, y, 3);
88 void DrawStone(Surface surface, Point where, Stone color)
90 Bitmap stone = stoneBmps[color].bitmap;
91 float x = upperLeftX + (where.x + .5f) * spaceX - stone.width / 2;
92 float y = upperLeftY + (where.y + .5f) * spaceY - stone.height / 2;
93 DrawBitmap(surface, stoneBmps[color], (int)x, (int)y, 1);
96 void OnRedraw(Surface surface)
98 Bitmap board = boardBmp.bitmap;
99 Bitmap remove = removeBmp.bitmap;
100 Bitmap wStone = stoneBmps[Stone::white].bitmap;
106 DrawBitmap(surface, boardBmp, 0,0, 1);
108 // Draw the stones in the drawers
109 for(c = black; c <= white; c++)
111 Bitmap stone = stoneBmps[c].bitmap;
112 int bx = (clientSize.w - board.width * scale)/2;
113 int drawerX = (c == black) ? blackDrawer : whiteDrawer;
116 for(r = 0; r < game.numStones[c]; r++)
117 DrawBitmap(surface, stoneBmps[c],
118 drawerX - stone.width/2, topDrawer + r*wStone.height*2/3, 1);
122 for(y = 0; y < 4; y++)
124 for(x = 0; x < 4; x++)
126 Stone stone = game.stones[y][x];
128 DrawStone(surface, { x, y }, stone);
132 // Inform the player he can remove a stone
134 DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
140 for(i = 0; i < 4; i++)
144 for(j = 0; j < 4; j++)
145 if(game.stones[i][j] != game.winner)
148 DrawWin(surface, {0, i}, { 3, i });
150 for(j = 0; j < 4; j++)
151 if(game.stones[j][i] != game.winner)
154 DrawWin(surface, {i, 0}, { i, 3 });
156 for(i = 0; i < 4; i++)
157 if(game.stones[i][i] != game.winner)
160 DrawWin(surface, {0, 0}, { 3, 3 });
162 for(i = 0; i < 4; i++)
163 if(game.stones[3-i][i] != game.winner)
166 DrawWin(surface, {0, 3}, { 3, 0 });
170 // Display the current turn
171 DrawBitmap(surface, arrowBmp,
172 ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
176 bool OnLeftButtonDown(int x, int y, Modifiers mods)
178 int w = (int)(boardBmp.bitmap.width * scale);
179 int bx = (clientSize.w - w) / 2;
180 int by = (clientSize.h - clientSize.h) / 2;
181 x = (int)((x - bx) / scale);
182 y = (int)((y - by) / scale);
183 if(x > upperLeftX && y > upperLeftY)
185 int sx = (x - upperLeftX) / spaceX;
186 int sy = (y - upperLeftY) / spaceY;
189 GameConnection player = (player1.color == game.turn) ? player1 : player2;
190 if(player.Click(sx, sy))
198 MainWindow mainWindow {};