Adding an end game lighting. Now to add the logic for the win condition...

This commit is contained in:
Daniel Weber 2023-01-09 21:46:55 -05:00
parent afd42b9164
commit a488636424
3 changed files with 93 additions and 2 deletions

View File

@ -5,7 +5,6 @@
#include "pieces.hpp" #include "pieces.hpp"
static uint8_t Board_State[12][8] = {{0}}; static uint8_t Board_State[12][8] = {{0}};
static uint8_t Saved_Binary_Board[12] = {0}; static uint8_t Saved_Binary_Board[12] = {0};
static uint8_t Board_Lights[12][8] = {{0}}; static uint8_t Board_Lights[12][8] = {{0}};
@ -1038,3 +1037,8 @@ void chess_board_init(void)
Board_State[6][i] = PAWN_WHITE; Board_State[6][i] = PAWN_WHITE;
} }
} }
void Board_get_game_state(uint8_t * game_state)
{
*game_state = Game_State;
}

View File

@ -36,6 +36,12 @@
#define GAME_STATE_P2_TURN_TAKING 6u #define GAME_STATE_P2_TURN_TAKING 6u
#define GAME_STATE_ERROR_DETECTED 7u #define GAME_STATE_ERROR_DETECTED 7u
#define GAME_STATE_OVER 8u
#define GAME_STATE_OVER_WHITE_WIN 8u
#define GAME_STATE_OVER_BLACK_WIN 9u
#define GAME_STATE_OVER_STALE_MATE 10u
void chess_board_init(void); void chess_board_init(void);
void Board_Changed(uint8_t current_binary_board[12]); 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]); void Board_get_lights_and_state(uint8_t board_lights[12][8], uint8_t board_state[12][8]);
void Board_get_game_state(uint8_t * game_state);

View File

@ -24,6 +24,77 @@ SDL_Texture * bitmapTextures[12] = {NULL};
static uint8_t Current_Binary_Board[12] = {0}; static uint8_t Current_Binary_Board[12] = {0};
static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[12][8], uint8_t game_state)
{
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;
uint8_t white_color[4] = {0xFF, 0xFF, 0x00, 0x00};
uint8_t black_color[4] = {0xFF, 0xFF, 0x00, 0x00};
if(game_state == GAME_STATE_OVER_WHITE_WIN)
{
white_color[0] = 0x00; white_color[1] = 0xFF; white_color[2] = 0x00; white_color[3] = 0x00;
black_color[0] = 0xFF; black_color[1] = 0x00; black_color[2] = 0x00; black_color[3] = 0x00;
}
else if (game_state == GAME_STATE_OVER_BLACK_WIN)
{
black_color[0] = 0x00; black_color[1] = 0xFF; black_color[2] = 0x00; black_color[3] = 0x00;
white_color[0] = 0xFF; white_color[1] = 0x00; white_color[2] = 0x00; white_color[3] = 0x00;
}
for (size_t j = 0; j < 8; j++)
{
Rectangle.x = starting_x;
for (size_t i = 0; i < 8; i++)
{
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
{
uint8_t * render_color;
if((board_state[j][i] % 2u) == 0u )
{
render_color = white_color;
}
else
{
render_color = black_color;
}
SDL_SetRenderDrawColor(p_renderer, render_color[0], render_color[1], render_color[2], render_color[3]);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &Rectangle);
}
else if (((i % 2) + (j % 2)) == 1)
{
SDL_RenderFillRect(p_renderer, &Rectangle);
}
else
{
/* code */
}
Rectangle.x += square_size;
}
Rectangle.y += square_size;
}
SDL_SetRenderTarget(p_renderer, NULL);
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
}
/** /**
* @brief Funtion for that will draw the current state of the board including pecies and colors for suggested and possible moves. * @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: * @param *p_renderer pointer to the renderer object:
@ -158,8 +229,18 @@ void ui_redraw_board(SDL_Renderer *p_renderer)
{ {
uint8_t board_lights[12][8]; uint8_t board_lights[12][8];
uint8_t board_state[12][8]; uint8_t board_state[12][8];
uint8_t game_state;
Board_get_game_state(&game_state);
Board_get_lights_and_state(board_lights, board_state); Board_get_lights_and_state(board_lights, board_state);
ui_draw_board(p_renderer, board_lights, board_state); if(game_state < GAME_STATE_OVER)
{
ui_draw_board(p_renderer, board_lights, board_state);
}
else
{
ui_draw_end_game(p_renderer, board_state, game_state);
}
} }
/** /**