origin/fixingSettings
marouault 4 years ago
commit dd93f9508a

@ -2,7 +2,11 @@
#include <stdlib.h>
#include "engine/GeneralState.h"
#include "view/MainMenu.h"
#include "view/MenuEndGame.h"
#include "view/GameCreationMenu.h"
#include "view/GameMain.h"
#include "engine/FontLoader.h"
#include "model/Player.h"
int main(int argc, char const *argv[]) {
GeneralState generalState;
@ -37,13 +41,41 @@ int main(int argc, char const *argv[]) {
exit(2);
}
FontHandler fontHandler = loadFonts();
AudioHandler audioHandler = newAudioHandler(128, 128, 128);
generalState = GS_Quit;
generalState = GS_GameCreationMenu;
while(generalState != GS_Quit){
switch (generalState) {
case GS_MainMenu:
mainMenu(renderer,window,&generalState, fontHandler);
break;
case GS_MainMenu:
mainMenu(renderer,window,&generalState, fontHandler, audioHandler);
break;
case GS_GameCreationMenu:{
int windowW;
int windowH;
SDL_GetWindowSize(window, &windowW, &windowH);
size_t nbPlayers = 2;
SDL_Color color = {0,0,0,0};
Player* players = (Player*)malloc(sizeof(Player)*2);
players[0] = newPlayer("Bépo", color);
players[1] = newPlayer("Azeryty", color);
//bool crashed = gameCreationMenu(renderer, &generalState, &fontHandler, windowW, windowH, &players, &nbPlayers);
/* if (crashed) {
fprintf(stderr,"sorry");
exit(-1);
}*/
generalState = GS_Game;
gameView(&generalState, window, renderer, players, nbPlayers);
endGameMenu(&generalState, window, renderer, &fontHandler, players, nbPlayers);
break;
}
case GS_Game: {
break;
}
}
}

@ -2,14 +2,15 @@
// #include "../test/testFontLoader.c"
// #include "../test/testAudioHandler.c"
// #include "../test/testGenerateurTexture.c"
/*#include "../test/testButton.c"
#include "../test/testTextInput.c"
#include "../test/testConnectionMenu.c"*/
#include "../test/testMenuEndGame.c"
/*#include "../test/testGameInterface.c"
#include "../test/testConnectionMenu.c"*/
//#include "../test/testDrawMainMenu.c
//#include "../test/testButton.c"
//#include "../test/testTextInput.c"
//#include "../test/testConnectionMenu.c"
//#include "../test/testMenuEndGame.c"
#include "../test/testGameInterface.c"
//#include "../test/testConnectionMenu.c"
//#include "../test/testDrawMainMenu.c"
//#include "../test/testSettingsView.c"
//#include "../test/oldMain__ThisCanBeGameMain.c"
/*
This file is meant to be modified (used only to called other tests functions)
*/
@ -22,8 +23,9 @@ int main(int argc, char *argv[]) {
//testTextInput();
//testButtonTextureLoader();
//testConnectionMenu();
testMenuEndGame();
//testGameInterface();
//testMenuEndGame();
//testButton();
testGameInterface();
//testConnectionMenu();
//testDrawMainMenu();
//testSettingsView();

@ -31,7 +31,8 @@
* \sa #MACRO_FOR_ALL_MUSICS(M)
*/
#define MACRO_FOR_ALL_SFX(M) \
M(testClick)
M(testClick) \
M(menu_sound_effect)
/**
* Macro used to generate the entries for the musics in #EnumAudios.

@ -54,11 +54,18 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button);
/**
* \brief Test if a point is on a button
* \param[in] button the button target
* \return true if the cursor is on the button
*/
bool isHover(P_Button* button);// dit si le bouton est survolé en donnant les coordonnées x,y
/**
* \brief Test if the cursor has just entered the button.
* \param[in] button the button target
* \param[in] x x of the point
* \param[in] y y of the point
* \return SDL_TRUE if the point is on the button
* \return true if the cursor has juste entered the button.
*/
bool isHover(P_Button* button, int x,int y);// dit si le bouton est survolé en donnant les coordonnées x,y
bool isButtonEntry(P_Button * button,const int x,const int y);
/**
* \brief Free the texture of a button.

@ -15,7 +15,7 @@
#include "model/Coord.h"
#include "model/arrayCoord.h"
#include <SDL2/SDL_pixels.h>
#include <stdbool.h>
#include <stdbool.h>
/**
* \enum Phase
@ -38,7 +38,7 @@ typedef struct {
//TODO duree
Phase phase; ///< The current state of the 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
} Game;
@ -47,19 +47,19 @@ typedef struct {
* \param[in] nbPlayers The number of players for this game
* \return A struct representing the game
*/
Game newGame(const size_t nbPlayers, const char* pseudos[]);
Game newGame(const size_t nbPlayers, const Player player[]);
/**
* \brief (Should not be called outside Game.c) Used to change phase or player (or both) after an action
*
*
* \param [in, out] game The game to mutate
*/
void changePhaseOrPlayerTurn(Game* game);
/**
* \brief Place a piece into the board
*
*
* \param [in, out] p The piece to place
* \param [in] island The island where the piece is placed
* \param [in] b The board in which the piece is placed
@ -78,20 +78,20 @@ bool movePiece(Piece* p, const Island i, const Board* b);
/**
* \brief Test if a movement is possible for a piece to a destination island
*
*
* \param p The piece to test
* \param i The destination island
* \param i The destination island
* \param b The board
* \return true if the piece p can move to the island i
* \return true if the piece p can move to the island i
*/
bool pieceCanMoveTo(const Piece* p, const Island i, const Board* b);
/**
* \brief Check if an island is empty
*
* \brief Check if an island is empty
*
* \param [in] island The island to check
* \param [in] arrPieces the array of piece from the board
* \param [in] nbPieces number of pieces
* \param [in] nbPieces number of pieces
* \return true if none of the pieces is on th island, false otherwise
*/
bool isIslandEmpty(const Island island, const Piece arrPieces[], const size_t nbPieces);
@ -107,16 +107,16 @@ bool isPieceAdjacentToIsland(const Piece p, const Island i);
/**
* \brief test if a piece is isolated
*
* \param [in] piece The piece which is checked
*
* \param [in] piece The piece which is checked
* \param [in] board The board were the piece is
* \return true if no bridge go from piece's island to another island, false otherwise
*/
bool isPieceIsolated(const Piece* piece, const Board* board);
/**
* \brief test if all pieces from a player are stucked
*
* \brief test if all pieces from a player are stucked
*
* \param idJ Player id
* \param arrPieces All pieces from board
* \param nbPieces Number of pieces
@ -126,10 +126,10 @@ bool areAllPlayerPiecesStucked(const size_t idJ, const Piece arrPieces[], const
/**
* \brief Test if one piece of a player can move
*
*
* \param playerID The player to check
* \param board The board
* \return true if at least one of player's piece can move
* \return true if at least one of player's piece can move
*/
bool anyOfPlayersPiecesCanMove(const size_t playerID, const Board* board);
@ -154,7 +154,7 @@ Piece* getPieceFromIsland(Piece arrPieces[9], const size_t logicalSize, const Is
/**
* \brief Handle global game action move
*
*
* \param start Board coord were the move started
* \param end Board coord were the move ended
* \param game Game's state
@ -164,7 +164,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game);
/**
* \brief Handle global game action click
*
*
* \param [in] coord Board coord were the click is
* \param [in, out] game Game's state
* \return true if an action was realised, false otherwise
@ -181,7 +181,7 @@ bool rmBridge(Bridge bridge, Board* board);
/**
* \brief List cases that can be interacted with for movement
*
*
* \param[in] game The game
* \param[in] selectedCase The selected case
* \return struct array_Coord An array of coord /!\ Care to free this array with array_Coord_Free
@ -189,4 +189,3 @@ bool rmBridge(Bridge bridge, Board* board);
struct array_Coord getInteractiveCases(const Game* const game, const Coord selectedCase);
#endif //GAME_H

@ -11,6 +11,7 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "model/Board.h"
#include "model/Coord.h"
/**
* \brief Draw the board (water, islands and bridges)
@ -23,7 +24,9 @@
* \param water Texture for water
* \return true I don't know what to return
*/
bool drawBoard(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);
void drawRemoveBridge(SDL_Renderer* renderer, const SDL_Rect* boardRect, SDL_Texture* water, const Coord* coordBridge);
#endif

@ -0,0 +1,39 @@
/**
* \file GameInterface.h
* \breif Interface of game
* \author Jacques Thomas
* \date 24/01/20222
*/
#ifndef GAME_INTERFACE_INCLUDED
#define GAME_INTERFACE_INCLUDED
#include <SDL2/SDL.h>
#include "engine/Button.h"
#include <engine/FontLoader.h>
//move pion
//delete pion
//draw menu Ponton (top left corner)
/**
* \brief Draw different buttons on the game interface : menu, setting, sound, nbTurn, and timers
* param Renderer
*/
void drawButtons(SDL_Renderer* renderer,FontHandler fontHandler);
//draw setting button (top right corner)
//draw sound button (top right corner)
//draw nbTurn (bottom left corner)
//draw timer (bottom right corner)
#endif

@ -0,0 +1,10 @@
#ifndef GAME_MAIN_INCLUDED
#define GAME_MAIN_INCLUDED
#include "engine/GeneralState.h"
#include <SDL2/SDL.h>
#include "model/Player.h"
void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, Player players[], size_t nbPlayers);
#endif //GAME_MAIN_INCLUDED

@ -7,9 +7,10 @@
#include "engine/TextureLoader.h"
#include "engine/FontLoader.h"
#include "engine/GeneralState.h"
#include "engine/AudioHandler.h"
P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize);
P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize, GeneralState* generalState);
int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler);
int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler);
#endif

