Tweaks for Android
[chess] / src / chess2D.ec
1 import "chess.ec"
2
3 // #define FLIPBOARD
4
5 /*
6 define OFFSET_X = 23;
7 define OFFSET_Y = 23;
8 define SQUARE_W = 49;
9 define SQUARE_H = 49;
10 */
11
12 define OFFSET_X = 42;
13 define OFFSET_Y = 42;
14 define SQUARE_W = 90;
15 define SQUARE_H = 90;
16
17 define BOARD_WIDTH = SQUARE_W * 8;
18 define BOARD_HEIGHT = SQUARE_H * 8;
19
20 static char * names[12] =
21 {
22    "whitePawn", "whiteKnight", "whiteBishop", "whiteRook", "whiteQueen", "whiteKing",
23    "blackPawn", "blackKnight", "blackBishop", "blackRook", "blackQueen", "blackKing"
24 };
25
26 class Chess2D : Window
27 {
28    ChessState * chessState;
29    Point drag, moving;
30    bool dragging;
31    Bitmap piecesBMP[12];
32    //BitmapResource boardBMP { ":board.pcx", window = this };
33    BitmapResource boardBMP { ":board.jpg", window = this };
34
35    isActiveClient = true, background = black, borderStyle = fixed, hasMinimize = true;
36
37    bool OnLoadGraphics()
38    {
39       int c;
40
41 #if 0
42       Bitmap chessPieces {};
43       chessPieces.Load(":pieces.pcx", null, null);
44       for(c = 0; c<12; c++)
45       {
46          piecesBMP[c] = Bitmap {};
47          piecesBMP[c].Allocate(null, SQUARE_W, SQUARE_H, 0, chessPieces.format, true);
48          piecesBMP[c].Grab(chessPieces, (c % 6) * SQUARE_H,(c / 6) * SQUARE_H);
49          piecesBMP[c].transparent = true;
50          piecesBMP[c].MakeDD(displaySystem);
51       }
52       delete chessPieces;
53 #endif
54
55       for(c = 0; c<12; c++)
56       {
57          char fileName[MAX_FILENAME];
58          sprintf(fileName, ":%s.png", names[c]);
59          piecesBMP[c] = Bitmap{};
60          piecesBMP[c].LoadT(fileName, null, displaySystem);
61       }
62       return true;
63    }
64
65    bool OnResizing(int * w, int * h)
66    {
67       *w = Max(*w, BOARD_WIDTH  + OFFSET_X * 2);
68       *h = Max(*h, BOARD_HEIGHT + OFFSET_Y * 2);
69       return true;
70    }
71
72    void OnUnloadGraphics()
73    {
74       int c;
75       for(c = 0; c<12; c++)
76       {
77          piecesBMP[c].Free();
78          piecesBMP[c] = null;
79       }
80    }
81
82    void OnRedraw(Surface surface)
83    {
84       bool flip;
85       int rx, ry;
86
87    #ifdef FLIPBOARD
88       flip = chessState->turn == Black;
89    #else
90       flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
91    #endif
92
93       surface.SetForeground(white);
94       surface.Blit(boardBMP.bitmap, 0,0, 0,0, boardBMP.bitmap.width, boardBMP.bitmap.height);
95
96       if(chessState->gameRunning)
97       {
98          int x,y;
99          for (y=0; y<8; y++)
100          {
101                  for (x=0; x<8; x++)
102                  {
103                if(!dragging || x != moving.x || y != moving.y)
104                {
105                   Piece atBoard = chessState->board[y][x];
106                   int piece = (int)atBoard.player * 6 + (int)atBoard.type;
107                   if(piece)
108                   {
109                      rx = flip ? (7-x) : x;
110                      ry = flip ? y : (7-y);
111
112                      surface.Blit(piecesBMP[piece-1],
113                         OFFSET_X + rx * SQUARE_W, OFFSET_Y + ry * SQUARE_H,
114                         0,0, piecesBMP[piece-1].width, piecesBMP[piece-1].height);
115                   }
116                }
117                  }
118          }
119
120          if(dragging)
121          {
122             Piece atBoard = chessState->board[moving.y][moving.x];
123             int piece = (int)atBoard.player * 6 + (int)atBoard.type;
124
125             rx = flip ? (7-drag.x) : drag.x;
126             ry = flip ? drag.y : (7-drag.y);
127
128             surface.Blit(piecesBMP[piece-1],
129                OFFSET_X + rx * SQUARE_W, OFFSET_Y + ry * SQUARE_H,
130                0,0, piecesBMP[piece-1].width, piecesBMP[piece-1].height);
131          }
132       }
133    }
134
135    bool OnLeftButtonDown(int x, int y, Modifiers mods)
136    {
137       bool flip;
138    #ifdef FLIPBOARD
139       flip = chessState->turn == Black;
140    #else
141       flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
142    #endif
143
144       x = (x - OFFSET_X) / SQUARE_W;
145       y = (y - OFFSET_Y) / SQUARE_H;
146
147       if(!flip) y = 7-y; else x = 7-x;
148
149       if(chessState->gameRunning &&
150          x < 8 && y < 8 && x >= 0 && y >= 0 && chessState->board[y][x] && 
151          chessState->isLocalPlayer[chessState->turn] && 
152          chessState->board[y][x].player == chessState->turn)
153       {
154          dragging = true;
155          drag.x = moving.x = x;
156          drag.y = moving.y = y;
157          Capture();
158          SetMouseRange( Box { OFFSET_X, OFFSET_Y, OFFSET_X + BOARD_WIDTH - 1, OFFSET_Y + BOARD_HEIGHT - 1 } );
159       }
160       return true;
161    }
162
163    bool OnLeftButtonUp(int x, int y, Modifiers mods)
164    {
165       bool flip;
166
167    #ifdef FLIPBOARD
168       flip = chessState->turn == Black;
169    #else
170       flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
171    #endif
172
173       if(chessState->gameRunning)
174       {
175          x = (x - OFFSET_X) / SQUARE_W;
176          y = (y - OFFSET_Y) / SQUARE_H;
177
178          if(!flip) y = 7-y; else x = 7-x;
179
180          if(x < 8 && y < 8 && x >= 0 && y >= 0 && dragging)
181          {
182             ReleaseCapture();
183             FreeMouseRange();
184             dragging = false;
185             ((Chess)master).ProcessUserMove(moving.x, moving.y, x, y);
186          }
187       }
188       return true;
189    }
190
191    bool OnMouseMove(int x, int y, Modifiers mods)
192    {
193       bool flip;
194
195    #ifdef FLIPBOARD
196       flip = chessState->turn == Black;
197    #else
198       flip = chessState->isLocalPlayer[Black] && !chessState->isLocalPlayer[White];
199    #endif
200
201       x = (x - OFFSET_X) / SQUARE_W;
202       y = (y - OFFSET_Y) / SQUARE_H;
203
204       if(!flip) y = 7-y; else x = 7-x;
205
206       if(x < 8 && y < 8 && x >= 0 && y >= 0 && dragging)
207       {
208          drag.x = x;    
209          drag.y = y;
210          Update(null);
211       }
212       return true;
213    }
214
215    property ChessState * chessState { set { chessState = value; } }
216 }