/*
 * main.cpp
 *
 *  Created on: 2010.03.04.
 *      Author: Szabó László
 */

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"

//minigames
#include "minigames/minig_class.hpp"
#include "minigames/fight_class.hpp"

#include <fstream>


#include <sstream>
#include <vector>

#include <time.h>


const int WIDTH = 640;
const int HEIGHT = 480;
const int BPP = 32;

const int PIRATE_WIDTH = 80;
const int PIRATE_HEIGHT = 80;

const int VELOCITY = 10;

const int FRAMES_PER_SECOND = 25;

//************MAIN VARIABLES

	SDL_Surface* screen = NULL;
	bool quit = false;
	bool start = false;
	bool restart = true;
	SDL_Event event;

//************MAIN VARIABLES*
SDL_Surface* load_image( std::string filename ){

	SDL_Surface* temp = NULL;
	SDL_Surface* rpic = NULL;

	temp = IMG_Load( filename.c_str() );

	if( temp != NULL ){
		rpic = SDL_DisplayFormat( temp );
		SDL_FreeSurface( temp );
	}

	SDL_SetColorKey( rpic, SDL_SRCCOLORKEY, SDL_MapRGB( rpic->format, 0, 0xFF, 0xFF ) );

	return rpic;
}

void apply_surface( int x, int y, SDL_Surface* src, SDL_Surface* dest, SDL_Rect* clip = NULL ){
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	SDL_BlitSurface( src, clip, dest, &offset );
}

class TIMER{
	private:

	int
	startTicks;

	public:

	TIMER();
	void start();
	int get_ticks();
};

TIMER :: TIMER(){
	startTicks = 0;
}

void TIMER :: start(){
	startTicks = SDL_GetTicks();
}

int TIMER :: get_ticks(){
	return SDL_GetTicks() - startTicks;
}

class CANNONBALL{
	private:

		int
		x, y,
		xVel, yVel, rVel,
		airway;

		SDL_Surface* sprite_sheet;

		std::vector< SDL_Rect > c_boxes;

		Mix_Chunk* effect;

	public:
		CANNONBALL();
		~CANNONBALL();

		void move();
		void start_over();
		void show( SDL_Surface* dest );
		void shift_boxes();
		std::vector< SDL_Rect > & get_c_boxes();

		void zero_vel();
};

CANNONBALL :: ~CANNONBALL(){
	SDL_FreeSurface( sprite_sheet );
	Mix_FreeChunk( effect );
}

void CANNONBALL :: zero_vel(){
	xVel = 5;
	yVel = 5;
}

std::vector< SDL_Rect > & CANNONBALL :: get_c_boxes(){
	return c_boxes;
}

void CANNONBALL :: shift_boxes(){
	c_boxes[ 0 ].x = x+0;
	c_boxes[ 0 ].y = y+7;
	c_boxes[ 0 ].w = 20;
	c_boxes[ 0 ].h = 6;

	c_boxes[ 1 ].x = x+7;
	c_boxes[ 1 ].y = y+0;
	c_boxes[ 1 ].w = 6;
	c_boxes[ 1 ].h = 20;
}

void CANNONBALL :: start_over(){

	Mix_PlayChannel( -1, effect, 0 );

	airway = rand() % 4;
	if( airway == 0 ){
		x = 0;
		y = rand() % 440 + 20;
		if( rVel % 2 == 0 ) xVel += 1;
	}

	if( airway == 1 ){
		x = WIDTH;
		y = rand() % 440 + 20;
		if( rVel % 2 == 0 ) xVel += 1;
	}

	if( airway == 2 ){
		y = 0;
		x = rand() % 600 + 20;
		if( rVel % 2 == 0 ) yVel += 1;
	}

	if( airway == 3 ){
		y = HEIGHT;
		x = rand() % 600 + 20;
		if( rVel % 2 == 0 ) yVel += 1;
	}

	rVel += 1;

	shift_boxes();
}

CANNONBALL :: CANNONBALL(){

	effect = Mix_LoadWAV( "sfx/cannon.wav" );

	sprite_sheet = load_image( "sprites\\ball.bmp" );
	x = 0;
	y = 0;
	xVel = 5;
	yVel = 5;
	rVel = 0;

	airway = 0;

	c_boxes.resize( 2 );

	shift_boxes();
}

void CANNONBALL :: move(){
	if( airway == 0 ) x += xVel;
	if( airway == 1 ) x -= xVel;
	if( airway == 2 ) y += yVel;
	if( airway == 3 ) y -= yVel;

	shift_boxes();

	if( x > WIDTH + 40 || y > HEIGHT + 40 || x < -40 || y < -40 )
	start_over();
}

void CANNONBALL :: show( SDL_Surface* dest ){
	apply_surface( x, y, sprite_sheet, dest );
}


class PIRATE{
	private:

		bool moved;

		int
		x, y,
		xVel, yVel,
		frame,
		status,
		g_counter;

		bool
		ghost,
		dead;

		SDL_Rect
		offSets[ 4 ][ 3 ];

		SDL_Surface*
		sprite_sheet;

		std::vector< SDL_Rect > c_boxes;

	public:
		PIRATE();
		~PIRATE();

		void make_position( int X, int Y );
		void handle_input( SDL_Event e );
		void move();
		void show( SDL_Surface* dest );

		void zero_vel();

		void shift_boxes();

		std::vector< SDL_Rect > & get_c_boxes();

		void go_ghost();
		void not_ghost();
		void die();
		void live();
		bool is_ghost();
		bool is_dead();

		void not_moved();

};

void PIRATE :: not_moved(){
	moved = false;
}

void PIRATE :: die(){
	dead = true;
}
void PIRATE :: live(){
	dead = false;
}
bool PIRATE :: is_dead(){
	return dead;
}

bool PIRATE :: is_ghost(){
	return ghost;
}

void PIRATE :: go_ghost(){
	ghost = true;
	SDL_SetAlpha( sprite_sheet, SDL_SRCALPHA, 100 );
}

void PIRATE :: not_ghost(){
	ghost = false;
	SDL_SetAlpha( sprite_sheet, SDL_SRCALPHA, 255 );
}
PIRATE :: ~PIRATE(){
	SDL_FreeSurface( sprite_sheet );
}

std::vector< SDL_Rect > & PIRATE :: get_c_boxes(){
	return c_boxes;
}

void PIRATE :: zero_vel(){
	xVel = 0;
	yVel = 0;
}

void PIRATE :: shift_boxes(){
		c_boxes[ 0 ].x = x+27;
		c_boxes[ 0 ].y = y+10;
		c_boxes[ 0 ].w = 31;
		c_boxes[ 0 ].h = 28;

		c_boxes[ 1 ].x = x+16;
		c_boxes[ 1 ].y = y+38;
		c_boxes[ 1 ].w = 40;
		c_boxes[ 1 ].h = 28;
}

PIRATE :: PIRATE(){

	moved = false;

	x = 0;
	y = 0;
	xVel = 0;
	yVel = 0;

	dead = false;
	ghost = false;
	g_counter = 0;

	frame = 0;
	status = 0;

	sprite_sheet = load_image( "sprites\\pirate_ssheet.png" );

	//COLLISION BOXES
	c_boxes.resize( 2 );
	shift_boxes();

	//RIGHT MOVE
	offSets[ 0 ][ 0 ].x = 0;
	offSets[ 0 ][ 0 ].y = 0;
	offSets[ 0 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 0 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 0 ][ 1 ].y = 0;
	offSets[ 0 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 0 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 0 ][ 2 ].y = 0;
	offSets[ 0 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 2 ].h = PIRATE_HEIGHT;

	//LEFT MOVE
	offSets[ 1 ][ 0 ].x = 0;
	offSets[ 1 ][ 0 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 1 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 1 ][ 1 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 1 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 1 ][ 2 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 2 ].h = PIRATE_HEIGHT;

	//UP MOVE
	offSets[ 2 ][ 0 ].x = 0;
	offSets[ 2 ][ 0 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 2 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 2 ][ 1 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 2 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 2 ][ 2 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 2 ].h = PIRATE_HEIGHT;

	//DOWN MOVE

	offSets[ 3 ][ 0 ].x = 0;
	offSets[ 3 ][ 0 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 3 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 3 ][ 1 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 3 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 3 ][ 2 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 2 ].h = PIRATE_HEIGHT;

}

void PIRATE :: make_position( int X, int Y ){
	x = X;
	y = Y;
}

void PIRATE :: handle_input( SDL_Event e ){
	if( e.type == SDL_KEYDOWN ){
		switch( e.key.keysym.sym ){

			case SDLK_UP :    if( yVel > -VELOCITY ) yVel -= VELOCITY; moved = true; break;
			case SDLK_DOWN :  if( yVel < VELOCITY  ) yVel += VELOCITY; moved = true; break;
			case SDLK_LEFT :  if( xVel > -VELOCITY ) xVel -= VELOCITY; moved = true; break;
			case SDLK_RIGHT : if( xVel < VELOCITY  ) xVel += VELOCITY; moved = true; break;

			default : break;
		}
	}

	if( moved )
	if( e.type == SDL_KEYUP ){
		switch( e.key.keysym.sym ){

			case SDLK_UP :    if( yVel < VELOCITY  ) yVel  += VELOCITY; break;
			case SDLK_DOWN :  if( yVel > -VELOCITY ) yVel  -= VELOCITY; break;
			case SDLK_LEFT :  if( xVel < VELOCITY  ) xVel  += VELOCITY; break;
			case SDLK_RIGHT : if( xVel > -VELOCITY ) xVel  -= VELOCITY; break;

			default : break;
		}
	}
}

void PIRATE :: move(){
	x += xVel;
	shift_boxes();
	if( x < 20 || x + PIRATE_WIDTH > WIDTH - 20 ){
		x -= xVel;
		shift_boxes();
	}

	y += yVel;
	shift_boxes();
	if( y < -40 || y + PIRATE_HEIGHT > HEIGHT - 30 ){
		y -= yVel;
		shift_boxes();
	}
}

void PIRATE :: show( SDL_Surface* dest ){
	if( xVel > 0 ){
		status = 0;
		frame++;
	}
	else if( xVel < 0 ){
		status = 1;
		frame++;
	}
	else if( yVel < 0 ){
		status = 2;
		frame++;
	}
	else if( yVel > 0 ){
		status = 3;
		frame++;
	}
	else frame = 0;

	if( frame >= 8 )frame = 1;

	apply_surface( x, y, sprite_sheet, dest, &offSets[ status ][ frame / 3 ] );
}

class PIRATE2{
	private:

		bool moved;

		int
		x, y,
		xVel, yVel,
		frame,
		status,
		g_counter;

		bool
		ghost,
		dead;

		SDL_Rect
		offSets[ 4 ][ 3 ];

		SDL_Surface*
		sprite_sheet;

		std::vector< SDL_Rect > c_boxes;

	public:
		PIRATE2();
		~PIRATE2();

		void make_position( int X, int Y );
		void handle_input( SDL_Event e );
		void move();
		void show( SDL_Surface* dest );

		void zero_vel();

		void shift_boxes();

		std::vector< SDL_Rect > & get_c_boxes();

		void go_ghost();
		void not_ghost();
		void die();
		void live();
		bool is_dead();
		bool is_ghost();

		void not_moved();

};

void PIRATE2 :: not_moved(){
	moved = false;
}

void PIRATE2 :: die(){
	dead = true;
}
void PIRATE2 :: live(){
	dead = false;
}
bool PIRATE2 :: is_dead(){
	return dead;
}

bool PIRATE2 :: is_ghost(){
	return ghost;
}

void PIRATE2 :: go_ghost(){
	ghost = true;
	SDL_SetAlpha( sprite_sheet, SDL_SRCALPHA, 100 );
}

void PIRATE2 :: not_ghost(){
	ghost = false;
	SDL_SetAlpha( sprite_sheet, SDL_SRCALPHA, 255 );
}
PIRATE2 :: ~PIRATE2(){
	SDL_FreeSurface( sprite_sheet );
}

std::vector< SDL_Rect > & PIRATE2 :: get_c_boxes(){
	return c_boxes;
}

void PIRATE2 :: zero_vel(){
	xVel = 0;
	yVel = 0;
}

void PIRATE2 :: shift_boxes(){
		c_boxes[ 0 ].x = x+27;
		c_boxes[ 0 ].y = y+10;
		c_boxes[ 0 ].w = 31;
		c_boxes[ 0 ].h = 28;

		c_boxes[ 1 ].x = x+16;
		c_boxes[ 1 ].y = y+38;
		c_boxes[ 1 ].w = 40;
		c_boxes[ 1 ].h = 28;
}