@ -7,4 +7,7 @@
void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, const size_t numPlayer, SDL_Texture* piece);
#endif //PIECES_DRAWER_INCLUDED
void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Island* startMove, const Island* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture);
#endif //PIECES_DRAWER_INCLUDED

@ -2,7 +2,7 @@
// A channel represents the number of SFX we can play at the same time.
// We normally should use only 1 channel, and we add one for safety.
#define NBCHANNELS 2
#define NBCHANNELS 10
// Local functions
@ -19,7 +19,7 @@ int fadeOut(void* args) {
// casting args to a pointer to Mix_Music
Mix_Music* music = (Mix_Music*)args;
int ret;
if(Mix_FadeOutMusic(500) == 1) { // Starting the fadeout
while (Mix_PlayingMusic()) {
; // Waiting until it's done
@ -60,7 +60,7 @@ AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) {
audioHandler.masterVol = masterVol;
fprintf(stderr,"Musics: %d\nSFX: %d\n",NB_MUSIC_DEFINED,nb_SFX);
// Loading musics
for (size_t i = 0; i < NB_MUSIC_DEFINED; i++) {
audioHandler.musics[i] = Mix_LoadMUS(musicsPaths[i]);
@ -140,13 +140,13 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) {
fprintf(stderr,"WARNING: tried to play an arbitrary value as a music\n");
return;
}
// Checking if audio has been opened.
if (!(audioHandler.canPlayAudio)) {
fprintf(stderr,"WARNING: tried to play a music with an unusable AudioHandler\n");
return;
}
// If another music is playing, fading the previous one out
if (Mix_PlayingMusic()) {
// Creating the thread, passing the music as parameter
@ -180,7 +180,7 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) {
fprintf(stderr,"WARNING: tried to play an SFX with an unusable AudioHandler\n");
return;
}
// Getting actual chunk
chunkSFX = audioHandler.sfx[sfx - NB_MUSIC_DEFINED - 1];
// Getting first available channel
@ -196,4 +196,3 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) {
return;
}
}

@ -27,12 +27,8 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button)
return true;
}
bool isHover(P_Button* button,const int x,const int y)
bool isHover(P_Button* button)
{
SDL_Point coord;
coord.x = x;
coord.y = y;
button->hover = SDL_PointInRect(&coord,&(button->rect));
return button->hover && button->drawn;
}
@ -60,3 +56,15 @@ void freeButton(P_Button * button){
SDL_DestroyTexture(button->texture);
SDL_DestroyTexture(button->hoverTexture);
}
bool isButtonEntry(P_Button * button,const int x,const int y){
SDL_Point coord;
coord.x = x;
coord.y = y;
if(isHover(button)){
button->hover = SDL_PointInRect(&coord,&(button->rect));
return false;
}
button->hover = SDL_PointInRect(&coord,&(button->rect));
return button->hover;
}

@ -81,11 +81,10 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL
{
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
P_Button* b = &gameInputProcessor->tabButton.elems[i];
isHover(b, event.motion.x, event.motion.y);
isButtonEntry(b, event.motion.x, event.motion.y);
}
}
}
return createInputElementNone();
}

@ -26,7 +26,7 @@ InputElement proccessInput(InputProcessor *inputProcessor)
case SDL_MOUSEBUTTONUP:
{
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) {
P_Button* b = &inputProcessor->tabButton.elems[i];
if (SDL_PointInRect(&mousePoint, &b->rect)) {
@ -39,7 +39,7 @@ InputElement proccessInput(InputProcessor *inputProcessor)
{
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) {
P_Button* b = &inputProcessor->tabButton.elems[i];
isHover(b, event.motion.x, event.motion.y);
isButtonEntry(b, event.motion.x, event.motion.y);
}
break;
}
@ -51,4 +51,3 @@ InputElement proccessInput(InputProcessor *inputProcessor)
return createInputElementNone();
}

@ -30,7 +30,7 @@ void applySpecificRulesFor2PlayersGame(Game* g)
}
}
Game newGame(const size_t nbPlayers, const char* pseudos[])
Game newGame(const size_t nbPlayers, const Player player[])
{
Game g = { // In Placement phase, the last player initialized is the 1st to play
.currentPlayerID = nbPlayers - 1,
@ -40,13 +40,9 @@ Game newGame(const size_t nbPlayers, const char* pseudos[])
.nbPlayers = nbPlayers
};
// red, green, blue, yellow
// TODO meilleures couleurs (?)
SDL_Color colors[4] = { { 255, 0, 0, 255 }, { 0, 255, 0, 255 }, { 0, 0, 255, 255 }, { 255, 255, 0, 255 } };
for (size_t player_i = 0; player_i < nbPlayers; player_i++)
{
g.arrPlayers[player_i] = newPlayer(pseudos[player_i], colors[player_i]);
g.arrPlayers[player_i] = player[player_i];
}
if (nbPlayers == 2)
@ -72,8 +68,8 @@ void changePhaseOrPlayerTurn(Game* game)
game->currentPlayerID--;
}
break;
case MOVE_PIECE:
game->phase = RM_BRIDGE;
case MOVE_PIECE:
game->phase = RM_BRIDGE;
break;
case RM_BRIDGE:
{
@ -93,7 +89,7 @@ void changePhaseOrPlayerTurn(Game* game)
} while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces,
game->board.nbPieces));
fprintf(stderr, "Player n°%ld turn\n", game->currentPlayerID);
fflush(stderr);
@ -103,7 +99,7 @@ void changePhaseOrPlayerTurn(Game* game)
}
break;
}
default:
default:
break;
}
}
@ -243,7 +239,7 @@ bool anyOfPlayersPiecesCanMove(const size_t playerID, const Board* board) {
size_t nbNeighbors;
Island* neighbors = islandsAround(board->arrPieces[i].island, &nbNeighbors);
for (size_t n = 0; n < nbNeighbors; ++n)
{
{
if (board->arrPieces[i].idJ == playerID && pieceCanMoveTo(&board->arrPieces[i], neighbors[n], board)) {
return true;
}
@ -304,7 +300,7 @@ bool clickOnBoard(const Coord coord, Game* game)
}
}
break;
default:
default:
break;
}
@ -348,7 +344,7 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game)
}
}
break;
default:
default:
break;
}
@ -385,7 +381,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec
case PLACEMENT: {
struct array_Coord retVal = array_Coord_Create();
array_Coord_Reserve(&retVal, 25);
for (int y = 0; y<5; y+=2) {
for (int x = 0; x<5; x+=2) {
array_Coord_AddElement(&retVal, newCoord(x,y));
@ -398,7 +394,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec
array_Coord_RemoveElement(&retVal, islandToCoord(&game->board.arrPieces[i].island), &coordEqual);
}
}
array_Coord_FitToSize(&retVal);
return retVal;
@ -412,7 +408,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec
if (game->board.arrPieces[i].idJ == game->currentPlayerID && !game->board.arrPieces[i].stuck) {
size_t nbIsland;
Island* islands = islandsAround(game->board.arrPieces[i].island, &nbIsland);
if (nbIsland != 0) {
Coord pieceCoord = islandToCoord(&game->board.arrPieces[i].island);
if (!coordValid(selectedCase)) {
@ -442,7 +438,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec
case RM_BRIDGE: {
struct array_Coord retVal = array_Coord_Create();
array_Coord_Reserve(&retVal, 40);
for (size_t y = 0; y<5; ++y) {
for (size_t x = 0; x<4; ++x) {
if (game->board.hBridges[y][x]) {
@ -459,7 +455,7 @@ struct array_Coord getInteractiveCases(const Game* const game, const Coord selec
}
}
}
return retVal;
}
default:

@ -1,12 +1,44 @@
#include "view/BoardDrawer.h"
bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water)
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) {
const SDL_Rect destRect = coordToRect(boardRect, coordBridge);
SDL_RenderCopy(renderer, water, &destRect, boardRect);
}
bool drawFullBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water)
{
int h = boardRect->h / 9;
int w = boardRect->w / 9;
SDL_RenderCopy(renderer, water, NULL, boardRect);
//Water (Possible to optimize)
for (size_t y = 0; y < 9; ++y)
{
for (size_t x = 0; x < 9; ++x)
{
const SDL_Rect destRect = {
.x = boardRect->x+x*w,
.y = boardRect->y+y*h,
.w = w,
.h = h,
};
SDL_RenderCopy(renderer, water, NULL, &destRect);
}
}
//Islands
for (int y=0; y<9; y+=2) {

@ -16,7 +16,7 @@ bool drawGame(SDL_Renderer* renderer, const SDL_Rect* windowSize, const SDL_Rect
//P_Button menu = createButton(menuTexture, NULL, 10, 10, 50, 70, NULL);
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]);

@ -0,0 +1,138 @@
#include "view/GameInterface.h"
#include "engine/TextureLoader.h"
#include <SDL2/SDL_ttf.h>
#include <engine/FontLoader.h>
#include <engine/TextLabel.h>
//void action boutton
void action(P_Button* buttonCaller){
printf("Bouton menu\n");
//changeButtonTexture(arg->buttonCaller,arg->texture);
}
void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler)
{
//DRAW MENU BUTTON (TOP RIGHT CORNER)
SDL_bool quit = SDL_FALSE;
SDL_Event event;
int sizex=20,sizey=20;
/*
if(TTF_Init() == -1)
{
fprintf(stderr, "Erreur d'inistialisation de TTF_Init : %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
TTF_Font* retroFont = NULL;
//load ttf
int fontSize = 100;
int size = fontSize*100/88;
retroFont=TTF_OpenFont("rsrc/font/retro/retro.TTF", size);
if(!retroFont) {
printf("TTF_OpenFont: %s\n", TTF_GetError());
// handle error
}
*/
//FontHandler fontHandler=loadFonts();
//Menu Button's colors
SDL_Color menuBorderColor= {0,0,255,255};
SDL_Color menuBackgroundColor = {0,255,0,255};
//Postion text label
SDL_Point positonNbTurnLabel = {.x=60, .y=800};
SDL_Point positionTimeLablel = {.x=770, .y=800};
//Color labal
SDL_Color colorLabel = {0, 255, 0, 255};
//Position label
POSITIONX_TYPE positionX = POSX_CENTER;
POSITIONY_TYPE positionY = POSY_CENTER;
//SDL_Texture *buttonTexture = createGenericButtonTexture("Menu", NULL, 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer);
SDL_Texture *menuButtonTexture = createGenericButtonTexture("Menu", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBorderColor,24,5,&sizex,&sizey,renderer);
SDL_Texture *menuButtonHoverTexture = createGenericButtonTexture("MenuHover", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer);
//Buttons
P_Button menuButton = createButton(menuButtonTexture, menuButtonHoverTexture,20,20,100,50,&action); //top left corner (rectangle)
P_Button settingButton = createButton(menuButtonTexture, menuButtonHoverTexture, 750,10,50,50,&action); //top right corner (square or circle)
P_Button soundButton = createButton(menuButtonTexture, menuButtonHoverTexture, 825,10,50,50,&action); //top right cornre (square or circle)
//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);
//P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(P_ButtonArg* arg));
//Create Button Texture
//SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int fontSize, SDL_Color border_color, SDL_Color background_color,int thickness, int padding, int* sizex, int* sizey, SDL_Renderer* renderer);
SDL_Texture* violetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,20,20);
SDL_SetRenderDrawColor(renderer, 150,75,200,255);
SDL_SetRenderTarget(renderer, violetTexture);
//SDL_RenderFillRect(renderer, &buttonRect);
SDL_SetRenderDrawColor(renderer,255,0,0,0);
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderClear(renderer);
while(!quit)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit = SDL_TRUE;
break;
case SDL_MOUSEBUTTONUP:
//if(isHover(&menuButton,event.button.x,event.button.y))
if(isHover(&menuButton))
menuButton.onClick(&menuButton);
break;
case SDL_MOUSEMOTION:
//on vérifie à chaque tour ou est la souris, drawButtonOnRenderer choisit la bonne texture à afficher
//isHover(&menuButton,event.motion.x,event.motion.y);
isHover(&menuButton);
break;
}
}
drawButtonOnRenderer(renderer,&menuButton);
drawButtonOnRenderer(renderer,&settingButton);
drawButtonOnRenderer(renderer,&soundButton);
drawTextLabel(renderer,&nbTurnLabel);
drawTextLabel(renderer,&timeLabel);
SDL_RenderPresent(renderer);
SDL_Delay(20);
}
//FREE TEXT LABEL + BUTTON
}

