Merge branch 'master' of gitlab.iut-clermont.uca.fr:/maribemont/projet-tut

origin/fixingSettings
Mathis RIBEMONT 3 years ago
commit 06f903e9f5

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <SDL2/SDL.h>
#include "engine/GeneralState.h" #include "engine/GeneralState.h"
#include "view/MainMenu.h" #include "view/MainMenu.h"
#include "view/MenuEndGame.h" #include "view/MenuEndGame.h"
@ -8,7 +9,7 @@
#include "engine/FontLoader.h" #include "engine/FontLoader.h"
#include "model/Player.h" #include "model/Player.h"
int main(int argc, char const *argv[]) { int main(int argc, char *argv[]) {
GeneralState generalState; GeneralState generalState;
SDL_Window* window = NULL; SDL_Window* window = NULL;
@ -22,7 +23,7 @@ int main(int argc, char const *argv[]) {
goto Quit; 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) if (!window)
{ {
fprintf(stderr, "Error : %s\n", SDL_GetError()); fprintf(stderr, "Error : %s\n", SDL_GetError());
@ -43,7 +44,7 @@ int main(int argc, char const *argv[]) {
FontHandler fontHandler = loadFonts(); FontHandler fontHandler = loadFonts();
AudioHandler audioHandler = newAudioHandler(128, 128, 128); AudioHandler audioHandler = newAudioHandler(128, 128, 128);
generalState = GS_MainMenu; generalState = GS_GameCreationMenu;
while(generalState != GS_Quit){ while(generalState != GS_Quit){
switch (generalState) { switch (generalState) {
case GS_MainMenu: case GS_MainMenu:
@ -56,17 +57,20 @@ int main(int argc, char const *argv[]) {
SDL_GetWindowSize(window, &windowW, &windowH); SDL_GetWindowSize(window, &windowW, &windowH);
size_t nbPlayers = 2; size_t nbPlayers = 2;
SDL_Color color = {0,0,0,0}; Player players[] = {
Player* players = (Player*)malloc(sizeof(Player)*2); newPlayer("Bépo", PlayerViolet),
players[0] = newPlayer("Bépo", color); newPlayer("Azeryty", PlayerYellow),
players[1] = newPlayer("Azeryty", color); //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"); fprintf(stderr,"sorry");
exit(-1); exit(-1);
}*/ }*/
generalState = GS_Game;
gameView(&generalState, window, renderer, players, nbPlayers); gameView(&generalState, window, renderer, players, nbPlayers);

@ -2,11 +2,12 @@
// #include "../test/testFontLoader.c" // #include "../test/testFontLoader.c"
// #include "../test/testAudioHandler.c" // #include "../test/testAudioHandler.c"
// #include "../test/testGenerateurTexture.c" // #include "../test/testGenerateurTexture.c"
//#include "../test/testButton.c" //#include "../test/testGameInterface.c"
//#include "../test/testTextInput.c"
//#include "../test/testConnectionMenu.c" //#include "../test/testConnectionMenu.c"
//#include "../test/testMenuEndGame.c" //#include "../test/testDrawMainMenu.c
#include "../test/testGameInterface.c" //#include "../test/testSettingsView.c"
#include "../test/testCreationMenu.c"
//#include "../test/testGameInterface.c"
//#include "../test/testConnectionMenu.c" //#include "../test/testConnectionMenu.c"
//#include "../test/testDrawMainMenu.c" //#include "../test/testDrawMainMenu.c"
//#include "../test/testSettingsView.c" //#include "../test/testSettingsView.c"
@ -25,9 +26,10 @@ int main(int argc, char *argv[]) {
//testConnectionMenu(); //testConnectionMenu();
//testMenuEndGame(); //testMenuEndGame();
//testButton(); //testButton();
testGameInterface(); //testGameInterface();
//testConnectionMenu(); //testConnectionMenu();
//testDrawMainMenu(); //testDrawMainMenu();
testCreationMenu();
//testSettingsView(); //testSettingsView();
return 0; return 0;

@ -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) { \ inline void array_##T##_Reserve(struct array_##T* array, const size_t space) { \
array->space = space; \ array->space = space; \
array->elems = realloc(array->elems, sizeof(T)*(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); \
} \
} \ } \
\ \
/*Fit space to size for an array*/\ /*Fit space to size for an array*/\
inline void array_##T##_FitToSize(struct array_##T* array) { \ inline void array_##T##_FitToSize(struct array_##T* array) { \
array->space = array->size; \ array->space = array->size; \
array->elems = realloc(array->elems, sizeof(T)*(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); \
} \
} \ } \
\ \
/*Add an element to an array*/\ /*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) { \ if (array->size > array->space) { \
++(array->space); \ ++(array->space); \
array->elems = realloc(array->elems, sizeof(T)*(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; \ array->elems[array->size - 1] = element; \

@ -7,5 +7,7 @@ extern const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND;
extern const SDL_Color COLOR_GENERIC_BUTTON_BORDER; extern const SDL_Color COLOR_GENERIC_BUTTON_BORDER;
extern const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER; extern const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER;
extern const SDL_Color PLAYER_SDL_COLORS[];
#endif #endif

@ -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,36 @@
#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,
ASPECT_KEEP_FIT
} AspectRatioType;
typedef struct {
SDL_Rect base100;
PositionX_Type tpX;
PositionY_Type tpY;
AspectRatioType aspectType;
} PositionSpecifier;
PositionSpecifier newPositionSpecifier(const SDL_Rect* const base100, const PositionX_Type tpX, const PositionY_Type tpY, const AspectRatioType aspectType);
SDL_Rect adaptPosToRect(const PositionSpecifier *const positionSpecifier, const SDL_Rect *const globalRect);
#endif //UI_ELEMENT_UTILS_INCLUDED

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

@ -28,6 +28,13 @@ typedef enum {
GAME_ENDED GAME_ENDED
} Phase; } Phase;
typedef enum {
GameAction_None,
GameAction_PlacePiece,
GameAction_MovePiece,
GameAction_RemoveBridge,
} GameAction;
/** /**
* \struct Game * \struct Game
* \brief Represents a game * \brief Represents a game
@ -40,6 +47,7 @@ typedef struct {
Player arrPlayers[4]; ///< The array of all the players in this game Player arrPlayers[4]; ///< The array of all the players in this game
size_t nbPlayers; size_t nbPlayers;
Board board; ///< The board for this game Board board; ///< The board for this game
int lastRank;
} Game; } Game;
/** /**
@ -158,18 +166,18 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is
* \param start Board coord were the move started * \param start Board coord were the move started
* \param end Board coord were the move ended * \param end Board coord were the move ended
* \param game Game's state * \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 * \brief Handle global game action click
* *
* \param [in] coord Board coord were the click is * \param [in] coord Board coord were the click is
* \param [in, out] game Game's state * \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) * \brief Remove bridge from board at (coord->x; coord->y)

@ -10,17 +10,18 @@
#define PSEUDO_LENMAX 50 #define PSEUDO_LENMAX 50
#include <SDL2/SDL_pixels.h> #include "model/PlayersColors.h"
#include <string.h> #include <string.h>
/** /**
* \struct Player * \struct Player
* \brief Player during a game * \brief Player during a game
*/ */
typedef struct { typedef struct {
char pseudo[PSEUDO_LENMAX]; //< The player's pseudo 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 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) int eliminationTurn; //< When the player has been eliminated (0 if the player isn't out yet)
} Player; } Player;
@ -30,7 +31,7 @@ typedef struct {
* \param[in] color The color of the new Player's Piece * \param[in] color The color of the new Player's Piece
* \return A struct representing the new Player * \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 #endif // JOUEUR_H

@ -0,0 +1,20 @@
#ifndef PLAYERS_COLORS_INCLUDED
#define PLAYERS_COLORS_INCLUDED
#define MACRO_FOR_EACH_PLAYER_COLOR(M) \
M(Red) \
M(Violet) \
M(Blue) \
M(Yellow)
#define MACRO_GEN_ENUM_PLAYER_COLOR(Elem) Player##Elem,
typedef enum {
MACRO_FOR_EACH_PLAYER_COLOR(MACRO_GEN_ENUM_PLAYER_COLOR)
NB_PLAYER_COLORS
} PlayersColors;
extern const PlayersColors playersColors[];
#endif //PLAYERS_COLORS_INCLUDED

@ -5,5 +5,50 @@
#include "engine/TextureLoader.h" #include "engine/TextureLoader.h"
#include "engine/Colors.h" #include "engine/Colors.h"
#include "engine/FontUtils.h" #include "engine/FontUtils.h"
#include "engine/GeneralState.h"
#include "engine/InputProcessor.h"
#include "engine/Colors.h"
#include "model/Player.h"
#include "engine/TextInput.h"
#define NB_COLORS 4
#define NB_PLAYER_MAX 4
/*
bool drawGameCreationMenu(SDL_Renderer* renderer, P_Button* incrementBtn, P_Button* decrementBtn, TextLabel* nbJoueurTxtLabel, TextLabel* nbPlayerLabel);
*/
typedef struct
{
P_Button aiButton;
P_Button* colorButtons;
TextInput pseudoInput;
Player* player;
}CreateMenuLine;
typedef struct
{
Player* p;
PlayersColors color;
} ChangeColorParams;
typedef struct
{
int* nbPlayers;
TTF_Font* font;
int minx;
int maxx;
int miny;
CreateMenuLine* lines;
SDL_Renderer* renderer;
}IncrementParams;
typedef struct
{
int* nbPlayers;
CreateMenuLine* lines;
SDL_Renderer* renderer;
}DecrementParams;
bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height); bool gameCreationMenu(SDL_Renderer* renderer, GeneralState* generalState, TTF_Font* font, int width, int height);

@ -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); int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler);
#endif #endif //MAIN_MENU_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
* *

@ -4,10 +4,16 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h> #include <stdbool.h>
#include "model/Piece.h" #include "model/Piece.h"
#include "model/Coord.h"
#include "model/PlayersColors.h"
#include "engine/TextureHandler.h"
void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece); void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, const TextureHandler* textureHandler, const PlayersColors color);
void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Island* startMove, const Island* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture); void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, const TextureHandler* textureHandler, const PlayersColors color);
void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const TextureHandler* textureHandler, const PlayersColors color, const Coord* coordPlace);
#endif //PIECES_DRAWER_INCLUDED #endif //PIECES_DRAWER_INCLUDED

