114 lines
3.1 KiB
C++
114 lines
3.1 KiB
C++
#include "chess_board.h"
|
|
#include <SDL.h>
|
|
#include <SDL_video.h>
|
|
#include "stdio.h"
|
|
|
|
#define MARGIN 200
|
|
|
|
typedef enum
|
|
{
|
|
SQUARE_EMPTY = 0,
|
|
KING,
|
|
QUEEN,
|
|
ROOK,
|
|
KNIGHT,
|
|
BISHOP,
|
|
PAWN
|
|
};
|
|
|
|
static int height;
|
|
static int width;
|
|
static SDL_Texture *board_texture;
|
|
static SDL_Rect rectangle;
|
|
static int board_width;
|
|
uint8_t board_state[8][8] = 0u;
|
|
|
|
|
|
bool click(SDL_Renderer *p_renderer, int x, int y)
|
|
{
|
|
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))
|
|
{
|
|
|
|
goto draw_square;
|
|
}
|
|
rectangle.x += square_size;
|
|
}
|
|
rectangle.y += square_size;
|
|
}
|
|
draw_square:
|
|
SDL_SetRenderTarget(p_renderer, board_texture);
|
|
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
|
SDL_RenderDrawRect(p_renderer, &rectangle);
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
|
SDL_SetRenderTarget(p_renderer, NULL);
|
|
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
|
SDL_RenderPresent(p_renderer);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void chess_board_init(SDL_Renderer *p_renderer, int w, int h)
|
|
{
|
|
width = w;
|
|
height = h;
|
|
SDL_DestroyTexture(board_texture);
|
|
board_texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
|
board_width = ((w > h) ? h : w ) - MARGIN;
|
|
|
|
// get rid of rounding errors
|
|
board_width -= board_width % 8;
|
|
}
|
|
|
|
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);
|
|
SDL_SetRenderDrawColor(p_renderer, 0, 0, 0, 0x00);
|
|
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;
|
|
if ((j % 2) == 0)
|
|
{
|
|
rectangle.x += square_size;
|
|
}
|
|
for (size_t i = 0; i < 4; i++)
|
|
{
|
|
SDL_RenderFillRect(p_renderer, &rectangle);
|
|
rectangle.x += 2 * square_size;
|
|
}
|
|
rectangle.y += square_size;
|
|
}
|
|
|
|
SDL_SetRenderTarget(p_renderer, NULL);
|
|
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
|
}
|