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());