PIRATE2 :: PIRATE2(){
	moved = false;
	x = 0;
	y = 0;
	xVel = 0;
	yVel = 0;

	dead = false;
	ghost = false;
	g_counter = 0;

	frame = 0;
	status = 0;

	sprite_sheet = load_image( "sprites\\pirate2_ssheet.png" );

	//COLLISION BOXES
	c_boxes.resize( 2 );
	shift_boxes();

	//RIGHT MOVE
	offSets[ 0 ][ 0 ].x = 0;
	offSets[ 0 ][ 0 ].y = 0;
	offSets[ 0 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 0 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 0 ][ 1 ].y = 0;
	offSets[ 0 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 0 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 0 ][ 2 ].y = 0;
	offSets[ 0 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 0 ][ 2 ].h = PIRATE_HEIGHT;

	//LEFT MOVE
	offSets[ 1 ][ 0 ].x = 0;
	offSets[ 1 ][ 0 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 1 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 1 ][ 1 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 1 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 1 ][ 2 ].y = PIRATE_HEIGHT;
	offSets[ 1 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 1 ][ 2 ].h = PIRATE_HEIGHT;

	//UP MOVE
	offSets[ 2 ][ 0 ].x = 0;
	offSets[ 2 ][ 0 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 2 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 2 ][ 1 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 2 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 2 ][ 2 ].y = PIRATE_HEIGHT*2;
	offSets[ 2 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 2 ][ 2 ].h = PIRATE_HEIGHT;

	//DOWN MOVE

	offSets[ 3 ][ 0 ].x = 0;
	offSets[ 3 ][ 0 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 0 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 0 ].h = PIRATE_HEIGHT;

	offSets[ 3 ][ 1 ].x = PIRATE_WIDTH;
	offSets[ 3 ][ 1 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 1 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 1 ].h = PIRATE_HEIGHT;

	offSets[ 3 ][ 2 ].x = PIRATE_WIDTH*2;
	offSets[ 3 ][ 2 ].y = PIRATE_HEIGHT*3;
	offSets[ 3 ][ 2 ].w = PIRATE_WIDTH;
	offSets[ 3 ][ 2 ].h = PIRATE_HEIGHT;

}

void PIRATE2 :: make_position( int X, int Y ){
	x = X;
	y = Y;
}

void PIRATE2 :: handle_input( SDL_Event e ){
	if( e.type == SDL_KEYDOWN ){
		switch( e.key.keysym.sym ){

			case SDLK_w : if( yVel > -VELOCITY ) yVel -= VELOCITY; break;
			case SDLK_s : if( yVel < VELOCITY  ) yVel += VELOCITY; break;
			case SDLK_a : if( xVel > -VELOCITY ) xVel -= VELOCITY; break;
			case SDLK_d : if( xVel < VELOCITY  ) xVel += VELOCITY; break;

			default : break;
		}
		moved = true;
	}

	if( moved )
	if( e.type == SDL_KEYUP ){
		switch( e.key.keysym.sym ){

			case SDLK_w : if( yVel < VELOCITY  ) yVel += VELOCITY; break;
			case SDLK_s : if( yVel > -VELOCITY ) yVel -= VELOCITY; break;
			case SDLK_a : if( xVel < VELOCITY  ) xVel += VELOCITY; break;
			case SDLK_d : if( xVel > -VELOCITY ) xVel -= VELOCITY; break;

			default : break;
		}
	}
}

void PIRATE2 :: move(){
	x += xVel;
	shift_boxes();
	if( x < 20 || x + PIRATE_WIDTH > WIDTH - 20 ){
		x -= xVel;
		shift_boxes();
	}

	y += yVel;
	shift_boxes();
	if( y < -40 || y + PIRATE_HEIGHT > HEIGHT - 30 ){
		y -= yVel;
		shift_boxes();
	}
}

void PIRATE2 :: show( SDL_Surface* dest ){
	if( xVel > 0 ){
		status = 0;
		frame++;
	}
	else if( xVel < 0 ){
		status = 1;
		frame++;
	}
	else if( yVel < 0 ){
		status = 2;
		frame++;
	}
	else if( yVel > 0 ){
		status = 3;
		frame++;
	}
	else frame = 0;

	if( frame >= 8 )frame = 1;

	apply_surface( x, y, sprite_sheet, dest, &offSets[ status ][ frame / 3 ] );

}

class COIN{
	private:

		int
		x, y,
		frame;

		SDL_Surface* sprite_sheet;

		SDL_Rect offSets[ 4 ];

		std::vector< SDL_Rect > c_boxes;

		Mix_Chunk* effect;

	public:

		COIN();
		~COIN();

		void replace();
		void show( SDL_Surface* dest );

		std::vector< SDL_Rect >& get_c_boxes();
};

COIN :: ~COIN(){
	SDL_FreeSurface( sprite_sheet );
	Mix_FreeChunk( effect );
}

COIN :: COIN(){

	effect = Mix_LoadWAV( "sfx/coin.wav" );

	x = rand() % 520 + 40;
	y = rand() % 375 + 40;
	frame = 0;

	sprite_sheet = load_image( "sprites\\treasure_sheet.png" );



	offSets[ 0 ].x = 0;
	offSets[ 0 ].y = 0;
	offSets[ 0 ].w = 40;
	offSets[ 0 ].h = 70;

	offSets[ 1 ].x = 40;
	offSets[ 1 ].y = 0;
	offSets[ 1 ].w = 40;
	offSets[ 1 ].h = 70;

	offSets[ 2 ].x = 80;
	offSets[ 2 ].y = 0;
	offSets[ 2 ].w = 40;
	offSets[ 2 ].h = 70;

	offSets[ 3 ].x = 120;
	offSets[ 3 ].y = 0;
	offSets[ 3 ].w = 40;
	offSets[ 3 ].h = 70;

	c_boxes.resize( 1 );
}

void COIN :: replace(){

	Mix_PlayChannel( -1, effect, 0 );

	x = rand() % 520 + 40;
	y = rand() % 375 + 40;

	c_boxes[ 0 ].x = x;
	c_boxes[ 0 ].y = y;
	c_boxes[ 0 ].w = 40;
	c_boxes[ 0 ].h = 40;
}

void COIN :: show( SDL_Surface* dest ){
	frame++;
	if( frame >= 15 )frame = 0;
	apply_surface( x, y, sprite_sheet, dest, &offSets[ frame / 4 ] );
}

std::vector< SDL_Rect >& COIN :: get_c_boxes(){
	return c_boxes;
}

class RUBY{
	private:

		int
		x, y,
		frame;

		bool
		collected;

		SDL_Surface* sprite_sheet;
		SDL_Rect offSets[ 4 ];
		std::vector< SDL_Rect > c_boxes;

		Mix_Chunk *effect;

	public:
		RUBY();
		~RUBY();

		void show( SDL_Surface* dest );
		void replace();
		void collect();
		bool is_collected();

		std::vector< SDL_Rect >& get_c_boxes();

};

RUBY :: RUBY(){

	effect = Mix_LoadWAV( "sfx/ruby.wav" );

	x = 0;
	y = 0;
	frame = 0;

	collected = true;

	sprite_sheet = load_image( "sprites\\r_sheet.png" );
	c_boxes.resize( 1 );

	offSets[ 0 ].x = 0;
	offSets[ 0 ].y = 0;
	offSets[ 0 ].w = 40;
	offSets[ 0 ].h = 70;

	offSets[ 1 ].x = 40;
	offSets[ 1 ].y = 0;
	offSets[ 1 ].w = 40;
	offSets[ 1 ].h = 70;

	offSets[ 2 ].x = 80;
	offSets[ 2 ].y = 0;
	offSets[ 2 ].w = 40;
	offSets[ 2 ].h = 70;

	offSets[ 3 ].x = 120;
	offSets[ 3 ].y = 0;
	offSets[ 3 ].w = 40;
	offSets[ 3 ].h = 70;
}

RUBY :: ~RUBY(){
	SDL_FreeSurface( sprite_sheet );
	Mix_FreeChunk( effect );
}

void RUBY :: show( SDL_Surface* dest ){
	if( collected == false ){
		frame++;
		if( frame >= 15 )frame = 0;
		apply_surface( x, y, sprite_sheet, dest, &offSets[ frame / 4 ] );
	}
}
void RUBY :: replace(){
	if( collected == true ){
		x = rand() % 560 + 30;
		y = rand() % 375 + 40;
		collected = false;
	}

	c_boxes[ 0 ].x = x;
	c_boxes[ 0 ].y = y;
	c_boxes[ 0 ].w = 40;
	c_boxes[ 0 ].h = 40;
}
void RUBY :: collect(){
	collected = true;
	Mix_PlayChannel( -1, effect, 0 );
}

bool RUBY :: is_collected(){
	return collected;
}

std::vector< SDL_Rect >& RUBY :: get_c_boxes(){
	return c_boxes;
}

bool check_collision( std::vector< SDL_Rect > & A, std::vector< SDL_Rect > & B ){
	int
	topA, topB,
	rightA, rightB,
	leftA, leftB,
	bottomA, bottomB;

	for( unsigned int a = 0; a < A.size(); a++ ){

		topA = A[ a ].y;
		bottomA = A[ a ].y + A[ a ].h;
		leftA = A[ a ].x;
		rightA = A[ a ].x + A[ a ].w;

		for( unsigned int b = 0; b < B.size(); b++ ){

			topB = B[ b ].y;
			bottomB = B[ b ].y + B[ b ].h;
			leftB = B[ b ].x;
			rightB = B[ b ].x + B[ b ].w;

			if( ( topA >= bottomB || bottomA <= topB || leftA >= rightB || rightA <= leftB ) == false )
				return true;

		}
	}
	return false;
}

bool check_collision( std::vector< SDL_Rect > & A, SDL_Rect B ){
	int
	topA, topB,
	rightA, rightB,
	leftA, leftB,
	bottomA, bottomB;

			topB = B.y;
			bottomB = B.y + B.h;
			leftB = B.x;
			rightB = B.x + B.w;

	for( unsigned int a = 0; a < A.size(); a++ ){

		topA = A[ a ].y;
		bottomA = A[ a ].y + A[ a ].h;
		leftA = A[ a ].x;
		rightA = A[ a ].x + A[ a ].w;



			if( ( topA >= bottomB || bottomA <= topB || leftA >= rightB || rightA <= leftB ) == false )
				return true;

	}
	return false;
}

class GHOSTPIRATE{

	private:

		int
		x, y,
		frame;

		bool
		collected;

		SDL_Surface* sprite_sheet;

		SDL_Rect offSets[ 4 ];

		std::vector< SDL_Rect > c_boxes;

		Mix_Chunk* effect;

	public:

		GHOSTPIRATE();
		~GHOSTPIRATE();

		void show( SDL_Surface* dest );
		void replace();
		bool is_collected();
		void collect();
		std::vector< SDL_Rect >& get_c_boxes();
};

std::vector< SDL_Rect >& GHOSTPIRATE :: get_c_boxes(){
	return c_boxes;
}

void GHOSTPIRATE :: show( SDL_Surface* dest ){
	if( collected == false ){
			frame++;
			if( frame >= 15 )frame = 0;
			apply_surface( x, y, sprite_sheet, dest, &offSets[ frame / 4 ] );
	}
}

void GHOSTPIRATE :: replace(){
	if( collected == true ){
		x = rand() % 550 + 30;
		y = rand() % 375 + 40;
		collected = false;

		c_boxes[ 0 ].x = x;
		c_boxes[ 0 ].y = y;
		c_boxes[ 0 ].w = 40;
		c_boxes[ 0 ].h = 40;
	}
}

bool GHOSTPIRATE :: is_collected(){
	return collected;
}

void GHOSTPIRATE :: collect(){
	Mix_PlayChannel( -1, effect, 0 );
	collected = true;
}

GHOSTPIRATE :: GHOSTPIRATE(){

	effect = Mix_LoadWAV( "sfx/ghost.wav" );

	x = 0;
	y = 0;

	frame = 0;

	collected = true;

	sprite_sheet = load_image( "sprites\\ghost_sheet.png" );

	c_boxes.resize( 1 );

	offSets[ 0 ].x = 0;
	offSets[ 0 ].y = 0;
	offSets[ 0 ].w = 40;
	offSets[ 0 ].h = 70;

	offSets[ 1 ].x = 40;
	offSets[ 1 ].y = 0;
	offSets[ 1 ].w = 40;
	offSets[ 1 ].h = 70;

	offSets[ 2 ].x = 80;
	offSets[ 2 ].y = 0;
	offSets[ 2 ].w = 40;
	offSets[ 2 ].h = 70;

	offSets[ 3 ].x = 120;
	offSets[ 3 ].y = 0;
	offSets[ 3 ].w = 40;
	offSets[ 3 ].h = 70;
}

GHOSTPIRATE :: ~GHOSTPIRATE(){
	SDL_FreeSurface( sprite_sheet );
	Mix_FreeChunk( effect );
}

class TREASURE{

	private:

		int
		x, y,
		frame;

		bool up[ 7 ][ 7 ];

		bool
		c_collected;

		SDL_Rect offSets[ 4 ];

		SDL_Surface* sprite_sheet;
		SDL_Surface* chest_sheet;

		Mix_Chunk* effect1;
		Mix_Chunk* effect2;

		std::vector< SDL_Rect > chest_box;

	public:

		TREASURE();
		~TREASURE();

		void show( SDL_Surface* dest );

		void replace();
		void collect();

		bool is_collected();

		void gather( std::vector< SDL_Rect >& A, int & score );

		std::vector< SDL_Rect >& get_c_boxes();
};

bool TREASURE :: is_collected(){
	return c_collected;
}

void TREASURE :: gather( std::vector< SDL_Rect >& A, int & score ){
	std::vector< SDL_Rect > temp_box;
	temp_box.resize( 1 );
	for( unsigned int i = 0; i < 7; i++ )
		for( unsigned int j = 0; j < 7; j++ ){

			temp_box[ 0 ].x = ( WIDTH / 7 )*i + 30;
			temp_box[ 0 ].y = ( HEIGHT / 7 )*j;
			temp_box[ 0 ].w = 40;
			temp_box[ 0 ].h = 40;

			if( up[ i ][ j ] && check_collision( temp_box, A )){
				score += 500;
				up[ i ][ j ] = false;
				Mix_PlayChannel( -1, effect2, 0 );
			}

		}
}

std::vector< SDL_Rect >& TREASURE :: get_c_boxes(){
	return chest_box;
}

void TREASURE :: collect(){
	c_collected = true;
	Mix_PlayChannel( -1, effect1, 0 );
	for( unsigned int i = 0; i < 7; i++ )
		for( unsigned int j = 0; j < 7; j++ )
			up[ i ][ j ] = true;
}

void TREASURE :: replace(){
		x = rand() % 570 + 30;
		y = rand() % 375 + 40;

		c_collected = false;

		chest_box[ 0 ].x = x;
		chest_box[ 0 ].y = y;
		chest_box[ 0 ].w = 40;
		chest_box[ 0 ].h = 70;
}

TREASURE :: TREASURE(){

	effect1 = Mix_LoadWAV( "sfx/treasure.wav" );
	effect2 = Mix_LoadWAV( "sfx/coin.wav" );

	sprite_sheet = load_image( "sprites\\treasure_sheet.png" );
	chest_sheet = load_image( "sprites\\chest_sheet.png" );

	frame = 0;

	chest_box.resize( 1 );

	c_collected = false;

	for( unsigned int i = 0; i < 7; i++ ){
		for( unsigned int j = 0; j < 7; j++ )
			up[ i ][ j ] = false;
	}

	offSets[ 0 ].x = 0;
	offSets[ 0 ].y = 0;
	offSets[ 0 ].w = 40;
	offSets[ 0 ].h = 70;

	offSets[ 1 ].x = 40;
	offSets[ 1 ].y = 0;
	offSets[ 1 ].w = 40;
	offSets[ 1 ].h = 70;

	offSets[ 2 ].x = 80;
	offSets[ 2 ].y = 0;
	offSets[ 2 ].w = 40;
	offSets[ 2 ].h = 70;

	offSets[ 3 ].x = 120;
	offSets[ 3 ].y = 0;
	offSets[ 3 ].w = 40;
	offSets[ 3 ].h = 70;
}

TREASURE :: ~TREASURE(){
	SDL_FreeSurface( sprite_sheet );
	Mix_FreeChunk( effect1 );
	Mix_FreeChunk( effect2 );
}

void TREASURE :: show( SDL_Surface* dest ){
	if( c_collected == true ){
		frame++;
		if( frame >= 15 )frame = 0;

		for( unsigned int i = 0; i < 7; i++ ){
			for( unsigned int j = 0; j < 7; j++ )
				if( up[ i ][ j ] ){
					apply_surface( ( (WIDTH / 7 ) * i + 30 ), ( (HEIGHT / 8) * j + 30 ), sprite_sheet, dest, &offSets[ frame / 4 ] );
				}
		}
	}
	else{
		frame++;
		if( frame >= 15 ) frame = 0;
		apply_surface( x, y, chest_sheet, dest, &offSets[ frame / 4 ] );
	}

}

int main( int argc, char** args ){

	srand( time( NULL ) );

	SDL_Init( SDL_INIT_EVERYTHING );
	Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 );
	TTF_Init();
	screen = SDL_SetVideoMode( WIDTH, HEIGHT, BPP, SDL_FULLSCREEN );
	SDL_WM_SetCaption( "Animation", NULL );


	minis *myminis = new minis;
	fighting *myfight = new fighting;




	TIMER fps;

	int score = 0,
		ghost_counter = 0,
		ghost_counter2 = 0;

	//LIVES
	int lives1, lives2;
	SDL_Surface* heart_sheet = load_image( "sprites/heart_sheet.png" );
	SDL_SetAlpha( heart_sheet, SDL_SRCALPHA, 100 );
	SDL_Rect heart_offset[ 2 ];
	heart_offset[ 0 ].x = 0;
	heart_offset[ 0 ].y = 0;
	heart_offset[ 0 ].w = 40;
	heart_offset[ 0 ].h = 40;

	heart_offset[ 1 ].x = 40;
	heart_offset[ 1 ].y = 0;
	heart_offset[ 1 ].w = 40;
	heart_offset[ 1 ].h = 40;

	bool multi = false;

	PIRATE *myPirate = new PIRATE;
	CANNONBALL *myCannonball = new CANNONBALL;
	COIN *myCoin = new COIN;
	RUBY *myRuby = new RUBY;
	GHOSTPIRATE *myGPIRATE = new GHOSTPIRATE;
	PIRATE2 *myPirate2 = new PIRATE2;
	TREASURE *myTreasure = new TREASURE;

	SDL_Surface* message = NULL;
	SDL_Surface* bg = load_image( "sprites\\bg.jpg" );

	TTF_Font* font = NULL;
	TTF_Font* font2 = NULL;

	SDL_Color textcolor = { 200, 150, 0 };
	SDL_Color textcolor2 = { 100, 50, 10 };

	font = TTF_OpenFont( "title.ttf", 86 );
	font2 = TTF_OpenFont( "text.ttf", 36 );


	Mix_Music* music;




	start = false;
	quit = false;
	score = 0;


	Mix_HaltMusic();
	Mix_FreeMusic( music );
	music = Mix_LoadMUS( "sfx\\open.wav" );
	Mix_PlayMusic( music, -1 );

	SDL_Surface* logo = NULL;
	logo = load_image( "JF_pics\\jf_logo.gif" );
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0 ) );
	apply_surface( ( WIDTH - logo->w ) / 2, ( HEIGHT - logo->h ) /2, logo, screen );
	SDL_Flip( screen );
	SDL_Delay( 5000 );

	SDL_FreeSurface( logo );
	logo = load_image( "JF_pics\\comp_logo.png" );
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );
	apply_surface( ( WIDTH - logo->w ) / 2, ( HEIGHT - logo->h ) /2, logo, screen );
	SDL_Flip( screen );
	SDL_Delay( 5000 );

	SDL_FreeSurface( logo );
	logo = load_image( "sprites\\main_pic.png" );
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );
	apply_surface( ( WIDTH - logo->w ) / 2, ( HEIGHT - logo->h ) /2, logo, screen );
	SDL_Flip( screen );
	SDL_Delay( 5000 );

	myPirate->make_position( (WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 - 80  );
	if( multi == true ){
		myPirate2->make_position(( WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 + 80 );
	}

	myRuby->replace();
	myGPIRATE->replace();
	while( !start ){
		while( SDL_PollEvent( &event ) ){

			if( event.type == SDL_KEYDOWN )
				if( event.key.keysym.sym == SDLK_RETURN )
					start = true;
				else if( event.key.keysym.sym == SDLK_ESCAPE ){
					start = true;
					quit = true;
				}
		}

		apply_surface( 0, 0, bg, screen );

		message = TTF_RenderText_Solid( font, "Pirate Trap", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 10, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "Game by:", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 120, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "László Szabó", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 170, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "lezli01.atw.hu", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 205, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "Your goal is to earn more and more", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 250, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "score by avoiding the cannon balls!", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 300, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "Next ~ Enter", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 400, message, screen );
		SDL_FreeSurface( message );

		SDL_Flip( screen );
	}


	start = false;
	while( !start ){
			while( SDL_PollEvent( &event ) ){

				if( event.type == SDL_KEYDOWN )
					if( event.key.keysym.sym == SDLK_RETURN )
						start = true;
					else if( event.key.keysym.sym == SDLK_ESCAPE ){
						start = true;
						quit = true;
					}
			}

			apply_surface( 0, 0, bg, screen );

			message = TTF_RenderText_Solid( font, "Pirate Trap", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 10, message, screen );
			SDL_FreeSurface( message );

			message = TTF_RenderText_Solid( font2, "Hints:", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 120, message, screen );
			SDL_FreeSurface( message );


			message = TTF_RenderText_Solid( font2, "~The cannon balls are getting faster", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 200, message, screen );
			SDL_FreeSurface( message );

			message = TTF_RenderText_Solid( font2, "and faster while you are playing!", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 250, message, screen );
			SDL_FreeSurface( message );

			message = TTF_RenderText_Solid( font2, "~Collect the treasures for 500 scores!", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 300, message, screen );
			SDL_FreeSurface( message );

			message = TTF_RenderText_Solid( font2, "Begin ~ Enter       Quit ~ Escape", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 400, message, screen );
			SDL_FreeSurface( message );

			SDL_Flip( screen );
		}

	start = false;
		while( !start ){
				while( SDL_PollEvent( &event ) ){

					if( event.type == SDL_KEYDOWN )
						if( event.key.keysym.sym == SDLK_RETURN )
							start = true;
						else if( event.key.keysym.sym == SDLK_ESCAPE ){
							start = true;
							quit = true;
						}
				}

				apply_surface( 0, 0, bg, screen );

				message = TTF_RenderText_Solid( font, "Pirate Trap", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 10, message, screen );
				SDL_FreeSurface( message );

				message = TTF_RenderText_Solid( font2, "Hints:", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 120, message, screen );
				SDL_FreeSurface( message );


				message = TTF_RenderText_Solid( font2, "~Collect the rubies for 2500 point!", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 200, message, screen );
				SDL_FreeSurface( message );

				message = TTF_RenderText_Solid( font2, "~Sometimes an old pirate's ghost appears,", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 250, message, screen );
				SDL_FreeSurface( message );

				message = TTF_RenderText_Solid( font2, "get him if you want to go across the cannon balls!", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 300, message, screen );
				SDL_FreeSurface( message );

				message = TTF_RenderText_Solid( font2, "Next ~ Enter       Quit ~ Escape", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 400, message, screen );
				SDL_FreeSurface( message );

				SDL_Flip( screen );
			}


while( restart ){

	start = false;
	quit = false;
	score = 0;


	Mix_HaltMusic();
	Mix_FreeMusic( music );
	music = Mix_LoadMUS( "sfx\\open.wav" );
	Mix_PlayMusic( music, -1 );

	myPirate->make_position( (WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 - 80  );
	if( multi == true ){
		myPirate2->make_position(( WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 + 80 );
	}

	myRuby->replace();
	myGPIRATE->replace();
	myTreasure->replace();
	myminis->place();

		start = false;
			while( !start ){
					while( SDL_PollEvent( &event ) ){

						if( event.type == SDL_KEYDOWN )
							if( event.key.keysym.sym == SDLK_RETURN )
								start = true;
							else if( event.key.keysym.sym == SDLK_ESCAPE ){
								start = true;
								quit = true;
							}
							else if( event.key.keysym.sym == SDLK_SPACE ){
								multi = !multi;
							}
					}

					apply_surface( 0, 0, bg, screen );

					message = TTF_RenderText_Solid( font, "Pirate Trap", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 10, message, screen );
					SDL_FreeSurface( message );

					message = TTF_RenderText_Solid( font2, "MULTIPLAYER", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 120, message, screen );
					SDL_FreeSurface( message );


					message = TTF_RenderText_Solid( font2, "Press [space] to change the mode!", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 170, message, screen );
					SDL_FreeSurface( message );

					message = TTF_RenderText_Solid( font2, "Multiplayer:", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 220, message, screen );
					SDL_FreeSurface( message );

					if( multi == true ){
						message = TTF_RenderText_Solid( font2, "~YES", textcolor2 );
						apply_surface( (WIDTH - message->w)/2, 270, message, screen );
						SDL_FreeSurface( message );
					}
					else{
						message = TTF_RenderText_Solid( font2, "~NO", textcolor2 );
						apply_surface( (WIDTH - message->w)/2, 270, message, screen );
						SDL_FreeSurface( message );
					}

					message = TTF_RenderText_Solid( font2, "P1: Arrow keys / Ctrl      P2: W,A,S,D / G", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 330, message, screen );
					SDL_FreeSurface( message );

					message = TTF_RenderText_Solid( font2, "Begin ~ Enter           Escape ~ Quit", textcolor2 );
					apply_surface( (WIDTH - message->w)/2, 400, message, screen );
					SDL_FreeSurface( message );

					SDL_Flip( screen );
				}

	Mix_HaltMusic();
	Mix_FreeMusic( music );
	music = Mix_LoadMUS( "sfx\\game.wav" );
	Mix_PlayMusic( music, -1 );
	myCoin->replace();

	myPirate->zero_vel();
	myPirate->live();
	myPirate2->die();
	if( multi == true ){
		myPirate2->zero_vel();
		myPirate2->live();
	}
	myCannonball->zero_vel();

	myCannonball->start_over();

	lives1 = 3;
	lives2 = 3;

	myPirate->make_position( (WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 - 80  );
	if( multi == true ){
		myPirate2->make_position(( WIDTH - PIRATE_WIDTH) /2, (HEIGHT - PIRATE_HEIGHT)/2 + 80 );
	}

	while( !quit ){
		fps.start();
		while( SDL_PollEvent( &event ) ){

			myPirate->handle_input( event );
			if( multi == true ){
				myPirate2->handle_input( event );
			}

			if( event.type == SDL_QUIT )
				quit = true;

			if( event.type == SDL_KEYDOWN )
				if( event.key.keysym.sym == SDLK_ESCAPE ){
					quit = true;
					Mix_HaltMusic();
					Mix_FreeMusic( music );
					music = Mix_LoadMUS( "sfx\\end.wav" );
					Mix_PlayMusic( music, -1 );
				}
		}
		myPirate->move();
		if( multi == true ){
			myPirate2->move();
		}

		myCannonball->move();

		if( myPirate->is_dead() == false )
		myTreasure->gather( myPirate->get_c_boxes(), score );
		if( multi == true && myPirate2->is_dead() == false )
			myTreasure->gather( myPirate2->get_c_boxes(), score );

		//collecting map
		if( multi && myminis->is_on() ){
			if( myPirate->is_dead() == false ){
				if( check_collision( myPirate->get_c_boxes(), myminis->get_hitbox() )){
					myminis->collect( score, screen );

					myPirate->zero_vel();
					myPirate2->zero_vel();

					myPirate->not_moved();
					myPirate2->not_moved();

				}
			}

			if( myPirate2->is_dead() == false ){
				if( check_collision( myPirate2->get_c_boxes(), myminis->get_hitbox() )){
					myminis->collect( score, screen );

					myPirate->zero_vel();
					myPirate2->zero_vel();

					myPirate->not_moved();
					myPirate2->not_moved();


				}
			}
		}

		//COLLECT FIGHTING
		if( multi && myfight->is_on() ){
					if( myPirate->is_dead() == false ){
						if( check_collision( myPirate->get_c_boxes(), myfight->get_hitbox() )){
							myfight->collect( score, screen );

							myPirate->zero_vel();
							myPirate2->zero_vel();

							myPirate->not_moved();
							myPirate2->not_moved();

						}
					}

					if( myPirate2->is_dead() == false ){
						if( check_collision( myPirate2->get_c_boxes(), myfight->get_hitbox() )){
							myfight->collect( score, screen );

							myPirate->zero_vel();
							myPirate2->zero_vel();

							myPirate->not_moved();
							myPirate2->not_moved();


						}
					}
				}

		apply_surface( 0, 0, bg, screen );

		message = TTF_RenderText_Solid( font2, "score", textcolor );
		apply_surface( (WIDTH - message->w)/2, (HEIGHT - message->h)/2 - 20, message, screen );
		SDL_FreeSurface( message );

		std::stringstream caption;
		caption<<score;
		message = TTF_RenderText_Solid( font2, caption.str().c_str(), textcolor );
		apply_surface( (WIDTH - message->w)/2, (HEIGHT - message->h)/2, message, screen );
		//lives
		for( int i = 0; i < 3; i++ ){
			if( i+1 <= lives1 ){
				apply_surface( 50 + i*45, (HEIGHT - message->h)/2, heart_sheet, screen, &heart_offset[ 0 ] );
			}
			else
				apply_surface( 50 + i*45, (HEIGHT - message->h)/2, heart_sheet, screen, &heart_offset[ 1 ] );
		}
		if( multi ){
			for( int i = 0; i < 3; i++ ){
						if( i+1 <= lives2 ){
							apply_surface( 455 + i*45, (HEIGHT - message->h)/2, heart_sheet, screen, &heart_offset[ 0 ] );
						}
						else
							apply_surface( 455 + i*45, (HEIGHT - message->h)/2, heart_sheet, screen, &heart_offset[ 1 ] );
					}
		}
		SDL_FreeSurface( message );



		myTreasure->show( screen );
		myGPIRATE->show( screen );
		myRuby->show( screen );
		myCoin->show( screen );
		if( multi )
		myminis->show( screen );
		if( multi )
		myfight->show( screen );

		if( myPirate->is_dead() == false )
			myPirate->show( screen );

		if( multi == true && myPirate2->is_dead() == false ){
			myPirate2->show( screen );
		}
		myCannonball->show( screen );



		if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){
		SDL_Delay( 1000 / FRAMES_PER_SECOND - fps.get_ticks() );
		score += 1;
		}

		if( myPirate->is_dead() == false ){
			if( myTreasure->is_collected() == false && check_collision( myTreasure->get_c_boxes(), myPirate->get_c_boxes() ) ){
				myTreasure->collect();
			}

			if( check_collision( myPirate->get_c_boxes(), myCoin->get_c_boxes() ) ){
				myCoin->replace();
				score += 500;
			}

			if(  myRuby->is_collected() == false && check_collision( myPirate->get_c_boxes(), myRuby->get_c_boxes() ) ){
				score += 2500;
				myRuby->collect();
			}

			if( myRuby->is_collected() && ( rand() % 1000 + 1 ) < 6 ){
				myRuby->replace();
			}

			if(  myGPIRATE->is_collected() == false && check_collision( myPirate->get_c_boxes(), myGPIRATE->get_c_boxes() ) ){
				ghost_counter = 0;
				myPirate->go_ghost();
				myGPIRATE->collect();
			}

			if( myGPIRATE->is_collected() && ( rand() % 2000 + 1 ) < 3 ){
				myGPIRATE->replace();
			}

			if( myPirate->is_ghost() ){
				ghost_counter++;

				if( ghost_counter == 250 ){
					myPirate->not_ghost();
					ghost_counter = 0;
			}
		}




		if( myPirate->is_ghost() == false && check_collision( myPirate->get_c_boxes(), myCannonball->get_c_boxes() ) ){
			if( lives1 > 0 ){
				lives1--;
				myPirate->go_ghost();
			}
			else
				myPirate->die();
			message = TTF_RenderText_Solid( font2, "#1 Pirate died!", textcolor2 );
			apply_surface( (WIDTH - message->w)/2, 50, message, screen );
			SDL_FreeSurface( message );
			SDL_Flip( screen );
			SDL_Delay( 1000 );
		}
	}

		//TREASURE_REPLACE
		if( myTreasure->is_collected() == true && rand() % 2000 + 1 < 2 )myTreasure->replace();

		if( multi && ( ( rand() % 1000 ) < 5 )){
			myminis->place();
		}
		if( multi && ( ( rand() % 1000 ) < 5 )){
			myfight->replace();
		}

		//PIRATE 2 ACTIONS
		if( multi == true && myPirate2->is_dead() == false ){

			if( myTreasure->is_collected() == false && check_collision( myTreasure->get_c_boxes(), myPirate2->get_c_boxes() ) ){
				myTreasure->collect();
			}

			if( check_collision( myPirate2->get_c_boxes(), myCoin->get_c_boxes() ) ){
				myCoin->replace();
				score += 500;
			}

			if(  myRuby->is_collected() == false && check_collision( myPirate2->get_c_boxes(), myRuby->get_c_boxes() ) ){
				score += 2500;
				myRuby->collect();
			}

			if( myRuby->is_collected() && ( rand() % 1000 + 1 ) < 6 ){
				myRuby->replace();
			}

			if(  myGPIRATE->is_collected() == false && check_collision( myPirate2->get_c_boxes(), myGPIRATE->get_c_boxes() ) ){
				ghost_counter2 = 0;
				myPirate2->go_ghost();
				myGPIRATE->collect();
			}

			if( myGPIRATE->is_collected() && ( rand() % 2000 + 1 ) < 3 ){
				myGPIRATE->replace();
			}

			if( myPirate2->is_ghost() ){
				ghost_counter2++;

				if( ghost_counter2 == 250 ){
					myPirate2->not_ghost();
					ghost_counter2 = 0;
				}
			}




			if( myPirate2->is_ghost() == false && check_collision( myPirate2->get_c_boxes(), myCannonball->get_c_boxes() ) ){
				if( lives2 > 0 ){
					lives2--;
					myPirate2->go_ghost();
				}
				else myPirate2->die();
				message = TTF_RenderText_Solid( font2, "#2 Pirate died!", textcolor2 );
				apply_surface( (WIDTH - message->w)/2, 50, message, screen );
				SDL_FreeSurface( message );
				SDL_Flip( screen );
				SDL_Delay( 1000 );
			}
		}

		if( myPirate->is_dead() || ( multi == true && myPirate2->is_dead() ) ){
			quit = true;
						Mix_HaltMusic();
						Mix_FreeMusic( music );
						music = Mix_LoadMUS( "sfx\\end.wav" );
						Mix_PlayMusic( music, -1 );

						message = TTF_RenderText_Solid( font, "Aaaaaaarrrrrrrgggh!", textcolor2 );
						apply_surface( (WIDTH - message->w) / 2, (HEIGHT - message->h) / 2, message, screen );
						SDL_FreeSurface( message );

						SDL_Flip( screen );

						SDL_Delay( 5000 );

		}
		SDL_Flip( screen );
	}

	quit = false;

	//WRITING IN NEW RECORD
	int solo = 0, multis = 0;
	std::ifstream load1( "records.dat" );
		load1 >> solo;
		load1 >> multis;
	load1.close();
	if( multi ){
		if( score > multis )
			multis = score;
	}
	else if( score > solo )
		solo = score;

	std::ofstream load2( "records.dat" );
		load2<<solo;
		load2<<" ";
		load2<<multis;
	load2.close();

	while( !quit ){

		while( SDL_PollEvent( &event) ){
			if( event.type == SDL_KEYDOWN ){
				if( event.key.keysym.sym == SDLK_ESCAPE ){
					quit = true;
					restart = false;
				}

				if( event.key.keysym.sym == SDLK_RETURN )
					quit = true;
			}
		}

		apply_surface( 0, 0, bg, screen );

		message = TTF_RenderText_Solid( font, "Your score:", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 15, message, screen );
		SDL_FreeSurface( message );


		std::stringstream caption;
		caption<<score;
		message = TTF_RenderText_Solid( font, caption.str().c_str(), textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 115, message, screen );
		SDL_FreeSurface( message );

		message = TTF_RenderText_Solid( font2, "Aaargh, try again!", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 300, message, screen );
		SDL_FreeSurface( message );

		//WRITING RECORDS

			std::ifstream records( "records.dat" );

				int temp_record;

				records >> temp_record;

				caption.str("");

				caption << temp_record;


				message = TTF_RenderText_Solid( font2, "Solo Record:", textcolor2 );
				apply_surface( 45, 220, message, screen );
				SDL_FreeSurface( message );
				message = TTF_RenderText_Solid( font2, caption.str().c_str(), textcolor2 );
				apply_surface( 45, 250, message, screen );
				SDL_FreeSurface( message );

				records >> temp_record;

				caption.str("");

				caption << temp_record;

				message = TTF_RenderText_Solid( font2, "Multi Record:", textcolor2 );
				apply_surface( WIDTH - message->w-45,220, message, screen );
				SDL_FreeSurface( message );
				message = TTF_RenderText_Solid( font2, caption.str().c_str(), textcolor2 );
				apply_surface( WIDTH - message->w-45,250, message, screen );
				SDL_FreeSurface( message );

			records.close();

		//WRITING RECORDS

		message = TTF_RenderText_Solid( font2, "Enter ~ Restart!      Escape ~ Quit!", textcolor2 );
		apply_surface( (WIDTH - message->w)/2, 400, message, screen );
		SDL_FreeSurface( message );

		SDL_Flip( screen );
	}
}//RESTART SESSION

	Mix_HaltMusic();

	Mix_FreeMusic( music );

	SDL_FreeSurface( message );
	SDL_FreeSurface( bg );
	SDL_FreeSurface( screen );

	TTF_CloseFont( font );
	TTF_CloseFont( font2 );

	Mix_CloseAudio();
	SDL_Quit();


	//GET RID OF OBJECTS
	delete myCannonball;
	myCannonball = NULL;

	delete myPirate;
	myPirate = NULL;

	delete myCoin;
	myCoin = NULL;

	delete myRuby;
	myRuby = NULL;

	delete myPirate2;
	myPirate2 = NULL;

	delete myTreasure;
	myTreasure = NULL;



	delete myminis;
	myminis = NULL;

	delete myfight;
	myfight = NULL;

	SDL_FreeSurface( heart_sheet );

	return 0;
}
