diff --git a/CMakeLists.txt b/CMakeLists.txt index 4948bf4..7ffb65d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,21 @@ project(Chess C CXX) find_package(SDL2 REQUIRED) -file(GLOB sources +file(GLOB_RECURSE sources CONFIGURE_DEPENDS - src/*.cpp) + "src/*.cpp") + +file (GLOB_RECURSE headers CONFIGURE_DEPENDS "src/*.h") + +set (include_dirs "") +foreach (_headerFile ${headers}) + get_filename_component(_dir ${_headerFile} PATH) + list (APPEND include_dirs ${_dir}) +endforeach() add_executable(Chess ${sources}) set_target_properties(Chess PROPERTIES CXX_STANDARD 17) # set standard level +target_include_directories(Chess PRIVATE ${include_dirs}) target_compile_options(Chess PRIVATE -Wall -Wextra -Wredundant-decls -Wcast-align -Wshadow -Wnon-virtual-dtor diff --git a/src/chess_board.cpp b/src/game_logic/chess_board.cpp similarity index 93% rename from src/chess_board.cpp rename to src/game_logic/chess_board.cpp index ddb077c..464dbf1 100644 --- a/src/chess_board.cpp +++ b/src/game_logic/chess_board.cpp @@ -25,7 +25,7 @@ static uint8_t Pawn_Converted_To = QUEEN_WHITE; /** - * @brief Function for clearing all of the lights on the board. Except for error moves. + * @brief Function for clearing all of the lights on the board. Except for error moves. * @retval None */ static void clear_lights(void) @@ -40,13 +40,13 @@ static void clear_lights(void) } } } - + } /** - * @brief Function for determining if the piece is on the white team or on the black team. - * @note Pieces should be enumerated even if white and odd if black. - * @param piece: The piece under question. + * @brief Function for determining if the piece is on the white team or on the black team. + * @note Pieces should be enumerated even if white and odd if black. + * @param piece: The piece under question. * @retval Return true if on the white team, else false. */ static bool white_team(uint8_t piece) @@ -55,9 +55,9 @@ static bool white_team(uint8_t piece) } /** - * @brief Function for determining if two pieces are on the same team or not. + * @brief Function for determining if two pieces are on the same team or not. * @param piece_one: Piece one ofcoarse. - * @param piece_two: Piece two obviously. + * @param piece_two: Piece two obviously. * @retval True if on opposite teams, else false. */ static bool opposite_teams(uint8_t piece_one, uint8_t piece_two) @@ -69,7 +69,7 @@ static bool opposite_teams(uint8_t piece_one, uint8_t piece_two) * @brief Check to see if the square is safe from the other team. * @param column: Column of potential move * @param row: Row of the potential move - * @retval True if the square is safe, else is false + * @retval True if the square is safe, else is false */ bool square_is_safe(uint8_t row, uint8_t column) { @@ -292,7 +292,7 @@ static bool Set_Light(uint8_t piece, uint8_t row, uint8_t column, uint8_t state) } /** - * @brief Function for marking potential moves for pawns. + * @brief Function for marking potential moves for pawns. * @param row: row to move to * @param column: column to move to * @retval None @@ -309,12 +309,12 @@ static bool pawn_move(uint8_t piece, uint8_t row, uint8_t column) /** * @brief Function for "casting" a ray in any direction, vertical, horizontal, or diagonal. If the ray hits someone from the other team - * or the end of the board the array will be terminated. + * or the end of the board the array will be terminated. * @param direction_r: Row direction * @param direction_c: Column direction * @param row: current row location * @param column: current column location - * @param piece: the piece that is casting the ray. + * @param piece: the piece that is casting the ray. * @retval None */ static bool cast_a_ray(uint8_t piece, int8_t direction_r, int8_t direction_c, uint8_t column, uint8_t row) @@ -366,10 +366,10 @@ static bool pawn_take(uint8_t piece, uint8_t row, uint8_t column) } /** - * @brief Function for marking the potential moves. - * @param piece: Piece that we are marking the potential moves for. - * @param row: Current row location of the piece. - * @param column: Current column location of the piece. + * @brief Function for marking the potential moves. + * @param piece: Piece that we are marking the potential moves for. + * @param row: Current row location of the piece. + * @param column: Current column location of the piece. * @retval None */ static bool Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row) @@ -431,8 +431,8 @@ static bool Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row) } ret_val = cast_a_ray(piece, up_down_step, left_right_step, column, row) || ret_val; } - break; - + break; + } case KNIGHT_WHITE: case KNIGHT_BLACK: @@ -472,7 +472,7 @@ static bool Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row) { up_down_step = (i == 0u) ? -1 : 1; } - + int8_t x = (int8_t)row + left_right_step; int8_t y = (int8_t)column + up_down_step; if ((x >= 0) && (y >= 0) && (x < 8) && (y < 8)) @@ -565,7 +565,7 @@ static bool Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row) if (square_is_safe(row, column)) { // Queen side castle - if(Castling_Allowed[white_black_idx][0u] && (Board_State[kings_row][1u] == SQUARE_EMPTY) + if(Castling_Allowed[white_black_idx][0u] && (Board_State[kings_row][1u] == SQUARE_EMPTY) && (Board_State[kings_row][2u] == SQUARE_EMPTY) && (Board_State[kings_row][3u]) == SQUARE_EMPTY) { //First Check to see if the king will pass through check @@ -617,7 +617,7 @@ bool Check_If_Player_Can_Move(bool white) } } } - + } clear_lights(); // SDL_Log("Player cant move"); @@ -625,18 +625,18 @@ bool Check_If_Player_Can_Move(bool white) } /** - * @brief Function for switching the players turn. Incharge of handling the state machine reset. + * @brief Function for switching the players turn. Incharge of handling the state machine reset. */ static void Switch_Turns(void) { Game_State = (White_Turn ? GAME_STATE_P2_TURN_BEGINING : GAME_STATE_P1_TURN_BEGINING); White_Turn = !White_Turn; - // Square is safe assumes the other team is trying to attack the square so for example at the end of - // White's turn we want to see if the black king is now in check, so we will switch teams and then + // Square is safe assumes the other team is trying to attack the square so for example at the end of + // White's turn we want to see if the black king is now in check, so we will switch teams and then // Check if the current kings locations is safe. If it is safe then check is false, if it isnt safe then check is true. uint8_t white_black_idx = White_Turn ? 0u : 1u; Check[white_black_idx] = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]); - + //Last thing we need to check before sitching turns is to check if the game is over. bool player_can_play = Check_If_Player_Can_Move(White_Turn); if(!player_can_play) @@ -654,7 +654,7 @@ static void Switch_Turns(void) } /** - * @brief Function for checking the selected piece to see if we are moving the king. + * @brief Function for checking the selected piece to see if we are moving the king. * If we are then we also want to update the new location of the corresponding king. * @param row: Current row location of the piece. * @param column: Current column location of the piece. @@ -670,7 +670,7 @@ static void Check_If_Moving_King(uint8_t row, uint8_t column) Castling_Allowed[white_black_idx][0u] = false; Castling_Allowed[white_black_idx][1u] = false; } - // Disable the castling of the corresponding side if the rook is being moved. + // Disable the castling of the corresponding side if the rook is being moved. else if (((Selected_Piece == ROOK_WHITE) && (row == 7u)) || ((Selected_Piece == ROOK_BLACK) && (row == 0u))) { @@ -727,7 +727,7 @@ static bool Converting_Pawn_If_Applicable(uint8_t row, uint8_t column) { Pawn_Converted_To += 8u; } - + } ret_val = true; } @@ -739,9 +739,9 @@ static bool Converting_Pawn_If_Applicable(uint8_t row, uint8_t column) /** - * @brief Function for toggeling a square's state. - * @param j: row location that was toggled. - * @param i: column location that was toggled. + * @brief Function for toggeling a square's state. + * @param j: row location that was toggled. + * @param i: column location that was toggled. * @retval None */ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) @@ -885,7 +885,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) { /* Do nothing. */ } - + } else if (Board_Lights[j][i] == PIECE_NEEDS_TO_BE_REMOVED) { @@ -948,9 +948,9 @@ void Board_get_lights_and_state(uint8_t board_lights[8][8], uint8_t 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. + * @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(uint8_t current_binary_board[8]) @@ -970,17 +970,16 @@ void Board_Changed(uint8_t current_binary_board[8]) } Saved_Binary_Board[j] = current_binary_board[j]; } - } /** * @brief Function for initializing the board - * @note + * @note * @retval None */ void chess_board_init(void) -{ +{ for (uint8_t i = 0u; i < 8u; i++) { for (uint8_t j = 0u; j < 8u; j++) diff --git a/src/chess_board.h b/src/game_logic/chess_board.h similarity index 100% rename from src/chess_board.h rename to src/game_logic/chess_board.h diff --git a/src/game.cpp b/src/pc_app/game.cpp similarity index 95% rename from src/game.cpp rename to src/pc_app/game.cpp index e5926f2..5b41b8b 100644 --- a/src/game.cpp +++ b/src/pc_app/game.cpp @@ -56,7 +56,7 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win) { /* Do nothing. */ } - + } else if (event.type == SDL_QUIT) { @@ -92,7 +92,7 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win) start_time = clock(); ui_redraw_board(renderer); - + end_time = clock(); SDL_RenderPresent(renderer); clock_t t = end_time - start_time; @@ -105,5 +105,5 @@ int begin_game(SDL_Renderer *renderer, SDL_Window *win) } //g++ main.cpp -o blah `sdl2-config --cflags --libs` /* -g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain +g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain */ diff --git a/src/game.h b/src/pc_app/game.h similarity index 100% rename from src/game.h rename to src/pc_app/game.h diff --git a/src/main.cpp b/src/pc_app/main.cpp similarity index 95% rename from src/main.cpp rename to src/pc_app/main.cpp index 9d717b4..35229e6 100644 --- a/src/main.cpp +++ b/src/pc_app/main.cpp @@ -28,6 +28,7 @@ int main( int argc, const char* argv[] ) printf("Could not create window: %s\n", SDL_GetError()); return 1; } + renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_Rect rectangle; @@ -48,5 +49,5 @@ int main( int argc, const char* argv[] ) } //g++ main.cpp -o blah `sdl2-config --cflags --libs` /* -g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain -*/ \ No newline at end of file +g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain +*/ diff --git a/src/user_interface_abstraction.cpp b/src/pc_app/user_interface_abstraction.cpp similarity index 96% rename from src/user_interface_abstraction.cpp rename to src/pc_app/user_interface_abstraction.cpp index 592d1ab..b09568e 100644 --- a/src/user_interface_abstraction.cpp +++ b/src/pc_app/user_interface_abstraction.cpp @@ -23,6 +23,14 @@ SDL_Texture * bitmapTextures[12] = {NULL}; static uint8_t Current_Binary_Board[8] = {0}; +/** + * @brief Function for setting the lights on the board to the end game state, + * clearly indicating which player won. + * + * @param p_renderer Sdl Renderer + * @param board_state board state + * @param game_state games state + */ static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[8][8], uint8_t game_state) { SDL_SetRenderTarget(p_renderer, Board_Texture); @@ -52,7 +60,7 @@ static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[8][8] 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++) { @@ -83,7 +91,7 @@ static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[8][8] { /* code */ } - + Rectangle.x += square_size; } @@ -96,7 +104,7 @@ static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[8][8] /** * @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: * @retval None */ static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8], uint8_t board_state[12][8]) @@ -159,7 +167,7 @@ static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8], { /* code */ } - + if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY) { SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &Rectangle); @@ -197,9 +205,9 @@ static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8], { 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); @@ -264,8 +272,8 @@ void ui_resize(SDL_Renderer *p_renderer, int w, int h) * @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 + * @param y: y location of the click + * @retval */ void ui_click(SDL_Renderer *p_renderer, int x, int y) { @@ -332,8 +340,13 @@ void ui_click(SDL_Renderer *p_renderer, int x, int y) } } +/** + * @brief Initialize the ui for the board. Setting all pieces in the correct starting positions + * + * @param p_renderer pointer to the sdl renderer + */ void ui_init(SDL_Renderer *p_renderer) -{ +{ Current_Binary_Board[0] = 0xFF; Current_Binary_Board[1] = 0xFF; Current_Binary_Board[6] = 0xFF; diff --git a/src/user_interface_abstraction.h b/src/pc_app/user_interface_abstraction.h similarity index 96% rename from src/user_interface_abstraction.h rename to src/pc_app/user_interface_abstraction.h index b70d6ce..e25c610 100644 --- a/src/user_interface_abstraction.h +++ b/src/pc_app/user_interface_abstraction.h @@ -1,7 +1,7 @@ /** * 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. + * 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