Refactoring Code some more. Game logic is completely isolated into chess_board
This commit is contained in:
parent
9afc19cba0
commit
72d2f4e4fd
@ -1,65 +1,14 @@
|
||||
#include "chess_board.h"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include "stdio.h"
|
||||
#include <stack>
|
||||
#include <string.h>
|
||||
#include "pieces.hpp"
|
||||
|
||||
#define MARGIN 300
|
||||
|
||||
#define PAWN_WHITE 0u
|
||||
#define PAWN_BLACK 1u
|
||||
#define KING_WHITE 2u
|
||||
#define KING_BLACK 3u
|
||||
#define ROOK_WHITE 4u
|
||||
#define ROOK_BLACK 5u
|
||||
#define KNIGHT_WHITE 6u
|
||||
#define KNIGHT_BLACK 7u
|
||||
#define BISHOP_WHITE 8u
|
||||
#define BISHOP_BLACK 9u
|
||||
#define QUEEN_WHITE 10u
|
||||
#define QUEEN_BLACK 11u
|
||||
#define SQUARE_EMPTY 12u
|
||||
|
||||
#define LIGHT_OFF 0u
|
||||
#define POTENTIAL_MOVE 1u
|
||||
#define POTENTIAL_TAKE 2u
|
||||
#define SUGGESTED_MOVE 3u
|
||||
#define ERROR_MOVE 4u
|
||||
#define PIECE_ORIGIN 5u
|
||||
#define PIECE_NEEDS_TO_BE_HERE 6u
|
||||
#define POTENTIAL_CASTLE 7u
|
||||
#define PIECE_NEEDS_TO_BE_REMOVED 8u
|
||||
|
||||
#define GAME_STATE_IDLE 0u
|
||||
#define GAME_STATE_P1_TURN_BEGINING 1u
|
||||
#define GAME_STATE_P1_TURN_IN_PROGRESS 2u
|
||||
#define GAME_STATE_P1_TURN_TAKING 3u
|
||||
#define GAME_STATE_P2_TURN_BEGINING 4u
|
||||
#define GAME_STATE_P2_TURN_IN_PROGRESS 5u
|
||||
#define GAME_STATE_P2_TURN_TAKING 6u
|
||||
#define GAME_STATE_ERROR_DETECTED 7u
|
||||
|
||||
const char File_Names[12][17] = {"pawn_white.bmp", "pawn_black.bmp",
|
||||
"king_white.bmp", "king_black.bmp",
|
||||
"tower_white.bmp", "tower_black.bmp",
|
||||
"horse_white.bmp", "horse_black.bmp",
|
||||
"bishop_white.bmp", "bishop_black.bmp",
|
||||
"queen_white.bmp", "queen_black.bmp",
|
||||
};
|
||||
|
||||
static int Height;
|
||||
static int Width;
|
||||
static SDL_Texture * Board_Texture;
|
||||
static SDL_Rect Rectangle;
|
||||
static int Board_Width;
|
||||
static uint8_t Board_State[12][8] = {{0}};
|
||||
static uint8_t Current_Binary_Board[12] = {0};
|
||||
static uint8_t Saved_Binary_Board[12] = {0};
|
||||
static uint8_t Board_Lights[12][8] = {{0}};
|
||||
SDL_Surface *bitmapSurface = NULL;
|
||||
SDL_Texture * bitmapTextures[12] = {NULL};
|
||||
static uint8_t Game_State = GAME_STATE_P1_TURN_BEGINING;
|
||||
static uint8_t Last_Game_State = GAME_STATE_P1_TURN_BEGINING;
|
||||
static bool White_Turn = true;
|
||||
@ -637,7 +586,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
|
||||
/**
|
||||
* @brief Function for marking the taken pieces potention moves to jail.
|
||||
*/
|
||||
void Mark_Taken_Piece_Spots_In_Jail(void)
|
||||
static void Mark_Taken_Piece_Spots_In_Jail(void)
|
||||
{
|
||||
uint8_t add = (white_team(Taken_Piece) ? 8u : 10u);
|
||||
switch (Taken_Piece)
|
||||
@ -690,7 +639,7 @@ void Mark_Taken_Piece_Spots_In_Jail(void)
|
||||
/**
|
||||
* @brief Function for switching the players turn. Incharge of handling the state machine reset.
|
||||
*/
|
||||
void Switch_Turns(void)
|
||||
static void Switch_Turns(void)
|
||||
{
|
||||
Game_State = (White_Turn ? GAME_STATE_P2_TURN_BEGINING : GAME_STATE_P1_TURN_BEGINING);
|
||||
White_Turn = !White_Turn;
|
||||
@ -708,7 +657,7 @@ void Switch_Turns(void)
|
||||
* @param column: Current column location of the piece.
|
||||
* @retval None
|
||||
*/
|
||||
void Check_If_Moving_King(uint8_t row, uint8_t column)
|
||||
static void Check_If_Moving_King(uint8_t row, uint8_t column)
|
||||
{
|
||||
uint8_t white_black_idx = White_Turn ? 0u : 1u;
|
||||
if((Selected_Piece == KING_WHITE) || (Selected_Piece == KING_BLACK))
|
||||
@ -739,7 +688,7 @@ void Check_If_Moving_King(uint8_t row, uint8_t column)
|
||||
* @param i: column location that was toggled.
|
||||
* @retval None
|
||||
*/
|
||||
void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
|
||||
static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
|
||||
{
|
||||
switch (Game_State)
|
||||
{
|
||||
@ -935,17 +884,24 @@ void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
|
||||
}
|
||||
}
|
||||
|
||||
void Board_get_lights_and_state(uint8_t board_lights[12][8], uint8_t board_state[12][8])
|
||||
{
|
||||
memcpy(&board_lights[0][0], &Board_Lights[0][0], sizeof(Board_Lights));
|
||||
memcpy(&board_state[0][0], &Board_State[0][0], sizeof(Board_State));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief The Board changed so now we have to see what is different and act accordingly.
|
||||
* @note Yes i know the design of this seems really bad but it's important to remember this is supposed to simulate the chess board I'm creating.
|
||||
* so I'm designing it this way because of the hardware that I'm using.
|
||||
* @retval None
|
||||
*/
|
||||
void Board_Changed(void)
|
||||
void Board_Changed(uint8_t current_binary_board[12])
|
||||
{
|
||||
for (uint8_t j = 0u; j < 12u; j++)
|
||||
{
|
||||
uint8_t difference = (Current_Binary_Board[j] ^ Saved_Binary_Board[j]);
|
||||
uint8_t difference = (current_binary_board[j] ^ Saved_Binary_Board[j]);
|
||||
if (difference != 0u)
|
||||
{
|
||||
for (uint8_t i = 0u; i < 8u; i++)
|
||||
@ -956,108 +912,18 @@ void Board_Changed(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
Saved_Binary_Board[j] = Current_Binary_Board[j];
|
||||
Saved_Binary_Board[j] = current_binary_board[j];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for registering an incoming click
|
||||
* @param p_renderer: Pointer to the renderer
|
||||
* @param x: x location of the click
|
||||
* @param y: y location of the click
|
||||
* @retval
|
||||
*/
|
||||
void click(SDL_Renderer *p_renderer, int x, int y)
|
||||
{
|
||||
SDL_Point const point = {x, y};
|
||||
const int square_size = Board_Width / 8;
|
||||
Rectangle.w = Board_Width + (4 * square_size);
|
||||
Rectangle.h = Board_Width;
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
Rectangle.y = (Height - Board_Width) / 2;
|
||||
if (SDL_PointInRect(&point, &Rectangle))
|
||||
{
|
||||
Rectangle.x = (Width - Board_Width) / 2;
|
||||
Rectangle.w = Board_Width;
|
||||
int starting_y = Rectangle.y;
|
||||
const 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))
|
||||
{
|
||||
Current_Binary_Board[j] ^= (1u << i);
|
||||
Board_Changed();
|
||||
goto draw_square;
|
||||
}
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
/* Now we draw the jail */
|
||||
for (size_t j = 8; j < 12; j++)
|
||||
{
|
||||
Rectangle.y = starting_y;
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (SDL_PointInRect(&point, &Rectangle))
|
||||
{
|
||||
Current_Binary_Board[j] ^= (1u << i);
|
||||
Board_Changed();
|
||||
goto draw_square;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
/*If we are at the end of second jail row, jump to the other side */
|
||||
if (j == 9)
|
||||
{
|
||||
Rectangle.x += (Board_Width + square_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
}
|
||||
draw_square:
|
||||
SDL_SetRenderTarget(p_renderer, Board_Texture);
|
||||
draw_board(p_renderer);
|
||||
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||
SDL_RenderPresent(p_renderer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for resizing the display of the board
|
||||
* @param p_renderer: pointer to the renderer
|
||||
* @param w: width of the new window
|
||||
* @param h: hight of the new window
|
||||
* @retval None
|
||||
*/
|
||||
void chess_board_resize(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for initializing the board
|
||||
* @note
|
||||
* @param *p_renderer pointer to the renderer:
|
||||
* @retval None
|
||||
*/
|
||||
void chess_board_init(SDL_Renderer *p_renderer)
|
||||
void chess_board_init(void)
|
||||
{
|
||||
for (uint8_t i = 0u; i < 12u; i++)
|
||||
{
|
||||
@ -1066,11 +932,6 @@ void chess_board_init(SDL_Renderer *p_renderer)
|
||||
Board_State[i][j] = SQUARE_EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
Current_Binary_Board[0] = 0xFF;
|
||||
Current_Binary_Board[1] = 0xFF;
|
||||
Current_Binary_Board[6] = 0xFF;
|
||||
Current_Binary_Board[7] = 0xFF;
|
||||
Saved_Binary_Board[0] = 0xFF;
|
||||
Saved_Binary_Board[1] = 0xFF;
|
||||
Saved_Binary_Board[6] = 0xFF;
|
||||
@ -1099,136 +960,4 @@ void chess_board_init(SDL_Renderer *p_renderer)
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Funtion for that will draw the current state of the board including pecies and colors for suggested and possible moves.
|
||||
* @param *p_renderer pointer to the renderer object:
|
||||
* @retval None
|
||||
*/
|
||||
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, 0x85, 0x5E, 0x42, 0x00);
|
||||
const 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 ((Board_Lights[j][i] == POTENTIAL_MOVE) || (Board_Lights[j][i] == POTENTIAL_CASTLE))
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if ((Board_Lights[j][i] == POTENTIAL_TAKE) || (Board_Lights[j][i] == PIECE_NEEDS_TO_BE_HERE) || (Board_Lights[j][i] == PIECE_NEEDS_TO_BE_REMOVED))
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if (Board_Lights[j][i] == PIECE_ORIGIN)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0xFF, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if (Board_Lights[j][i] == ERROR_MOVE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 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);
|
||||
}
|
||||
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
Rectangle.y = (Height - Board_Width) / 2;
|
||||
int starting_y = Rectangle.y;
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6f, 0x6f, 0x00);
|
||||
|
||||
/* Now we draw the jail */
|
||||
for (size_t j = 8; j < 12; j++)
|
||||
{
|
||||
Rectangle.y = starting_y;
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (Board_Lights[j][i] == PIECE_NEEDS_TO_BE_HERE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||
}
|
||||
else if (Board_Lights[j][i] == ERROR_MOVE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((Board_State[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
||||
{
|
||||
SDL_RenderCopy(p_renderer, bitmapTextures[(Board_State[j][i] & 0x0Fu)], NULL, &Rectangle);
|
||||
}
|
||||
|
||||
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
/*If we are at the end of second jail row, jump to the other side */
|
||||
if(j == 9)
|
||||
{
|
||||
Rectangle.x += (Board_Width + square_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(p_renderer, NULL);
|
||||
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||
}
|
||||
|
@ -1,7 +1,41 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
void click(SDL_Renderer *p_renderer, int x, int y);
|
||||
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);
|
||||
#define LIGHT_OFF 0u
|
||||
#define POTENTIAL_MOVE 1u
|
||||
#define POTENTIAL_TAKE 2u
|
||||
#define SUGGESTED_MOVE 3u
|
||||
#define ERROR_MOVE 4u
|
||||
#define PIECE_ORIGIN 5u
|
||||
#define PIECE_NEEDS_TO_BE_HERE 6u
|
||||
#define POTENTIAL_CASTLE 7u
|
||||
#define PIECE_NEEDS_TO_BE_REMOVED 8u
|
||||
|
||||
#define PAWN_WHITE 0u
|
||||
#define PAWN_BLACK 1u
|
||||
#define KING_WHITE 2u
|
||||
#define KING_BLACK 3u
|
||||
#define ROOK_WHITE 4u
|
||||
#define ROOK_BLACK 5u
|
||||
#define KNIGHT_WHITE 6u
|
||||
#define KNIGHT_BLACK 7u
|
||||
#define BISHOP_WHITE 8u
|
||||
#define BISHOP_BLACK 9u
|
||||
#define QUEEN_WHITE 10u
|
||||
#define QUEEN_BLACK 11u
|
||||
#define SQUARE_EMPTY 12u
|
||||
|
||||
|
||||
|
||||
#define GAME_STATE_IDLE 0u
|
||||
#define GAME_STATE_P1_TURN_BEGINING 1u
|
||||
#define GAME_STATE_P1_TURN_IN_PROGRESS 2u
|
||||
#define GAME_STATE_P1_TURN_TAKING 3u
|
||||
#define GAME_STATE_P2_TURN_BEGINING 4u
|
||||
#define GAME_STATE_P2_TURN_IN_PROGRESS 5u
|
||||
#define GAME_STATE_P2_TURN_TAKING 6u
|
||||
#define GAME_STATE_ERROR_DETECTED 7u
|
||||
|
||||
void chess_board_init(void);
|
||||
void Board_Changed(uint8_t current_binary_board[12]);
|
||||
void Board_get_lights_and_state(uint8_t board_lights[12][8], uint8_t board_state[12][8]);
|
||||
|
12
src/game.cpp
12
src/game.cpp
@ -7,6 +7,7 @@
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <time.h>
|
||||
#include "chess_board.h"
|
||||
#include "user_interface_abstraction.h"
|
||||
|
||||
static clock_t start_time, end_time;
|
||||
|
||||
@ -18,8 +19,9 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win)
|
||||
srcR.w = 800;
|
||||
destR.h = 800;
|
||||
destR.w = 800;
|
||||
chess_board_resize(renderer, 800, 800);
|
||||
chess_board_init(renderer);
|
||||
chess_board_init();
|
||||
ui_resize(renderer, 800, 800);
|
||||
ui_init(renderer);
|
||||
srcR.x = 0;
|
||||
srcR.y = 0;
|
||||
destR.x = 0;
|
||||
@ -44,7 +46,7 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win)
|
||||
{
|
||||
start_time = clock();
|
||||
|
||||
click(renderer, event.button.x, event.button.y);
|
||||
ui_click(renderer, event.button.x, event.button.y);
|
||||
|
||||
end_time = clock();
|
||||
SDL_RenderPresent(renderer);
|
||||
@ -77,7 +79,7 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win)
|
||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||
redraw = true;
|
||||
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
||||
chess_board_resize(renderer, destR.w, destR.h);
|
||||
ui_resize(renderer, destR.w, destR.h);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -90,7 +92,7 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win)
|
||||
{
|
||||
start_time = clock();
|
||||
|
||||
draw_board(renderer);
|
||||
ui_redraw_board(renderer);
|
||||
|
||||
end_time = clock();
|
||||
SDL_RenderPresent(renderer);
|
||||
|
265
src/user_interface_abstraction.cpp
Normal file
265
src/user_interface_abstraction.cpp
Normal file
@ -0,0 +1,265 @@
|
||||
#include "user_interface_abstraction.h"
|
||||
#include "chess_board.h"
|
||||
#include <stack>
|
||||
#include <string.h>
|
||||
|
||||
#define MARGIN 300
|
||||
|
||||
const char File_Names[12][17] = {"pawn_white.bmp", "pawn_black.bmp",
|
||||
"king_white.bmp", "king_black.bmp",
|
||||
"tower_white.bmp", "tower_black.bmp",
|
||||
"horse_white.bmp", "horse_black.bmp",
|
||||
"bishop_white.bmp", "bishop_black.bmp",
|
||||
"queen_white.bmp", "queen_black.bmp",
|
||||
};
|
||||
|
||||
static int Height;
|
||||
static int Width;
|
||||
static SDL_Texture * Board_Texture;
|
||||
static SDL_Rect Rectangle;
|
||||
static int Board_Width;
|
||||
SDL_Surface *bitmapSurface = NULL;
|
||||
SDL_Texture * bitmapTextures[12] = {NULL};
|
||||
|
||||
static uint8_t Current_Binary_Board[12] = {0};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Funtion for that will draw the current state of the board including pecies and colors for suggested and possible moves.
|
||||
* @param *p_renderer pointer to the renderer object:
|
||||
* @retval None
|
||||
*/
|
||||
static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8], uint8_t board_state[12][8])
|
||||
{
|
||||
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, 0x85, 0x5E, 0x42, 0x00);
|
||||
const 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 ((board_lights[j][i] == POTENTIAL_MOVE) || (board_lights[j][i] == POTENTIAL_CASTLE))
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if ((board_lights[j][i] == POTENTIAL_TAKE) || (board_lights[j][i] == PIECE_NEEDS_TO_BE_HERE) || (board_lights[j][i] == PIECE_NEEDS_TO_BE_REMOVED))
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if (board_lights[j][i] == PIECE_ORIGIN)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0xFF, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||
}
|
||||
else if (board_lights[j][i] == ERROR_MOVE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 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);
|
||||
}
|
||||
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
Rectangle.y = (Height - Board_Width) / 2;
|
||||
int starting_y = Rectangle.y;
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6f, 0x6f, 0x00);
|
||||
|
||||
/* Now we draw the jail */
|
||||
for (size_t j = 8; j < 12; j++)
|
||||
{
|
||||
Rectangle.y = starting_y;
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (board_lights[j][i] == PIECE_NEEDS_TO_BE_HERE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||
}
|
||||
else if (board_lights[j][i] == ERROR_MOVE)
|
||||
{
|
||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0x00, 0x00);
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
||||
{
|
||||
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &Rectangle);
|
||||
}
|
||||
|
||||
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
/*If we are at the end of second jail row, jump to the other side */
|
||||
if(j == 9)
|
||||
{
|
||||
Rectangle.x += (Board_Width + square_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(p_renderer, NULL);
|
||||
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
void ui_redraw_board(SDL_Renderer *p_renderer)
|
||||
{
|
||||
uint8_t board_lights[12][8];
|
||||
uint8_t board_state[12][8];
|
||||
Board_get_lights_and_state(board_lights, board_state);
|
||||
ui_draw_board(p_renderer, board_lights, board_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for resizing the display of the board
|
||||
* @param p_renderer: pointer to the renderer
|
||||
* @param w: width of the new window
|
||||
* @param h: hight of the new window
|
||||
* @retval None
|
||||
*/
|
||||
void ui_resize(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for registering an incoming click
|
||||
* @param p_renderer: Pointer to the renderer
|
||||
* @param x: x location of the click
|
||||
* @param y: y location of the click
|
||||
* @retval
|
||||
*/
|
||||
void ui_click(SDL_Renderer *p_renderer, int x, int y)
|
||||
{
|
||||
SDL_Point const point = {x, y};
|
||||
const int square_size = Board_Width / 8;
|
||||
Rectangle.w = Board_Width + (4 * square_size);
|
||||
Rectangle.h = Board_Width;
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
Rectangle.y = (Height - Board_Width) / 2;
|
||||
if (SDL_PointInRect(&point, &Rectangle))
|
||||
{
|
||||
Rectangle.x = (Width - Board_Width) / 2;
|
||||
Rectangle.w = Board_Width;
|
||||
int starting_y = Rectangle.y;
|
||||
const 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))
|
||||
{
|
||||
Current_Binary_Board[j] ^= (1u << i);
|
||||
Board_Changed(Current_Binary_Board);
|
||||
goto draw_square;
|
||||
}
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
|
||||
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||
/* Now we draw the jail */
|
||||
for (size_t j = 8; j < 12; j++)
|
||||
{
|
||||
Rectangle.y = starting_y;
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (SDL_PointInRect(&point, &Rectangle))
|
||||
{
|
||||
Current_Binary_Board[j] ^= (1u << i);
|
||||
Board_Changed(Current_Binary_Board);
|
||||
goto draw_square;
|
||||
}
|
||||
Rectangle.y += square_size;
|
||||
}
|
||||
/*If we are at the end of second jail row, jump to the other side */
|
||||
if (j == 9)
|
||||
{
|
||||
Rectangle.x += (Board_Width + square_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rectangle.x += square_size;
|
||||
}
|
||||
}
|
||||
draw_square:
|
||||
SDL_SetRenderTarget(p_renderer, Board_Texture);
|
||||
ui_redraw_board(p_renderer);//, Board_Lights, Board_State);
|
||||
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||
SDL_RenderPresent(p_renderer);
|
||||
}
|
||||
}
|
||||
|
||||
void ui_init(SDL_Renderer *p_renderer)
|
||||
{
|
||||
Current_Binary_Board[0] = 0xFF;
|
||||
Current_Binary_Board[1] = 0xFF;
|
||||
Current_Binary_Board[6] = 0xFF;
|
||||
Current_Binary_Board[7] = 0xFF;
|
||||
|
||||
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);
|
||||
}
|
14
src/user_interface_abstraction.h
Normal file
14
src/user_interface_abstraction.h
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* The file is used to to abstract away anything realated to the user interface.
|
||||
* The intent is that the only thing that would need to be re-written between
|
||||
* the PC test software and the actual chess board is this module.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
|
||||
void ui_resize(SDL_Renderer *p_renderer, int w, int h);
|
||||
void ui_redraw_board(SDL_Renderer *p_renderer);
|
||||
void ui_click(SDL_Renderer *p_renderer, int x, int y);
|
||||
void ui_init(SDL_Renderer *p_renderer);
|
||||
|
Loading…
Reference in New Issue
Block a user