2187ff6e85e4f4ec93b1391fb12c2af143eb096d
[sdk] / samples / games / stonePairs / stonePairs.ec
1 import "game"
2
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;
12
13 class MainWindow : Window
14 {
15    GameConnection player1 { };
16    GameConnection player2 { };
17
18    text = "Stone Pairs";
19    background = black;
20    borderStyle = sizable;
21    hasMaximize = true;
22    hasMinimize = true;
23    hasClose = true;
24    clientSize = { 1019, 824 };
25
26    hasMenuBar = true;
27
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
32    { [
33       null,
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 }
38    ] };
39
40    menu = { };
41    Menu fileMenu { menu, "File", f };
42    MenuItem newGame
43    {
44       fileMenu, "New Game", n, ctrlN;
45
46       bool NotifySelect(MenuItem selection, Modifiers mods)
47       {
48          player1.NewGame();
49          Update(null);
50          return true;
51       }
52    };
53    MenuDivider { fileMenu };
54    MenuItem exit { fileMenu, "Exit", x, altF4, NotifySelect = MenuFileExit };
55
56    bool OnCreate()
57    {
58       player1.Join();
59       player2.Join();
60
61       player1.NewGame();
62       return true;
63    }
64
65    void DrawBitmap(Surface surface, BitmapResource res, int x, int y, float s)
66    {
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;
73
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);
78    }
79
80    void DrawStone(Surface surface, Point where, Stone color)
81    {
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);
86    }
87
88    void OnRedraw(Surface surface)
89    {
90       Bitmap board = boardBmp.bitmap;
91       Bitmap remove = removeBmp.bitmap;
92       Bitmap wStone = stoneBmps[Stone::white].bitmap;
93
94       int x, y;
95       Stone c;
96       bool draw = false;
97
98       // Draw the board
99       DrawBitmap(surface, boardBmp, 0,0, 1);
100
101       // Draw the stones in the drawers
102       for(c = black; c <= white; c++)
103       {
104          Bitmap stone = stoneBmps[c].bitmap;
105          int bx = (clientSize.w - board.width * scale)/2;
106          int drawerX = (c == black) ? blackDrawer : whiteDrawer;
107          int r;
108
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);
112       }
113
114       if(!game.takeOut)
115       {
116          draw = true;
117          for(y = 0; draw && y < 4; y++)
118          {
119             for(x = 0; draw && x < 4; x++)
120             {
121                if(!game.stones[y][x])
122                   draw = false;
123             }
124          }
125       }
126
127       // Draw the stones
128       for(y = 0; y < 4; y++)
129       {
130          for(x = 0; x < 4; x++)
131          {
132             Stone stone = game.stones[y][x];
133             if(stone)
134             {
135                if(game.winner || draw) stone += 2;
136                DrawStone(surface, { x, y }, stone);
137             }
138          }
139       }
140
141       if(game.winner)
142       {
143          // Display winning stones only in color
144          int i;
145          for(i = 0; i < 4; i++)
146          {
147             int j;
148
149             for(j = 0; j < 4; j++)
150                if(game.stones[i][j] != game.winner)
151                   break;
152             if(j == 4)
153                for(j = 0; j < 4; j++)
154                   DrawStone(surface, { j, i }, game.winner);
155
156             for(j = 0; j < 4; j++)
157                if(game.stones[j][i] != game.winner)
158                   break;
159             if(j == 4)
160                for(j = 0; j < 4; j++)
161                   DrawStone(surface, { i, j }, game.winner);
162          }
163          for(i = 0; i < 4; i++)
164             if(game.stones[i][i] != game.winner)
165                break;
166          if(i == 4)
167             for(i = 0; i < 4; i++)
168                DrawStone(surface, { i, i }, game.winner);
169
170          for(i = 0; i < 4; i++)
171             if(game.stones[3-i][i] != game.winner)
172                break;
173          if(i == 4)
174             for(i = 0; i < 4; i++)
175                DrawStone(surface, { i, 3-i }, game.winner);
176       }
177
178       // Inform the player he can remove a stone
179       if(game.takeOut)
180          DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
181
182       if(!game.winner && !draw)
183       {
184          // Display the current turn
185          DrawBitmap(surface, arrowBmp,
186             ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
187       }
188    }
189
190    bool OnLeftButtonDown(int x, int y, Modifiers mods)
191    {
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)
198       {
199          int sx = (x - upperLeftX) / spaceX;
200          int sy = (y - upperLeftY) / spaceY;
201          if(sx < 4 && sy < 4)
202          {
203             GameConnection player = (player1.color == game.turn) ? player1 : player2;
204             if(player.Click(sx, sy))
205                Update(null);
206          }
207       }
208       return true;
209    }
210 }
211
212 MainWindow mainWindow {};