PositionAdapter for redimensionnable window

origin/fixingSettings
marouault 3 years ago
parent 755106d512
commit 0db8531880

@ -16,7 +16,7 @@
* \enum InputType * \enum InputType
* \brief Different types for input * \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 * \enum UIAction
@ -43,6 +43,10 @@ typedef struct {
} move; ///< Pair of coordinates for move on the board } move; ///< Pair of coordinates for move on the board
UIAction uiAction; ///< L'action UIAction uiAction; ///< L'action
struct windowSize {
int w;
int h;
} windowSize; ///< La nouvelle taille de l'ecran
} data; ///< Informations about the input } data; ///< Informations about the input
InputType type; ///< Type of input InputType type; ///< Type of input
@ -75,4 +79,11 @@ InputElement createInputElementClickBoard(const Coord newCoord);
*/ */
InputElement createInputElementMoveBoard(const Coord start, const Coord end); 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 #endif // INPUT_ELEMENT_INCLUDED

@ -4,18 +4,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include "engine/FontLoader.h" #include "engine/FontLoader.h"
#include "engine/UIElementUtils.h"
typedef enum {
POSX_LEFT,
POSX_CENTER,
POSX_RIGHT
} POSITIONX_TYPE;
typedef enum {
POSY_TOP,
POSY_CENTER,
POSY_BOTTOM
} POSITIONY_TYPE;
typedef struct typedef struct
{ {
@ -25,7 +14,8 @@ typedef struct
SDL_Texture* texture; SDL_Texture* texture;
}TextLabel; }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 freeTextLabel(TextLabel* label);
void drawTextLabel(SDL_Renderer* renderer, TextLabel* label); void drawTextLabel(SDL_Renderer* renderer, TextLabel* label);

@ -0,0 +1,35 @@
#ifndef UI_ELEMENT_UTILS_INCLUDED
#define UI_ELEMENT_UTILS_INCLUDED
#include <SDL2/SDL.h>
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

@ -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

@ -0,0 +1,9 @@
#ifndef ARRAY_SDL_RECT_INCLUDED
#define ARRAY_SDL_RECT_INCLUDED
#include <SDL2/SDL_rect.h>
#include "engine/ArrayUtils.h"
GENERATE_DYNAMIC_ARRAY(SDL_Rect)
#endif //ARRAY_SDL_RECT_INCLUDED

@ -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

@ -9,6 +9,7 @@
#include "engine/TextureLoader.h" #include "engine/TextureLoader.h"
#include "engine/GeneralState.h" #include "engine/GeneralState.h"
/** /**
* @brief Handle end game menu * @brief Handle end game menu
* *

@ -22,3 +22,11 @@ InputElement createInputElementMoveBoard(const Coord start, const Coord end) {
InputElement i = {.type=InputType_MoveGame, .data.move={.start=start, .end=end}}; InputElement i = {.type=InputType_MoveGame, .data.move={.start=start, .end=end}};
return i; return i;
} }
InputElement createInputElementResizeWindow(const int w, const int h) {
InputElement i = {
.type = InputType_Window_Resize,
.data.windowSize={.w = w, .h = h}
};
return i;
}

@ -43,6 +43,10 @@ InputElement proccessInput(InputProcessor *inputProcessor)
} }
break; break;
} }
case SDL_WINDOWEVENT_SIZE_CHANGED:
return createInputElementResizeWindow(event.window.data1, event.window.data2);
break;
} }
return createInputElementNone(); return createInputElementNone();

@ -3,23 +3,8 @@
#include "engine/TextureLoader.h" #include "engine/TextureLoader.h"
#include <errno.h> #include <errno.h>
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) { void attachTextureToTextLabel(SDL_Renderer* renderer, TextLabel* label, TTF_Font* font) {
TextLabel label = { SDL_Surface* surface = TTF_RenderText_Solid(font, label->text, label->color);
.color = *color,
.texture = NULL
};
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);
{
SDL_Surface* surface = TTF_RenderText_Solid(font, label.text, label.color);
if(surface == NULL) if(surface == NULL)
{ {
@ -27,8 +12,8 @@ TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float f
fflush(stderr); fflush(stderr);
} }
else { else {
label.texture = SDL_CreateTextureFromSurface(renderer, surface); label->texture = SDL_CreateTextureFromSurface(renderer, surface);
if(label.texture == NULL) if(label->texture == NULL)
{ {
fprintf(stderr, "WARNING: Can't create texture from surface: %s\n", SDL_GetError()); fprintf(stderr, "WARNING: Can't create texture from surface: %s\n", SDL_GetError());
fflush(stderr); fflush(stderr);
@ -37,6 +22,44 @@ TextLabel createTextLabel(const char text[], const SDL_Point* pos, const float f
} }
} }
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);
}
TextLabel createUnsizedTextLabel(const char text[], const SDL_Color* color, TTF_Font* font, SDL_Renderer* renderer) {
TextLabel label = {
.color = color,
.texture = NULL
};
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.w = calculateStringPixelLenght(font, label.text)*factorSize;
label.textZone.h = TTF_FontHeight(font)*factorSize; label.textZone.h = TTF_FontHeight(font)*factorSize;

@ -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;
}

@ -4,6 +4,15 @@
#include <errno.h> #include <errno.h>
#include "engine/Colors.h" #include "engine/Colors.h"
#include "engine/InputProcessor.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 * @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 * @param font Font used for title
*/ */
void drawTitle(SDL_Renderer* renderer, const SDL_Rect* rect, TTF_Font* font) { 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,8 +147,36 @@ void drawEndGameMenu(SDL_Renderer* renderer, const Player players[], const size_
drawPlayersScores(renderer, players, nbPlayers, rect, fontHandler->fonts[FONT_retro]); drawPlayersScores(renderer, players, nbPlayers, rect, fontHandler->fonts[FONT_retro]);
} }
void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, FontHandler* fontHandler, const Player players[], const size_t nbPlayers) { 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 windowW;
int windowH; int windowH;
@ -160,7 +193,12 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
InputProcessor inputProcessor = createInputProcessor(); InputProcessor inputProcessor = createInputProcessor();
array_P_Button_AddElement(&inputProcessor.tabButton, createButtonForEndGameMenu(renderer, fontHandler->fonts[FONT_retro], &rectMenuEndGame, generalState)); array_P_Button_AddElement(&inputProcessor.tabButton, createButtonForEndGameMenu(renderer, fontHandler->fonts[FONT_retro], &rectMenuEndGame, generalState));
P_Button* buttonMenuEndGame = array_P_Button_Last(&inputProcessor.tabButton); 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); drawEndGameMenu(renderer, players, nbPlayers, &rectMenuEndGame, fontHandler);
while(*generalState == GS_EndOfGameMenu) while(*generalState == GS_EndOfGameMenu)
@ -185,6 +223,19 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
break; break;
} }
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: default:
break; break;
} }
@ -193,6 +244,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
drawButtonOnRenderer(renderer, buttonMenuEndGame); drawButtonOnRenderer(renderer, buttonMenuEndGame);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(50); SDL_Delay(50);
} }

@ -18,7 +18,7 @@ void testMenuEndGame() {
} }
//fenetre //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) if(window == NULL)
{ {
fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError()); fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError());

Loading…
Cancel
Save