@ -1,57 +1,53 @@
#include <SDL2/SDL.h>
#include "view/GameMain.h"
#include <stdio.h>
#include <stdbool.h>
#include "engine/InputProcessor.h"
#include "engine/GameInputProcessor.h"
#include "engine/InputElement.h"
#include "engine/TextureHandler.h"
#include "model/Game.h"
#include "view/GameDrawer.h"
#include "model/arrayCoord.h"
#include "debug/printer.h"
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Rect windowSize = {10, 10, 600, 600};
SDL_Renderer* renderer = NULL;
#include "view/PiecesDrawer.h"
#include "view/BoardDrawer.h"
#include "view/GameDrawer.h"
int statut = EXIT_FAILURE;
SDL_Rect boardRectFromWindowSize(int windowW, int windowH) {
SDL_Rect boardRect = {.x=windowW/10.0, .y=windowH/10, .w=windowW*8.0/10.0, .h=windowH*8.0/10.0};
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Error : %s\n", SDL_GetError());
goto Quit;
}
window = SDL_CreateWindow("Pontu",windowSize.x, windowSize.y, windowSize.w, windowSize.h, SDL_WINDOW_SHOWN);
if (!window)
{
fprintf(stderr, "Error : %s\n", SDL_GetError());
goto Quit;
}
return boardRect;
}
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
if(!renderer)
{
fprintf(stderr, "Erreur : %s", SDL_GetError());
goto Quit;
void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, Player players[], size_t nbPlayers)
{
if (*generalState != GS_Game) {
return;
}
InputProcessor inputProcessor = createInputProcessor();
GameInputProcessor inputProcessor = createGameInputProcessor();
struct array_Coord interactiveCases = array_Coord_Create();
int wBoardRect=99*3, hBoardRect=99*3;
SDL_Rect boardRect = {.x=windowSize.w/2 - wBoardRect/2, .y=windowSize.h/2 - hBoardRect/2, .w=wBoardRect, .h=99*3};
const char* pseudos[] = {"Azerty","Bépo"};
Game game = newGame(2, pseudos);
Game game = newGame(nbPlayers, players);
TextureHandler textureHandler = newTextureHandler(renderer);
int windowW;
int windowH;
SDL_GetWindowSize(window, &windowW, &windowH);
SDL_Rect boardRect = boardRectFromWindowSize(windowW, windowH);
//Draw
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_RenderPresent(renderer);
bool quit = false;
while(!quit)
while(*generalState == GS_Game)
{
// Event handling
InputElement inputElement;
while (InputType_None != (inputElement = proccessInput(&inputProcessor, &boardRect)).type) {
while (InputType_None != (inputElement = proccessGameInput(&inputProcessor, &boardRect)).type) {
switch (inputElement.type)
{
@ -59,7 +55,7 @@ int main(int argc, char* argv[])
switch (inputElement.data.uiAction)
{
case UIAction_Quit:
quit = true;
*generalState = GS_Quit;
break;
case UIAction_Validate:
break;
@ -73,8 +69,12 @@ int main(int argc, char* argv[])
fprintf(stderr, "Move on board\n");
fprintf(stderr, "From (%d; %d)\n", inputElement.data.move.start.x, inputElement.data.move.start.y);
fprintf(stderr, "To (%d; %d)\n", inputElement.data.move.end.x, inputElement.data.move.end.y);
moveOnBoard(inputElement.data.move.start, inputElement.data.move.end, &game);
drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, textureHandler.textures[TEXTURE_PieceRed], textureHandler.textures[TEXTURE_Island]);
SDL_RenderPresent(renderer);
break;
case InputType_ClickGame:
fprintf(stderr, "Clic on board (%d; %d)\n", inputElement.data.coord.x, inputElement.data.coord.y);
@ -89,43 +89,26 @@ int main(int argc, char* argv[])
fprintf(stderr, "\tselected case reset\n");
inputProcessor.selectedCase = newCoord(-1,-1);
}
break;
case InputType_None:
default:
break;
}
array_Coord_Free(&interactiveCases);
interactiveCases = getInteractiveCases(&game, inputProcessor.selectedCase);
fprintf(stderr, "Interactive cases : {");
array_Coord_Foreach(&interactiveCases, *printCoord);
fprintf(stderr, "}\n");
}
fflush(stderr);
// Drawing
drawGame(renderer, &windowSize, &boardRect, &game, &textureHandler);
SDL_RenderPresent(renderer);
SDL_Delay(20);
}
statut = EXIT_SUCCESS;
Quit:
freeTextureHandler(&textureHandler);
array_Coord_Free(&interactiveCases);
if(renderer != NULL) {
SDL_DestroyRenderer(renderer);
}
if(window != NULL) {
SDL_DestroyWindow(window);
}
SDL_Quit();
return statut;
}