@ -0,0 +1,10 @@
#ifndef TO_RECT_INCLUDED
#define TO_RECT_INCLUDED
#include <SDL2/SDL_rect.h>
#include "model/Coord.h"
SDL_Rect coordToRect(const SDL_Rect* boardRect, const Coord* coord);
#endif //TO_RECT_INCLUDED

@ -3,3 +3,11 @@
const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND = {225, 225, 225, 255}; 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_BORDER = {10, 10, 10, 255};
const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER = {250, 250, 250, 255}; const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND_HOVER = {250, 250, 250, 255};
const SDL_Color PLAYER_SDL_COLORS[] = {//Order mater (need to be the same as in model/PlayersColors.h)
{241, 0, 4 , 255}, // Red
{175, 0, 202, 255}, // Violet
{6 , 0, 202, 255}, // Blue
{202, 152, 0, 255}, // Yellow
};

@ -83,7 +83,13 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL
P_Button* b = &gameInputProcessor->tabButton.elems[i]; P_Button* b = &gameInputProcessor->tabButton.elems[i];
isButtonEntry(b, event.motion.x, event.motion.y); 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(); return createInputElementNone();

@ -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,12 @@ InputElement proccessInput(InputProcessor *inputProcessor)
} }
break; break;
} }
case SDL_WINDOWEVENT:
if (event.window.event == 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,15 +12,53 @@ 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);
} }
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
} }
}
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;

@ -15,6 +15,9 @@ TextureHandler newTextureHandler(SDL_Renderer* renderer) {
for (size_t i = 0; i<NB_TEXTURES_DEFINED; ++i) { for (size_t i = 0; i<NB_TEXTURES_DEFINED; ++i) {
char* path = (char*)malloc((strlen(directoryPath)+strlen(texturesNames[i])+1)*sizeof(char)); char* path = (char*)malloc((strlen(directoryPath)+strlen(texturesNames[i])+1)*sizeof(char));
if (path == NULL) {
perror("Malloc newTextureHandler"); exit(errno);
}
strcpy(path, directoryPath); strcpy(path, directoryPath);
tH.textures[i] = createTextureFromPath(renderer, strcat(path,texturesNames[i])); tH.textures[i] = createTextureFromPath(renderer, strcat(path,texturesNames[i]));
free(path); free(path);

@ -0,0 +1,90 @@
#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;
case ASPECT_KEEP_FIT: {
const int preferedW = globalRect->w * positionSpecifier->base100.w/100.0;
const int preferedH = globalRect->h * positionSpecifier->base100.h/100.0;
const int associatedH = preferedW * positionSpecifier->base100.h/positionSpecifier->base100.w;
const int associatedW = preferedH * positionSpecifier->base100.w/positionSpecifier->base100.h;
if (associatedH > preferedH) {
r.h = preferedH;
r.w = associatedW;
}
else {
r.w = preferedW;
r.h = associatedH;
}
}
break;
default:
break;
}
switch (positionSpecifier->tpX)
{
case POSX_LEFT:
r.x = globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0;
break;
case POSX_CENTER:
r.x = (globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0) - r.w/2;
break;
case POSX_RIGHT:
r.x = (globalRect->x + globalRect->w * positionSpecifier->base100.x/100.0) - r.w;
break;
default:
break;
}
switch (positionSpecifier->tpY)
{
case POSY_TOP:
r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0;
break;
case POSY_CENTER:
r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h/2;
break;
case POSY_BOTTOM:
r.y = globalRect->y + globalRect->h * positionSpecifier->base100.y/100.0 - r.h;
break;
default:
break;
}
return r;
}

@ -37,12 +37,16 @@ Game newGame(const size_t nbPlayers, const Player player[])
.nb_rounds = 0, .nb_rounds = 0,
.phase = PLACEMENT, .phase = PLACEMENT,
.board = newBoard(nbPlayers), .board = newBoard(nbPlayers),
.nbPlayers = nbPlayers .nbPlayers = nbPlayers,
.lastRank = nbPlayers+1
}; };
for (size_t player_i = 0; player_i < nbPlayers; player_i++) 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) if (nbPlayers == 2)
@ -53,6 +57,15 @@ Game newGame(const size_t nbPlayers, const Player player[])
return g; return g;
} }
void eliminatePlayer(Game* game, const size_t playerId) {
game->arrPlayers[playerId].eliminationTurn = game->nb_rounds;
game->arrPlayers[playerId].rank = --game->lastRank;
fprintf(stderr, "Rank : %d, %d\n", game->lastRank, game->arrPlayers[playerId].rank);
}
void endGame(Game* game) {
game->phase = GAME_ENDED;
}
void changePhaseOrPlayerTurn(Game* game) void changePhaseOrPlayerTurn(Game* game)
{ {
@ -74,6 +87,14 @@ void changePhaseOrPlayerTurn(Game* game)
case RM_BRIDGE: case RM_BRIDGE:
{ {
const size_t lastPlayerId = game->currentPlayerID; const size_t lastPlayerId = game->currentPlayerID;
if (areAllPlayerPiecesStucked(lastPlayerId, game->board.arrPieces, game->board.nbPieces)) {
eliminatePlayer(game, lastPlayerId);
if (game->lastRank == 2) {
endGame(game);
return;
}
}
do do
{ {
game->currentPlayerID++; game->currentPlayerID++;
@ -81,21 +102,28 @@ void changePhaseOrPlayerTurn(Game* game)
{ {
game->currentPlayerID = 0; game->currentPlayerID = 0;
} }
if (lastPlayerId == game->currentPlayerID) { /*if (lastPlayerId == game->currentPlayerID) {
game->phase = GAME_ENDED; game->phase = GAME_ENDED;
return; return;
}*/
if (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) {
eliminatePlayer(game, game->currentPlayerID);
if (game->lastRank == 2) {
endGame(game);
return;
}
} }
} while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, } while (game->arrPlayers[game->currentPlayerID].eliminationTurn != 0);
game->board.nbPieces));
fprintf(stderr, "Player n°%ld turn\n", game->currentPlayerID);
fflush(stderr); fflush(stderr);
if (anyOfPlayersPiecesCanMove(game->currentPlayerID, &game->board)) if (anyOfPlayersPiecesCanMove(game->currentPlayerID, &game->board))
{ {
game->phase = MOVE_PIECE; game->phase = MOVE_PIECE;
game->nb_rounds++;
} }
break; break;
} }
@ -258,17 +286,12 @@ void updatePieceIsolated(Game* game, const Island* island)
{ {
Piece* piecePotentialyIsolated = getPieceFromIsland(game->board.arrPieces, game->board.nbPieces, *island); Piece* piecePotentialyIsolated = getPieceFromIsland(game->board.arrPieces, game->board.nbPieces, *island);
if (piecePotentialyIsolated != NULL && isPieceIsolated(piecePotentialyIsolated, &game->board)) 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; piecePotentialyIsolated->stuck = true;
if (areAllPlayerPiecesStucked(piecePotentialyIsolated->idJ, game->board.arrPieces,
game->board.nbPieces))
{
game->arrPlayers[piecePotentialyIsolated->idJ].rank = game->nb_rounds; // TODO : See what we put in rank
}
} }
} }
bool clickOnBoard(const Coord coord, Game* game) GameAction clickOnBoard(const Coord coord, Game* game)
{ {
const IslandOrBridge islandOrBridge = coordToEntity(coord); const IslandOrBridge islandOrBridge = coordToEntity(coord);
@ -281,7 +304,7 @@ bool clickOnBoard(const Coord coord, Game* game)
if (piece != NULL) { if (piece != NULL) {
if (placePiece(piece, islandOrBridge.data.island, &game->board)) { if (placePiece(piece, islandOrBridge.data.island, &game->board)) {
changePhaseOrPlayerTurn(game); changePhaseOrPlayerTurn(game);
return true; return GameAction_PlacePiece;
} }
} }
} }
@ -296,7 +319,7 @@ bool clickOnBoard(const Coord coord, Game* game)
changePhaseOrPlayerTurn(game); changePhaseOrPlayerTurn(game);
return true; return GameAction_RemoveBridge;
} }
} }
break; break;
@ -304,7 +327,7 @@ bool clickOnBoard(const Coord coord, Game* game)
break; break;
} }
return false; return GameAction_None;
} }
Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Island island) Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Island island)
@ -319,7 +342,7 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is
return NULL; 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 islandOrBridgeStart = coordToEntity(start);
const IslandOrBridge islandOrBridgeEnd = coordToEntity(end); const IslandOrBridge islandOrBridgeEnd = coordToEntity(end);
@ -340,7 +363,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game)
{ {
changePhaseOrPlayerTurn(game); changePhaseOrPlayerTurn(game);
} }
return pieceMoved; return pieceMoved ? GameAction_MovePiece : GameAction_None;
} }
} }
break; break;
@ -348,7 +371,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game)
break; break;
} }
return false; return GameAction_None;
} }
bool rmBridge(Bridge bridge, Board* board) bool rmBridge(Bridge bridge, Board* board)

