Merge branch 'master' of https://gitlab.iut-clermont.uca.fr/maribemont/projet-tut
commit
46c146d2be
@ -1,34 +1,44 @@
|
|||||||
#include "engine/Button.h"
|
#include "engine/Button.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
P_Button createButton(SDL_Texture* texture, const int coordx,
|
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))
|
||||||
const int coordy, const int sizex, const int sizey,
|
|
||||||
void (*onClick)(void))
|
|
||||||
{
|
{
|
||||||
// Declarations
|
// Declarations
|
||||||
P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick };
|
P_Button b = { .rect = { .x = coordx, .y = coordy, .w = sizex, .h = sizey }, .onClick = onClick, .drawn = false};
|
||||||
|
|
||||||
assert(texture != NULL && "ERROR: Button created without texture");
|
assert(texture != NULL && "ERROR: Button created without texture");
|
||||||
|
|
||||||
b.texture = texture;
|
b.texture = texture;
|
||||||
|
b.hoverTexture = hoverTexture;
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawButtonOnRenderer(SDL_Renderer* renderer,const P_Button* button)
|
bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button)
|
||||||
{
|
{
|
||||||
if(SDL_RenderCopy(renderer,button->texture,NULL,&(button->rect)))
|
if(SDL_RenderCopy(renderer,button->hover && button->hoverTexture != NULL ? button->hoverTexture : button->texture,NULL,&(button->rect)))
|
||||||
{
|
{
|
||||||
fprintf(stderr,"Error: %s\n",SDL_GetError());
|
fprintf(stderr,"Warning: %s\n",SDL_GetError());
|
||||||
exit(1);
|
return false;
|
||||||
}
|
}
|
||||||
|
button->drawn = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool isHover(const P_Button button,const int x,const int y)
|
bool isHover(P_Button* button,const int x,const int y)
|
||||||
{
|
{
|
||||||
SDL_Point coord;
|
SDL_Point coord;
|
||||||
coord.x = x;
|
coord.x = x;
|
||||||
coord.y = y;
|
coord.y = y;
|
||||||
return SDL_PointInRect(&coord,&(button.rect));
|
button->hover = SDL_PointInRect(&coord,&(button->rect));
|
||||||
|
return button->hover && button->drawn;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool changeButtonTexture(P_Button* button, SDL_Texture* texture)
|
||||||
|
{
|
||||||
|
if(texture == NULL){
|
||||||
|
fprintf(stderr,"Warning: button texture cannot change to NULL\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
button->texture = texture;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue