diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index aba456f..2b46c07 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -1,5 +1,6 @@ #include #include +#include #include "engine/GeneralState.h" #include "view/MainMenu.h" #include "view/MenuEndGame.h" @@ -8,9 +9,9 @@ #include "engine/FontLoader.h" #include "model/Player.h" -int main(int argc, char const *argv[]) { +int main(int argc, char *argv[]) { GeneralState generalState; - + SDL_Window* window = NULL; SDL_Rect windowSize = {10, 10, 900, 900}; SDL_Renderer* renderer = NULL; @@ -22,7 +23,7 @@ int main(int argc, char const *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()); @@ -43,7 +44,7 @@ int main(int argc, char const *argv[]) { FontHandler fontHandler = loadFonts(); AudioHandler audioHandler = newAudioHandler(128, 128, 128); - generalState = GS_MainMenu; + generalState = GS_GameCreationMenu; while(generalState != GS_Quit){ switch (generalState) { case GS_MainMenu: @@ -56,17 +57,20 @@ int main(int argc, char const *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); + Player players[] = { + newPlayer("Bépo", PlayerViolet), + newPlayer("Azeryty", PlayerYellow), + //newPlayer("Adcsg", PlayerRed) + }; + //players[2] = ; //bool crashed = gameCreationMenu(renderer, &generalState, &fontHandler, windowW, windowH, &players, &nbPlayers); - /* if (crashed) { + /*if (crashed) { fprintf(stderr,"sorry"); exit(-1); }*/ + generalState = GS_Game; gameView(&generalState, window, renderer, players, nbPlayers); diff --git a/Pontu/entryPoints/test.c b/Pontu/entryPoints/test.c index d9a26d9..4b2780a 100644 --- a/Pontu/entryPoints/test.c +++ b/Pontu/entryPoints/test.c @@ -2,11 +2,12 @@ // #include "../test/testFontLoader.c" // #include "../test/testAudioHandler.c" // #include "../test/testGenerateurTexture.c" -//#include "../test/testButton.c" -//#include "../test/testTextInput.c" +//#include "../test/testGameInterface.c" //#include "../test/testConnectionMenu.c" -//#include "../test/testMenuEndGame.c" -#include "../test/testGameInterface.c" +//#include "../test/testDrawMainMenu.c +//#include "../test/testSettingsView.c" +#include "../test/testCreationMenu.c" +//#include "../test/testGameInterface.c" //#include "../test/testConnectionMenu.c" //#include "../test/testDrawMainMenu.c" //#include "../test/testSettingsView.c" @@ -25,9 +26,10 @@ int main(int argc, char *argv[]) { //testConnectionMenu(); //testMenuEndGame(); //testButton(); - testGameInterface(); + //testGameInterface(); //testConnectionMenu(); //testDrawMainMenu(); + testCreationMenu(); //testSettingsView(); return 0; 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/Colors.h b/Pontu/include/engine/Colors.h index d15dd7a..e9b6df3 100644 --- a/Pontu/include/engine/Colors.h +++ b/Pontu/include/engine/Colors.h @@ -7,5 +7,7 @@ extern const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND; extern const SDL_Color COLOR_GENERIC_BUTTON_BORDER; extern const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER; +extern const SDL_Color PLAYER_SDL_COLORS[]; + #endif diff --git a/Pontu/include/engine/InputElement.h b/Pontu/include/engine/InputElement.h index 18ed0c5..792d667 100644 --- a/Pontu/include/engine/InputElement.h +++ b/Pontu/include/engine/InputElement.h @@ -16,7 +16,7 @@ * \enum InputType * \brief Different types for input */ -typedef enum {InputType_None, InputType_ClickGame, InputType_MoveGame, InputType_ActivateUI} InputType; +typedef enum {InputType_None, InputType_ClickGame, InputType_MoveGame, InputType_ActivateUI, InputType_Window_Resize} InputType; /** * \enum UIAction @@ -43,6 +43,10 @@ typedef struct { } move; ///< Pair of coordinates for move on the board UIAction uiAction; ///< L'action + struct windowSize { + int w; + int h; + } windowSize; ///< La nouvelle taille de l'ecran } data; ///< Informations about the input InputType type; ///< Type of input @@ -75,4 +79,11 @@ InputElement createInputElementClickBoard(const Coord newCoord); */ InputElement createInputElementMoveBoard(const Coord start, const Coord end); +/** + * @brief Create a Input Element Resize Window + * + * @return InputElement InputType_Window_Resize + */ +InputElement createInputElementResizeWindow(); + #endif // INPUT_ELEMENT_INCLUDED diff --git a/Pontu/include/engine/TextLabel.h b/Pontu/include/engine/TextLabel.h index 3cfab8f..a1ec026 100644 --- a/Pontu/include/engine/TextLabel.h +++ b/Pontu/include/engine/TextLabel.h @@ -4,18 +4,7 @@ #include #include #include "engine/FontLoader.h" - -typedef enum { - POSX_LEFT, - POSX_CENTER, - POSX_RIGHT -} POSITIONX_TYPE; - -typedef enum { - POSY_TOP, - POSY_CENTER, - POSY_BOTTOM -} POSITIONY_TYPE; +#include "engine/UIElementUtils.h" typedef struct { @@ -25,7 +14,8 @@ typedef struct SDL_Texture* texture; }TextLabel; -TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float factorSize,const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer, const POSITIONX_TYPE posXType, const POSITIONY_TYPE posYType); +TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float factorSize,const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer, const PositionX_Type posXType, const PositionY_Type posYType); +TextLabel createUnsizedTextLabel(const char text[], const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer); void freeTextLabel(TextLabel* label); void drawTextLabel(SDL_Renderer* renderer, TextLabel* label); diff --git a/Pontu/include/engine/UIElementUtils.h b/Pontu/include/engine/UIElementUtils.h new file mode 100644 index 0000000..f7968bc --- /dev/null +++ b/Pontu/include/engine/UIElementUtils.h @@ -0,0 +1,36 @@ +#ifndef UI_ELEMENT_UTILS_INCLUDED +#define UI_ELEMENT_UTILS_INCLUDED + +#include + +typedef enum { + POSX_LEFT, + POSX_CENTER, + POSX_RIGHT +} PositionX_Type; + +typedef enum { + POSY_TOP, + POSY_CENTER, + POSY_BOTTOM +} PositionY_Type; + +typedef enum { + ASPECT_KEEP_W, + ASPECT_KEEP_H, + ASPECT_IGNORE, + ASPECT_KEEP_FIT +} AspectRatioType; + +typedef struct { + SDL_Rect base100; + PositionX_Type tpX; + PositionY_Type tpY; + AspectRatioType aspectType; +} PositionSpecifier; + +PositionSpecifier newPositionSpecifier(const SDL_Rect* const base100, const PositionX_Type tpX, const PositionY_Type tpY, const AspectRatioType aspectType); + +SDL_Rect adaptPosToRect(const PositionSpecifier *const positionSpecifier, const SDL_Rect *const globalRect); + +#endif //UI_ELEMENT_UTILS_INCLUDED diff --git a/Pontu/include/engine/arrayPositionSpecifier.h b/Pontu/include/engine/arrayPositionSpecifier.h new file mode 100644 index 0000000..c41356e --- /dev/null +++ b/Pontu/include/engine/arrayPositionSpecifier.h @@ -0,0 +1,9 @@ +#ifndef ARRAY_POSITION_SPECIFIER_INCLUDED +#define ARRAY_POSITION_SPECIFIER_INCLUDED + +#include "engine/UIElementUtils.h" +#include "engine/ArrayUtils.h" + +GENERATE_DYNAMIC_ARRAY(PositionSpecifier) + +#endif //ARRAY_POSITION_SPECIFIER_INCLUDED diff --git a/Pontu/include/engine/arraySDL_Rect.h b/Pontu/include/engine/arraySDL_Rect.h new file mode 100644 index 0000000..e11d783 --- /dev/null +++ b/Pontu/include/engine/arraySDL_Rect.h @@ -0,0 +1,9 @@ +#ifndef ARRAY_SDL_RECT_INCLUDED +#define ARRAY_SDL_RECT_INCLUDED + +#include +#include "engine/ArrayUtils.h" + +GENERATE_DYNAMIC_ARRAY(SDL_Rect) + +#endif //ARRAY_SDL_RECT_INCLUDED diff --git a/Pontu/include/engine/arrayTextLabel.h b/Pontu/include/engine/arrayTextLabel.h new file mode 100644 index 0000000..9815e57 --- /dev/null +++ b/Pontu/include/engine/arrayTextLabel.h @@ -0,0 +1,9 @@ +#ifndef ARRAY_TEXT_LABEL_INCLUDED +#define ARRAY_TEXT_LABEL_INCLUDED + +#include "engine/TextLabel.h" +#include "engine/ArrayUtils.h" + +GENERATE_DYNAMIC_ARRAY(TextLabel) + +#endif //ARRAY_TEXT_LABEL_INCLUDED diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 0a88024..fb11a41 100644 --- a/Pontu/include/model/Game.h +++ b/Pontu/include/model/Game.h @@ -28,6 +28,13 @@ typedef enum { GAME_ENDED } Phase; +typedef enum { + GameAction_None, + GameAction_PlacePiece, + GameAction_MovePiece, + GameAction_RemoveBridge, +} GameAction; + /** * \struct Game * \brief Represents a game @@ -40,6 +47,7 @@ typedef struct { Player arrPlayers[4]; ///< The array of all the players in this game size_t nbPlayers; Board board; ///< The board for this game + int lastRank; } Game; /** @@ -158,18 +166,18 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is * \param start Board coord were the move started * \param end Board coord were the move ended * \param game Game's state - * \return true if an action was realised, false otherwise + * \return The action realised, GameAction_None if no action was realized */ -bool moveOnBoard(const Coord start, const Coord end, Game* game); +GameAction 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 + * \return The action realised, GameAction_None if no action was realized */ -bool clickOnBoard(const Coord coord, Game* game); +GameAction clickOnBoard(const Coord coord, Game* game); /** * \brief Remove bridge from board at (coord->x; coord->y) diff --git a/Pontu/include/model/Player.h b/Pontu/include/model/Player.h index edf197e..2dc3b4f 100644 --- a/Pontu/include/model/Player.h +++ b/Pontu/include/model/Player.h @@ -10,17 +10,18 @@ #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 - int rank; //< The player's rank (0 if the player isn't out yet) + PlayersColors color; //< The player's Piece' color + int rank; //< The player's rank (1 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..a824209 --- /dev/null +++ b/Pontu/include/model/PlayersColors.h @@ -0,0 +1,20 @@ +#ifndef PLAYERS_COLORS_INCLUDED +#define PLAYERS_COLORS_INCLUDED + + +#define MACRO_FOR_EACH_PLAYER_COLOR(M) \ + M(Red) \ + M(Violet) \ + M(Blue) \ + M(Yellow) + +#define MACRO_GEN_ENUM_PLAYER_COLOR(Elem) Player##Elem, + +typedef enum { + MACRO_FOR_EACH_PLAYER_COLOR(MACRO_GEN_ENUM_PLAYER_COLOR) + NB_PLAYER_COLORS +} PlayersColors; + +extern const PlayersColors playersColors[]; + +#endif //PLAYERS_COLORS_INCLUDED diff --git a/Pontu/include/view/GameCreationMenu.h b/Pontu/include/view/GameCreationMenu.h index 6e5300a..eb7de43 100644 --- a/Pontu/include/view/GameCreationMenu.h +++ b/Pontu/include/view/GameCreationMenu.h @@ -5,5 +5,50 @@ #include "engine/TextureLoader.h" #include "engine/Colors.h" #include "engine/FontUtils.h" +#include "engine/GeneralState.h" +#include "engine/InputProcessor.h" +#include "engine/Colors.h" +#include "model/Player.h" +#include "engine/TextInput.h" + +#define NB_COLORS 4 +#define NB_PLAYER_MAX 4 + + +/* +bool drawGameCreationMenu(SDL_Renderer* renderer, P_Button* incrementBtn, P_Button* decrementBtn, TextLabel* nbJoueurTxtLabel, TextLabel* nbPlayerLabel); +*/ + +typedef struct +{ + P_Button aiButton; + P_Button* colorButtons; + TextInput pseudoInput; + Player* player; +}CreateMenuLine; + +typedef struct +{ + Player* p; + PlayersColors color; +} ChangeColorParams; + +typedef struct +{ + int* nbPlayers; + TTF_Font* font; + int minx; + int maxx; + int miny; + CreateMenuLine* lines; + SDL_Renderer* renderer; +}IncrementParams; + +typedef struct +{ + int* nbPlayers; + CreateMenuLine* lines; + SDL_Renderer* renderer; +}DecrementParams; -bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height); +bool gameCreationMenu(SDL_Renderer* renderer, GeneralState* generalState, TTF_Font* font, int width, int height); diff --git a/Pontu/include/view/MainMenu.h b/Pontu/include/view/MainMenu.h index cfd9e84..9966d98 100644 --- a/Pontu/include/view/MainMenu.h +++ b/Pontu/include/view/MainMenu.h @@ -13,4 +13,4 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler); -#endif +#endif //MAIN_MENU_INCLUDED diff --git a/Pontu/include/view/MenuEndGame.h b/Pontu/include/view/MenuEndGame.h index b17ec2f..1da3336 100644 --- a/Pontu/include/view/MenuEndGame.h +++ b/Pontu/include/view/MenuEndGame.h @@ -9,6 +9,7 @@ #include "engine/TextureLoader.h" #include "engine/GeneralState.h" + /** * @brief Handle end game menu * diff --git a/Pontu/include/view/PiecesDrawer.h b/Pontu/include/view/PiecesDrawer.h index 92b4c1f..c702dbf 100644 --- a/Pontu/include/view/PiecesDrawer.h +++ b/Pontu/include/view/PiecesDrawer.h @@ -4,10 +4,16 @@ #include #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 Island* startMove, const Island* 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, const TextureHandler* textureHandler, const PlayersColors color, const Coord* coordPlace); #endif //PIECES_DRAWER_INCLUDED diff --git a/Pontu/include/view/ToRect.h b/Pontu/include/view/ToRect.h new file mode 100644 index 0000000..066be5d --- /dev/null +++ b/Pontu/include/view/ToRect.h @@ -0,0 +1,10 @@ +#ifndef TO_RECT_INCLUDED +#define TO_RECT_INCLUDED + +#include +#include "model/Coord.h" + +SDL_Rect coordToRect(const SDL_Rect* boardRect, const Coord* coord); + + +#endif //TO_RECT_INCLUDED diff --git a/Pontu/src/engine/Colors.c b/Pontu/src/engine/Colors.c index 1375ae6..de1dfc5 100644 --- a/Pontu/src/engine/Colors.c +++ b/Pontu/src/engine/Colors.c @@ -3,3 +3,11 @@ const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND = {225, 225, 225, 255}; const SDL_Color COLOR_GENERIC_BUTTON_BORDER = {10, 10, 10, 255}; const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER = {250, 250, 250, 255}; + +const SDL_Color PLAYER_SDL_COLORS[] = {//Order mater (need to be the same as in model/PlayersColors.h) + {241, 0, 4 , 255}, // Red + {175, 0, 202, 255}, // Violet + {6 , 0, 202, 255}, // Blue + {202, 152, 0, 255}, // Yellow +}; + 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/InputElement.c b/Pontu/src/engine/InputElement.c index 5076204..5a609c7 100644 --- a/Pontu/src/engine/InputElement.c +++ b/Pontu/src/engine/InputElement.c @@ -22,3 +22,11 @@ InputElement createInputElementMoveBoard(const Coord start, const Coord end) { InputElement i = {.type=InputType_MoveGame, .data.move={.start=start, .end=end}}; return i; } + +InputElement createInputElementResizeWindow(const int w, const int h) { + InputElement i = { + .type = InputType_Window_Resize, + .data.windowSize={.w = w, .h = h} + }; + return i; +} diff --git a/Pontu/src/engine/InputProcessor.c b/Pontu/src/engine/InputProcessor.c index c970099..8802fa2 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -43,6 +43,12 @@ InputElement proccessInput(InputProcessor *inputProcessor) } 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/TextLabel.c b/Pontu/src/engine/TextLabel.c index ed0a123..0cf5c2e 100644 --- a/Pontu/src/engine/TextLabel.c +++ b/Pontu/src/engine/TextLabel.c @@ -3,39 +3,62 @@ #include "engine/TextureLoader.h" #include -TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float factorSize, const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer, const POSITIONX_TYPE posXType, const POSITIONY_TYPE posYType) { - TextLabel label = { - .color = *color, - .texture = NULL - }; +void attachTextureToTextLabel(SDL_Renderer* renderer, TextLabel* label, TTF_Font* font) { + SDL_Surface* surface = TTF_RenderText_Solid(font, label->text, label->color); + if(surface == NULL) + { + fprintf(stderr, "WARNING: Can't write on TextLabel\n"); + fflush(stderr); + } + else { + label->texture = SDL_CreateTextureFromSurface(renderer, surface); + if(label->texture == NULL) + { + fprintf(stderr, "WARNING: Can't create texture from surface: %s\n", SDL_GetError()); + fflush(stderr); + } + SDL_FreeSurface(surface); + } +} - label.text = (char*) malloc(sizeof(char)*(strlen(text)+1)); - if (label.text == NULL) { +void copyTextIntoTextLabel(TextLabel* label, const char text[]) { + label->text = (char*) malloc(sizeof(char)*(strlen(text)+1)); + if (label->text == NULL) { fprintf(stderr, "ERROR: allocation error (createTextLabel)\n"); fflush(stderr); exit(errno); } - strcpy(label.text, text); + strcpy(label->text, text); +} - { - SDL_Surface* surface = TTF_RenderText_Solid(font, label.text, label.color); +TextLabel createUnsizedTextLabel(const char text[], const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer) { + TextLabel label = { + .color = color, + .texture = NULL + }; - if(surface == NULL) - { - fprintf(stderr, "WARNING: Can't write on TextLabel\n"); - fflush(stderr); - } - else { - label.texture = SDL_CreateTextureFromSurface(renderer, surface); - if(label.texture == NULL) - { - fprintf(stderr, "WARNING: Can't create texture from surface: %s\n", SDL_GetError()); - fflush(stderr); - } - SDL_FreeSurface(surface); - } - } + copyTextIntoTextLabel(&label, text); + + attachTextureToTextLabel(renderer, &label, font); + + label.textZone.x = 0; + label.textZone.y = 0; + label.textZone.w = calculateStringPixelLenght(font, label.text); + label.textZone.h = TTF_FontHeight(font); + + return label; +} + +TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float factorSize, const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer, const PositionX_Type posXType, const PositionY_Type posYType) { + TextLabel label = { + .color = *color, + .texture = NULL + }; + + copyTextIntoTextLabel(&label, text); + + attachTextureToTextLabel(renderer, &label, font); label.textZone.w = calculateStringPixelLenght(font, label.text)*factorSize; label.textZone.h = TTF_FontHeight(font)*factorSize; 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; iaspectType) + { + case ASPECT_IGNORE: + r.w = globalRect->w * positionSpecifier->base100.w/100.0; + r.h = globalRect->h * positionSpecifier->base100.h/100.0; + break; + case ASPECT_KEEP_W: + r.w = globalRect->w * positionSpecifier->base100.w/100.0; + r.h = r.w * positionSpecifier->base100.h/positionSpecifier->base100.w; + break; + case ASPECT_KEEP_H: + r.h = globalRect->h * 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; + } + + switch (positionSpecifier->tpX) + { + case POSX_LEFT: + r.x = globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0; + break; + case POSX_CENTER: + r.x = (globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0) - r.w/2; + break; + case POSX_RIGHT: + r.x = (globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0) - r.w; + break; + default: + break; + } + + switch (positionSpecifier->tpY) + { + case POSY_TOP: + r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0; + break; + case POSY_CENTER: + r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h/2; + break; + case POSY_BOTTOM: + r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h; + break; + default: + break; + } + + return r; +} + diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index e223827..396d5d3 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -37,12 +37,16 @@ Game newGame(const size_t nbPlayers, const Player player[]) .nb_rounds = 0, .phase = PLACEMENT, .board = newBoard(nbPlayers), - .nbPlayers = nbPlayers + .nbPlayers = nbPlayers, + .lastRank = nbPlayers+1 }; 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) @@ -53,6 +57,15 @@ Game newGame(const size_t nbPlayers, const Player player[]) return g; } +void eliminatePlayer(Game* game, const size_t playerId) { + game->arrPlayers[playerId].eliminationTurn = game->nb_rounds; + game->arrPlayers[playerId].rank = --game->lastRank; + fprintf(stderr, "Rank : %d, %d\n", game->lastRank, game->arrPlayers[playerId].rank); +} + +void endGame(Game* game) { + game->phase = GAME_ENDED; +} void changePhaseOrPlayerTurn(Game* game) { @@ -74,6 +87,14 @@ void changePhaseOrPlayerTurn(Game* game) case RM_BRIDGE: { const size_t lastPlayerId = game->currentPlayerID; + if (areAllPlayerPiecesStucked(lastPlayerId, game->board.arrPieces, game->board.nbPieces)) { + eliminatePlayer(game, lastPlayerId); + if (game->lastRank == 2) { + endGame(game); + return; + } + } + do { game->currentPlayerID++; @@ -81,21 +102,28 @@ void changePhaseOrPlayerTurn(Game* game) { game->currentPlayerID = 0; } - if (lastPlayerId == game->currentPlayerID) { + /*if (lastPlayerId == game->currentPlayerID) { game->phase = GAME_ENDED; return; + }*/ + + if (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) { + eliminatePlayer(game, game->currentPlayerID); + if (game->lastRank == 2) { + endGame(game); + return; + } } - } while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, - game->board.nbPieces)); + } while (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0); - fprintf(stderr, "Player n°%ld turn\n", game->currentPlayerID); fflush(stderr); if (anyOfPlayersPiecesCanMove(game->currentPlayerID, &game->board)) { game->phase = MOVE_PIECE; + game->nb_rounds++; } break; } @@ -258,17 +286,12 @@ void updatePieceIsolated(Game* game, const Island* island) { Piece* piecePotentialyIsolated = getPieceFromIsland(game->board.arrPieces, game->board.nbPieces, *island); if (piecePotentialyIsolated != NULL && isPieceIsolated(piecePotentialyIsolated, &game->board)) - { // Check is a piece is isolated and then if the player is eliminated + { // Check is a piece is isolated //and then if the player is eliminated piecePotentialyIsolated->stuck = true; - if (areAllPlayerPiecesStucked(piecePotentialyIsolated->idJ, game->board.arrPieces, - game->board.nbPieces)) - { - game->arrPlayers[piecePotentialyIsolated->idJ].rank = game->nb_rounds; // TODO : See what we put in rank - } } } -bool clickOnBoard(const Coord coord, Game* game) +GameAction clickOnBoard(const Coord coord, Game* game) { const IslandOrBridge islandOrBridge = coordToEntity(coord); @@ -281,7 +304,7 @@ bool clickOnBoard(const Coord coord, Game* game) if (piece != NULL) { if (placePiece(piece, islandOrBridge.data.island, &game->board)) { changePhaseOrPlayerTurn(game); - return true; + return GameAction_PlacePiece; } } } @@ -296,7 +319,7 @@ bool clickOnBoard(const Coord coord, Game* game) changePhaseOrPlayerTurn(game); - return true; + return GameAction_RemoveBridge; } } break; @@ -304,7 +327,7 @@ bool clickOnBoard(const Coord coord, Game* game) break; } - return false; + return GameAction_None; } Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Island island) @@ -319,7 +342,7 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is return NULL; } -bool moveOnBoard(const Coord start, const Coord end, Game* game) +GameAction moveOnBoard(const Coord start, const Coord end, Game* game) { const IslandOrBridge islandOrBridgeStart = coordToEntity(start); const IslandOrBridge islandOrBridgeEnd = coordToEntity(end); @@ -340,7 +363,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game) { changePhaseOrPlayerTurn(game); } - return pieceMoved; + return pieceMoved ? GameAction_MovePiece : GameAction_None; } } break; @@ -348,7 +371,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game) break; } - return false; + return GameAction_None; } bool rmBridge(Bridge bridge, Board* board) diff --git a/Pontu/src/model/Player.c b/Pontu/src/model/Player.c index 2d9d346..33b62ff 100644 --- a/Pontu/src/model/Player.c +++ b/Pontu/src/model/Player.c @@ -1,10 +1,10 @@ #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; - player.rank = 0; + player.rank = 1; player.eliminationTurn = 0; return player; diff --git a/Pontu/src/model/PlayersColors.c b/Pontu/src/model/PlayersColors.c new file mode 100644 index 0000000..67486b7 --- /dev/null +++ b/Pontu/src/model/PlayersColors.c @@ -0,0 +1,6 @@ +#include "model/PlayersColors.h" + + +const PlayersColors playersColors[] = { + MACRO_FOR_EACH_PLAYER_COLOR(MACRO_GEN_ENUM_PLAYER_COLOR) +}; diff --git a/Pontu/src/view/BoardDrawer.c b/Pontu/src/view/BoardDrawer.c index 2b2dac0..1e7d0a2 100644 --- a/Pontu/src/view/BoardDrawer.c +++ b/Pontu/src/view/BoardDrawer.c @@ -1,22 +1,9 @@ #include "view/BoardDrawer.h" - - -SDL_Rect coordToRect(const SDL_Rect* boardRect, const Coord* coord) { - const int w = boardRect->w/9; - const int h = boardRect->h/9; - SDL_Rect r = { - .x = boardRect->x + w*coord->x, - .y = boardRect->y + h*coord->y, - .w = w, - .h = h - }; - - return r; -} +#include "view/ToRect.h" void drawRemoveBridge(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* water, const Coord* coordBridge) { const SDL_Rect destRect = coordToRect(boardRect, coordBridge); - SDL_RenderCopy(renderer, water, &destRect, boardRect); + SDL_RenderCopy(renderer, water, NULL, &destRect); } bool drawFullBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water) diff --git a/Pontu/src/view/GameCreationMenu.c b/Pontu/src/view/GameCreationMenu.c index 4cf9b85..327b75f 100644 --- a/Pontu/src/view/GameCreationMenu.c +++ b/Pontu/src/view/GameCreationMenu.c @@ -1,14 +1,177 @@ #include "view/GameCreationMenu.h" -bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height) +#include + +bool gameCreationMenu(SDL_Renderer* renderer, GeneralState* generalState,TTF_Font* font, int width, int height); +void freeCreateMenuLine(CreateMenuLine* line); +CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int xmax, TTF_Font* font, Player* player); +void createPlayersLines(SDL_Renderer* renderer, TTF_Font* font, int minx, int maxx, int miny,int nbPlayer, CreateMenuLine* lines); +bool drawGameCreationMenu(SDL_Renderer* renderer, TextLabel** labels, int nbLabels, P_Button* buttons, int nbButtons, CreateMenuLine* lines, int nbPlayer); +bool drawCreateMenuLine(SDL_Renderer* renderer, CreateMenuLine* line); +void changePlayerColor(P_Button* caller); +void decrementNbPlayer(P_Button* caller); +void incrementNbPlayer(P_Button* caller); + + + + +void incrementNbPlayer(P_Button* caller) +{ + IncrementParams* params = (IncrementParams*) caller->arg; + int* nbPlayers = params->nbPlayers; + if(*nbPlayers == 4) + { + fprintf(stderr, "WARNING: Can't increment up to 5 and more\n"); + return; + } + ++(*nbPlayers); + createPlayersLines(params->renderer, params->font, params->minx, params->maxx, params->miny, *params->nbPlayers, params->lines); + drawCreateMenuLine(params->renderer, params->lines+*params->nbPlayers-1); +} +void decrementNbPlayer(P_Button* caller) +{ + DecrementParams* params = (DecrementParams*)caller->arg; + int* nbPlayers = params->nbPlayers; + if(*nbPlayers == 2) + { + fprintf(stderr, "WARNING: Can't decrement down to 1 and less\n"); + return; + } + freeCreateMenuLine(¶ms->lines[*nbPlayers-1]); + *nbPlayers -= 1; +} + +void changePlayerColor(P_Button* caller) +{ + ChangeColorParams* params = (ChangeColorParams*)caller->arg; + params->p->color = params->color; +} + +bool drawGameCreationMenu(SDL_Renderer* renderer, TextLabel** labels, int nbLabels, P_Button* buttons, int nbButtons, CreateMenuLine* lines, int nbPlayer) +{ + //Draw everything + + for(int i=0; iaiButton); + for(int i=0; icolorButtons[i]); + } + drawTextInputOnRenderer(renderer, &line->pseudoInput); + return true; +} + +CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int xmax, TTF_Font* font, Player* player) +{ + int const wColorBtn = 32; + int const hColorBtn = 32; + int const colorBtnXMargin = 8; + SDL_Texture* btnTexture, *btnTextureHover; + + P_Button* colorsBtn = (P_Button*) malloc(sizeof(P_Button)*NB_COLORS); + P_Button ai = createButton(NULL, NULL, xmin, y, 0, 0, NULL); + + CreateMenuLine line; + + SDL_Texture* aiTexture = createGenericButtonTexture("X", font, 16, COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(ai.rect.w), &(ai.rect.h), renderer); + SDL_Texture* aiTextureHovered = createGenericButtonTexture("X", font, 16, COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &ai.rect.w, &ai.rect.h, renderer); + ai.texture = aiTexture; + ai.hoverTexture = aiTextureHovered; + + SDL_Rect rect = {.x = xmin + ai.rect.x + ai.rect.w, .y = y, .h = TTF_FontHeight(font),.w = xmax - ai.rect.w - NB_COLORS * (wColorBtn + colorBtnXMargin*2)- 16}; + + TextInput pseudoInput; + initTextInput(&pseudoInput, &rect, NULL, font); + + ChangeColorParams* params; + + for(int i=0; ip = player; + params->color=playersColors[i]; + colorsBtn[i] = createButton(NULL, NULL, xmax-wColorBtn*(i+1), y, wColorBtn, hColorBtn, changePlayerColor); + colorsBtn[i].arg = params; + btnTexture = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BORDER, PLAYER_SDL_COLORS[i], 4, 8, NULL, NULL, renderer); + btnTextureHover = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BACKGROUND, PLAYER_SDL_COLORS[i], 4, 8, NULL, NULL, renderer); + colorsBtn[i].texture = btnTexture; + colorsBtn[i].hoverTexture = btnTextureHover; + + } + line.aiButton=ai; + line.colorButtons=colorsBtn; + line.player=player; + line.pseudoInput=pseudoInput; + return line; +} + +void freeCreateMenuLine(CreateMenuLine* line) +{ + destroyTextInput(&line->pseudoInput); + freeButton(&line->aiButton); + for(int i=0; icolorButtons[i].arg); + freeButton(&line->colorButtons[i]); + // free(&line->colorButtons[i]); + } +} +bool gameCreationMenu(SDL_Renderer* renderer, GeneralState* generalState,TTF_Font* font, int width, int height) { + int nbPlayers = 2; + int const nbLabels = 5; + int nbButtons = 2; + TextLabel *labels[nbLabels]; + P_Button* buttons = (P_Button*) malloc(sizeof(P_Button)*nbButtons); + + // TextLabel for "Nombre de joueur.euse.s" creation - SDL_Point nbJoueurLabelPos = {.x=width*0.05, .y=height*0.05}; - SDL_Color black = {0,0,0,255}; + SDL_Point titleLabelPos = {.x=width*0.05, .y=height*0.05}; SDL_Color white = {225, 255, 255, 255}; - TextLabel nbJoueurLabel = createTextLabel( - "Nombre de joueur.euse.s", - &nbJoueurLabelPos, + CreateMenuLine lines[NB_PLAYER_MAX]; + TextLabel titleLabel = createTextLabel( + "Nombre de joueur·euse·s", + &titleLabelPos, 1, &white, font, @@ -17,52 +180,181 @@ bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int POSY_TOP ); - // Incrementation Btn creation - - P_Button incrementBtn = createButton(NULL, NULL, nbJoueurLabelPos.x+calculateStringPixelLenght(font, nbJoueurLabel.text)+32, nbJoueurLabelPos.y, 0, 0, NULL); - SDL_Texture* btnTexture = createGenericButtonTexture("+", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer); + // Decrementation Btn creation + P_Button decrementBtn = createButton(NULL, NULL, titleLabelPos.x+calculateStringPixelLenght(font, titleLabel.text)+32, titleLabelPos.y, 0, 0, &decrementNbPlayer); + SDL_Texture* btnTexture = createGenericButtonTexture("-", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer); if(btnTexture == NULL) { fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError()); return false; } - SDL_Texture* btnHoveredTexture = createGenericButtonTexture("+", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer); + SDL_Texture* btnHoveredTexture = createGenericButtonTexture("-", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer); if(btnHoveredTexture == NULL) { fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError()); return false; } - incrementBtn.texture = btnTexture; - incrementBtn.hoverTexture = btnHoveredTexture; + decrementBtn.texture = btnTexture; + decrementBtn.hoverTexture = btnHoveredTexture; - // Decrementation Btn creation - - P_Button decrementBtn = createButton(NULL, NULL, nbJoueurLabelPos.x+calculateStringPixelLenght(font, nbJoueurLabel.text)+32, nbJoueurLabelPos.y, 0, 0, NULL); - btnTexture = createGenericButtonTexture("-", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer); + // TextLabel for the number of players creation + SDL_Point nbPlayerLabelPos = {.x=decrementBtn.rect.x+decrementBtn.rect.w+8, .y=decrementBtn.rect.y*1.5}; + char nbPlayerStr[2]; + if(nbPlayers < 0 || nbPlayers > 9) + { + fprintf(stderr, "WARNING: The number of players has to be between 0 and 9\n"); + return false; + } + nbPlayerStr[0] = nbPlayers + 48; // ASCII code of '0' is 48 and ASSCI code of '9' is 57 (48+9) + nbPlayerStr[1] = '\0'; + TextLabel nbPlayerLabel = createTextLabel( + nbPlayerStr, + &nbPlayerLabelPos, + 1, + &white, + font, + renderer, + POSX_LEFT, + POSY_CENTER + ); + + + + + // Incrementation Btn creation + P_Button incrementBtn = createButton(NULL, NULL, nbPlayerLabel.textZone.x + nbPlayerLabel.textZone.w + 8, titleLabelPos.y, 0, 0, &incrementNbPlayer); + btnTexture = createGenericButtonTexture("+", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer); if(btnTexture == NULL) { fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError()); return false; } - btnHoveredTexture = createGenericButtonTexture("-", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer); + btnHoveredTexture = createGenericButtonTexture("+", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer); if(btnHoveredTexture == NULL) { fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError()); return false; } - decrementBtn.texture = btnTexture; - decrementBtn.hoverTexture = btnHoveredTexture; + incrementBtn.texture = btnTexture; + incrementBtn.hoverTexture = btnHoveredTexture; - //Draw everything - drawButtonOnRenderer(renderer, &incrementBtn); - drawButtonOnRenderer(renderer, &decrementBtn); - drawTextLabel(renderer, &nbJoueurLabel); + SDL_Point aiLabelPos = {.x=titleLabel.textZone.x, .y=decrementBtn.rect.y+ decrementBtn.rect.h + 16}; + TextLabel aiLabel = createTextLabel( + "IA", + &aiLabelPos, + 1, + &white, + font, + renderer, + POSX_LEFT, + POSY_TOP + ); + + SDL_Point colorLabelPos = { + .x=incrementBtn.rect.x+incrementBtn.rect.w, + decrementBtn.rect.y+ decrementBtn.rect.h + 16 + }; + TextLabel colorLabel = createTextLabel( + "Couleur", + &colorLabelPos, + 1, + &white, + font, + renderer, + POSX_RIGHT, + POSY_TOP + ); + + SDL_Point pseudoLabelPos = { + .x= (titleLabel.textZone.x+ + aiLabel.textZone.w+ + colorLabelPos.x) + /2, + .y=decrementBtn.rect.y+ decrementBtn.rect.h + 16 + }; + TextLabel pseudoLabel = createTextLabel( + "Pseudonyme", + &pseudoLabelPos, + 1, + &white, + font, + renderer, + POSX_CENTER, + POSY_TOP + ); + + + createPlayersLines(renderer, font, titleLabelPos.x, incrementBtn.rect.x+incrementBtn.rect.w, colorLabel.textZone.y+colorLabel.textZone.h , nbPlayers, lines); + + labels[0] = &titleLabel; + labels[1] = &nbPlayerLabel; + labels[2] = &aiLabel; + labels[3] = &pseudoLabel; + labels[4] = &colorLabel; + DecrementParams dparams= {.nbPlayers=&nbPlayers, .lines=lines, .renderer=renderer}; + decrementBtn.arg = &dparams; + + IncrementParams iparams= {.nbPlayers=&nbPlayers, .lines=lines, .minx=titleLabelPos.x, .maxx=incrementBtn.rect.x+incrementBtn.rect.w, .miny=colorLabelPos.y+colorLabel.textZone.h + 16, .font=font, .renderer=renderer}; + incrementBtn.arg = &iparams; + + buttons[0] = decrementBtn; + buttons[1] = incrementBtn; + + InputProcessor inputProcessor = createInputProcessor(); + array_P_Button_AddElement(&inputProcessor.tabButton, incrementBtn); + array_P_Button_AddElement(&inputProcessor.tabButton, decrementBtn); + + while(*generalState == GS_GameCreationMenu) + { + printf("%d\n", nbPlayers); + SDL_SetRenderDrawColor(renderer, 55, 120, 175, 255); + SDL_RenderClear(renderer); + InputElement inputElement; + while (InputType_None != (inputElement = proccessInput(&inputProcessor)).type) + { + switch (inputElement.type) + { + case InputType_ActivateUI: + switch (inputElement.data.uiAction) + { + case UIAction_Quit: + *generalState = GS_MainMenu; + break; + case UIAction_Validate: + break; + case UIAction_Cancel: + break; + default: + break; + } + break; + case InputType_MoveGame: + + break; + case InputType_ClickGame: + + break; + case InputType_None: + default: + break; + } + } + nbPlayerLabel.text[0] = nbPlayers+48; + drawGameCreationMenu(renderer, labels, nbLabels, buttons, nbButtons, lines, nbPlayers); + SDL_RenderPresent(renderer); + SDL_Delay(25); + } + + + + //free - freeTextLabel(&nbJoueurLabel); + freeInputProcessor(&inputProcessor); + freeTextLabel(&titleLabel); freeButton(&incrementBtn); - + freeButton(&decrementBtn); return true; } 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/GameInterface.c b/Pontu/src/view/GameInterface.c index c984da4..485d4da 100644 --- a/Pontu/src/view/GameInterface.c +++ b/Pontu/src/view/GameInterface.c @@ -52,17 +52,31 @@ void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler) SDL_Color menuBackgroundColor = {0,255,0,255}; //Postion text label - //SDL_Point pos + SDL_Point positonNbTurnLabel = {.x=60, .y=800}; + SDL_Point positionTimeLablel = {.x=770, .y=800}; + + //Color labal + SDL_Color colorLabel = {0, 255, 0, 255}; + + //Position label + PositionX_Type positionX = POSX_CENTER; + PositionY_Type positionY = POSY_CENTER; + //SDL_Texture *buttonTexture = createGenericButtonTexture("Menu", NULL, 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer); SDL_Texture *menuButtonTexture = createGenericButtonTexture("Menu", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBorderColor,24,5,&sizex,&sizey,renderer); SDL_Texture *menuButtonHoverTexture = createGenericButtonTexture("MenuHover", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer); + //Buttons P_Button menuButton = createButton(menuButtonTexture, menuButtonHoverTexture,20,20,100,50,&action); //top left corner (rectangle) P_Button settingButton = createButton(menuButtonTexture, menuButtonHoverTexture, 750,10,50,50,&action); //top right corner (square or circle) P_Button soundButton = createButton(menuButtonTexture, menuButtonHoverTexture, 825,10,50,50,&action); //top right cornre (square or circle) - //TextLabel nbTurn = createTextLabel("Turn",) + + //Labels + TextLabel nbTurnLabel = createTextLabel("Turn : ",&positonNbTurnLabel,1,&colorLabel,fontHandler.fonts[FONT_retro],renderer,positionX,positionY); + TextLabel timeLabel = createTextLabel("Time : ",&positionTimeLablel,1,&colorLabel,fontHandler.fonts[FONT_retro],renderer,positionX,positionY); + //bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button); @@ -108,12 +122,17 @@ void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler) drawButtonOnRenderer(renderer,&menuButton); drawButtonOnRenderer(renderer,&settingButton); drawButtonOnRenderer(renderer,&soundButton); + drawTextLabel(renderer,&nbTurnLabel); + drawTextLabel(renderer,&timeLabel); SDL_RenderPresent(renderer); SDL_Delay(20); } + //FREE TEXT LABEL + BUTTON + + } \ No newline at end of file diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index 5b08067..18e7505 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -5,22 +5,35 @@ #include "engine/TextureHandler.h" #include "model/Game.h" #include "model/arrayCoord.h" -#include "debug/printer.h" #include "view/PiecesDrawer.h" #include "view/BoardDrawer.h" #include "view/GameDrawer.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}; +#include "engine/UIElementUtils.h" - return boardRect; + +PositionSpecifier boardRectPositionSpecifier() { + SDL_Rect b100 = { + .x= 50, + .y= 50, + .w= 80, + .h= 80, + }; + return newPositionSpecifier(&b100, POSX_CENTER, POSY_CENTER, ASPECT_KEEP_FIT); +} + +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) { if (*generalState != GS_Game) { - return ; + return; } GameInputProcessor inputProcessor = createGameInputProcessor(); struct array_Coord interactiveCases = array_Coord_Create(); @@ -28,19 +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: + drawRemoveBridge(renderer, &boardRect, textureHandler.textures[TEXTURE_Water], &inputElement.data.coord); + SDL_RenderPresent(renderer); + break; } + if (actionRealized != GameAction_None) { + inputProcessor.selectedCase = newCoord(-1,-1); + if (game.phase == GAME_ENDED) { + *generalState = GS_EndOfGameMenu; + } + } 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; @@ -98,16 +132,17 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend array_Coord_Free(&interactiveCases); interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase); - fprintf(stderr, "Interactive cases : {"); - array_Coord_Foreach(&interactiveCases, *printCoord); - fprintf(stderr, "}\n"); } - SDL_Delay(20); + SDL_Delay(5); + } + + for (size_t i = 0; i #include "engine/Colors.h" #include "engine/InputProcessor.h" +#include "engine/UIElementUtils.h" +#include "engine/arrayTextLabel.h" +#include "engine/arrayPositionSpecifier.h" + + +struct endGameMenuTextLabel { + struct array_TextLabel textLabels; + struct array_PositionSpecifier positionSpecifiers; +}; /** * @brief Button handle which set a generalState to GS_MainMenu @@ -54,14 +63,10 @@ P_Button createButtonForEndGameMenu(SDL_Renderer* renderer, TTF_Font* font, cons * @param font Font used for title */ void drawTitle(SDL_Renderer* renderer, const SDL_Rect* rect, TTF_Font* font) { - SDL_Point pos = {rect->x+rect->w/2, rect->y+rect->h/100}; - SDL_Color color = {0,0,0,0}; - - TextLabel titre = createTextLabel("Scores", &pos, 4, &color, font, renderer, POSX_CENTER, POSY_TOP); - drawTextLabel(renderer, &titre); + /*drawTextLabel(renderer, &titre); - freeTextLabel(&titre); + freeTextLabel(&titre);*/ } /** @@ -142,6 +147,37 @@ void drawEndGameMenu(SDL_Renderer* renderer, const Player players[], const size_ drawPlayersScores(renderer, players, nbPlayers, rect, fontHandler->fonts[FONT_retro]); } +TextLabel createTitleLabel(SDL_Renderer* renderer, TTF_Font* font) { + SDL_Color color = {0,0,0,0}; + + return createUnsizedTextLabel("Scores", &color, font, renderer); +} + +PositionSpecifier getTitleRect100(SDL_Rect* labelSize) { + SDL_Rect base100 = { + .x=50, + .y=1, + .w = 30, + .h = 30*labelSize->h/labelSize->w + }; + return newPositionSpecifier(&base100, POSX_CENTER, POSY_TOP, ASPECT_KEEP_W); +} + +struct endGameMenuTextLabel createLabels(SDL_Renderer* renderer, const Player players[], const size_t nbPlayers, FontHandler* fontHandler) { + struct endGameMenuTextLabel labels = { + .textLabels = array_TextLabel_Create(), + .positionSpecifiers = array_PositionSpecifier_Create() + }; + + // Titre + array_TextLabel_AddElement(&labels.textLabels, createTitleLabel(renderer, fontHandler->fonts[FONT_retro])); + array_PositionSpecifier_AddElement(&labels.positionSpecifiers, getTitleRect100(&array_TextLabel_Last(&labels.textLabels)->textZone)); + + + + return labels; +} + void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, FontHandler* fontHandler, const Player players[], const size_t nbPlayers) { int windowW; int windowH; @@ -158,9 +194,20 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r InputProcessor inputProcessor = createInputProcessor(); array_P_Button_AddElement(&inputProcessor.tabButton, createButtonForEndGameMenu(renderer, fontHandler->fonts[FONT_retro], &rectMenuEndGame, generalState)); P_Button* buttonMenuEndGame = array_P_Button_Last(&inputProcessor.tabButton); - + SDL_Rect base100 = { + .x = 50, + .y = 99, + .w = 30, + .h = 30*buttonMenuEndGame->rect.h/buttonMenuEndGame->rect.w + }; drawEndGameMenu(renderer, players, nbPlayers, &rectMenuEndGame, fontHandler); + struct endGameMenuTextLabel labels = createLabels(renderer, players, nbPlayers, fontHandler); + for (size_t i=0; irect = adaptPosToRect(&base100, &rectM, POSX_CENTER, POSY_BOTTOM, ASPECT_KEEP_W); + fprintf(stderr, "Resize\n"); fflush(stderr); + } default: break; } @@ -191,6 +256,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r drawButtonOnRenderer(renderer, buttonMenuEndGame); + SDL_RenderPresent(renderer); SDL_Delay(50); } diff --git a/Pontu/src/view/PiecesDrawer.c b/Pontu/src/view/PiecesDrawer.c index 494c603..73b2cb1 100644 --- a/Pontu/src/view/PiecesDrawer.c +++ b/Pontu/src/view/PiecesDrawer.c @@ -1,37 +1,35 @@ #include "view/PiecesDrawer.h" -#include "model/Island.h" +#include "view/ToRect.h" -//Don't put this in model -SDL_Rect islandToRect(const SDL_Rect* boardRect, const Island* island) { - const int w = boardRect->w/9; - const int h = boardRect->h/9; - SDL_Rect r = { - .x = boardRect->x + w*(island->x*2), - .y = boardRect->y + h*(island->y*2), - .w = w, - .h = h - }; - - return r; -} - -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) { - const SDL_Rect rDest = islandToRect(boardRect, &arrPieces[i].island); - SDL_RenderCopy(renderer, piece, NULL, &rDest); + Coord c = islandToCoord(&arrPieces[i].island); + const SDL_Rect rDest = coordToRect(boardRect, &c); + SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); } } } -void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Island* startMove, const Island* 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 = islandToRect(boardRect, startMove); - SDL_RenderCopy(renderer, islandTexture, NULL, &rDest); + SDL_Rect rDest = coordToRect(boardRect, startMove); + SDL_RenderCopy(renderer, textureHandler->textures[TEXTURE_Island], NULL, &rDest); - rDest = islandToRect(boardRect, endMove); + rDest = coordToRect(boardRect, endMove); SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); } + + +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); +} + + diff --git a/Pontu/src/view/ToRect.c b/Pontu/src/view/ToRect.c new file mode 100644 index 0000000..9f899bd --- /dev/null +++ b/Pontu/src/view/ToRect.c @@ -0,0 +1,14 @@ +#include "view/ToRect.h" + +SDL_Rect coordToRect(const SDL_Rect* boardRect, const Coord* coord) { + const int w = boardRect->w/9; + const int h = boardRect->h/9; + SDL_Rect r = { + .x = boardRect->x + w*coord->x, + .y = boardRect->y + h*coord->y, + .w = w, + .h = h + }; + + return r; +} diff --git a/Pontu/test/testCreationMenu.c b/Pontu/test/testCreationMenu.c index 4d907dc..ab82e9f 100644 --- a/Pontu/test/testCreationMenu.c +++ b/Pontu/test/testCreationMenu.c @@ -13,6 +13,7 @@ int testCreationMenu(void) { char* path = "rsrc/img/Lenna.png"; int i=0; int w, h; + int nbPlayer=1; if(0 != SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr, "Erreur SDL_INIT: %s\n", SDL_GetError()); @@ -42,7 +43,7 @@ int testCreationMenu(void) { goto Quit; } - if(0 != SDL_SetRenderDrawColor(renderer, 0,0,0,0)) //choisi la couleur avec laquelle travailler + if(0 != SDL_SetRenderDrawColor(renderer, 55,120,175,0)) //choisi la couleur avec laquelle travailler { fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError()); goto Quit; @@ -61,30 +62,12 @@ int testCreationMenu(void) { } - SDL_bool quit = SDL_FALSE; - SDL_Event event; - FontHandler fontHandler = loadFonts(); SDL_GetWindowSize(window, &w, &h); - while(!quit) - { - while(SDL_PollEvent(&event)) - { - switch(event.type) - { - case SDL_QUIT: - quit = SDL_TRUE; - break; - case SDL_MOUSEBUTTONUP: - break; - } - } - drawGameCreationMenu(renderer, fontHandler.fonts[FONT_retro], w, h); - SDL_RenderPresent(renderer); - - SDL_Delay(20); - } + GeneralState generalState = GS_GameCreationMenu; + printf("%d/%d\n", w, h); + gameCreationMenu(renderer, &generalState, fontHandler.fonts[FONT_retro], w, h); Quit: freeFonts(fontHandler); diff --git a/Pontu/test/testMenuEndGame.c b/Pontu/test/testMenuEndGame.c index 79bc552..c59b346 100644 --- a/Pontu/test/testMenuEndGame.c +++ b/Pontu/test/testMenuEndGame.c @@ -18,7 +18,7 @@ void testMenuEndGame() { } //fenetre - window = SDL_CreateWindow("Fenêtre", windowSize.x, windowSize.y, windowSize.w, windowSize.h, SDL_WINDOW_SHOWN); + window = SDL_CreateWindow("Fenêtre", windowSize.x, windowSize.y, windowSize.w, windowSize.h, SDL_WINDOW_SHOWN |SDL_WINDOW_RESIZABLE); if(window == NULL) { fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError());