//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The attributes of the square
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;

const int BAT_WIDTH = 100;
const int BAT_HEIGHT = 20;

const int BAT_SPEED = 20;

const int BRICK_WIDTH = 50;
const int BRICK_HEIGHT = 20;

//The surfaces
SDL_Surface *square = NULL;
SDL_Surface *bat = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

//The bats
class bats
{
    private:    
    //The velocity of the square
    int xVel;

    
    public:		
	//The collision box of the square
    SDL_Rect box;
	
	//Score
	int sco;
	
    //Initializes the variables
    void init(int x, int y);
    
    //Takes key presses and adjusts the square's velocity
    void handle_input(int speed);
       
    //Shows the square on the screen
    void show();
	
};
bats myBats;


//the ball
class ball
{
    private:
    //The collision box of the square
    SDL_Rect box;
    
    //The velocity of the square
    int xVel, yVel;
    
    public:
    //Initializes the variables
    void init();    
       
    //Shows the square on the screen
    void show();
};

//The bricks
class bricks
{
	private:

	
	//The number of collision 
	//int nbcol;
	
	public:
	//The collision box of the bricks
	SDL_Rect box;
	//Initializes the variables
    void init(int x, int y);
	void show();    
};

typedef struct bri
{
	bri* suiv;
	bricks mybrick;
} ListBrick;

ListBrick bricklist;

void create_list(ListBrick *br1,int nb)
{
	static int i=0;
	br1->mybrick.init((i%11)*(BRICK_WIDTH+3)+25,(i/11)*(BRICK_HEIGHT+3) + 50);
	br1->suiv = new ListBrick;
	if(++i < nb)
		create_list(br1->suiv,nb);
	else br1->suiv=NULL;
}
			
			
//The timer
class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;
    
    //The ticks stored when the timer was paused
    int pausedTicks;
    
    //The timer status
    bool paused;
    bool started;
    
    public:
    //Initializes variables
    Timer();
    
    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();
    
    //Get the number of ticks since the timer started
    //or gets the number of ticks when the timer was paused
    int get_ticks();
    
    //Checks the status of the timer
    bool is_started();
    bool is_paused();    
};

SDL_Surface *load_image( std::string filename ) 
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;
    
    //Load the image
    loadedImage = IMG_Load( filename.c_str() );
    
    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old surface
        SDL_FreeSurface( loadedImage );
        
        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }
    
    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;
    
    //Get offsets
    offset.x = x;
    offset.y = y;
    
    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool check_collision( SDL_Rect &A, SDL_Rect &B )
{
    //The sides of the rectangles
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    //Calculate the sides of rect A
    leftA = A.x;
    rightA = A.x + A.w;
    topA = A.y;
    bottomA = A.y + A.h;
        
    //Calculate the sides of rect B
    leftB = B.x;
    rightB = B.x + B.w;
    topB = B.y;
    bottomB = B.y + B.h;
            
    //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }
    
    if( topA >= bottomB )
    {
        return false;
    }
    
    if( rightA <= leftB )
    {
        return false;
    }
    
    if( leftA >= rightB )
    {
        return false;
    }
    
    //If none of the sides from A are outside B
    return true;
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;    
    }
    
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    //If there was in error in setting up the screen
    if( screen == NULL )
    {
        return false;    
    }
    
	
    //Set the window caption
    SDL_WM_SetCaption( "Casse-Brique", NULL );
    
    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the square image
    square = load_image( "square.bmp" );
	bat = load_image( "bat.bmp" );
    
    //If there was a problem in loading the square
    if( square == NULL )
    {
        return false;    
    }
	if( bat == NULL )
    {
        return false;    
    }
	
    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( square );
	SDL_FreeSurface( bat );
	SDL_FreeSurface( screen );
    //Quit SDL
    SDL_Quit();
}

void bats::init(int x, int y)
{
    //Initialize the offsets
    box.x = x;
    box.y = y;
	
	//Set the square's dimentions
    box.w = BAT_WIDTH;
    box.h = BAT_HEIGHT;
    
    //Initialize the velocity
    xVel = 0;
	sco = 0;

}

void ball::init()
{
    //Initialize the offsets
    box.x = SCREEN_WIDTH /2 - SQUARE_WIDTH /2;
    box.y = SCREEN_HEIGHT /2 - SQUARE_HEIGHT /2;
	
	//Set the square's dimentions
    box.w = SQUARE_WIDTH;
    box.h = SQUARE_HEIGHT;
    
    //Initialize the velocity
    xVel = 5;
	yVel = 5;
}


void bats::handle_input(int speed)
{
	xVel += speed;
}

void bats::show()
{
    //Move the square left or right
    box.x += xVel;
       
    //If the square went too far to the left or right or has collided with the wall
    if( ( box.x < 0 ) || ( box.x + BAT_WIDTH > SCREEN_WIDTH ) )
    {
        //Move back
        box.x -= xVel;
    }    
	
    //Show the square
    apply_surface( box.x, box.y, bat, screen );
}


void ball::show()
{
	int milball,milbat;
	ListBrick *lsuiv= &bricklist, *lavt=&bricklist;
	milball=box.x+SQUARE_WIDTH/2;
    //Move the square left or right
    box.x += xVel;
       
    //If the square went too far to the left or right or has collided with the wall
    if( ( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) )
    {
        //Move back
        box.x -= xVel;
		//Invert Velocity
		xVel = -xVel;
    }
	
	if( ( box.y < 0 ) )
	{
		box.y -= yVel;
		yVel = -yVel;
	}
	
	if ( (box.y + SQUARE_HEIGHT > SCREEN_HEIGHT))
	{
		box.x=SCREEN_WIDTH /2 - SQUARE_WIDTH /2;
		box.y=SCREEN_HEIGHT /2 - SQUARE_HEIGHT /2;
		yVel=5;
		xVel=5;
	}		
    
    //Move the square up or down
    box.y += yVel;
    
    //If the square went too far up or down or has collided with the wall
    if( check_collision( box, myBats.box ))
    {
		milbat=myBats.box.x + BAT_WIDTH/2;
        //Move back
        box.y -= yVel;
		//Invert velocity
		yVel = (yVel > 0)? -(yVel+1):-(yVel-1);
		xVel += (milball-milbat) /5;
		
    }
	while(lsuiv != NULL)
	{		
		if ( check_collision(box, lsuiv->mybrick.box) ) 
		{
			//Move back
			box.y -= yVel;
			//Invert velocity
			yVel = (yVel > 0)? -(yVel+1):-(yVel-1);
			xVel += 2;
			lavt->suiv=lsuiv->suiv;
			delete lsuiv;
			lsuiv= lavt->suiv;
						
		}
		
		else
		{
			lavt=lsuiv;
			lsuiv=lsuiv->suiv;
		}
		
	}
	
    
    //Show the square
    apply_surface( box.x, box.y, square, screen );
}

void bricks::init(int x, int y)
{
    //Initialize the offsets
    box.x = x;
    box.y = y;
	
	//Set the square's dimentions
    box.w = BRICK_WIDTH;
    box.h = BRICK_HEIGHT;

}

void bricks::show()
{
	SDL_FillRect( screen, &box, SDL_MapRGB( screen->format, 0, 0xFF, 0xFF ) );
}

Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;    
}

void Timer::start()
{
    //Start the timer
    started = true;
    
    //Unpause the timer
    paused = false;
    
    //Get the current clock time
    startTicks = SDL_GetTicks();    
}

void Timer::stop()
{
    //Stop the timer
    started = false;
    
    //Unpause the timer
    paused = false;    
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;
    
        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;
    
        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;
        
        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }    
    }
    
    //If the timer isn't running
    return 0;    
}

bool Timer::is_started()
{
    return started;    
}

bool Timer::is_paused()
{
    return paused;    
}

void listaff (ListBrick * bricklist)
{
	while(bricklist!=NULL)
	{
		bricklist->mybrick.show();
		bricklist=bricklist->suiv;
	}
}
	
void handle_input()
{
	if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: myBats.handle_input(-BAT_SPEED); break;
            case SDLK_RIGHT: myBats.handle_input(BAT_SPEED); break;
				default: break;						
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: myBats.handle_input(BAT_SPEED); break;
            case SDLK_RIGHT: myBats.handle_input(-BAT_SPEED); break;
				default: break;									
        }        
    }
}

int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;
	
	//String score for the screen
	char scor[10];
    
    //The ball
    ball mySquare;
	
	
    
	//init bats
	myBats.init((SCREEN_WIDTH - BAT_WIDTH) /2, SCREEN_HEIGHT - (5+BAT_HEIGHT));
	
    //init ball
	mySquare.init();
	
	create_list(&bricklist,66);

	//The frame rate regulator
    Timer fps;
    
    //Initialize
    if( init() == false )
    {
        return 1;
    }
    
    //Load the files
    if( load_files() == false )
    {
        return 1;
    }
    

    
    //While the user hasn't quit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();
        
        //While there's events to handle
        while ( SDL_PollEvent( &event ) )
        {
            //Handle events for the dot
            handle_input();
            
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        
        //Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
          
				
		listaff(&bricklist);
        //Show the square on the screen
        mySquare.show();
		
		//Show the bats
		myBats.show();


        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;    
        }
        
        //Cap the frame rate
        while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            //wait    
        }
    }
        
    //Clean up
    clean_up();
    
    return 0;    
}
