-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardGUI.cpp
More file actions
182 lines (152 loc) · 6.52 KB
/
BoardGUI.cpp
File metadata and controls
182 lines (152 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "BoardGUI.h"
#include <stdio.h>
BoardGUI::BoardGUI(Board* brd, SDL_Renderer* renderer) {
this->b = brd;
////////////////////////////////////////////////////////////////////////////
// Set an array of boxes so that GUI can return clicks on these boxes
////////////////////////////////////////////////////////////////////////////
// buttons for 64 squares of chess board
for(int i = INPUT_MIN_SQUARE - 1; i < INPUT_MAX_SQUARE; i++) {
// coordinate of the top left corner of the square
int x = 48 + (i%8) * 507 / 8;
int y = 50 + (7- i/8) * 507 / 8;
boxes.push_back( Box(x, y , x+60, y+60, i+1) );
}
// buttons on the side bar
boxes.push_back( Box(626, 18, 682, 87, INPUT_UNDO) );//undo button
boxes.push_back( Box(721, 18, 782, 87, INPUT_HOME) );//home button
boxes.push_back( Box(611, 342, 790, 392, INPUT_PROMOTE_QUEEN) );//queen promote
boxes.push_back( Box(611, 403, 790, 453, INPUT_PROMOTE_ROOK) );//rook promote
boxes.push_back( Box(611, 465, 790, 515, INPUT_PROMOTE_BISHOP) );//bishop promote
boxes.push_back( Box(611, 528, 790, 578, INPUT_PROMOTE_KNIGHT) );//knight promote
// pass the array to GUI
//setBoxes(boxArr, 70);
/////////////////////////////////////////////////////////////////////////////
// Load media and initialize position
/////////////////////////////////////////////////////////////////////////////
boardImg.loadFromFile(renderer, "img/boardGUI/board.jpg");
piecesSprite.loadFromFile(renderer, "img/boardGUI/pieces.png");
piecesSprite.setBlendMode(SDL_BLENDMODE_BLEND); // piece can be made transparent
moveArrow.loadFromFile(renderer, "img/boardGUI/arrow.png");
undoButton.loadFromFile(renderer, "img/boardGUI/undoButton.png");
homeButton.loadFromFile(renderer, "img/boardGUI/homeButton.png");
playerTxt.loadFromFile(renderer, "img/boardGUI/Player.png");
comTxt.loadFromFile(renderer, "img/boardGUI/COM.png");
promoteTxt.loadFromFile(renderer, "img/boardGUI/promotionTxt.png");
colorSymbols[0].loadFromFile(renderer, "img/boardGUI/whiteSymbol.png");
colorSymbols[1].loadFromFile(renderer, "img/boardGUI/blackSymbol.png");
crosshair.loadFromFile(renderer, "img/boardGUI/crosshair.png");
//position of piece's image on sprite sheet
for(int i = 0; i < 12; i++) {
pieceClips[i] = { (i%6)*60, (i/6)*60, 60, 60 };
}
// positions of squares in chessboard
for(int i = 0; i < 64; i++) {
int x = 48 + (i%8) * 507 / 8;
int y = 50 + (7 - i/8) * 507 / 8;
boardSquares[i] = {x, y ,60, 60};
}
undoRect = {626, 18, 56, 69};
homeRect = {721, 18, 61, 58};
playerTxtRect = {633, 106, 141, 52};
colorSymbolRect = {620, 175, 164, 88};
promoteTxtRect = {613, 290, 172, 290};
promotePieceRects[0] = {617, 342, 50, 50};
promotePieceRects[1] = {617, 403, 50, 50};
promotePieceRects[2] = {617, 465, 50, 50};
promotePieceRects[3] = {617, 528, 50, 50};
/////////////////////////////////////////////////////////////////////////////
// Load audio
/////////////////////////////////////////////////////////////////////////////
bgm = Mix_LoadMUS("sound/bgm.mp3");
if (!bgm) {
printf("Failed to load music. SDL_Mix error: %s\n", Mix_GetError());
}
moveSFX = Mix_LoadWAV("sound/chess move.wav");
if (!moveSFX) {
printf("Failed to load music. SDL_Mix error: %s\n", Mix_GetError());
}
}
BoardGUI::~BoardGUI() {};
void BoardGUI::updateMovePointers() {
availableMoves.clear();
int chosenSquare = b->getChosenSquare();
if (chosenSquare >= 0 && chosenSquare < Board::NUM_SQUARES) {
b->getMovesFromSquare(availableMoves, chosenSquare);
}
}
void BoardGUI::setPlayer(int color) {
humanSide = color;
}
void BoardGUI::draw(SDL_Renderer* renderer) {
// Clear screen
SDL_RenderClear( renderer );
// Draw chessboard
boardImg.render(renderer, 0, 0);
// Draw click boxes
//SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0x00 );
//SDL_RenderDrawRects(renderer, boardSquares, 64);
// Draw all chess pieces from board
for (int i = 0; i < Board::NUM_SQUARES; i++) {
int piece = b->getPieceGUI(i);
if (piece == Board::EMPTY) continue;
if (piece < Board::NUM_COLORED_TYPES) {
// if piece is not chosen, draw as normal
piecesSprite.render(renderer, &pieceClips[piece], &boardSquares[i]);
} else {
// if piece is chosen, draw it half transparent
piece -= Board::NUM_COLORED_TYPES;
piecesSprite.setTransparency(ALPHA_FADED);
piecesSprite.render(renderer, &pieceClips[piece], &boardSquares[i]);
piecesSprite.setTransparency(ALPHA_NORMAL);
}
}
// Draw cross hair on checked king
if (b->isKingChecked()) {
int kingSquare = b->getKingSquare(b->getPlayer());
SDL_Rect rect = {boardSquares[kingSquare].x - 10, boardSquares[kingSquare].y - 10, 80, 80};
crosshair.render(renderer, NULL, &rect);
}
// Draw side Bar
undoButton.render(renderer, NULL, &undoRect);
homeButton.render(renderer, NULL, &homeRect);
if (humanSide == Board::BOTH_COLOR || humanSide == b->getPlayer()) {
playerTxt.render(renderer, NULL, &playerTxtRect);
} else {
comTxt.render(renderer, NULL, &playerTxtRect);
}
colorSymbols[b->getPlayer()].render(renderer, NULL, &colorSymbolRect);
// Draw move pointers
for (int i = 0; i < availableMoves.size(); i++) {
int arrowX = boardSquares[availableMoves[i]].x;
int arrowY = boardSquares[availableMoves[i]].y;
arrowY -= arrowHeight;
moveArrow.render(renderer, arrowX, arrowY);
}
arrowHeight += arrowSpeed;
if (arrowHeight > ARROW_HIGH || arrowHeight < ARROW_LOW) arrowSpeed = -arrowSpeed;
// Draw promotion panel
if (b->hasPromotion()) {
// Draw promotion options
promoteTxt.render(renderer, NULL, &promoteTxtRect);
int color = b->getPlayer()? 0 : 6;
piecesSprite.render(renderer, &pieceClips[Board::BQ + color], &promotePieceRects[0]);
piecesSprite.render(renderer, &pieceClips[Board::BR + color], &promotePieceRects[1]);
piecesSprite.render(renderer, &pieceClips[Board::BB + color], &promotePieceRects[2]);
piecesSprite.render(renderer, &pieceClips[Board::BN + color], &promotePieceRects[3]);
}
SDL_RenderPresent(renderer);
}
void BoardGUI::playMoveSFX() {
Mix_PlayChannel(-1, moveSFX, 0);
}
void BoardGUI::playMusic() {
Mix_PlayMusic(bgm, -1);
}
void BoardGUI::stopMusic() {
Mix_HaltMusic();
}
void BoardGUI::destroyMedia() {
Mix_FreeChunk(moveSFX);
Mix_FreeMusic(bgm);
}