merge-requests/2/merge
marouault 4 years ago
commit ae9852ea0b

@ -61,19 +61,9 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button);
bool isHover(P_Button* button, int x,int y);// dit si le bouton est survolé en donnant les coordonnées x,y bool isHover(P_Button* button, int x,int y);// dit si le bouton est survolé en donnant les coordonnées x,y
/** /**
* \brief Change the texture of the button * \brief Free the texture of a button.
* \param[in] button the button target * \param[in] button the button you want to destroy the texture
* \param[in] texture the texture you want to apply to the button
* \return false if the texture is NULL
*/
bool changeButtonTexture(P_Button* button, SDL_Texture* texture);
/**
* \brief Change the texture to use when the button is hovered
* \param[in] button the button target
* \param[in] texture the texture you want to apply to the button
* \return false if the texture is NULL
*/ */
bool changeButtonHoverTexture(P_Button* button, SDL_Texture* texture); void freeButton(P_Button * button);
#endif #endif

@ -55,3 +55,8 @@ bool changeButtonHoverTexture(P_Button* button, SDL_Texture* texture)
button->hoverTexture = texture; button->hoverTexture = texture;
return true; return true;
} }
void freeButton(P_Button * button){
SDL_DestroyTexture(button->texture);
SDL_DestroyTexture(button->hoverTexture);
}

@ -0,0 +1,11 @@
#ifndef COLORS_H_INCLUDED
#define COLORS_H_INCLUDED
#include <SDL2/SDL_Pixel.h>
const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND = {225, 225, 225, 255};
const SDL_Color COLOR_GENERIC_BUTTON_BORDER = {10, 10, 10, 255};
#endif

@ -10,38 +10,48 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns
*nb = 0; *nb = 0;
SDL_Color lightBlue = {.r = 0,.g = 195,.b = 255,.a = 0}; //0, 195, 255 SDL_Color lightBlue = {.r = 0,.g = 195,.b = 255,.a = 0}; //0, 195, 255
SDL_Color darkBlue = {.r = 0,.g = 123,.b = 161,.a = 0}; //0, 123, 161 SDL_Color darkBlue = {.r = 0,.g = 123,.b = 161,.a = 0}; //0, 123, 161
char* path = "../rsrc/img/Lenna.png";
SDL_SetRenderTarget(renderer, NULL); SDL_SetRenderTarget(renderer, NULL);
TTF_Font* font = fontHandler.fonts[FONT_retro]; TTF_Font* font = fontHandler.fonts[FONT_retro];
int fontSize = 80; int fontSize = 80;
*nb = (*nb)+1; *nb = 3;
buttons = (P_Button*)malloc(sizeof(P_Button)*(*nb)); 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, NULL);
SDL_Texture* newGameButtonTexture = createGenericButtonTexture("Nouvelle Partie",font,fontSize,darkBlue,lightBlue,3, 3,&(buttons[0].rect.w),&(buttons[0].rect.h),renderer); 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,3, 3,NULL,NULL,renderer); SDL_Texture* newGameButtonTextureHover = createGenericButtonTexture("Nouvelle Partie",font,fontSize,lightBlue,darkBlue,5, 10,NULL,NULL,renderer);
buttons[0].texture = newGameButtonTexture; buttons[0].texture = newGameButtonTexture;
buttons[0].hoverTexture = newGameButtonTextureHover; buttons[0].hoverTexture = newGameButtonTextureHover;
buttons[0].rect.x = (windowSize->w/2)-(buttons[0].rect.w/2);
buttons[1] = createButton(NULL,NULL,20, buttons[0].rect.y+buttons[0].rect.h+20, 20, 20, NULL); buttons[1] = createButton(NULL,NULL,20, buttons[0].rect.y+buttons[0].rect.h+20, 20, 20, NULL);
SDL_Texture* optionButtonTexture = createGenericButtonTexture("Option",font,fontSize,darkBlue,lightBlue,3, 3,&(buttons[1].rect.w),&(buttons[1].rect.h),renderer); SDL_Texture* optionButtonTexture = createGenericButtonTexture("Options",font,fontSize,darkBlue,lightBlue,5, 10,&(buttons[1].rect.w),&(buttons[1].rect.h),renderer);
SDL_Texture* optionButtonTextureHover = createGenericButtonTexture("Option",font,fontSize,lightBlue,darkBlue,3, 3,NULL,NULL,renderer); SDL_Texture* optionButtonTextureHover = createGenericButtonTexture("Options",font,fontSize,lightBlue,darkBlue,5, 10,NULL,NULL,renderer);
buttons[1].texture = optionButtonTexture; buttons[1].texture = optionButtonTexture;
buttons[1].hoverTexture = optionButtonTextureHover; buttons[1].hoverTexture = optionButtonTextureHover;
buttons[1].rect.x = (windowSize->w/2)-(buttons[1].rect.w/2);
buttons[2] = createButton(NULL,NULL,20, buttons[1].rect.y+buttons[1].rect.h+20, 20, 20, quit); buttons[2] = createButton(NULL,NULL,20, buttons[1].rect.y+buttons[1].rect.h+20, 20, 20, quit);
SDL_Texture* quitButtonTexture = createGenericButtonTexture("Quitter",font,fontSize,darkBlue,lightBlue,3, 3,&(buttons[2].rect.w),&(buttons[2].rect.h),renderer); SDL_Texture* quitButtonTexture = createGenericButtonTexture("Quitter",font,fontSize,darkBlue,lightBlue,5, 10,&(buttons[2].rect.w),&(buttons[2].rect.h),renderer);
SDL_Texture* quitButtonTextureHover = createGenericButtonTexture("Quitter",font,fontSize,lightBlue,darkBlue,3, 3,NULL,NULL,renderer); SDL_Texture* quitButtonTextureHover = createGenericButtonTexture("Quitter",font,fontSize,lightBlue,darkBlue,5, 10,NULL,NULL,renderer);
buttons[2].texture = quitButtonTexture; buttons[2].texture = quitButtonTexture;
buttons[2].hoverTexture = quitButtonTextureHover; buttons[2].hoverTexture = quitButtonTextureHover;
buttons[2].rect.x = (windowSize->w/2)-(buttons[2].rect.w/2);
SDL_SetRenderTarget(renderer,NULL);
SDL_Texture* picture = createTextureFromPath(renderer, path);
SDL_RenderCopy(renderer, picture, NULL, NULL);
SDL_RenderPresent(renderer);
return buttons; return buttons;
} }

