From dc8d05c09171dc0e56cef3982946ca2648954881 Mon Sep 17 00:00:00 2001 From: jathomas2 Date: Mon, 7 Feb 2022 11:55:05 +0100 Subject: [PATCH 01/11] On game interface, add text label for nbTurn and time --- Pontu/src/view/GameInterface.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Pontu/src/view/GameInterface.c b/Pontu/src/view/GameInterface.c index c984da4..339784f 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 From 4babc2db8efc397eff64b080dbdfe6d988d28b58 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 12:01:36 +0100 Subject: [PATCH 02/11] Game Main modified (need to correct drawMovePiece and drawDeleteBridge) --- Pontu/entryPoints/main.c | 4 ++-- Pontu/src/view/GameMain.c | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index dfeee84..5e11e97 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -43,7 +43,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: @@ -67,7 +67,7 @@ int main(int argc, char const *argv[]) { fprintf(stderr,"sorry"); exit(-1); }*/ - + generalState = GS_Game; gameView(&generalState, window, renderer, players, nbPlayers); endGameMenu(&generalState, window, renderer, &fontHandler, players, nbPlayers); diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index 5b08067..bd19a9c 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -20,7 +20,7 @@ SDL_Rect boardRectFromWindowSize(int windowW, int windowH) { 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(); @@ -38,9 +38,8 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend drawFullBoard(renderer, &boardRect, &game.board, textureHandler.textures[TEXTURE_Island], textureHandler.textures[TEXTURE_Bridge], textureHandler.textures[TEXTURE_Water]); for (int iPlayer=0; iPlayer Date: Mon, 7 Feb 2022 16:21:35 +0100 Subject: [PATCH 03/11] PositionAdapter for redimensionnable window --- Pontu/include/engine/InputElement.h | 13 +++- Pontu/include/engine/TextLabel.h | 16 +--- Pontu/include/engine/UIElementUtils.h | 35 +++++++++ Pontu/include/engine/arrayPositionSpecifier.h | 9 +++ Pontu/include/engine/arraySDL_Rect.h | 9 +++ Pontu/include/engine/arrayTextLabel.h | 9 +++ Pontu/include/view/MenuEndGame.h | 1 + Pontu/src/engine/InputElement.c | 8 ++ Pontu/src/engine/InputProcessor.c | 4 + Pontu/src/engine/TextLabel.c | 73 ++++++++++++------- Pontu/src/engine/UIElementUtils.c | 72 ++++++++++++++++++ Pontu/src/view/MenuEndGame.c | 68 +++++++++++++++-- Pontu/test/testMenuEndGame.c | 2 +- 13 files changed, 271 insertions(+), 48 deletions(-) create mode 100644 Pontu/include/engine/UIElementUtils.h create mode 100644 Pontu/include/engine/arrayPositionSpecifier.h create mode 100644 Pontu/include/engine/arraySDL_Rect.h create mode 100644 Pontu/include/engine/arrayTextLabel.h create mode 100644 Pontu/src/engine/UIElementUtils.c 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..e3ae962 --- /dev/null +++ b/Pontu/include/engine/UIElementUtils.h @@ -0,0 +1,35 @@ +#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 +} 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/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/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 5b931b3..752febd 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -43,6 +43,10 @@ InputElement proccessInput(InputProcessor *inputProcessor) } break; } + case 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/UIElementUtils.c b/Pontu/src/engine/UIElementUtils.c new file mode 100644 index 0000000..ddc1d8a --- /dev/null +++ b/Pontu/src/engine/UIElementUtils.c @@ -0,0 +1,72 @@ +#include "engine/UIElementUtils.h" + + +PositionSpecifier newPositionSpecifier(const SDL_Rect* const base100, const PositionX_Type tpX, const PositionY_Type tpY, const AspectRatioType aspectType) { + PositionSpecifier ps = { + .base100 = *base100, + .tpX = tpX, + .tpY = tpY, + .aspectType = aspectType + }; + return ps; +} + +SDL_Rect adaptPosToRect(const PositionSpecifier *const positionSpecifier, const SDL_Rect *const globalRect) { + SDL_Rect r = { + .x = 0, + .y = 0, + .w = 100, + .h = 100 + }; + + switch (positionSpecifier->aspectType) + { + 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; + 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 POSX_CENTER: + r.x = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h/2; + break; + case POSX_RIGHT: + r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h; + break; + default: + break; + } + + return r; +} + diff --git a/Pontu/src/view/MenuEndGame.c b/Pontu/src/view/MenuEndGame.c index b13efea..1b4f331 100644 --- a/Pontu/src/view/MenuEndGame.c +++ b/Pontu/src/view/MenuEndGame.c @@ -4,6 +4,15 @@ #include #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,9 +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, const SDL_Rect* rect, 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; @@ -160,7 +193,12 @@ 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); while(*generalState == GS_EndOfGameMenu) @@ -185,6 +223,19 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r break; } break; + case InputType_Window_Resize: { + + SDL_Rect rectM = { + .x=inputElement.data.windowSize.w/10, + .y=0, + .w=inputElement.data.windowSize.w*80/100, + .h=inputElement.data.windowSize.h + }; + drawEndGameMenu(renderer, players, nbPlayers, &rectM, fontHandler); + + //buttonMenuEndGame->rect = adaptPosToRect(&base100, &rectM, POSX_CENTER, POSY_BOTTOM, ASPECT_KEEP_W); + fprintf(stderr, "Resize\n"); fflush(stderr); + } default: break; } @@ -193,6 +244,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r drawButtonOnRenderer(renderer, buttonMenuEndGame); + SDL_RenderPresent(renderer); SDL_Delay(50); } 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()); From 33191714c1db899b3c4870bae84df6b3f4c8f1c9 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 16:41:21 +0100 Subject: [PATCH 04/11] Modified gameMenu, adapted some calls --- Pontu/entryPoints/main.c | 3 ++- Pontu/include/view/MainMenu.h | 2 +- Pontu/include/view/PiecesDrawer.h | 3 ++- Pontu/include/view/ToRect.h | 10 ++++++++++ Pontu/src/view/BoardDrawer.c | 15 +-------------- Pontu/src/view/GameInterface.c | 4 ++-- Pontu/src/view/PiecesDrawer.c | 26 ++++++-------------------- Pontu/src/view/ToRect.c | 14 ++++++++++++++ 8 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 Pontu/include/view/ToRect.h create mode 100644 Pontu/src/view/ToRect.c diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index 5e11e97..a35636a 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,7 +9,7 @@ #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; 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/PiecesDrawer.h b/Pontu/include/view/PiecesDrawer.h index 92b4c1f..212f70b 100644 --- a/Pontu/include/view/PiecesDrawer.h +++ b/Pontu/include/view/PiecesDrawer.h @@ -4,10 +4,11 @@ #include #include #include "model/Piece.h" +#include "model/Coord.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 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, SDL_Texture* pieceTexture, SDL_Texture* islandTexture); #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/view/BoardDrawer.c b/Pontu/src/view/BoardDrawer.c index 2b2dac0..317b2e4 100644 --- a/Pontu/src/view/BoardDrawer.c +++ b/Pontu/src/view/BoardDrawer.c @@ -1,18 +1,5 @@ #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); diff --git a/Pontu/src/view/GameInterface.c b/Pontu/src/view/GameInterface.c index 339784f..485d4da 100644 --- a/Pontu/src/view/GameInterface.c +++ b/Pontu/src/view/GameInterface.c @@ -59,8 +59,8 @@ void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler) SDL_Color colorLabel = {0, 255, 0, 255}; //Position label - POSITIONX_TYPE positionX = POSX_CENTER; - POSITIONY_TYPE positionY = POSY_CENTER; + PositionX_Type positionX = POSX_CENTER; + PositionY_Type positionY = POSY_CENTER; diff --git a/Pontu/src/view/PiecesDrawer.c b/Pontu/src/view/PiecesDrawer.c index 494c603..b1ba427 100644 --- a/Pontu/src/view/PiecesDrawer.c +++ b/Pontu/src/view/PiecesDrawer.c @@ -1,37 +1,23 @@ #include "view/PiecesDrawer.h" -#include "model/Island.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; -} +#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) { for (size_t i = 0; i < nbPieces; ++i) { if (arrPieces[i].idJ == numPlayer) { - const SDL_Rect rDest = islandToRect(boardRect, &arrPieces[i].island); + Coord c = islandToCoord(&arrPieces[i].island); + const SDL_Rect rDest = coordToRect(boardRect, &c); SDL_RenderCopy(renderer, piece, 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, SDL_Texture* pieceTexture, SDL_Texture* islandTexture) { - SDL_Rect rDest = islandToRect(boardRect, startMove); + SDL_Rect rDest = coordToRect(boardRect, startMove); SDL_RenderCopy(renderer, islandTexture, NULL, &rDest); - rDest = islandToRect(boardRect, endMove); + rDest = coordToRect(boardRect, endMove); 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; +} From 9eb65816dfe0df632e8e75e0243c78d19abdc57a Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 17:07:41 +0100 Subject: [PATCH 05/11] New Drawing system in place -> going to test it on guacamole --- Pontu/include/model/Game.h | 15 ++++++++++---- Pontu/include/view/PiecesDrawer.h | 3 +++ Pontu/src/model/Game.c | 14 ++++++------- Pontu/src/view/BoardDrawer.c | 3 ++- Pontu/src/view/GameMain.c | 34 +++++++++++++++++++++++-------- Pontu/src/view/PiecesDrawer.c | 8 ++++++++ 6 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 0a88024..13d9ce1 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 @@ -158,18 +165,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/view/PiecesDrawer.h b/Pontu/include/view/PiecesDrawer.h index 212f70b..b0e0fae 100644 --- a/Pontu/include/view/PiecesDrawer.h +++ b/Pontu/include/view/PiecesDrawer.h @@ -10,5 +10,8 @@ void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const P void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture); + +void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* pieceTexture, const Coord* coordPlace); + #endif //PIECES_DRAWER_INCLUDED diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index e223827..07cf702 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -268,7 +268,7 @@ void updatePieceIsolated(Game* game, const Island* island) } } -bool clickOnBoard(const Coord coord, Game* game) +GameAction clickOnBoard(const Coord coord, Game* game) { const IslandOrBridge islandOrBridge = coordToEntity(coord); @@ -281,7 +281,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 +296,7 @@ bool clickOnBoard(const Coord coord, Game* game) changePhaseOrPlayerTurn(game); - return true; + return GameAction_RemoveBridge; } } break; @@ -304,7 +304,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 +319,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 +340,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game) { changePhaseOrPlayerTurn(game); } - return pieceMoved; + return pieceMoved ? GameAction_MovePiece : GameAction_None; } } break; @@ -348,7 +348,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/view/BoardDrawer.c b/Pontu/src/view/BoardDrawer.c index 317b2e4..bf80d9f 100644 --- a/Pontu/src/view/BoardDrawer.c +++ b/Pontu/src/view/BoardDrawer.c @@ -3,7 +3,8 @@ 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); + fprintf(stderr, "RedrawWater\n"); fflush(stderr); } 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/GameMain.c b/Pontu/src/view/GameMain.c index bd19a9c..cefef67 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -65,18 +65,23 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend break; } break; - case InputType_MoveGame: + case InputType_MoveGame: { 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); - - drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, textureHandler.textures[TEXTURE_PieceRed], textureHandler.textures[TEXTURE_Island]); + const GameAction actionRealized = moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game); + switch (actionRealized) + { + case GameAction_MovePiece: + drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, textureHandler.textures[TEXTURE_PieceRed], textureHandler.textures[TEXTURE_Island]); + SDL_RenderPresent(renderer); + break; + } - SDL_RenderPresent(renderer); break; - case InputType_ClickGame: + } + case InputType_ClickGame: { fprintf(stderr, "Clic on board (%d; %d)\n", inputElement.data.coord.x, inputElement.data.coord.y); fprintf(stderr, "\tSelected case : (%d; %d)\n", inputProcessor.selectedCase.x, inputProcessor.selectedCase.y); @@ -85,13 +90,26 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend inputProcessor.selectedCase = newCoord(-1,-1); } - if (clickOnBoard(inputElement.data.coord, &game)) { + const GameAction actionRealized = clickOnBoard(inputElement.data.coord, &game); + switch (actionRealized) + { + case GameAction_PlacePiece: + drawPlacePiece(renderer, &boardRect, textureHandler.textures[TEXTURE_PieceViolet], &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) { fprintf(stderr, "\tselected case reset\n"); inputProcessor.selectedCase = newCoord(-1,-1); } - break; + } case InputType_None: default: break; diff --git a/Pontu/src/view/PiecesDrawer.c b/Pontu/src/view/PiecesDrawer.c index b1ba427..f51f170 100644 --- a/Pontu/src/view/PiecesDrawer.c +++ b/Pontu/src/view/PiecesDrawer.c @@ -21,3 +21,11 @@ void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coor 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) { + SDL_Rect rDest = coordToRect(boardRect, coordPlace); + SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); +} + + From 8cb3ccef1b6cbca73f1b28ffc35921575f711b62 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 17:38:21 +0100 Subject: [PATCH 06/11] Game can be fluid in IUT's PC --- Pontu/entryPoints/main.c | 2 +- Pontu/src/model/Game.c | 1 - Pontu/src/view/BoardDrawer.c | 1 - Pontu/src/view/GameMain.c | 17 +++-------------- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index a35636a..369db60 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) { GeneralState generalState; - + SDL_Window* window = NULL; SDL_Rect windowSize = {10, 10, 900, 900}; SDL_Renderer* renderer = NULL; diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 07cf702..015e871 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -90,7 +90,6 @@ void changePhaseOrPlayerTurn(Game* game) game->board.nbPieces)); - fprintf(stderr, "Player n°%ld turn\n", game->currentPlayerID); fflush(stderr); if (anyOfPlayersPiecesCanMove(game->currentPlayerID, &game->board)) diff --git a/Pontu/src/view/BoardDrawer.c b/Pontu/src/view/BoardDrawer.c index bf80d9f..1e7d0a2 100644 --- a/Pontu/src/view/BoardDrawer.c +++ b/Pontu/src/view/BoardDrawer.c @@ -4,7 +4,6 @@ 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, NULL, &destRect); - fprintf(stderr, "RedrawWater\n"); fflush(stderr); } 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/GameMain.c b/Pontu/src/view/GameMain.c index cefef67..bdddc2a 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -5,12 +5,13 @@ #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}; @@ -66,10 +67,6 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend } break; case InputType_MoveGame: { - 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); - const GameAction actionRealized = moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game); switch (actionRealized) { @@ -82,11 +79,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend break; } case InputType_ClickGame: { - fprintf(stderr, "Clic on board (%d; %d)\n", inputElement.data.coord.x, inputElement.data.coord.y); - fprintf(stderr, "\tSelected case : (%d; %d)\n", inputProcessor.selectedCase.x, inputProcessor.selectedCase.y); - if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) { - fprintf(stderr, "\tselected case reset\n"); inputProcessor.selectedCase = newCoord(-1,-1); } @@ -104,7 +97,6 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend } if (actionRealized != GameAction_None) { - fprintf(stderr, "\tselected case reset\n"); inputProcessor.selectedCase = newCoord(-1,-1); } @@ -117,12 +109,9 @@ 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); } freeTextureHandler(&textureHandler); From 930719ce7d3571efb9d1f93b901b3ac76e6f7946 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 18:30:44 +0100 Subject: [PATCH 07/11] End game modified but not totally fixed --- Pontu/include/model/Game.h | 1 + Pontu/src/model/Game.c | 34 ++++++++++++++++++++++++++++++---- Pontu/src/view/GameMain.c | 7 +++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 13d9ce1..fb11a41 100644 --- a/Pontu/include/model/Game.h +++ b/Pontu/include/model/Game.h @@ -47,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; /** diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 015e871..03e30b6 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -37,7 +37,8 @@ Game newGame(const size_t nbPlayers, const Player player[]) .nb_rounds = 0, .phase = PLACEMENT, .board = newBoard(nbPlayers), - .nbPlayers = nbPlayers + .nbPlayers = nbPlayers, + .lastRank = 0 }; for (size_t player_i = 0; player_i < nbPlayers; player_i++) @@ -53,6 +54,16 @@ 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->lastRank; + game->arrPlayers[playerId].rank = game->lastRank; + fprintf(stderr, "Rank : %d\n", game->lastRank); +} + +void endGame(Game* game) { + game->phase = GAME_ENDED; +} void changePhaseOrPlayerTurn(Game* game) { @@ -74,6 +85,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->nbPlayers-1 == game->lastRank) { + endGame(game); + return; + } + } + do { game->currentPlayerID++; @@ -81,13 +100,20 @@ 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].rank != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) { + eliminatePlayer(game, game->currentPlayerID); + if (game->nbPlayers-1 == game->lastRank) { + endGame(game); + return; + } } - } while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, - game->board.nbPieces)); + } while (game->arrPlayers[game->currentPlayerID].rank != 0); fflush(stderr); diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index bdddc2a..f3ffddb 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -73,6 +73,9 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend case GameAction_MovePiece: drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, textureHandler.textures[TEXTURE_PieceRed], textureHandler.textures[TEXTURE_Island]); SDL_RenderPresent(renderer); + if (game.phase == GAME_ENDED) { + *generalState = GS_EndOfGameMenu; + } break; } @@ -98,6 +101,9 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend if (actionRealized != GameAction_None) { inputProcessor.selectedCase = newCoord(-1,-1); + if (game.phase == GAME_ENDED) { + *generalState = GS_EndOfGameMenu; + } } break; @@ -116,6 +122,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend freeTextureHandler(&textureHandler); array_Coord_Free(&interactiveCases); + freeGameInputProcessor(&inputProcessor); SDL_Quit(); } From abdb6656e83af7b2b9bce10161dcbce290b55ba9 Mon Sep 17 00:00:00 2001 From: marouault Date: Mon, 7 Feb 2022 23:01:30 +0100 Subject: [PATCH 08/11] ViewGame functionnal, color for player modified, bug with 3 players due to bad allocation -> use valgrind to try to find it (we need to be more carefull when we allocate space) --- Pontu/entryPoints/main.c | 8 ++--- Pontu/include/engine/ArrayUtils.h | 12 +++++-- Pontu/include/engine/UIElementUtils.h | 3 +- Pontu/include/model/Player.h | 7 +++-- Pontu/include/model/PlayersColors.h | 11 +++++++ Pontu/include/view/PiecesDrawer.h | 8 +++-- Pontu/src/engine/GameInputProcessor.c | 6 ++++ Pontu/src/engine/InputProcessor.c | 6 ++-- Pontu/src/engine/TextureHandler.c | 3 ++ Pontu/src/engine/UIElementUtils.c | 24 ++++++++++++-- Pontu/src/model/Game.c | 5 ++- Pontu/src/model/Player.c | 2 +- Pontu/src/view/GameDrawer.c | 4 +-- Pontu/src/view/GameMain.c | 45 ++++++++++++++++++--------- Pontu/src/view/PiecesDrawer.c | 14 ++++++--- 15 files changed, 116 insertions(+), 42 deletions(-) create mode 100644 Pontu/include/model/PlayersColors.h 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); } From d0500097c8f6e8e2513059ec81fa85f45786bf79 Mon Sep 17 00:00:00 2001 From: Allan POINT Date: Thu, 10 Feb 2022 19:10:54 +0100 Subject: [PATCH 09/11] Add CreationMenu (to be upgradeed (redraw permanetly)) (also, the numbre of player is not update on the scree) --- Pontu/entryPoints/test.c | 24 +- Pontu/include/engine/Colors.h | 4 + Pontu/include/view/GameCreationMenu.h | 47 +++- Pontu/src/engine/Colors.c | 5 + Pontu/src/view/GameCreationMenu.c | 349 ++++++++++++++++++++++++-- Pontu/test/testCreationMenu.c | 27 +- 6 files changed, 396 insertions(+), 60 deletions(-) diff --git a/Pontu/entryPoints/test.c b/Pontu/entryPoints/test.c index f431a15..87691ad 100644 --- a/Pontu/entryPoints/test.c +++ b/Pontu/entryPoints/test.c @@ -1,15 +1,16 @@ -// #include "../test/testTextureLoader.c" -// #include "../test/testFontLoader.c" -// #include "../test/testAudioHandler.c" -// #include "../test/testGenerateurTexture.c" -/*#include "../test/testButton.c" -#include "../test/testTextInput.c" -#include "../test/testConnectionMenu.c"*/ -#include "../test/testMenuEndGame.c" -/*#include "../test/testGameInterface.c" -#include "../test/testConnectionMenu.c"*/ +//#include "../test/testTextureLoader.c" +//#include "../test/testFontLoader.c" +//#include "../test/testAudioHandler.c" +//#include "../test/testGenerateurTexture.c" +//#include "../test/testButton.c" +//#include "../test/testTextInput.c" +//#include "../test/testConnectionMenu.c" +//#include "../test/testMenuEndGame.c" +//#include "../test/testGameInterface.c" +//#include "../test/testConnectionMenu.c" //#include "../test/testDrawMainMenu.c //#include "../test/testSettingsView.c" +#include "../test/testCreationMenu.c" /* This file is meant to be modified (used only to called other tests functions) */ @@ -22,10 +23,11 @@ int main(int argc, char *argv[]) { //testTextInput(); //testButtonTextureLoader(); //testConnectionMenu(); - testMenuEndGame(); + //testMenuEndGame(); //testGameInterface(); //testConnectionMenu(); //testDrawMainMenu(); + testCreationMenu(); //testSettingsView(); return 0; diff --git a/Pontu/include/engine/Colors.h b/Pontu/include/engine/Colors.h index d15dd7a..83c5e6b 100644 --- a/Pontu/include/engine/Colors.h +++ b/Pontu/include/engine/Colors.h @@ -7,5 +7,9 @@ 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_ONE_COLOR; +extern const SDL_Color PLAYER_TWO_COLOR; +extern const SDL_Color PLAYER_THREE_COLOR; +extern const SDL_Color PLAYER_FOUR_COLOR; #endif diff --git a/Pontu/include/view/GameCreationMenu.h b/Pontu/include/view/GameCreationMenu.h index 6e5300a..98522e1 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; + SDL_Color 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/src/engine/Colors.c b/Pontu/src/engine/Colors.c index 1375ae6..1204c9b 100644 --- a/Pontu/src/engine/Colors.c +++ b/Pontu/src/engine/Colors.c @@ -3,3 +3,8 @@ 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_ONE_COLOR = {166, 58, 252, 255}; +const SDL_Color PLAYER_TWO_COLOR = {14, 159, 100, 255}; +const SDL_Color PLAYER_THREE_COLOR = {240, 45, 45, 255}; +const SDL_Color PLAYER_FOUR_COLOR = {255, 255, 255, 255}; diff --git a/Pontu/src/view/GameCreationMenu.c b/Pontu/src/view/GameCreationMenu.c index 4cf9b85..5bc2912 100644 --- a/Pontu/src/view/GameCreationMenu.c +++ b/Pontu/src/view/GameCreationMenu.c @@ -1,14 +1,182 @@ #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; + SDL_Color const colors[NB_COLORS]= { + PLAYER_ONE_COLOR, + PLAYER_TWO_COLOR, + PLAYER_THREE_COLOR, + PLAYER_FOUR_COLOR + }; + 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=colors[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, colors[i], 4, 8, NULL, NULL, renderer); + btnTextureHover = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BACKGROUND, 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 +185,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/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); From a841982dc57782f5c77c27b0bbb5dabd560364f7 Mon Sep 17 00:00:00 2001 From: marouault Date: Sun, 13 Feb 2022 11:55:59 +0100 Subject: [PATCH 10/11] Modified GameCreationMenu to make it use PlayersColors (goal -> remove SDL_Colors from model (+easier for some functions)) --- Pontu/entryPoints/main.c | 11 ++++++----- Pontu/entryPoints/test.c | 6 +++--- Pontu/include/engine/Colors.h | 6 ++---- Pontu/include/model/PlayersColors.h | 17 +++++++++++++---- Pontu/include/view/GameCreationMenu.h | 2 +- Pontu/src/engine/Colors.c | 11 +++++++---- Pontu/src/model/PlayersColors.c | 6 ++++++ Pontu/src/view/GameCreationMenu.c | 13 ++++--------- 8 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 Pontu/src/model/PlayersColors.c diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index 387bc13..fe2dfdf 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -58,16 +58,17 @@ int main(int argc, char *argv[]) { size_t nbPlayers = 2; Player* players = (Player*)malloc(sizeof(Player)*2); - players[0] = newPlayer("Bépo", PlayerViolet); - players[1] = newPlayer("Azeryty", PlayerYellow); + /*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); + 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 5f8258d..4b2780a 100644 --- a/Pontu/entryPoints/test.c +++ b/Pontu/entryPoints/test.c @@ -7,7 +7,7 @@ //#include "../test/testDrawMainMenu.c //#include "../test/testSettingsView.c" #include "../test/testCreationMenu.c" -#include "../test/testGameInterface.c" +//#include "../test/testGameInterface.c" //#include "../test/testConnectionMenu.c" //#include "../test/testDrawMainMenu.c" //#include "../test/testSettingsView.c" @@ -26,10 +26,10 @@ int main(int argc, char *argv[]) { //testConnectionMenu(); //testMenuEndGame(); //testButton(); - testGameInterface(); + //testGameInterface(); //testConnectionMenu(); //testDrawMainMenu(); - //testCreationMenu(); + testCreationMenu(); //testSettingsView(); return 0; diff --git a/Pontu/include/engine/Colors.h b/Pontu/include/engine/Colors.h index 83c5e6b..e9b6df3 100644 --- a/Pontu/include/engine/Colors.h +++ b/Pontu/include/engine/Colors.h @@ -7,9 +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_ONE_COLOR; -extern const SDL_Color PLAYER_TWO_COLOR; -extern const SDL_Color PLAYER_THREE_COLOR; -extern const SDL_Color PLAYER_FOUR_COLOR; +extern const SDL_Color PLAYER_SDL_COLORS[]; + #endif diff --git a/Pontu/include/model/PlayersColors.h b/Pontu/include/model/PlayersColors.h index 50a3aa9..a824209 100644 --- a/Pontu/include/model/PlayersColors.h +++ b/Pontu/include/model/PlayersColors.h @@ -1,11 +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 { - PlayerRed, - PlayerViolet, - PlayerBlue, - PlayerYellow + 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 98522e1..eb7de43 100644 --- a/Pontu/include/view/GameCreationMenu.h +++ b/Pontu/include/view/GameCreationMenu.h @@ -30,7 +30,7 @@ typedef struct typedef struct { Player* p; - SDL_Color color; + PlayersColors color; } ChangeColorParams; typedef struct diff --git a/Pontu/src/engine/Colors.c b/Pontu/src/engine/Colors.c index 1204c9b..de1dfc5 100644 --- a/Pontu/src/engine/Colors.c +++ b/Pontu/src/engine/Colors.c @@ -4,7 +4,10 @@ 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_ONE_COLOR = {166, 58, 252, 255}; -const SDL_Color PLAYER_TWO_COLOR = {14, 159, 100, 255}; -const SDL_Color PLAYER_THREE_COLOR = {240, 45, 45, 255}; -const SDL_Color PLAYER_FOUR_COLOR = {255, 255, 255, 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/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/GameCreationMenu.c b/Pontu/src/view/GameCreationMenu.c index 5bc2912..327b75f 100644 --- a/Pontu/src/view/GameCreationMenu.c +++ b/Pontu/src/view/GameCreationMenu.c @@ -107,12 +107,7 @@ CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int int const hColorBtn = 32; int const colorBtnXMargin = 8; SDL_Texture* btnTexture, *btnTextureHover; - SDL_Color const colors[NB_COLORS]= { - PLAYER_ONE_COLOR, - PLAYER_TWO_COLOR, - PLAYER_THREE_COLOR, - PLAYER_FOUR_COLOR - }; + P_Button* colorsBtn = (P_Button*) malloc(sizeof(P_Button)*NB_COLORS); P_Button ai = createButton(NULL, NULL, xmin, y, 0, 0, NULL); @@ -134,11 +129,11 @@ CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int { params = (ChangeColorParams*) malloc(sizeof(ChangeColorParams)); params->p = player; - params->color=colors[i]; + 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, colors[i], 4, 8, NULL, NULL, renderer); - btnTextureHover = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BACKGROUND, colors[i], 4, 8, NULL, NULL, renderer); + 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; From 1a29afc70a21270452731a2a329f6231febce30b Mon Sep 17 00:00:00 2001 From: marouault Date: Sun, 13 Feb 2022 13:00:28 +0100 Subject: [PATCH 11/11] Updated Game.c to set correctly rank and elminationTurn for player. Updated GameMain to make MenuEndGame work (a little fix will be made to resize but it's functionnal) --- Pontu/entryPoints/main.c | 18 ++++++++++-------- Pontu/include/model/Player.h | 2 +- Pontu/src/model/Game.c | 23 +++++++++-------------- Pontu/src/model/Player.c | 2 +- Pontu/src/view/GameMain.c | 7 +++++-- Pontu/src/view/MenuEndGame.c | 17 +++++++++++++++-- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index fe2dfdf..39d6a33 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -57,19 +57,21 @@ int main(int argc, char *argv[]) { SDL_GetWindowSize(window, &windowW, &windowH); size_t nbPlayers = 2; - Player* players = (Player*)malloc(sizeof(Player)*2); - /*players[0] = newPlayer("Bépo", PlayerViolet); - players[1] = newPlayer("Azeryty", PlayerYellow);*/ - //players[2] = newPlayer("Adcsg", PlayerRed); + Player players[] = { + newPlayer("Bépo", PlayerViolet), + newPlayer("Azeryty", PlayerYellow), + //newPlayer("Adcsg", PlayerRed) + }; + //players[2] = ; - bool crashed = gameCreationMenu(renderer, &generalState, &fontHandler, windowW, windowH, &players, &nbPlayers); + //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); endGameMenu(&generalState, window, renderer, &fontHandler, players, nbPlayers); diff --git a/Pontu/include/model/Player.h b/Pontu/include/model/Player.h index c818567..2dc3b4f 100644 --- a/Pontu/include/model/Player.h +++ b/Pontu/include/model/Player.h @@ -21,7 +21,7 @@ typedef struct { char pseudo[PSEUDO_LENMAX]; //< The player's pseudo PlayersColors color; //< The player's Piece' color - int rank; //< The player's rank (0 if the player isn't out yet) + 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; diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index e1168b4..396d5d3 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -38,7 +38,7 @@ Game newGame(const size_t nbPlayers, const Player player[]) .phase = PLACEMENT, .board = newBoard(nbPlayers), .nbPlayers = nbPlayers, - .lastRank = 0 + .lastRank = nbPlayers+1 }; for (size_t player_i = 0; player_i < nbPlayers; player_i++) @@ -59,9 +59,8 @@ Game newGame(const size_t nbPlayers, const Player player[]) void eliminatePlayer(Game* game, const size_t playerId) { game->arrPlayers[playerId].eliminationTurn = game->nb_rounds; - ++game->lastRank; - game->arrPlayers[playerId].rank = game->lastRank; - fprintf(stderr, "Rank : %d\n", game->lastRank); + game->arrPlayers[playerId].rank = --game->lastRank; + fprintf(stderr, "Rank : %d, %d\n", game->lastRank, game->arrPlayers[playerId].rank); } void endGame(Game* game) { @@ -90,7 +89,7 @@ void changePhaseOrPlayerTurn(Game* game) const size_t lastPlayerId = game->currentPlayerID; if (areAllPlayerPiecesStucked(lastPlayerId, game->board.arrPieces, game->board.nbPieces)) { eliminatePlayer(game, lastPlayerId); - if (game->nbPlayers-1 == game->lastRank) { + if (game->lastRank == 2) { endGame(game); return; } @@ -108,15 +107,15 @@ void changePhaseOrPlayerTurn(Game* game) return; }*/ - if (game->arrPlayers[game->currentPlayerID].rank != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) { + if (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) { eliminatePlayer(game, game->currentPlayerID); - if (game->nbPlayers-1 == game->lastRank) { + if (game->lastRank == 2) { endGame(game); return; } } - } while (game->arrPlayers[game->currentPlayerID].rank != 0); + } while (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0); fflush(stderr); @@ -124,6 +123,7 @@ void changePhaseOrPlayerTurn(Game* game) if (anyOfPlayersPiecesCanMove(game->currentPlayerID, &game->board)) { game->phase = MOVE_PIECE; + game->nb_rounds++; } break; } @@ -286,13 +286,8 @@ 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 - } } } diff --git a/Pontu/src/model/Player.c b/Pontu/src/model/Player.c index c1055e3..33b62ff 100644 --- a/Pontu/src/model/Player.c +++ b/Pontu/src/model/Player.c @@ -4,7 +4,7 @@ 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/view/GameMain.c b/Pontu/src/view/GameMain.c index c1f20b3..18e7505 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -137,9 +137,12 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend SDL_Delay(5); } + for (size_t i = 0; ifonts[FONT_retro])); array_PositionSpecifier_AddElement(&labels.positionSpecifiers, getTitleRect100(&array_TextLabel_Last(&labels.textLabels)->textZone)); + + return labels; } @@ -200,6 +202,12 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r }; 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); }