2020-02-27 18:36:55 -05:00
|
|
|
#include "chess_board.h"
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_video.h>
|
|
|
|
#include "stdio.h"
|
2020-04-16 21:23:25 -04:00
|
|
|
#include <stack>
|
|
|
|
#include <string.h>
|
2020-02-27 18:36:55 -05:00
|
|
|
|
|
|
|
#define MARGIN 200
|
|
|
|
|
2020-04-16 21:23:25 -04:00
|
|
|
#define PAWN_WHITE 0u
|
|
|
|
#define PAWN_BLACK 1u
|
|
|
|
#define KING_WHITE 2u
|
|
|
|
#define KING_BLACK 3u
|
|
|
|
#define QUEEN_WHITE 4u
|
|
|
|
#define QUEEN_BLACK 5u
|
|
|
|
#define ROOK_WHITE 6u
|
|
|
|
#define ROOK_BLACK 7u
|
|
|
|
#define KNIGHT_WHITE 8u
|
|
|
|
#define KNIGHT_BLACK 9u
|
|
|
|
#define BISHOP_WHITE 10u
|
|
|
|
#define BISHOP_BLACK 11u
|
|
|
|
#define SQUARE_EMPTY 12u
|
|
|
|
#define SQUARE_SHIFT 4u
|
|
|
|
|
|
|
|
#define POTENTIAL_MOVE (1u << SQUARE_SHIFT)
|
|
|
|
#define POTENTIAL_TAKE (2u << SQUARE_SHIFT)
|
|
|
|
#define SUGGESTED_MOVE (3u << SQUARE_SHIFT)
|
|
|
|
#define CHEATING_MOVE (4u << SQUARE_SHIFT)
|
|
|
|
|
|
|
|
const char file_names[12][16] = {"pawn_white.bmp", "pawn_black.bmp",
|
|
|
|
"king_white.bmp", "king_black.bmp",
|
|
|
|
"queen_white.bmp", "queen_black.bmp",
|
|
|
|
"rook_white.bmp", "rook_black.bmp",
|
|
|
|
"horse_white.bmp", "horse_black.bmp",
|
|
|
|
"tower_white.bmp", "tower_black.bmp"};
|
2020-02-27 18:36:55 -05:00
|
|
|
|
|
|
|
static int height;
|
|
|
|
static int width;
|
2020-04-16 21:23:25 -04:00
|
|
|
static SDL_Texture * board_texture;
|
2020-02-27 18:36:55 -05:00
|
|
|
static SDL_Rect rectangle;
|
|
|
|
static int board_width;
|
2020-04-16 21:23:25 -04:00
|
|
|
uint8_t board_state[8][8];
|
|
|
|
SDL_Surface *bitmapSurface = NULL;
|
|
|
|
SDL_Texture * bitmapTextures[12] = {NULL};
|
|
|
|
std::stack<uint8_t> selected_peices;
|
|
|
|
|
|
|
|
void pawn_move(uint8_t row, uint8_t column)
|
|
|
|
{
|
|
|
|
if((0x0Fu & board_state[row][column]) == SQUARE_EMPTY)
|
|
|
|
{
|
|
|
|
board_state[row][column] = (uint8_t)((POTENTIAL_MOVE) | (board_state[row][column] & 0x0Fu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pawn_take(uint8_t row, uint8_t column)
|
|
|
|
{
|
|
|
|
if ((0x0Fu & board_state[row][column]) < SQUARE_EMPTY)
|
|
|
|
{
|
|
|
|
board_state[row][column] = (uint8_t)((POTENTIAL_TAKE) | (board_state[row][column] & 0x0Fu));
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 18:36:55 -05:00
|
|
|
|
2020-04-16 21:23:25 -04:00
|
|
|
void Mark_Potential_Moves(uint8_t peice, uint8_t column, uint8_t row)
|
|
|
|
{
|
|
|
|
switch (peice)
|
|
|
|
{
|
|
|
|
case PAWN_WHITE :
|
|
|
|
if (row == 6)
|
|
|
|
{
|
|
|
|
pawn_move(row - 2, column);
|
|
|
|
}
|
|
|
|
pawn_move(row - 1, column);
|
|
|
|
if(column >= 1)
|
|
|
|
{
|
|
|
|
pawn_take(row - 1, column - 1);
|
|
|
|
}
|
|
|
|
if (column <= 6)
|
|
|
|
{
|
|
|
|
pawn_take(row - 1, column + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case PAWN_BLACK:
|
|
|
|
if (row == 1)
|
|
|
|
{
|
|
|
|
board_state[row + 2][column] = (uint8_t)((POTENTIAL_MOVE) | (board_state[row + 2][column] & 0x0Fu));
|
|
|
|
}
|
|
|
|
board_state[row + 1][column] = (uint8_t)((uint8_t)(POTENTIAL_MOVE) | (uint8_t)(board_state[row + 1][column] & 0x0Fu));
|
|
|
|
break;
|
|
|
|
case ROOK_WHITE:
|
|
|
|
case ROOK_BLACK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 18:36:55 -05:00
|
|
|
|
2020-04-16 21:23:25 -04:00
|
|
|
bool click(SDL_Renderer *p_renderer, int x, int y, bool pick_up)
|
2020-02-27 18:36:55 -05:00
|
|
|
{
|
|
|
|
SDL_Point const point = {x, y};
|
|
|
|
bool ret_val = false;
|
|
|
|
rectangle.w = board_width;
|
|
|
|
rectangle.h = board_width;
|
|
|
|
rectangle.x = (width - board_width) / 2;
|
|
|
|
rectangle.y = (height - board_width) / 2;
|
|
|
|
if (SDL_PointInRect(&point, &rectangle))
|
|
|
|
{
|
|
|
|
int square_size = board_width / 8;
|
|
|
|
int starting_x = rectangle.x;
|
|
|
|
rectangle.w = square_size;
|
|
|
|
rectangle.h = square_size;
|
|
|
|
for (size_t j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
rectangle.x = starting_x;
|
|
|
|
for (size_t i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if(SDL_PointInRect(&point, &rectangle))
|
|
|
|
{
|
2020-04-16 21:23:25 -04:00
|
|
|
if (pick_up && (board_state[j][i] < SQUARE_EMPTY))
|
|
|
|
{
|
|
|
|
selected_peices.push(board_state[j][i]);
|
|
|
|
Mark_Potential_Moves(board_state[j][i], i, j);
|
|
|
|
board_state[j][i] = SQUARE_EMPTY;
|
|
|
|
}
|
|
|
|
else if (!selected_peices.empty() && (board_state[j][i] >= SQUARE_EMPTY))
|
|
|
|
{
|
|
|
|
board_state[j][i] = selected_peices.top();
|
|
|
|
selected_peices.pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* code */
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:36:55 -05:00
|
|
|
goto draw_square;
|
|
|
|
}
|
|
|
|
rectangle.x += square_size;
|
|
|
|
}
|
|
|
|
rectangle.y += square_size;
|
|
|
|
}
|
|
|
|
draw_square:
|
|
|
|
SDL_SetRenderTarget(p_renderer, board_texture);
|
2020-04-16 21:23:25 -04:00
|
|
|
//SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
|
|
|
//SDL_RenderDrawRect(p_renderer, &rectangle);
|
|
|
|
//SDL_RenderFillRect(p_renderer, &rectangle);
|
|
|
|
//SDL_SetRenderTarget(p_renderer, NULL);
|
|
|
|
draw_board(p_renderer);
|
2020-02-27 18:36:55 -05:00
|
|
|
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
|
|
|
SDL_RenderPresent(p_renderer);
|
|
|
|
}
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:23:25 -04:00
|
|
|
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h)
|
2020-02-27 18:36:55 -05:00
|
|
|
{
|
|
|
|
width = w;
|
|
|
|
height = h;
|
|
|
|
SDL_DestroyTexture(board_texture);
|
|
|
|
board_texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
2020-04-16 21:23:25 -04:00
|
|
|
board_width = ((w > h) ? h : w) - MARGIN;
|
2020-02-27 18:36:55 -05:00
|
|
|
// get rid of rounding errors
|
|
|
|
board_width -= board_width % 8;
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:23:25 -04:00
|
|
|
void chess_board_init(SDL_Renderer *p_renderer)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
for (uint8_t j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
board_state[i][j] = SQUARE_EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Place black peices
|
|
|
|
board_state[0][0] = BISHOP_BLACK;
|
|
|
|
board_state[0][7] = BISHOP_BLACK;
|
|
|
|
board_state[0][1] = KNIGHT_BLACK;
|
|
|
|
board_state[0][6] = KNIGHT_BLACK;
|
|
|
|
board_state[0][2] = ROOK_BLACK;
|
|
|
|
board_state[0][5] = ROOK_BLACK;
|
|
|
|
board_state[0][3] = QUEEN_BLACK;
|
|
|
|
board_state[0][4] = KING_BLACK;
|
|
|
|
board_state[7][0] = BISHOP_WHITE;
|
|
|
|
board_state[7][7] = BISHOP_WHITE;
|
|
|
|
board_state[7][1] = KNIGHT_WHITE;
|
|
|
|
board_state[7][6] = KNIGHT_WHITE;
|
|
|
|
board_state[7][2] = ROOK_WHITE;
|
|
|
|
board_state[7][5] = ROOK_WHITE;
|
|
|
|
board_state[7][3] = QUEEN_WHITE;
|
|
|
|
board_state[7][4] = KING_WHITE;
|
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
board_state[1][i] = PAWN_BLACK;
|
|
|
|
board_state[6][i] = PAWN_WHITE;
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < 12; i++)
|
|
|
|
{
|
|
|
|
//location of all the sprites plus the size of file names
|
|
|
|
char file[25] = "sprites\\";
|
|
|
|
memcpy(&file[8], file_names[i], 16);
|
|
|
|
bitmapSurface = SDL_LoadBMP(file);
|
|
|
|
bitmapTextures[i] = SDL_CreateTextureFromSurface(p_renderer, bitmapSurface);
|
|
|
|
}
|
|
|
|
SDL_FreeSurface(bitmapSurface);
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:36:55 -05:00
|
|
|
void draw_board(SDL_Renderer *p_renderer)
|
|
|
|
{
|
|
|
|
SDL_SetRenderTarget(p_renderer, board_texture);
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0x7f, 0x7f, 0x7f, 0);
|
|
|
|
SDL_RenderClear(p_renderer);
|
|
|
|
SDL_RenderDrawRect(p_renderer, &rectangle);
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0xFF, 0x00);
|
|
|
|
rectangle.w = board_width;
|
|
|
|
rectangle.h = board_width;
|
|
|
|
rectangle.x = (width - board_width) / 2;
|
|
|
|
rectangle.y = (height - board_width) / 2;
|
|
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
2020-04-16 21:23:25 -04:00
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
2020-02-27 18:36:55 -05:00
|
|
|
int square_size = board_width / 8;
|
|
|
|
int starting_x = rectangle.x;
|
|
|
|
rectangle.w = square_size;
|
|
|
|
rectangle.h = square_size;
|
|
|
|
for (size_t j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
rectangle.x = starting_x;
|
2020-04-16 21:23:25 -04:00
|
|
|
for (size_t i = 0; i < 8; i++)
|
2020-02-27 18:36:55 -05:00
|
|
|
{
|
2020-04-16 21:23:25 -04:00
|
|
|
if((board_state[j][i] & 0xF0u) == POTENTIAL_MOVE)
|
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
|
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
|
|
|
}
|
|
|
|
else if ((board_state[j][i] & 0xF0u) == POTENTIAL_TAKE)
|
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
|
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
|
|
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
|
|
|
}
|
|
|
|
else if (((i % 2) + (j % 2)) == 1)
|
|
|
|
{
|
|
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* code */
|
|
|
|
}
|
|
|
|
|
|
|
|
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
|
|
|
{
|
|
|
|
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &rectangle);
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:36:55 -05:00
|
|
|
rectangle.x += square_size;
|
|
|
|
}
|
|
|
|
rectangle.y += square_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetRenderTarget(p_renderer, NULL);
|
|
|
|
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
|
|
|
}
|