@ -1,10 +1,10 @@
#include "model/Player.h" #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; Player player;
strcpy(player.pseudo, pseudo); strcpy(player.pseudo, pseudo);
player.color = color; player.color = color;
player.rank = 0; player.rank = 1;
player.eliminationTurn = 0; player.eliminationTurn = 0;
return player; return player;

@ -0,0 +1,6 @@
#include "model/PlayersColors.h"
const PlayersColors playersColors[] = {
MACRO_FOR_EACH_PLAYER_COLOR(MACRO_GEN_ENUM_PLAYER_COLOR)
};

@ -1,22 +1,9 @@
#include "view/BoardDrawer.h" #include "view/BoardDrawer.h"
#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;
}
void drawRemoveBridge(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* water, const Coord* coordBridge) { void drawRemoveBridge(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* water, const Coord* coordBridge) {
const SDL_Rect destRect = coordToRect(boardRect, coordBridge); const SDL_Rect destRect = coordToRect(boardRect, coordBridge);
SDL_RenderCopy(renderer, water, &destRect, boardRect); SDL_RenderCopy(renderer, water, NULL, &destRect);
} }
bool drawFullBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water) bool drawFullBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water)

@ -1,14 +1,177 @@
#include "view/GameCreationMenu.h" #include "view/GameCreationMenu.h"
bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height) #include <SDL2/SDL_ttf.h>
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(&params->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; i<nbButtons; ++i)
{
if(!drawButtonOnRenderer(renderer, &(buttons[i])))
{
fprintf(stderr, "WARNING: Can't draw button\n");
return false;
}
}
for(int i=0; i<nbLabels; ++i)
{
drawTextLabel(renderer, labels[i]);
}
for(int i=0; i<nbPlayer; ++i)
{
drawCreateMenuLine(renderer, &lines[i]);
}
SDL_RenderPresent(renderer);
return true;
}
void createPlayersLines(SDL_Renderer* renderer, TTF_Font* font, int minx, int maxx, int miny,int nbPlayer, CreateMenuLine* lines)
{
Player players[nbPlayer];
for(int i=0; i<nbPlayer; ++i)
{
if(i==0)
{
lines[i] = createCreateMenuLine(renderer, minx, miny + 16, maxx,font, &players[i]);
}else{
lines[i] = createCreateMenuLine(renderer, minx, miny + 16 + 16 + i* lines[i-1].aiButton.rect.h, maxx,font, &players[i]);
}
}
return;
}
bool drawCreateMenuLine(SDL_Renderer* renderer, CreateMenuLine* line)
{
drawButtonOnRenderer(renderer, &line->aiButton);
for(int i=0; i<NB_COLORS; ++i)
{
drawButtonOnRenderer(renderer, &line->colorButtons[i]);
}
drawTextInputOnRenderer(renderer, &line->pseudoInput);
return true;
}
CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int xmax, TTF_Font* font, Player* player)
{
int const wColorBtn = 32;
int const hColorBtn = 32;
int const colorBtnXMargin = 8;
SDL_Texture* btnTexture, *btnTextureHover;
P_Button* colorsBtn = (P_Button*) malloc(sizeof(P_Button)*NB_COLORS);
P_Button ai = createButton(NULL, NULL, xmin, y, 0, 0, NULL);
CreateMenuLine line;
SDL_Texture* aiTexture = createGenericButtonTexture("X", font, 16, COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(ai.rect.w), &(ai.rect.h), renderer);
SDL_Texture* aiTextureHovered = createGenericButtonTexture("X", font, 16, COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &ai.rect.w, &ai.rect.h, renderer);
ai.texture = aiTexture;
ai.hoverTexture = aiTextureHovered;
SDL_Rect rect = {.x = xmin + ai.rect.x + ai.rect.w, .y = y, .h = TTF_FontHeight(font),.w = xmax - ai.rect.w - NB_COLORS * (wColorBtn + colorBtnXMargin*2)- 16};
TextInput pseudoInput;
initTextInput(&pseudoInput, &rect, NULL, font);
ChangeColorParams* params;
for(int i=0; i<NB_COLORS; ++i)
{
params = (ChangeColorParams*) malloc(sizeof(ChangeColorParams));
params->p = player;
params->color=playersColors[i];
colorsBtn[i] = createButton(NULL, NULL, xmax-wColorBtn*(i+1), y, wColorBtn, hColorBtn, changePlayerColor);
colorsBtn[i].arg = params;
btnTexture = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BORDER, PLAYER_SDL_COLORS[i], 4, 8, NULL, NULL, renderer);
btnTextureHover = createGenericButtonTexture("", font, 0, COLOR_GENERIC_BUTTON_BACKGROUND, PLAYER_SDL_COLORS[i], 4, 8, NULL, NULL, renderer);
colorsBtn[i].texture = btnTexture;
colorsBtn[i].hoverTexture = btnTextureHover;
}
line.aiButton=ai;
line.colorButtons=colorsBtn;
line.player=player;
line.pseudoInput=pseudoInput;
return line;
}
void freeCreateMenuLine(CreateMenuLine* line)
{
destroyTextInput(&line->pseudoInput);
freeButton(&line->aiButton);
for(int i=0; i<NB_COLORS; ++i)
{
free(line->colorButtons[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 // TextLabel for "Nombre de joueur.euse.s" creation
SDL_Point nbJoueurLabelPos = {.x=width*0.05, .y=height*0.05}; SDL_Point titleLabelPos = {.x=width*0.05, .y=height*0.05};
SDL_Color black = {0,0,0,255};
SDL_Color white = {225, 255, 255, 255}; SDL_Color white = {225, 255, 255, 255};
TextLabel nbJoueurLabel = createTextLabel( CreateMenuLine lines[NB_PLAYER_MAX];
"Nombre de joueur.euse.s", TextLabel titleLabel = createTextLabel(
&nbJoueurLabelPos, "Nombre de joueur·euse·s",
&titleLabelPos,
1, 1,
&white, &white,
font, font,
@ -17,52 +180,181 @@ bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int
POSY_TOP POSY_TOP
); );
// Incrementation Btn creation // Decrementation Btn creation
P_Button decrementBtn = createButton(NULL, NULL, titleLabelPos.x+calculateStringPixelLenght(font, titleLabel.text)+32, titleLabelPos.y, 0, 0, &decrementNbPlayer);
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, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer);
SDL_Texture* btnTexture = createGenericButtonTexture("+", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer);
if(btnTexture == NULL) if(btnTexture == NULL)
{ {
fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError()); fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError());
return false; 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) if(btnHoveredTexture == NULL)
{ {
fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError()); fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError());
return false; return false;
} }
incrementBtn.texture = btnTexture; decrementBtn.texture = btnTexture;
incrementBtn.hoverTexture = btnHoveredTexture; decrementBtn.hoverTexture = btnHoveredTexture;
// 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
);
// 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);
// 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) if(btnTexture == NULL)
{ {
fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError()); fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError());
return false; 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) if(btnHoveredTexture == NULL)
{ {
fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError()); fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError());
return false; return false;
} }
decrementBtn.texture = btnTexture; incrementBtn.texture = btnTexture;
decrementBtn.hoverTexture = btnHoveredTexture; incrementBtn.hoverTexture = btnHoveredTexture;
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);
}
//Draw everything
drawButtonOnRenderer(renderer, &incrementBtn);
drawButtonOnRenderer(renderer, &decrementBtn);
drawTextLabel(renderer, &nbJoueurLabel);
//free //free
freeTextLabel(&nbJoueurLabel); freeInputProcessor(&inputProcessor);
freeTextLabel(&titleLabel);
freeButton(&incrementBtn); freeButton(&incrementBtn);
freeButton(&decrementBtn);
return true; return true;
} }

