extras; samples: Fixed warnings
[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 wStone = stoneBmps[Stone::white].bitmap;
92
93       int x, y;
94       Stone c;
95       bool draw = false;
96
97       // Draw the board
98       DrawBitmap(surface, boardBmp, 0,0, 1);
99
100       // Draw the stones in the drawers
101       for(c = black; c <= white; c++)
102       {
103          Bitmap stone = stoneBmps[c].bitmap;
104          int drawerX = (c == black) ? blackDrawer : whiteDrawer;
105          int r;
106
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);
110       }
111
112       if(!game.takeOut)
113       {
114          draw = true;
115          for(y = 0; draw && y < 4; y++)
116          {
117             for(x = 0; draw && x < 4; x++)
118             {
119                if(!game.stones[y][x])
120                   draw = false;
121             }
122          }
123       }
124
125       // Draw the stones
126       for(y = 0; y < 4; y++)
127       {
128          for(x = 0; x < 4; x++)
129          {
130             Stone stone = game.stones[y][x];
131             if(stone)
132             {
133                if(game.winner || draw) stone += 2;
134                DrawStone(surface, { x, y }, stone);
135             }
136          }
137       }
138
139       if(game.winner)
140       {
141          // Display winning stones only in color
142          int i;
143          for(i = 0; i < 4; i++)
144          {
145             int j;
146
147             for(j = 0; j < 4; j++)
148                if(game.stones[i][j] != game.winner)
149                   break;
150             if(j == 4)
151                for(j = 0; j < 4; j++)
152                   DrawStone(surface, { j, i }, game.winner);
153
154             for(j = 0; j < 4; j++)
155                if(game.stones[j][i] != game.winner)
156                   break;
157             if(j == 4)
158                for(j = 0; j < 4; j++)
159                   DrawStone(surface, { i, j }, game.winner);
160          }
161          for(i = 0; i < 4; i++)
162             if(game.stones[i][i] != game.winner)
163                break;
164          if(i == 4)
165             for(i = 0; i < 4; i++)
166                DrawStone(surface, { i, i }, game.winner);
167
168          for(i = 0; i < 4; i++)
169             if(game.stones[3-i][i] != game.winner)
170                break;
171          if(i == 4)
172             for(i = 0; i < 4; i++)
173                DrawStone(surface, { i, 3-i }, game.winner);
174       }
175
176       // Inform the player he can remove a stone
177       if(game.takeOut)
178          DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
179
180       if(!game.winner && !draw)
181       {
182          // Display the current turn
183          DrawBitmap(surface, arrowBmp,
184             ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
185       }
186    }
187
188    bool OnLeftButtonDown(int x, int y, Modifiers mods)
189    {
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)
196       {
197          int sx = (x - upperLeftX) / spaceX;
198          int sy = (y - upperLeftY) / spaceY;
199          if(sx < 4 && sy < 4)
200          {
201             GameConnection player = (player1.color == game.turn) ? player1 : player2;
202             if(player.Click(sx, sy))
203                Update(null);
204          }
205       }
206       return true;
207    }
208 }
209
210 MainWindow mainWindow {};