From 4857a08df24793f31ded10bd908b8cf0a5a9b35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathis=20Rib=C3=A9mont?= Date: Sun, 30 Jan 2022 19:24:59 +0100 Subject: [PATCH] =?UTF-8?q?r=C3=A9solution=20bug=20du=20createButtonTextur?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pontu/include/engine/TextureLoader.h | 4 ++-- Pontu/src/engine/Button.c | 4 +++- Pontu/src/engine/TextureLoader.c | 14 +++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Pontu/include/engine/TextureLoader.h b/Pontu/include/engine/TextureLoader.h index 513f6ba..862c574 100644 --- a/Pontu/include/engine/TextureLoader.h +++ b/Pontu/include/engine/TextureLoader.h @@ -25,8 +25,8 @@ SDL_Texture* createTextureFromPath(SDL_Renderer* renderer, char* path); * \param[in] background_color The color of the background * \param[in] thickness The thickness of the texture * \param[in] padding The padding around the text in the button - * \param[in] sizex The width of the texture. You have to apply to the width of the button - * \param[in] sizey The height of the texture. You have to apply to the height of the button + * \param[out] sizex The width of the texture. You have to apply it to the width of the button + * \param[out] sizey The height of the texture. You have to apply it to the height of the button * \param[in] renderer The renderer you want the texture to be associated * \return Return texture created on success. Else return NULL and print the error on STDERR. */ diff --git a/Pontu/src/engine/Button.c b/Pontu/src/engine/Button.c index b5983b2..d35ee9c 100644 --- a/Pontu/src/engine/Button.c +++ b/Pontu/src/engine/Button.c @@ -7,7 +7,8 @@ P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int { // Declarations P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick, .drawn = false}; - + if(onClick == NULL) + fprintf(stderr, "Attention: aucune action onClick n'est passé au bouton.\n"); b.texture = texture; b.hoverTexture = hoverTexture; return b; @@ -15,6 +16,7 @@ P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button) { + SDL_SetRenderTarget(renderer, NULL); if(SDL_RenderCopy(renderer,button->hover && button->hoverTexture != NULL ? button->hoverTexture : button->texture,NULL,&(button->rect))) { fprintf(stderr,"Warning: %s\n",SDL_GetError()); diff --git a/Pontu/src/engine/TextureLoader.c b/Pontu/src/engine/TextureLoader.c index ebb6282..33bcf5a 100644 --- a/Pontu/src/engine/TextureLoader.c +++ b/Pontu/src/engine/TextureLoader.c @@ -50,7 +50,7 @@ SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int size, SD SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); int minx,maxx,miny,maxy,advance; - if(TTF_GlyphMetrics(font,text,&minx,&maxx,&miny,&maxy,&advance)==-1){ + if(TTF_GlyphMetrics(font,'a',&minx,&maxx,&miny,&maxy,&advance)==-1){ printf("%s\n",TTF_GetError()); } /*else { @@ -64,7 +64,7 @@ SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int size, SD SDL_Rect Message_rect; //create a rect Message_rect.x = padding+thickness+minx; //controls the rect's x coordinate Message_rect.y = padding+thickness; // controls the rect's y coordinte - Message_rect.w = strlen(text)*size*2/3; // controls the width of the rect + Message_rect.w = (int)(strlen(text)*size*2/3); // controls the width of the rect Message_rect.h = size; // controls the height of the rect //pour les contour d'abord @@ -74,12 +74,14 @@ SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int size, SD button_rect.w = Message_rect.w+2*(padding+thickness)+minx; button_rect.h = Message_rect.h+2*(padding+thickness); SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,button_rect.w,button_rect.h); - SDL_SetRenderDrawColor(renderer, border_color.r,border_color.g,border_color.b,0); SDL_SetRenderTarget(renderer, texture); + SDL_SetRenderDrawColor(renderer, border_color.r,border_color.g,border_color.b,0); SDL_RenderFillRect(renderer, &button_rect); - *sizex = button_rect.w; - *sizey = button_rect.h; + if(sizex != NULL) + *sizex = button_rect.w; + if(sizey != NULL) + *sizey = button_rect.h; //pour le background button_rect.x = thickness; @@ -91,5 +93,7 @@ SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int size, SD SDL_SetRenderDrawColor(renderer, border_color.r,border_color.g,border_color.b,0); SDL_RenderCopy(renderer, message, NULL, &Message_rect); + + SDL_SetRenderTarget(renderer, NULL); return texture; }