00121e25ccd5b8246f8b0e0422bc9c853678c54e
[sdk] / samples / games / brainstonz / brainstonz.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 = 150;
9 define blackDrawer = 1888;
10 define topDrawer = 126;
11
12 class MainWindow : Window
13 {
14    GameConnection player1 { };
15    GameConnection player2 { };
16
17    text = "BrainStonz";
18    background = black;
19    borderStyle = sizable;
20    hasMaximize = true;
21    hasMinimize = true;
22    hasClose = true;
23    state = maximized;
24    size = { w = parent.clientSize.h * 2038/1647, h = parent.clientSize.h };
25    hasMenuBar = true;
26
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
32    { [
33       null,
34       BitmapResource { ":black.png", alphaBlend = true, window = this },
35       BitmapResource { ":white.png", alphaBlend = true, window = this }
36    ] };
37
38    menu = { };
39    Menu fileMenu { menu, "File", f };
40    MenuItem newGame
41    {
42       fileMenu, "New Game", n, ctrlN;
43
44       bool NotifySelect(MenuItem selection, Modifiers mods)
45       {
46          player1.NewGame();
47          Update(null);
48          return true;
49       }
50    };
51    MenuDivider { fileMenu };
52    MenuItem exit { fileMenu, "Exit", x, altF4, NotifySelect = MenuFileExit };
53
54    bool OnCreate()
55    {
56       player1.Join();
57       player2.Join();
58       
59       player1.NewGame();
60    }
61
62    void DrawBitmap(Surface surface, BitmapResource res, int x, int y, float s)
63    {
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;
70
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);
75    }
76
77    void DrawWin(Surface surface, Point p1, Point p2)
78    {
79       int c;
80       for(c = 0; c < 13; c++)
81       {
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);
85       }      
86    }
87       
88    void DrawStone(Surface surface, Point where, Stone color)
89    {
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);
94    }
95
96    void OnRedraw(Surface surface)
97    {
98       Bitmap board = boardBmp.bitmap;
99       Bitmap remove = removeBmp.bitmap;
100       Bitmap wStone = stoneBmps[Stone::white].bitmap;
101       
102       int x, y;
103       Stone c;
104
105       // Draw the board
106       DrawBitmap(surface, boardBmp, 0,0, 1);
107
108       // Draw the stones in the drawers
109       for(c = black; c <= white; c++)
110       {
111          Bitmap stone = stoneBmps[c].bitmap;
112          int bx = (clientSize.w - board.width * scale)/2;
113          int drawerX = (c == black) ? blackDrawer : whiteDrawer;
114          int r;
115
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);
119       }
120       
121       // Draw the stones
122       for(y = 0; y < 4; y++)
123       {
124          for(x = 0; x < 4; x++)
125          {
126             Stone stone = game.stones[y][x];
127             if(stone)
128                DrawStone(surface, { x, y }, stone);
129          }
130       }
131
132       // Inform the player he can remove a stone
133       if(game.takeOut)
134          DrawBitmap(surface, removeBmp, board.width/2 - removeBmp.bitmap.width*3/2, 30, 4);
135
136       // Display a win
137       if(game.winner)
138       {
139          int i;
140          for(i = 0; i < 4; i++)
141          {
142             int j;
143
144             for(j = 0; j < 4; j++)
145                if(game.stones[i][j] != game.winner)
146                   break;
147             if(j == 4)
148                DrawWin(surface, {0, i}, { 3, i });
149
150             for(j = 0; j < 4; j++)
151                if(game.stones[j][i] != game.winner)
152                   break;
153             if(j == 4)
154                DrawWin(surface, {i, 0}, { i, 3 });
155          }
156          for(i = 0; i < 4; i++)
157             if(game.stones[i][i] != game.winner)
158                break;
159          if(i == 4)
160             DrawWin(surface, {0, 0}, { 3, 3 });
161
162          for(i = 0; i < 4; i++)
163             if(game.stones[3-i][i] != game.winner)
164                break;
165          if(i == 4)
166             DrawWin(surface, {0, 3}, { 3, 0 });
167       }
168       else
169       {
170          // Display the current turn
171          DrawBitmap(surface, arrowBmp,
172             ((game.turn == white) ? whiteDrawer : blackDrawer) - arrowBmp.bitmap.width*4/2, 30, 4);
173       }
174    }
175
176    bool OnLeftButtonDown(int x, int y, Modifiers mods)
177    {
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)
184       {
185          int sx = (x - upperLeftX) / spaceX;
186          int sy = (y - upperLeftY) / spaceY;
187          if(sx < 4 && sy < 4)
188          {
189             GameConnection player = (player1.color == game.turn) ? player1 : player2;
190             if(player.Click(sx, sy))
191                Update(null);
192          }
193       }
194       return true;
195    }
196 }
197
198 MainWindow mainWindow {};