Adding chess Peice sprites
198
chess_board.cpp
@ -2,29 +2,101 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
#include <stack>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define MARGIN 200
|
#define MARGIN 200
|
||||||
|
|
||||||
typedef enum
|
#define PAWN_WHITE 0u
|
||||||
{
|
#define PAWN_BLACK 1u
|
||||||
SQUARE_EMPTY = 0,
|
#define KING_WHITE 2u
|
||||||
KING,
|
#define KING_BLACK 3u
|
||||||
QUEEN,
|
#define QUEEN_WHITE 4u
|
||||||
ROOK,
|
#define QUEEN_BLACK 5u
|
||||||
KNIGHT,
|
#define ROOK_WHITE 6u
|
||||||
BISHOP,
|
#define ROOK_BLACK 7u
|
||||||
PAWN
|
#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"};
|
||||||
|
|
||||||
static int height;
|
static int height;
|
||||||
static int width;
|
static int width;
|
||||||
static SDL_Texture * board_texture;
|
static SDL_Texture * board_texture;
|
||||||
static SDL_Rect rectangle;
|
static SDL_Rect rectangle;
|
||||||
static int board_width;
|
static int board_width;
|
||||||
uint8_t board_state[8][8] = 0u;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool click(SDL_Renderer *p_renderer, int x, int y)
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool click(SDL_Renderer *p_renderer, int x, int y, bool pick_up)
|
||||||
{
|
{
|
||||||
SDL_Point const point = {x, y};
|
SDL_Point const point = {x, y};
|
||||||
bool ret_val = false;
|
bool ret_val = false;
|
||||||
@ -45,6 +117,21 @@ bool click(SDL_Renderer *p_renderer, int x, int y)
|
|||||||
{
|
{
|
||||||
if(SDL_PointInRect(&point, &rectangle))
|
if(SDL_PointInRect(&point, &rectangle))
|
||||||
{
|
{
|
||||||
|
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 */
|
||||||
|
}
|
||||||
|
|
||||||
goto draw_square;
|
goto draw_square;
|
||||||
}
|
}
|
||||||
@ -54,28 +141,72 @@ bool click(SDL_Renderer *p_renderer, int x, int y)
|
|||||||
}
|
}
|
||||||
draw_square:
|
draw_square:
|
||||||
SDL_SetRenderTarget(p_renderer, board_texture);
|
SDL_SetRenderTarget(p_renderer, board_texture);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
//SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
||||||
SDL_RenderDrawRect(p_renderer, &rectangle);
|
//SDL_RenderDrawRect(p_renderer, &rectangle);
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
//SDL_RenderFillRect(p_renderer, &rectangle);
|
||||||
SDL_SetRenderTarget(p_renderer, NULL);
|
//SDL_SetRenderTarget(p_renderer, NULL);
|
||||||
|
draw_board(p_renderer);
|
||||||
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
||||||
SDL_RenderPresent(p_renderer);
|
SDL_RenderPresent(p_renderer);
|
||||||
}
|
}
|
||||||
return ret_val;
|
return ret_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void chess_board_init(SDL_Renderer *p_renderer, int w, int h)
|
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h)
|
||||||
{
|
{
|
||||||
width = w;
|
width = w;
|
||||||
height = h;
|
height = h;
|
||||||
SDL_DestroyTexture(board_texture);
|
SDL_DestroyTexture(board_texture);
|
||||||
board_texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
board_texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
||||||
board_width = ((w > h) ? h : w) - MARGIN;
|
board_width = ((w > h) ? h : w) - MARGIN;
|
||||||
|
|
||||||
// get rid of rounding errors
|
// get rid of rounding errors
|
||||||
board_width -= board_width % 8;
|
board_width -= board_width % 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
void draw_board(SDL_Renderer *p_renderer)
|
void draw_board(SDL_Renderer *p_renderer)
|
||||||
{
|
{
|
||||||
SDL_SetRenderTarget(p_renderer, board_texture);
|
SDL_SetRenderTarget(p_renderer, board_texture);
|
||||||
@ -88,7 +219,7 @@ void draw_board(SDL_Renderer *p_renderer)
|
|||||||
rectangle.x = (width - board_width) / 2;
|
rectangle.x = (width - board_width) / 2;
|
||||||
rectangle.y = (height - board_width) / 2;
|
rectangle.y = (height - board_width) / 2;
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0, 0, 0, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
int square_size = board_width / 8;
|
int square_size = board_width / 8;
|
||||||
int starting_x = rectangle.x;
|
int starting_x = rectangle.x;
|
||||||
rectangle.w = square_size;
|
rectangle.w = square_size;
|
||||||
@ -96,14 +227,35 @@ void draw_board(SDL_Renderer *p_renderer)
|
|||||||
for (size_t j = 0; j < 8; j++)
|
for (size_t j = 0; j < 8; j++)
|
||||||
{
|
{
|
||||||
rectangle.x = starting_x;
|
rectangle.x = starting_x;
|
||||||
if ((j % 2) == 0)
|
for (size_t i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
rectangle.x += square_size;
|
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);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < 4; i++)
|
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);
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
||||||
rectangle.x += 2 * square_size;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* code */
|
||||||
|
}
|
||||||
|
|
||||||
|
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &rectangle);
|
||||||
|
}
|
||||||
|
|
||||||
|
rectangle.x += square_size;
|
||||||
}
|
}
|
||||||
rectangle.y += square_size;
|
rectangle.y += square_size;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
|
|
||||||
bool click(SDL_Renderer *p_renderer, int x, int y);
|
bool click(SDL_Renderer *p_renderer, int x, int y, bool pick_up);
|
||||||
void chess_board_init(SDL_Renderer *p_renderer, int w, int h);
|
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h);
|
||||||
|
void chess_board_init(SDL_Renderer *p_renderer);
|
||||||
void draw_board(SDL_Renderer * p_renderer);
|
void draw_board(SDL_Renderer * p_renderer);
|
11
main.cpp
@ -48,7 +48,8 @@ int main( int argc, const char* argv[] )
|
|||||||
srcR.w = 800;
|
srcR.w = 800;
|
||||||
destR.h = 800;
|
destR.h = 800;
|
||||||
destR.w = 800;
|
destR.w = 800;
|
||||||
chess_board_init(renderer, 800, 800);
|
chess_board_resize(renderer, 800, 800);
|
||||||
|
chess_board_init(renderer);
|
||||||
srcR.x = 0;
|
srcR.x = 0;
|
||||||
srcR.y = 0;
|
srcR.y = 0;
|
||||||
destR.x = 0;
|
destR.x = 0;
|
||||||
@ -70,7 +71,11 @@ int main( int argc, const char* argv[] )
|
|||||||
}
|
}
|
||||||
else if (event.button.button == SDL_BUTTON_LEFT) // scroll up
|
else if (event.button.button == SDL_BUTTON_LEFT) // scroll up
|
||||||
{
|
{
|
||||||
redraw = click(renderer, event.button.x, event.button.y);
|
redraw = click(renderer, event.button.x, event.button.y, true);
|
||||||
|
}
|
||||||
|
else if (event.button.button == SDL_BUTTON_RIGHT) // scroll up
|
||||||
|
{
|
||||||
|
redraw = click(renderer, event.button.x, event.button.y, false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -98,7 +103,7 @@ int main( int argc, const char* argv[] )
|
|||||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||||
redraw = true;
|
redraw = true;
|
||||||
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
||||||
chess_board_init(renderer, destR.w, destR.h);
|
chess_board_resize(renderer, destR.w, destR.h);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
sprites/horse_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/horse_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/king_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/king_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/pawn_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/pawn_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/queen_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/queen_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/rook_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/rook_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/tower_black.bmp
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
sprites/tower_white.bmp
Normal file
After Width: | Height: | Size: 156 KiB |