@ -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]); //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]); drawPiecesPlayer(renderer, boardRect, game->board.arrPieces, game->board.nbPieces, 1, textureHandler->textures[TEXTURE_PieceViolet]);
*/
return true; return true;
} }

@ -52,17 +52,31 @@ void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler)
SDL_Color menuBackgroundColor = {0,255,0,255}; SDL_Color menuBackgroundColor = {0,255,0,255};
//Postion text label //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 *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 *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); 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 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 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) 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); //bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button);
@ -108,12 +122,17 @@ void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler)
drawButtonOnRenderer(renderer,&menuButton); drawButtonOnRenderer(renderer,&menuButton);
drawButtonOnRenderer(renderer,&settingButton); drawButtonOnRenderer(renderer,&settingButton);
drawButtonOnRenderer(renderer,&soundButton); drawButtonOnRenderer(renderer,&soundButton);
drawTextLabel(renderer,&nbTurnLabel);
drawTextLabel(renderer,&timeLabel);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(20); SDL_Delay(20);
} }
//FREE TEXT LABEL + BUTTON
} }

@ -5,22 +5,35 @@
#include "engine/TextureHandler.h" #include "engine/TextureHandler.h"
#include "model/Game.h" #include "model/Game.h"
#include "model/arrayCoord.h" #include "model/arrayCoord.h"
#include "debug/printer.h"
#include "view/PiecesDrawer.h" #include "view/PiecesDrawer.h"
#include "view/BoardDrawer.h" #include "view/BoardDrawer.h"
#include "view/GameDrawer.h" #include "view/GameDrawer.h"
SDL_Rect boardRectFromWindowSize(int windowW, int windowH) { #include "engine/UIElementUtils.h"
SDL_Rect boardRect = {.x=windowW/10.0, .y=windowH/10, .w=windowW*8.0/10.0, .h=windowH*8.0/10.0};
return boardRect;
PositionSpecifier boardRectPositionSpecifier() {
SDL_Rect b100 = {
.x= 50,
.y= 50,
.w= 80,
.h= 80,
};
return newPositionSpecifier(&b100, POSX_CENTER, POSY_CENTER, ASPECT_KEEP_FIT);
}
void redrawGameBoard(SDL_Renderer* renderer, const Player players[], const size_t nbPlayers, const TextureHandler* textureHandler, const SDL_Rect* boardRect, const Board* board) {
drawFullBoard(renderer, boardRect, board, textureHandler->textures[TEXTURE_Island], textureHandler->textures[TEXTURE_Bridge], textureHandler->textures[TEXTURE_Water]);
for (size_t iPlayer=0; iPlayer<nbPlayers; ++iPlayer) {
drawPiecesPlayer(renderer, boardRect, board->arrPieces, board->nbPieces, iPlayer, textureHandler, players[iPlayer].color);
}
} }
void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, Player players[], size_t nbPlayers) void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, Player players[], size_t nbPlayers)
{ {
if (*generalState != GS_Game) { if (*generalState != GS_Game) {
return ; return;
} }
GameInputProcessor inputProcessor = createGameInputProcessor(); GameInputProcessor inputProcessor = createGameInputProcessor();
struct array_Coord interactiveCases = array_Coord_Create(); struct array_Coord interactiveCases = array_Coord_Create();
@ -28,19 +41,15 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
Game game = newGame(nbPlayers, players); Game game = newGame(nbPlayers, players);
TextureHandler textureHandler = newTextureHandler(renderer); TextureHandler textureHandler = newTextureHandler(renderer);
int windowW;
int windowH;
SDL_GetWindowSize(window, &windowW, &windowH); SDL_Rect windowRect = {0,0,0,0};
SDL_Rect boardRect = boardRectFromWindowSize(windowW, windowH); SDL_GetWindowSize(window, &windowRect.w, &windowRect.h);
PositionSpecifier boardRPositionSpecifier = boardRectPositionSpecifier();
//Draw SDL_Rect boardRect = adaptPosToRect(&boardRPositionSpecifier, &windowRect);
drawFullBoard(renderer, &boardRect, &game.board, textureHandler.textures[TEXTURE_Island], textureHandler.textures[TEXTURE_Bridge], textureHandler.textures[TEXTURE_Water]);
for (int iPlayer=0; iPlayer<nbPlayers; ++iPlayer) {
drawPiecesPlayer(renderer, &boardRect, game.board.arrPieces, game.board.nbPieces, iPlayer, textureHandler.textures[TEXTURE_PieceRed]);
//SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece
}
//Draw
redrawGameBoard(renderer, game.arrPlayers, game.nbPlayers, &textureHandler, &boardRect, &game.board);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
@ -66,31 +75,56 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
break; break;
} }
break; break;
case InputType_MoveGame: case InputType_MoveGame: {
fprintf(stderr, "Move on board\n"); const GameAction actionRealized = moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game);
fprintf(stderr, "From (%d; %d)\n", inputElement.data.move.start.x, inputElement.data.move.start.y); switch (actionRealized)
fprintf(stderr, "To (%d; %d)\n", inputElement.data.move.end.x, inputElement.data.move.end.y); {
case GameAction_MovePiece:
moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game); drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, &textureHandler, game.arrPlayers[game.currentPlayerID].color);
SDL_RenderPresent(renderer);
if (game.phase == GAME_ENDED) {
*generalState = GS_EndOfGameMenu;
}
break; 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);
break;
}
case InputType_ClickGame: {
if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) { if(!array_Coord_Contains(&interactiveCases, inputElement.data.coord, *coordEqual)) {
fprintf(stderr, "\tselected case reset\n");
inputProcessor.selectedCase = newCoord(-1,-1); inputProcessor.selectedCase = newCoord(-1,-1);
} }
if (clickOnBoard(inputElement.data.coord, &game)) { const GameAction actionRealized = clickOnBoard(inputElement.data.coord, &game);
fprintf(stderr, "\tselected case reset\n"); switch (actionRealized)
inputProcessor.selectedCase = newCoord(-1,-1); {
case GameAction_PlacePiece:
drawPlacePiece(renderer, &boardRect, &textureHandler, game.arrPlayers[(game.currentPlayerID-1>0) ? game.currentPlayerID-1 : game.nbPlayers-1].color, &inputElement.data.coord);
SDL_RenderPresent(renderer);
break;
case GameAction_RemoveBridge:
drawRemoveBridge(renderer, &boardRect, textureHandler.textures[TEXTURE_Water], &inputElement.data.coord);
SDL_RenderPresent(renderer);
break;
} }
if (actionRealized != GameAction_None) {
inputProcessor.selectedCase = newCoord(-1,-1);
if (game.phase == GAME_ENDED) {
*generalState = GS_EndOfGameMenu;
}
}
break; 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: case InputType_None:
default: default:
break; break;
@ -98,16 +132,17 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend
array_Coord_Free(&interactiveCases); array_Coord_Free(&interactiveCases);
interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase); interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase);
fprintf(stderr, "Interactive cases : {");
array_Coord_Foreach(&interactiveCases, *printCoord);
fprintf(stderr, "}\n");
} }
SDL_Delay(20); SDL_Delay(5);
}
for (size_t i = 0; i<nbPlayers; ++i) {
players[i].rank = game.arrPlayers[i].rank;
players[i].eliminationTurn = game.arrPlayers[i].eliminationTurn;
} }
freeTextureHandler(&textureHandler); freeTextureHandler(&textureHandler);
array_Coord_Free(&interactiveCases); array_Coord_Free(&interactiveCases);
freeGameInputProcessor(&inputProcessor);
SDL_Quit();
} }

@ -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,6 +147,37 @@ 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]);
} }
TextLabel createTitleLabel(SDL_Renderer* renderer, TTF_Font* font) {
SDL_Color color = {0,0,0,0};
return createUnsizedTextLabel("Scores", &color, font, renderer);
}
PositionSpecifier getTitleRect100(SDL_Rect* labelSize) {
SDL_Rect base100 = {
.x=50,
.y=1,
.w = 30,
.h = 30*labelSize->h/labelSize->w
};
return newPositionSpecifier(&base100, POSX_CENTER, POSY_TOP, ASPECT_KEEP_W);
}
struct endGameMenuTextLabel createLabels(SDL_Renderer* renderer, const Player players[], const size_t nbPlayers, FontHandler* fontHandler) {
struct endGameMenuTextLabel labels = {
.textLabels = array_TextLabel_Create(),
.positionSpecifiers = array_PositionSpecifier_Create()
};
// Titre
array_TextLabel_AddElement(&labels.textLabels, createTitleLabel(renderer, fontHandler->fonts[FONT_retro]));
array_PositionSpecifier_AddElement(&labels.positionSpecifiers, getTitleRect100(&array_TextLabel_Last(&labels.textLabels)->textZone));
return labels;
}
void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, FontHandler* fontHandler, const Player players[], const size_t nbPlayers) { 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;
@ -158,9 +194,20 @@ 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);
struct endGameMenuTextLabel labels = createLabels(renderer, players, nbPlayers, fontHandler);
for (size_t i=0; i<labels.textLabels.size; ++i) {
labels.textLabels.elems[i].textZone = adaptPosToRect(&labels.positionSpecifiers.elems[i], &rectMenuEndGame);
drawTextLabel(renderer, &labels.textLabels.elems[i]);
}
while(*generalState == GS_EndOfGameMenu) while(*generalState == GS_EndOfGameMenu)
{ {
{ {
@ -183,6 +230,24 @@ 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);
for (size_t i=0; i<labels.textLabels.size; ++i) {
labels.textLabels.elems[i].textZone = adaptPosToRect(&labels.positionSpecifiers.elems[i], &rectM);
drawTextLabel(renderer, &labels.textLabels.elems[i]);
}
//buttonMenuEndGame->rect = adaptPosToRect(&base100, &rectM, POSX_CENTER, POSY_BOTTOM, ASPECT_KEEP_W);
fprintf(stderr, "Resize\n"); fflush(stderr);
}
default: default:
break; break;
} }
@ -191,6 +256,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);
} }