@ -9,6 +9,7 @@ int testDrawMainMenu(){
SDL_Window *window = NULL; SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
int statut = EXIT_FAILURE; int statut = EXIT_FAILURE;
char* path = "../rsrc/img/Lenna.png";
if(0 != SDL_Init(SDL_INIT_VIDEO)) if(0 != SDL_Init(SDL_INIT_VIDEO))
{ {
@ -43,17 +44,20 @@ int testDrawMainMenu(){
unsigned int nb = 0; unsigned int nb = 0;
FontHandler fontHandler = loadFonts(); FontHandler fontHandler = loadFonts();
int window_w, window_h; //récupère le numéro de l'écran
SDL_GetWindowSize(window,&window_w,&window_h); SDL_DisplayMode current;
int i = SDL_GetWindowDisplayIndex(window);
SDL_GetCurrentDisplayMode(i, &current); //retourne current, structure avec vrai valeurs de taille de fenêtre
SDL_Rect rect = {.x = 0, .y = 0, .w = window_w, .h = window_h}; SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderClear(renderer);
SDL_Rect rect = {.x = 0, .y = 0, .w = current.w, .h = current.h};
if(!(buttons = drawMainMenu(renderer,fontHandler,&nb,&rect))){ if(!(buttons = drawMainMenu(renderer,fontHandler,&nb,&rect))){
fprintf(stderr, "Le menu principale ne s'est pas déssiné correctement\n"); fprintf(stderr, "Le menu principale ne s'est pas déssiné correctement\n");
return statut; return statut;
} }
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderClear(renderer);
bool quit = false; bool quit = false;
SDL_Event event; SDL_Event event;
buttons[2].arg = &quit; buttons[2].arg = &quit;
@ -82,11 +86,6 @@ int testDrawMainMenu(){
break; break;
} }
} }
/*for(int i=0;i<nb;i++){
drawButtonOnRenderer(renderer,&(buttons[i]));
printf("%d\n",i);
}*/
drawButtonOnRenderer(renderer,&(buttons[0])); drawButtonOnRenderer(renderer,&(buttons[0]));
drawButtonOnRenderer(renderer,&(buttons[1])); drawButtonOnRenderer(renderer,&(buttons[1]));
drawButtonOnRenderer(renderer,&(buttons[2])); drawButtonOnRenderer(renderer,&(buttons[2]));

Loading…
Cancel
Save