diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index 369db60..387bc13 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -23,7 +23,7 @@ int main(int argc, char *argv[]) { goto Quit; } - window = SDL_CreateWindow("Pontu",windowSize.x, windowSize.y, windowSize.w, windowSize.h, SDL_WINDOW_SHOWN); + window = SDL_CreateWindow("Pontu",windowSize.x, windowSize.y, windowSize.w, windowSize.h, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); if (!window) { fprintf(stderr, "Error : %s\n", SDL_GetError()); @@ -57,10 +57,10 @@ int main(int argc, char *argv[]) { SDL_GetWindowSize(window, &windowW, &windowH); size_t nbPlayers = 2; - SDL_Color color = {0,0,0,0}; Player* players = (Player*)malloc(sizeof(Player)*2); - players[0] = newPlayer("Bépo", color); - players[1] = newPlayer("Azeryty", color); + players[0] = newPlayer("Bépo", PlayerViolet); + players[1] = newPlayer("Azeryty", PlayerYellow); + //players[2] = newPlayer("Adcsg", PlayerRed); //bool crashed = gameCreationMenu(renderer, &generalState, &fontHandler, windowW, windowH, &players, &nbPlayers); diff --git a/Pontu/include/engine/ArrayUtils.h b/Pontu/include/engine/ArrayUtils.h index 9ab3b34..e1438f3 100644 --- a/Pontu/include/engine/ArrayUtils.h +++ b/Pontu/include/engine/ArrayUtils.h @@ -48,14 +48,18 @@ inline void array_##T##_Free(struct array_##T* array) { \ inline void array_##T##_Reserve(struct array_##T* array, const size_t space) { \ array->space = space; \ array->elems = realloc(array->elems, sizeof(T)*(array->space)); \ - if (array->elems == NULL) exit(errno); \ + if (array->elems == NULL) { \ + perror("Realloc arrayUtils"); exit(errno); \ + } \ } \ \ /*Fit space to size for an array*/\ inline void array_##T##_FitToSize(struct array_##T* array) { \ array->space = array->size; \ array->elems = realloc(array->elems, sizeof(T)*(array->space)); \ - if (array->elems == NULL) exit(errno); \ + if (array->elems == NULL) { \ + perror("Realloc arrayUtils"); exit(errno); \ + } \ } \ \ /*Add an element to an array*/\ @@ -64,7 +68,9 @@ inline void array_##T##_AddElement(struct array_##T* array, const T element) { \ if (array->size > array->space) { \ ++(array->space); \ array->elems = realloc(array->elems, sizeof(T)*(array->space)); \ - if (array->elems == NULL) exit(errno); \ + if (array->elems == NULL) { \ + perror("Realloc arrayUtils"); exit(errno); \ + } \ } \ \ array->elems[array->size - 1] = element; \ diff --git a/Pontu/include/engine/UIElementUtils.h b/Pontu/include/engine/UIElementUtils.h index e3ae962..f7968bc 100644 --- a/Pontu/include/engine/UIElementUtils.h +++ b/Pontu/include/engine/UIElementUtils.h @@ -18,7 +18,8 @@ typedef enum { typedef enum { ASPECT_KEEP_W, ASPECT_KEEP_H, - ASPECT_IGNORE + ASPECT_IGNORE, + ASPECT_KEEP_FIT } AspectRatioType; typedef struct { diff --git a/Pontu/include/model/Player.h b/Pontu/include/model/Player.h index edf197e..c818567 100644 --- a/Pontu/include/model/Player.h +++ b/Pontu/include/model/Player.h @@ -10,16 +10,17 @@ #define PSEUDO_LENMAX 50 -#include +#include "model/PlayersColors.h" #include + /** * \struct Player * \brief Player during a game */ typedef struct { char pseudo[PSEUDO_LENMAX]; //< The player's pseudo - SDL_Color color; //< The player's Piece' color + PlayersColors color; //< The player's Piece' color int rank; //< The player's rank (0 if the player isn't out yet) int eliminationTurn; //< When the player has been eliminated (0 if the player isn't out yet) } Player; @@ -30,7 +31,7 @@ typedef struct { * \param[in] color The color of the new Player's Piece * \return A struct representing the new Player */ -Player newPlayer(const char pseudo[PSEUDO_LENMAX], const SDL_Color color); +Player newPlayer(const char pseudo[PSEUDO_LENMAX], const PlayersColors color); #endif // JOUEUR_H diff --git a/Pontu/include/model/PlayersColors.h b/Pontu/include/model/PlayersColors.h new file mode 100644 index 0000000..50a3aa9 --- /dev/null +++ b/Pontu/include/model/PlayersColors.h @@ -0,0 +1,11 @@ +#ifndef PLAYERS_COLORS_INCLUDED +#define PLAYERS_COLORS_INCLUDED + +typedef enum { + PlayerRed, + PlayerViolet, + PlayerBlue, + PlayerYellow +} PlayersColors; + +#endif //PLAYERS_COLORS_INCLUDED diff --git a/Pontu/include/view/PiecesDrawer.h b/Pontu/include/view/PiecesDrawer.h index b0e0fae..c702dbf 100644 --- a/Pontu/include/view/PiecesDrawer.h +++ b/Pontu/include/view/PiecesDrawer.h @@ -5,13 +5,15 @@ #include #include "model/Piece.h" #include "model/Coord.h" +#include "model/PlayersColors.h" +#include "engine/TextureHandler.h" -void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece); +void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, const TextureHandler* textureHandler, const PlayersColors color); -void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture); +void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, const TextureHandler* textureHandler, const PlayersColors color); -void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* pieceTexture, const Coord* coordPlace); +void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const TextureHandler* textureHandler, const PlayersColors color, const Coord* coordPlace); #endif //PIECES_DRAWER_INCLUDED diff --git a/Pontu/src/engine/GameInputProcessor.c b/Pontu/src/engine/GameInputProcessor.c index 73af6e5..2d40f1d 100644 --- a/Pontu/src/engine/GameInputProcessor.c +++ b/Pontu/src/engine/GameInputProcessor.c @@ -83,7 +83,13 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL P_Button* b = &gameInputProcessor->tabButton.elems[i]; isButtonEntry(b, event.motion.x, event.motion.y); } + break; } + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + return createInputElementResizeWindow(event.window.data1, event.window.data2); + } + break; } return createInputElementNone(); diff --git a/Pontu/src/engine/InputProcessor.c b/Pontu/src/engine/InputProcessor.c index 9158360..8802fa2 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -43,8 +43,10 @@ InputElement proccessInput(InputProcessor *inputProcessor) } break; } - case SDL_WINDOWEVENT_SIZE_CHANGED: - return createInputElementResizeWindow(event.window.data1, event.window.data2); + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + return createInputElementResizeWindow(event.window.data1, event.window.data2); + } break; } diff --git a/Pontu/src/engine/TextureHandler.c b/Pontu/src/engine/TextureHandler.c index 84762c3..9869492 100644 --- a/Pontu/src/engine/TextureHandler.c +++ b/Pontu/src/engine/TextureHandler.c @@ -15,6 +15,9 @@ TextureHandler newTextureHandler(SDL_Renderer* renderer) { for (size_t i = 0; ih * positionSpecifier->base100.h/100.0; r.w = r.h * positionSpecifier->base100.w/positionSpecifier->base100.h; + break; + case ASPECT_KEEP_FIT: { + const int preferedW = globalRect->w * positionSpecifier->base100.w/100.0; + const int preferedH = globalRect->h * positionSpecifier->base100.h/100.0; + const int associatedH = preferedW * positionSpecifier->base100.h/positionSpecifier->base100.w; + const int associatedW = preferedH * positionSpecifier->base100.w/positionSpecifier->base100.h; + + if (associatedH > preferedH) { + r.h = preferedH; + r.w = associatedW; + } + else { + r.w = preferedW; + r.h = associatedH; + } + } + + break; default: break; @@ -57,10 +75,10 @@ SDL_Rect adaptPosToRect(const PositionSpecifier *const positionSpecifier, const case POSY_TOP: r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0; break; - case POSX_CENTER: - r.x = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h/2; + case POSY_CENTER: + r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h/2; break; - case POSX_RIGHT: + case POSY_BOTTOM: r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h; break; default: diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 03e30b6..e1168b4 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -43,7 +43,10 @@ Game newGame(const size_t nbPlayers, const Player player[]) for (size_t player_i = 0; player_i < nbPlayers; player_i++) { - g.arrPlayers[player_i] = player[player_i]; + g.arrPlayers[player_i].color = player[player_i].color; + g.arrPlayers[player_i].eliminationTurn = player[player_i].eliminationTurn; + g.arrPlayers[player_i].rank = player[player_i].rank; + strcpy(g.arrPlayers[player_i].pseudo, player[player_i].pseudo); } if (nbPlayers == 2) diff --git a/Pontu/src/model/Player.c b/Pontu/src/model/Player.c index 2d9d346..c1055e3 100644 --- a/Pontu/src/model/Player.c +++ b/Pontu/src/model/Player.c @@ -1,6 +1,6 @@ #include "model/Player.h" -Player newPlayer(const char pseudo[PSEUDO_LENMAX], const SDL_Color color) { +Player newPlayer(const char pseudo[PSEUDO_LENMAX], const PlayersColors color) { Player player; strcpy(player.pseudo, pseudo); player.color = color; diff --git a/Pontu/src/view/GameDrawer.c b/Pontu/src/view/GameDrawer.c index c7c97db..f14b094 100644 --- a/Pontu/src/view/GameDrawer.c +++ b/Pontu/src/view/GameDrawer.c @@ -19,8 +19,8 @@ bool drawGame(SDL_Renderer* renderer, const SDL_Rect* windowSize, const SDL_Rect //drawBoard(renderer, boardRect, &(game->board), textureHandler->textures[TEXTURE_Island], textureHandler->textures[TEXTURE_Bridge], textureHandler->textures[TEXTURE_Water]); - drawPiecesPlayer(renderer, boardRect, game->board.arrPieces, game->board.nbPieces, 0, textureHandler->textures[TEXTURE_PieceRed]); + /*drawPiecesPlayer(renderer, boardRect, game->board.arrPieces, game->board.nbPieces, 0, textureHandler->textures[TEXTURE_PieceRed]); drawPiecesPlayer(renderer, boardRect, game->board.arrPieces, game->board.nbPieces, 1, textureHandler->textures[TEXTURE_PieceViolet]); - +*/ return true; } diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index f3ffddb..c1f20b3 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -10,12 +10,24 @@ #include "view/BoardDrawer.h" #include "view/GameDrawer.h" +#include "engine/UIElementUtils.h" -SDL_Rect boardRectFromWindowSize(int windowW, int windowH) { - SDL_Rect boardRect = {.x=windowW/10.0, .y=windowH/10, .w=windowW*8.0/10.0, .h=windowH*8.0/10.0}; +PositionSpecifier boardRectPositionSpecifier() { + SDL_Rect b100 = { + .x= 50, + .y= 50, + .w= 80, + .h= 80, + }; + return newPositionSpecifier(&b100, POSX_CENTER, POSY_CENTER, ASPECT_KEEP_FIT); +} - return boardRect; +void redrawGameBoard(SDL_Renderer* renderer, const Player players[], const size_t nbPlayers, const TextureHandler* textureHandler, const SDL_Rect* boardRect, const Board* board) { + drawFullBoard(renderer, boardRect, board, textureHandler->textures[TEXTURE_Island], textureHandler->textures[TEXTURE_Bridge], textureHandler->textures[TEXTURE_Water]); + for (size_t iPlayer=0; iPlayerarrPieces, board->nbPieces, iPlayer, textureHandler, players[iPlayer].color); + } } void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, Player players[], size_t nbPlayers) @@ -29,18 +41,15 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend Game game = newGame(nbPlayers, players); TextureHandler textureHandler = newTextureHandler(renderer); - int windowW; - int windowH; + + SDL_Rect windowRect = {0,0,0,0}; + SDL_GetWindowSize(window, &windowRect.w, &windowRect.h); + PositionSpecifier boardRPositionSpecifier = boardRectPositionSpecifier(); - SDL_GetWindowSize(window, &windowW, &windowH); - SDL_Rect boardRect = boardRectFromWindowSize(windowW, windowH); + SDL_Rect boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect); //Draw - drawFullBoard(renderer, &boardRect, &game.board, textureHandler.textures[TEXTURE_Island], textureHandler.textures[TEXTURE_Bridge], textureHandler.textures[TEXTURE_Water]); - for (int iPlayer=0; iPlayer0) ? game.currentPlayerID-1 : game.nbPlayers-1].color, &inputElement.data.coord); SDL_RenderPresent(renderer); break; case GameAction_RemoveBridge: @@ -108,6 +117,14 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend break; } + case InputType_Window_Resize: { + windowRect.w = inputElement.data.windowSize.w; + windowRect.h = inputElement.data.windowSize.h; + boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect); + + redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board); + SDL_RenderPresent(renderer); + } case InputType_None: default: break; diff --git a/Pontu/src/view/PiecesDrawer.c b/Pontu/src/view/PiecesDrawer.c index f51f170..73b2cb1 100644 --- a/Pontu/src/view/PiecesDrawer.c +++ b/Pontu/src/view/PiecesDrawer.c @@ -1,29 +1,33 @@ #include "view/PiecesDrawer.h" #include "view/ToRect.h" -void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece) { +void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, const TextureHandler* textureHandler, const PlayersColors color) { + SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color]; for (size_t i = 0; i < nbPieces; ++i) { if (arrPieces[i].idJ == numPlayer) { Coord c = islandToCoord(&arrPieces[i].island); const SDL_Rect rDest = coordToRect(boardRect, &c); - SDL_RenderCopy(renderer, piece, NULL, &rDest); + SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); } } } -void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture) { +void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, const TextureHandler* textureHandler, const PlayersColors color) { + SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color]; SDL_Rect rDest = coordToRect(boardRect, startMove); - SDL_RenderCopy(renderer, islandTexture, NULL, &rDest); + SDL_RenderCopy(renderer, textureHandler->textures[TEXTURE_Island], NULL, &rDest); rDest = coordToRect(boardRect, endMove); SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); } -void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* pieceTexture, const Coord* coordPlace) { +void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const TextureHandler* textureHandler, const PlayersColors color, const Coord* coordPlace) { + SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color]; + SDL_Rect rDest = coordToRect(boardRect, coordPlace); SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); }