new enum for button, drawn become enable

origin/fixingSettings
Mathis RIBEMONT 3 years ago
parent ef51abf562
commit f1ac5006f0

@ -48,6 +48,7 @@ int main(int argc, char *argv[]) {
switch (generalState) { switch (generalState) {
case GS_MainMenu: case GS_MainMenu:
mainMenu(renderer,window,&generalState, fontHandler, audioHandler); mainMenu(renderer,window,&generalState, fontHandler, audioHandler);
playSFX(validate_sound, audioHandler)
break; break;
case GS_GameCreationMenu:{ case GS_GameCreationMenu:{
int windowW; int windowW;

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

@ -13,6 +13,8 @@
struct P_buttonArg; struct P_buttonArg;
enum {BUTTON_NOTHING, BUTTON_ENTRY, BUTTON_EXIT};
/** /**
* \struct P_Button * \struct P_Button
* \brief Represents a button * \brief Represents a button
@ -23,7 +25,7 @@ typedef struct P_button
SDL_Texture* hoverTexture; ///> texture to draw when the button is hovered SDL_Texture* hoverTexture; ///> texture to draw when the button is hovered
SDL_Rect rect; ///> defines coordinates and size for hitbox and display SDL_Rect rect; ///> defines coordinates and size for hitbox and display
void (*onClick)(struct P_button* buttonCaller); ///> action done on click void (*onClick)(struct P_button* buttonCaller); ///> action done on click
bool drawn; ///> is the button drawn bool enable; ///> is the button enable and drawable
bool hover; ///> is the button hovered bool hover; ///> is the button hovered
void* arg; void* arg;
} P_Button; } P_Button;

Binary file not shown.

@ -6,7 +6,7 @@
P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(P_Button* buttonCaller)) P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(P_Button* buttonCaller))
{ {
// Declarations // Declarations
P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick, .drawn = false}; P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick, .enable = true};
if(onClick == NULL) if(onClick == NULL)
fprintf(stderr, "Attention: aucune action onClick n'est passé au bouton.\n"); fprintf(stderr, "Attention: aucune action onClick n'est passé au bouton.\n");
b.texture = texture; b.texture = texture;
@ -18,19 +18,20 @@ P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int
bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button) bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button)
{ {
SDL_SetRenderTarget(renderer, NULL); SDL_SetRenderTarget(renderer, NULL);
if(button->enable == false)
return false;
if(SDL_RenderCopy(renderer,button->hover && button->hoverTexture != NULL ? button->hoverTexture : button->texture,NULL,&(button->rect))) if(SDL_RenderCopy(renderer,button->hover && button->hoverTexture != NULL ? button->hoverTexture : button->texture,NULL,&(button->rect)))
{ {
fprintf(stderr,"SDLWarning: %s\n",SDL_GetError()); fprintf(stderr,"SDLWarning: %s\n",SDL_GetError());
return false; return false;
} }
button->drawn = true;
//printf("Redraw de %p\n",button); //printf("Redraw de %p\n",button);
return true; return true;
} }
bool isHover(P_Button* button) bool isHover(P_Button* button)
{ {
return button->hover && button->drawn; return button->hover && button->enable;
} }
bool changeButtonTexture(P_Button* button, SDL_Texture* texture) bool changeButtonTexture(P_Button* button, SDL_Texture* texture)
@ -65,12 +66,12 @@ int isButtonInteractWithCursor(P_Button * button,const int x,const int y){
if(isHover(button)){ if(isHover(button)){
button->hover = SDL_PointInRect(&coord,&(button->rect)); button->hover = SDL_PointInRect(&coord,&(button->rect));
if(button->hover == false){ if(button->hover == false){
return 2; return BUTTON_EXIT;
} }
return 0; return BUTTON_NOTHING;
} }
button->hover = SDL_PointInRect(&coord,&(button->rect)); button->hover = SDL_PointInRect(&coord,&(button->rect));
if(button->hover) if(button->hover)
return 1; return BUTTON_ENTRY;
return 0; return BUTTON_NOTHING;
} }

@ -114,32 +114,16 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
switch (isButtonInteractWithCursor(&(buttons[0]),event.motion.x,event.motion.y)) { for (size_t i = 0; i < nb; i++) {
switch (isButtonInteractWithCursor(&(buttons[i]),event.motion.x,event.motion.y)) {
case 1: case 1:
drawButtonOnRenderer(renderer,&(buttons[0])); drawButtonOnRenderer(renderer,&(buttons[i]));
playSFX(SFX_menu_sound_effect, audioHandler);
break;
case 2:
drawButtonOnRenderer(renderer,&(buttons[0]));
break;
}
switch (isButtonInteractWithCursor(&(buttons[1]),event.motion.x,event.motion.y)) {
case 1:
drawButtonOnRenderer(renderer,&(buttons[1]));
playSFX(SFX_menu_sound_effect, audioHandler); playSFX(SFX_menu_sound_effect, audioHandler);
break; break;
case 2: case 2:
drawButtonOnRenderer(renderer,&(buttons[1])); drawButtonOnRenderer(renderer,&(buttons[i]));
break; break;
} }
switch (isButtonInteractWithCursor(&(buttons[2]),event.motion.x,event.motion.y)) {
case 1:
drawButtonOnRenderer(renderer,&(buttons[2]));
playSFX(SFX_menu_sound_effect, audioHandler);
break;
case 2:
drawButtonOnRenderer(renderer,&(buttons[2]));
break;
} }
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
@ -152,9 +136,10 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general
event.window.data2); event.window.data2);
windowSize.w = event.window.data1; windowSize.w = event.window.data1;
windowSize.h = event.window.data2; windowSize.h = event.window.data2;
buttons[0].rect.x = (windowSize.w/2)-(buttons[0].rect.w/2); for (size_t i = 0; i < nb; i++) {
buttons[1].rect.x = (windowSize.w/2)-(buttons[1].rect.w/2); buttons[i].rect.x = (windowSize.w/2)-(buttons[i].rect.w/2);
buttons[2].rect.x = (windowSize.w/2)-(buttons[2].rect.w/2); drawButtonOnRenderer(renderer,&(buttons[i]));
}
} }
break; break;
default: default:

@ -40,9 +40,9 @@ SDL_Rect getEndGameMenuRect(SDL_Window* window) {
/** /**
* @brief Create button For EndGameMenu * @brief Create button For EndGameMenu
* *
* @param renderer The renderer where buttons will be drawn * @param renderer The renderer where buttons will be enable
* @param font Font used by buttons * @param font Font used by buttons
* @param rect Rect in which the endGameMenu is drawn * @param rect Rect in which the endGameMenu is enable
* @param state generalState which will be attached to the button * @param state generalState which will be attached to the button
* @return P_Button * @return P_Button
*/ */
@ -184,7 +184,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
}; };
PositionSpecifier positionSpecifierButtonRetour = newPositionSpecifier(&base100, POSX_CENTER, POSY_BOTTOM, ASPECT_KEEP_FIT); PositionSpecifier positionSpecifierButtonRetour = newPositionSpecifier(&base100, POSX_CENTER, POSY_BOTTOM, ASPECT_KEEP_FIT);
buttonMenuEndGame->rect = adaptPosToRect(&positionSpecifierButtonRetour, &endGameMenuRect); buttonMenuEndGame->rect = adaptPosToRect(&positionSpecifierButtonRetour, &endGameMenuRect);
buttonMenuEndGame->drawn = false; buttonMenuEndGame->enable = false;
struct endGameMenuTextLabel labels = createLabels(renderer, players, nbPlayers, fontHandler); struct endGameMenuTextLabel labels = createLabels(renderer, players, nbPlayers, fontHandler);
@ -223,7 +223,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
drawEndGameMenu(renderer, rectM, &labels); drawEndGameMenu(renderer, rectM, &labels);
buttonMenuEndGame->rect = adaptPosToRect(&positionSpecifierButtonRetour, &rectM); buttonMenuEndGame->rect = adaptPosToRect(&positionSpecifierButtonRetour, &rectM);
buttonMenuEndGame->drawn = false; buttonMenuEndGame->enable = false;
fprintf(stderr, "Resize\n"); fflush(stderr); fprintf(stderr, "Resize\n"); fflush(stderr);
} }
@ -233,7 +233,7 @@ void endGameMenu(GeneralState* generalState, SDL_Window* window, SDL_Renderer* r
} }
} }
if (!buttonMenuEndGame->drawn) { if (!buttonMenuEndGame->enable) {
drawButtonOnRenderer(renderer, buttonMenuEndGame); drawButtonOnRenderer(renderer, buttonMenuEndGame);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }

Loading…
Cancel
Save