#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "button.h"

void Button::SetAlpha(int alpha)
{
     SDL_SetAlpha(image,SDL_SRCALPHA | SDL_RLEACCEL,alpha);
}

void Button::Show(SDL_Surface *screen,int alpha,bool flip)
{
     if (!screen) return;
     SDL_BlitSurface(image,NULL,screen,&pos);
     SetAlpha(alpha);
//     if (flip) SDL_Flip(screen);
}

Button::Button(int x,int y,int w,int h,char *fname,int alpha,int Rkey,int Gkey,int Bkey)
{
    pos.x=x;
    pos.y=y;
    pos.w=w;
    pos.h=h;
    SDL_Surface *loaded;
    if (loaded=IMG_Load(fname))
      {
        if (image=SDL_DisplayFormat(loaded))
            SDL_SetColorKey(image,SDL_RLEACCEL | SDL_SRCCOLORKEY,SDL_MapRGB(image->format,Rkey,Gkey,Bkey));
        SetAlpha(alpha);
        SDL_FreeSurface(loaded);
      }
}

int Button::Inside(int x,int y)
{
     return (x>=pos.x && x<=pos.x+pos.w && y>=pos.y && y<=pos.y+pos.h)?1:0;
}

int Button::Inside(SDL_Event event)
{
     return (event.button.x>=pos.x && event.button.x<=pos.x+pos.w && event.button.y>=pos.y && event.button.y<=pos.y+pos.h)?1:0;
}