@ -11,7 +11,11 @@ void quit(P_Button* buttonCaller) {
*((GeneralState*)(buttonCaller->arg)) = GS_Quit;
}
P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize)
void generalStateToNewGame(P_Button* buttonCaller) {
*((GeneralState*)(buttonCaller->arg)) = GS_GameCreationMenu;
}
P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize, GeneralState* generalState)
{
P_Button* buttons = (P_Button*)malloc(sizeof(P_Button)*3);
*nb = 0;
@ -27,7 +31,7 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns
*nb = 3;
buttons = (P_Button*)malloc(sizeof(P_Button)*(*nb));
buttons[0] = createButton(NULL,NULL,20, 20, 20, 20, NULL);
buttons[0] = createButton(NULL,NULL,20, 20, 20, 20, generalStateToNewGame);
SDL_Texture* newGameButtonTexture = createGenericButtonTexture("Nouvelle Partie",font,fontSize,darkBlue,lightBlue,5, 10,&(buttons[0].rect.w),&(buttons[0].rect.h),renderer);
SDL_Texture* newGameButtonTextureHover = createGenericButtonTexture("Nouvelle Partie",font,fontSize,lightBlue,darkBlue,5, 10,NULL,NULL,renderer);
@ -36,6 +40,7 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns
buttons[0].hoverTexture = newGameButtonTextureHover;
buttons[0].rect.x = (windowSize->w/2)-(buttons[0].rect.w/2);
buttons[0].arg = generalState;
buttons[1] = createButton(NULL,NULL,20, buttons[0].rect.y+buttons[0].rect.h+20, 20, 20, NULL);
@ -54,18 +59,19 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns
buttons[2].texture = quitButtonTexture;
buttons[2].hoverTexture = quitButtonTextureHover;
buttons[2].rect.x = (windowSize->w/2)-(buttons[2].rect.w/2);
buttons[2].arg = generalState;
SDL_SetRenderTarget(renderer,NULL);
SDL_Texture* picture = createTextureFromPath(renderer, path);
SDL_RenderCopy(renderer, picture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(picture);
return buttons;
}
int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler){
int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler){
int statut = EXIT_FAILURE;
char* path = "../rsrc/img/Lenna.png";
//Initialisation
P_Button* buttons = NULL;
@ -76,13 +82,13 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general
SDL_Rect rect = {.x = 0, .y = 0, .w = 0, .h = 0};
SDL_GetWindowSize(window,&(rect.w),&(rect.h));
if(!(buttons = drawMainMenu(renderer,fontHandler,&nb,&rect))){
if(!(buttons = drawMainMenu(renderer,fontHandler,&nb,&rect, generalState))){
fprintf(stderr, "Le menu principale ne s'est pas déssiné correctement\n");
return statut;
}
SDL_Event event;
buttons[2].arg = generalState;
while(*generalState != GS_Quit)
while(*generalState == GS_MainMenu)
{
while(SDL_PollEvent(&event))
{
@ -92,17 +98,21 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general
*generalState = GS_Quit;
break;
case SDL_MOUSEBUTTONUP:
if(isHover(buttons,event.button.x,event.button.y))
printf("Nouvelle partie\n");
if(isHover(&(buttons[2]),event.motion.x,event.motion.y)){
if(isHover(&(buttons[2]))){
buttons[2].onClick(&(buttons[2]));
break;
}
if(isHover(&(buttons[0]))){
buttons[0].onClick(&(buttons[0]));
break;
}
break;
case SDL_MOUSEMOTION:
isHover(&(buttons[0]),event.motion.x,event.motion.y);
isHover(&(buttons[1]),event.motion.x,event.motion.y);
isHover(&(buttons[2]),event.motion.x,event.motion.y);
if(isButtonEntry(&(buttons[0]),event.motion.x,event.motion.y) ||
isButtonEntry(&(buttons[1]),event.motion.x,event.motion.y) ||
isButtonEntry(&(buttons[2]),event.motion.x,event.motion.y)){
playSFX(SFX_menu_sound_effect, audioHandler);
}
break;
default:
break;
@ -116,5 +126,11 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general
SDL_Delay(20);
}
Quit:
for(int i=0;i<nb;i++){
freeButton(&(buttons[i]));
}
free(buttons);
return 0;
}

@ -177,7 +177,6 @@ struct endGameMenuTextLabel createLabels(SDL_Renderer* renderer, const Player pl
}
void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* renderer, FontHandler* fontHandler, const Player players[], const size_t nbPlayers) {
int windowW;
int windowH;
@ -213,7 +212,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
switch (inputElement.data.uiAction)
{
case UIAction_Quit:
*generalState = GS_MainMenu;
*generalState = GS_Quit;
break;
case UIAction_Validate:
break;

@ -3,12 +3,12 @@
//Don't put this in model
SDL_Rect islandToRect(const SDL_Rect* boardRect, const Island island) {
SDL_Rect islandToRect(const SDL_Rect* boardRect, const Island* island) {
const int w = boardRect->w/9;
const int h = boardRect->h/9;
SDL_Rect r = {
.x = boardRect->x + w*(island.x*2),
.y = boardRect->y + h*(island.y*2),
.x = boardRect->x + w*(island->x*2),
.y = boardRect->y + h*(island->y*2),
.w = w,
.h = h
};
@ -21,8 +21,17 @@ void drawPiecesPlayer(SDL_Renderer* renderer, const SDL_Rect* boardRect, const P
for (size_t i = 0; i < nbPieces; ++i)
{
if (arrPieces[i].idJ == numPlayer) {
const SDL_Rect rDest = islandToRect(boardRect, arrPieces[i].island);
const SDL_Rect rDest = islandToRect(boardRect, &arrPieces[i].island);
SDL_RenderCopy(renderer, piece, NULL, &rDest);
}
}
}
void drawMovePiece(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Island* startMove, const Island* endMove, SDL_Texture* pieceTexture, SDL_Texture* islandTexture) {
SDL_Rect rDest = islandToRect(boardRect, startMove);
SDL_RenderCopy(renderer, islandTexture, NULL, &rDest);
rDest = islandToRect(boardRect, endMove);
SDL_RenderCopy(renderer, pieceTexture, NULL, &rDest);
}

@ -0,0 +1,93 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
#include <engine/FontLoader.h>
//#include "engine/TextureLoader.h"
//#include "view/GameInterface.h"
int testGameInterface()
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *buttonTexture= NULL;
int statut = EXIT_FAILURE;
//Initialiser TTF
if(TTF_Init() == -1)
{
fprintf(stderr, "Erreur d'inistialisation de TTF_Init : %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "Erreur SDL_Init : %s", SDL_GetError());
goto Quit;
}
//fenetre
window = SDL_CreateWindow("Fenêtre", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 900,900, SDL_WINDOW_SHOWN);
if(window == NULL)
{
fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError());
goto Quit;
}
//rendu
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == NULL)
{
fprintf(stderr, "Erreur SDL_CreateRenderer: %s\n", SDL_GetError());
goto Quit;
}
if(0 != SDL_SetRenderDrawColor(renderer, 255,0,0,0)) //choisi la couleur avec laquelle travailler
{
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
goto Quit;
}
if(0 != SDL_RenderClear(renderer)) //efface le rendu en le repeignant avec la couleur choisi
{
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
goto Quit;
}
//SDL_bool quit = SDL_FALSE;
SDL_Event event;
FontHandler fontHandler=loadFonts();
drawButtons(renderer,fontHandler);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
//TTF_Quit();
if(!freeFonts(fontHandler))
{
fprintf(stderr,"Erreur free font : %s\n", TTF_GetError());
}
Quit:
/*
if(NULL != texture)
SDL_DestroyTexture(texture);
*/
freeFonts(fontHandler);
if(NULL != renderer)
SDL_DestroyRenderer(renderer);
if(NULL != window)
SDL_DestroyWindow(window);
SDL_Quit();
return statut;
}
Loading…
Cancel
Save