diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index 3a72e56..9ae6ddf 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -40,12 +40,13 @@ int main(int argc, char const *argv[]) { exit(2); } FontHandler fontHandler = loadFonts(); + AudioHandler audioHandler = newAudioHandler(128, 128, 128); generalState = GS_MainMenu; while(generalState != GS_Quit){ switch (generalState) { case GS_MainMenu: - mainMenu(renderer,window,&generalState, fontHandler); + mainMenu(renderer,window,&generalState, fontHandler, audioHandler); break; case GS_GameCreationMenu:{ int windowW; diff --git a/Pontu/entryPoints/test.c b/Pontu/entryPoints/test.c index f431a15..6c7c5f4 100644 --- a/Pontu/entryPoints/test.c +++ b/Pontu/entryPoints/test.c @@ -10,11 +10,12 @@ #include "../test/testConnectionMenu.c"*/ //#include "../test/testDrawMainMenu.c //#include "../test/testSettingsView.c" +#include "../test/oldMain__ThisCanBeGameMain.c" /* This file is meant to be modified (used only to called other tests functions) */ -int main(int argc, char *argv[]) { +/*int main(int argc, char *argv[]) { //testTextureLoader(); //testAudioHandler(); //testFontLoader(); @@ -22,11 +23,11 @@ int main(int argc, char *argv[]) { //testTextInput(); //testButtonTextureLoader(); //testConnectionMenu(); - testMenuEndGame(); + //testMenuEndGame(); //testGameInterface(); //testConnectionMenu(); //testDrawMainMenu(); //testSettingsView(); return 0; -} +}*/ diff --git a/Pontu/include/engine/AudioHandler.h b/Pontu/include/engine/AudioHandler.h index 0163bab..01be82c 100644 --- a/Pontu/include/engine/AudioHandler.h +++ b/Pontu/include/engine/AudioHandler.h @@ -31,7 +31,8 @@ * \sa #MACRO_FOR_ALL_MUSICS(M) */ #define MACRO_FOR_ALL_SFX(M) \ - M(testClick) + M(testClick) \ + M(menu_sound_effect) /** * Macro used to generate the entries for the musics in #EnumAudios. diff --git a/Pontu/include/engine/Button.h b/Pontu/include/engine/Button.h index 1e7be9c..4fbf738 100644 --- a/Pontu/include/engine/Button.h +++ b/Pontu/include/engine/Button.h @@ -54,11 +54,18 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button); /** * \brief Test if a point is on a button * \param[in] button the button target + * \return true if the cursor is on the button + */ +bool isHover(P_Button* button);// dit si le bouton est survolé en donnant les coordonnées x,y + +/** + * \brief Test if the cursor has just entered the button. + * \param[in] button the button target * \param[in] x x of the point * \param[in] y y of the point - * \return SDL_TRUE if the point is on the button + * \return true if the cursor has juste entered the button. */ -bool isHover(P_Button* button, int x,int y);// dit si le bouton est survolé en donnant les coordonnées x,y +bool isButtonEntry(P_Button * button,const int x,const int y); /** * \brief Free the texture of a button. diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 3737dbb..0a88024 100644 --- a/Pontu/include/model/Game.h +++ b/Pontu/include/model/Game.h @@ -15,7 +15,7 @@ #include "model/Coord.h" #include "model/arrayCoord.h" #include -#include +#include /** * \enum Phase @@ -38,7 +38,7 @@ typedef struct { //TODO duree Phase phase; ///< The current state of the game Player arrPlayers[4]; ///< The array of all the players in this game - size_t nbPlayers; + size_t nbPlayers; Board board; ///< The board for this game } Game; @@ -47,19 +47,19 @@ typedef struct { * \param[in] nbPlayers The number of players for this game * \return A struct representing the game */ -Game newGame(const size_t nbPlayers, const char* pseudos[]); +Game newGame(const size_t nbPlayers, const Player player[]); /** * \brief (Should not be called outside Game.c) Used to change phase or player (or both) after an action - * + * * \param [in, out] game The game to mutate */ void changePhaseOrPlayerTurn(Game* game); /** * \brief Place a piece into the board - * + * * \param [in, out] p The piece to place * \param [in] island The island where the piece is placed * \param [in] b The board in which the piece is placed @@ -78,20 +78,20 @@ bool movePiece(Piece* p, const Island i, const Board* b); /** * \brief Test if a movement is possible for a piece to a destination island - * + * * \param p The piece to test - * \param i The destination island + * \param i The destination island * \param b The board - * \return true if the piece p can move to the island i + * \return true if the piece p can move to the island i */ bool pieceCanMoveTo(const Piece* p, const Island i, const Board* b); /** - * \brief Check if an island is empty - * + * \brief Check if an island is empty + * * \param [in] island The island to check * \param [in] arrPieces the array of piece from the board - * \param [in] nbPieces number of pieces + * \param [in] nbPieces number of pieces * \return true if none of the pieces is on th island, false otherwise */ bool isIslandEmpty(const Island island, const Piece arrPieces[], const size_t nbPieces); @@ -107,16 +107,16 @@ bool isPieceAdjacentToIsland(const Piece p, const Island i); /** * \brief test if a piece is isolated - * - * \param [in] piece The piece which is checked + * + * \param [in] piece The piece which is checked * \param [in] board The board were the piece is * \return true if no bridge go from piece's island to another island, false otherwise */ bool isPieceIsolated(const Piece* piece, const Board* board); /** - * \brief test if all pieces from a player are stucked - * + * \brief test if all pieces from a player are stucked + * * \param idJ Player id * \param arrPieces All pieces from board * \param nbPieces Number of pieces @@ -126,10 +126,10 @@ bool areAllPlayerPiecesStucked(const size_t idJ, const Piece arrPieces[], const /** * \brief Test if one piece of a player can move - * + * * \param playerID The player to check * \param board The board - * \return true if at least one of player's piece can move + * \return true if at least one of player's piece can move */ bool anyOfPlayersPiecesCanMove(const size_t playerID, const Board* board); @@ -154,7 +154,7 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is /** * \brief Handle global game action move - * + * * \param start Board coord were the move started * \param end Board coord were the move ended * \param game Game's state @@ -164,7 +164,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game); /** * \brief Handle global game action click - * + * * \param [in] coord Board coord were the click is * \param [in, out] game Game's state * \return true if an action was realised, false otherwise @@ -181,7 +181,7 @@ bool rmBridge(Bridge bridge, Board* board); /** * \brief List cases that can be interacted with for movement - * + * * \param[in] game The game * \param[in] selectedCase The selected case * \return struct array_Coord An array of coord /!\ Care to free this array with array_Coord_Free @@ -189,4 +189,3 @@ bool rmBridge(Bridge bridge, Board* board); struct array_Coord getInteractiveCases(const Game* const game, const Coord selectedCase); #endif //GAME_H - diff --git a/Pontu/include/view/MainMenu.h b/Pontu/include/view/MainMenu.h index d8aa932..af1e5e8 100644 --- a/Pontu/include/view/MainMenu.h +++ b/Pontu/include/view/MainMenu.h @@ -7,9 +7,10 @@ #include "engine/TextureLoader.h" #include "engine/FontLoader.h" #include "engine/GeneralState.h" +#include "engine/AudioHandler.h" P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize); -int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler); +int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler); #endif diff --git a/Pontu/rsrc/sfx/menu_sound_effect.wav b/Pontu/rsrc/sfx/menu_sound_effect.wav new file mode 100644 index 0000000..773aa5b Binary files /dev/null and b/Pontu/rsrc/sfx/menu_sound_effect.wav differ diff --git a/Pontu/src/engine/AudioHandler.c b/Pontu/src/engine/AudioHandler.c index a217a75..c468e41 100644 --- a/Pontu/src/engine/AudioHandler.c +++ b/Pontu/src/engine/AudioHandler.c @@ -2,7 +2,7 @@ // A channel represents the number of SFX we can play at the same time. // We normally should use only 1 channel, and we add one for safety. -#define NBCHANNELS 2 +#define NBCHANNELS 10 // Local functions @@ -19,7 +19,7 @@ int fadeOut(void* args) { // casting args to a pointer to Mix_Music Mix_Music* music = (Mix_Music*)args; int ret; - + if(Mix_FadeOutMusic(500) == 1) { // Starting the fadeout while (Mix_PlayingMusic()) { ; // Waiting until it's done @@ -60,7 +60,7 @@ AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) { audioHandler.masterVol = masterVol; fprintf(stderr,"Musics: %d\nSFX: %d\n",NB_MUSIC_DEFINED,nb_SFX); - + // Loading musics for (size_t i = 0; i < NB_MUSIC_DEFINED; i++) { audioHandler.musics[i] = Mix_LoadMUS(musicsPaths[i]); @@ -140,13 +140,13 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) { fprintf(stderr,"WARNING: tried to play an arbitrary value as a music\n"); return; } - + // Checking if audio has been opened. if (!(audioHandler.canPlayAudio)) { fprintf(stderr,"WARNING: tried to play a music with an unusable AudioHandler\n"); return; } - + // If another music is playing, fading the previous one out if (Mix_PlayingMusic()) { // Creating the thread, passing the music as parameter @@ -180,7 +180,7 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) { fprintf(stderr,"WARNING: tried to play an SFX with an unusable AudioHandler\n"); return; } - + // Getting actual chunk chunkSFX = audioHandler.sfx[sfx - NB_MUSIC_DEFINED - 1]; // Getting first available channel @@ -196,4 +196,3 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) { return; } } - diff --git a/Pontu/src/engine/Button.c b/Pontu/src/engine/Button.c index 5e3d0af..273c962 100644 --- a/Pontu/src/engine/Button.c +++ b/Pontu/src/engine/Button.c @@ -27,12 +27,8 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button) return true; } -bool isHover(P_Button* button,const int x,const int y) +bool isHover(P_Button* button) { - SDL_Point coord; - coord.x = x; - coord.y = y; - button->hover = SDL_PointInRect(&coord,&(button->rect)); return button->hover && button->drawn; } @@ -60,3 +56,15 @@ void freeButton(P_Button * button){ SDL_DestroyTexture(button->texture); SDL_DestroyTexture(button->hoverTexture); } + +bool isButtonEntry(P_Button * button,const int x,const int y){ + SDL_Point coord; + coord.x = x; + coord.y = y; + if(isHover(button)){ + button->hover = SDL_PointInRect(&coord,&(button->rect)); + return false; + } + button->hover = SDL_PointInRect(&coord,&(button->rect)); + return button->hover; +} diff --git a/Pontu/src/engine/GameInputProcessor.c b/Pontu/src/engine/GameInputProcessor.c index 64006c5..73af6e5 100644 --- a/Pontu/src/engine/GameInputProcessor.c +++ b/Pontu/src/engine/GameInputProcessor.c @@ -81,11 +81,10 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL { for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &gameInputProcessor->tabButton.elems[i]; - isHover(b, event.motion.x, event.motion.y); + isButtonEntry(b, event.motion.x, event.motion.y); } } } return createInputElementNone(); } - diff --git a/Pontu/src/engine/InputProcessor.c b/Pontu/src/engine/InputProcessor.c index 5b931b3..c970099 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -26,7 +26,7 @@ InputElement proccessInput(InputProcessor *inputProcessor) case SDL_MOUSEBUTTONUP: { const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; - + for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &inputProcessor->tabButton.elems[i]; if (SDL_PointInRect(&mousePoint, &b->rect)) { @@ -39,7 +39,7 @@ InputElement proccessInput(InputProcessor *inputProcessor) { for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &inputProcessor->tabButton.elems[i]; - isHover(b, event.motion.x, event.motion.y); + isButtonEntry(b, event.motion.x, event.motion.y); } break; } @@ -47,4 +47,3 @@ InputElement proccessInput(InputProcessor *inputProcessor) return createInputElementNone(); } - diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 7382f2e..e223827 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -30,7 +30,7 @@ void applySpecificRulesFor2PlayersGame(Game* g) } } -Game newGame(const size_t nbPlayers, const char* pseudos[]) +Game newGame(const size_t nbPlayers, const Player player[]) { Game g = { // In Placement phase, the last player initialized is the 1st to play .currentPlayerID = nbPlayers - 1, @@ -40,13 +40,9 @@ Game newGame(const size_t nbPlayers, const char* pseudos[]) .nbPlayers = nbPlayers }; - // red, green, blue, yellow - // TODO meilleures couleurs (?) - SDL_Color colors[4] = { { 255, 0, 0, 255 }, { 0, 255, 0, 255 }, { 0, 0, 255, 255 }, { 255, 255, 0, 255 } }; - for (size_t player_i = 0; player_i < nbPlayers; player_i++) { - g.arrPlayers[player_i] = newPlayer(pseudos[player_i], colors[player_i]); + g.arrPlayers[player_i] = player[player_i]; } if (nbPlayers == 2) @@ -72,8 +68,8 @@ void changePhaseOrPlayerTurn(Game* game) game->currentPlayerID--; } break; - case MOVE_PIECE: - game->phase = RM_BRIDGE; + case MOVE_PIECE: + game->phase = RM_BRIDGE; break; case RM_BRIDGE: { @@ -93,7 +89,7 @@ void changePhaseOrPlayerTurn(Game* game) } while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)); - + fprintf(stderr, "Player n°%ld turn\n", game->currentPlayerID); fflush(stderr); @@ -103,7 +99,7 @@ void changePhaseOrPlayerTurn(Game* game) } break; } - default: + default: break; } } @@ -243,7 +239,7 @@ bool anyOfPlayersPiecesCanMove(const size_t playerID, const Board* board) { size_t nbNeighbors; Island* neighbors = islandsAround(board->arrPieces[i].island, &nbNeighbors); for (size_t n = 0; n < nbNeighbors; ++n) - { + { if (board->arrPieces[i].idJ == playerID && pieceCanMoveTo(&board->arrPieces[i], neighbors[n], board)) { return true; } @@ -304,7 +300,7 @@ bool clickOnBoard(const Coord coord, Game* game) } } break; - default: + default: break; } @@ -348,7 +344,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game) } } break; - default: + default: break; } @@ -385,7 +381,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec case PLACEMENT: { struct array_Coord retVal = array_Coord_Create(); array_Coord_Reserve(&retVal, 25); - + for (int y = 0; y<5; y+=2) { for (int x = 0; x<5; x+=2) { array_Coord_AddElement(&retVal, newCoord(x,y)); @@ -398,7 +394,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec array_Coord_RemoveElement(&retVal, islandToCoord(&game->board.arrPieces[i].island), &coordEqual); } } - + array_Coord_FitToSize(&retVal); return retVal; @@ -412,7 +408,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec if (game->board.arrPieces[i].idJ == game->currentPlayerID && !game->board.arrPieces[i].stuck) { size_t nbIsland; Island* islands = islandsAround(game->board.arrPieces[i].island, &nbIsland); - + if (nbIsland != 0) { Coord pieceCoord = islandToCoord(&game->board.arrPieces[i].island); if (!coordValid(selectedCase)) { @@ -442,7 +438,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec case RM_BRIDGE: { struct array_Coord retVal = array_Coord_Create(); array_Coord_Reserve(&retVal, 40); - + for (size_t y = 0; y<5; ++y) { for (size_t x = 0; x<4; ++x) { if (game->board.hBridges[y][x]) { @@ -459,7 +455,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec } } } - + return retVal; } default: diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index 657afd5..bdcfbb3 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -56,7 +56,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend fprintf(stderr, "Move on board\n"); fprintf(stderr, "From (%d; %d)\n", inputElement.data.move.start.x, inputElement.data.move.start.y); fprintf(stderr, "To (%d; %d)\n", inputElement.data.move.end.x, inputElement.data.move.end.y); - + moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game); @@ -74,21 +74,21 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend fprintf(stderr, "\tselected case reset\n"); inputProcessor.selectedCase = newCoord(-1,-1); } - + break; case InputType_None: default: break; } - + array_Coord_Free(&interactiveCases); interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase); fprintf(stderr, "Interactive cases : {"); array_Coord_Foreach(&interactiveCases, *printCoord); fprintf(stderr, "}\n"); } - + fflush(stderr); // Drawing diff --git a/Pontu/src/view/MainMenu.c b/Pontu/src/view/MainMenu.c index 1bd2f85..0423fe1 100644 --- a/Pontu/src/view/MainMenu.c +++ b/Pontu/src/view/MainMenu.c @@ -63,7 +63,7 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns return buttons; } -int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler){ +int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler){ int statut = EXIT_FAILURE; char* path = "../rsrc/img/Lenna.png"; //Initialisation @@ -92,17 +92,20 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general *generalState = GS_Quit; break; case SDL_MOUSEBUTTONUP: - if(isHover(buttons,event.button.x,event.button.y)) + if(isHover(buttons)) printf("Nouvelle partie\n"); - if(isHover(&(buttons[2]),event.motion.x,event.motion.y)){ + if(isHover(&(buttons[2]))){ buttons[2].onClick(&(buttons[2])); break; } break; case SDL_MOUSEMOTION: - isHover(&(buttons[0]),event.motion.x,event.motion.y); - isHover(&(buttons[1]),event.motion.x,event.motion.y); - isHover(&(buttons[2]),event.motion.x,event.motion.y); + if(isButtonEntry(&(buttons[0]),event.motion.x,event.motion.y) || + isButtonEntry(&(buttons[1]),event.motion.x,event.motion.y) || + isButtonEntry(&(buttons[2]),event.motion.x,event.motion.y)){ + playSFX(SFX_menu_sound_effect, audioHandler); + printf("True\n"); + } break; default: break;