@ -1,37 +1,35 @@
#include "view/PiecesDrawer.h" #include "view/PiecesDrawer.h"
#include "model/Island.h" #include "view/ToRect.h"
//Don't put this in model 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_Rect islandToRect(const SDL_Rect* boardRect, const Island* island) { SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color];
const int w = boardRect->w/9;
const int h = boardRect->h/9;
SDL_Rect r = {
.x = boardRect->x + w*(island->x*2),
.y = boardRect->y + h*(island->y*2),
.w = w,
.h = h
};
return r;
}
void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece) {
for (size_t i = 0; i < nbPieces; ++i) for (size_t i = 0; i < nbPieces; ++i)
{ {
if (arrPieces[i].idJ == numPlayer) { if (arrPieces[i].idJ == numPlayer) {
const SDL_Rect rDest = islandToRect(boardRect, &arrPieces[i].island); Coord c = islandToCoord(&arrPieces[i].island);
SDL_RenderCopy(renderer, piece, NULL, &rDest); const SDL_Rect rDest = coordToRect(boardRect, &c);
SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest);
} }
} }
} }
void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Island* startMove, const Island* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture) { void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Coord* startMove, const Coord* endMove, const TextureHandler* textureHandler, const PlayersColors color) {
SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color];
SDL_Rect rDest = coordToRect(boardRect, startMove);
SDL_RenderCopy(renderer, textureHandler->textures[TEXTURE_Island], NULL, &rDest);
rDest = coordToRect(boardRect, endMove);
SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest);
}
SDL_Rect rDest = islandToRect(boardRect, startMove); void drawPlacePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const TextureHandler* textureHandler, const PlayersColors color, const Coord* coordPlace) {
SDL_RenderCopy(renderer, islandTexture, NULL, &rDest); SDL_Texture* pieceTexture = textureHandler->textures[TEXTURE_PieceRed+color];
rDest = islandToRect(boardRect, endMove); SDL_Rect rDest = coordToRect(boardRect, coordPlace);
SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest); SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest);
} }

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

@ -13,6 +13,7 @@ int testCreationMenu(void) {
char* path = "rsrc/img/Lenna.png"; char* path = "rsrc/img/Lenna.png";
int i=0; int i=0;
int w, h; int w, h;
int nbPlayer=1;
if(0 != SDL_Init(SDL_INIT_VIDEO)) if(0 != SDL_Init(SDL_INIT_VIDEO))
{ {
fprintf(stderr, "Erreur SDL_INIT: %s\n", SDL_GetError()); fprintf(stderr, "Erreur SDL_INIT: %s\n", SDL_GetError());
@ -42,7 +43,7 @@ int testCreationMenu(void) {
goto Quit; 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()); fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
goto Quit; goto Quit;
@ -61,30 +62,12 @@ int testCreationMenu(void) {
} }
SDL_bool quit = SDL_FALSE;
SDL_Event event;
FontHandler fontHandler = loadFonts(); FontHandler fontHandler = loadFonts();
SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSize(window, &w, &h);
while(!quit) GeneralState generalState = GS_GameCreationMenu;
{ printf("%d/%d\n", w, h);
while(SDL_PollEvent(&event)) gameCreationMenu(renderer, &generalState, fontHandler.fonts[FONT_retro], w, h);
{
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);
}
Quit: Quit:
freeFonts(fontHandler); freeFonts(fontHandler);

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