#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include <cstdlib>
#include "functions.h"
#include "clase.h"
#include "initializare.h"

int MISCARE=2;

void _buton::coordonate(int a, int b)
{
    x=a;
    y=b;
    dimensiuni();
}

void _buton::deschide(char *sir)
{
    sup=load_image(sir);
}

void _buton::deschide2(char *sir)
{
    supt=load_image(sir);
}

void _buton::dimensiuni(int latime, int inaltime)
{
    w=latime;
    h=inaltime;
}

void _buton::deseneaza()
{
    if (!hover)
    blit_surface(sup, screen, x, y, NULL);
    else
    blit_surface(supt, screen, x, y, NULL);
}

int _buton::click()
{
    int a, b;
    if (SDL_GetMouseState(&a, &b) && SDL_BUTTON(1) && a>=x && a<=x+w && b>=y && b<=y+h)
    {
        Mix_PlayChannel(-1, clic, 0);
        return 1;
    }
    return 0;
}

void _buton::miscare()
{
    int a, b;
    SDL_GetMouseState(&a, &b);
    if (a>=x && a<=x+w && b>=y && b<=y+h) hover=1;
    else hover=0;
}

void _fundal::coordonate(int a, int b)
{
    x=a;
    y=b;
    dimensiuni();
}

void _fundal::deschide(char *sir)
{
    sup=load_image(sir);
}

void _fundal::dimensiuni(int latime, int inaltime)
{
    w=latime;
    h=inaltime;
}

void _fundal::deseneaza()
{
    blit_surface(sup, screen, x, y, NULL);
}

void _player::deschide(char *sir)
{
    sup=load_image(sir);
}


void _player::miscare()
{
    Uint8 *keystates = SDL_GetKeyState( NULL );//tastele
    if (keystates[SDLK_LEFT])
    {
        x-=2;
        if (x<54) x=54;
    }
    if (keystates[SDLK_RIGHT])
    {
        x+=2;
        if (x>910) x=910;//950-40
    }
    if (keystates[SDLK_UP])
    {
        y-=2;
        if (y<36) y=36;
    }
    if (keystates[SDLK_DOWN])
    {
        y+=2;
        if (y>677) y=677;//717-40
    }
}

void _player::coordonate(int a, int b)
{
    x=a;
    y=b;
}

void _player::deseneaza()
{
    if (invincibil==0)
        blit_surface(sup, screen, x, y, NULL);
    else
        blit_surface(sup2, screen, x, y, NULL);
}

void _fantoma::deschide(char* sir)
{
    sup=load_image(sir);
}

void _fantoma::coordonate(int a, int b)
{
    x=a;
    y=b;
}

void _fantoma::miscare()
{
    if (directie==0)//jos
    {
        y+=MISCARE;
        if (y>677) directie=1;
    }
    else if (directie==1)//sus
    {
        y-=MISCARE;
        if (y<36) directie=0;
    }
    else if (directie==2)//stanga
    {
        x+=MISCARE;
        if (x>910) directie=3;
    }
    else if (directie==3)//dreapta
    {
        x-=MISCARE;
        if (x<54) directie=2;
    }
}

void _fantoma::deseneaza()
{
    blit_surface(sup, screen, x, y, NULL);
}

void _catch_me::deschide(char *sir)
{
    sup=load_image(sir);
}

void _catch_me::coordonate(int a, int b)
{
    x=a;
    y=b;
}

void _catch_me::deseneaza()
{
    blit_surface(sup, screen, x, y, NULL);
}

void _catch_me::seteaza()
{
    //910-54=856 x
    //677-36=641 y
    x=rand()%856+54;
    y=rand()%641+36;
}

int _catch_me::got_me()
{
    if (abs(x-player.x)<=40 && abs(y-player.y)<=40) return 1;
    return 0;
}

void _fantoma::seteaza()
{
    x=rand()%856+54;
    y=rand()%641+36;
    directie=rand()%4;
}

int _fantoma::mori()
{
    if (abs(x-player.x)<=40 && abs(y-player.y)<=40) return 1;
    return 0;
}

_buton inapoi;
_buton start;
_buton instructiuni;
_buton optiuni;
_buton iesire;
_buton pauza;
_buton highscores;
_buton meniu;
_buton restart;


_fundal wallpaper;
_fundal text_instructiuni;
_fundal joc;
_fundal gameover;

_player player;

_fantoma fantoma[200];
_fantoma power_up[20];
_fantoma cooldown[20];//dureaza 5 sec
_fantoma invincible[20];

_catch_me catch_me;


SDL_Surface* screen;
SDL_Event x_out;
SDL_Surface *message;
TTF_Font *font;
SDL_Color textColor;

Mix_Music *muzica=NULL;
Mix_Chunk *clic=NULL;
_buton sound;

char high[13];

