#include <allegro.h>
#include <stdio.h>

#define GFX_W 642
#define GFX_H 450


struct jennyfer {
          
        BITMAP * blood[6];
	BITMAP * right[3];
	BITMAP * left[3];
	int x, y;
	int direction; 
    BITMAP *bow[2];

};
typedef struct jennyfer jennyfer;

struct chien { 
BITMAP * right[3];
	BITMAP * left[3];
	int x, y;
	int direction; } ; 
typedef struct chien chien ; 

typedef struct { 
     int x;
	int y;
	int w;
	int h;
}Sprite;
 





//Module Background
BITMAP* load_background_bmp(char chemin_background[]);
void draw_background(BITMAP *Background,BITMAP*Buffer);
void destroy_background(BITMAP *Background);


// module jen
void load_jennyfer(jennyfer *jenn);
void destroy_jennyfer(jennyfer *jenn);
void init_jennyfer(jennyfer *jenn);

void move_jennyfer_right(jennyfer *jenn,int *compteur,BITMAP * Background);
void move_jennyfer_left(jennyfer *jenn,int *compteur,BITMAP * Background);
void jump_jennyfer(jennyfer *jenn,BITMAP * Background); //move up
void bow_jennyfer(jennyfer *jenn,BITMAP *Background);
void gravity(int *y);
void gravity_jennyfer(jennyfer *jenn,BITMAP * Background);
void move_jennyfer(jennyfer *jenn,int *compteur,BITMAP * Background);
void draw_jennyfer(jennyfer jenn,BITMAP *Buffer, int compteur);
void throw(jennyfer *jenn, BITMAP *Buffer,BITMAP *viande,int *s);
// module chien
void load_chien(chien * chien);
void destroy_chien(chien *chien);
void init_chien(chien *chien);

void move_chien_right(chien *chien,int *CO);
void move_chien_left(chien *chien,int *CO);
void move_chien(chien *chien,int *CO);
void draw_chien(chien chien,BITMAP *Buffer, int CO);
void verifdir(chien *chien  ) ;
//song
//void soundm(sound);
//collision
int collision_background_left(Sprite sprite1,BITMAP* Backgroundcol);
int collision_background_right(Sprite sprite1,BITMAP* Backgroundcol);
int collision_background_up(Sprite sprite1,BITMAP* Backgroundcol);
int collision_background_down(Sprite sprite1,BITMAP* Backgroundcol);
int get_pixel(BITMAP* Buffer,int x,int y);
int bounding_box_collision(Sprite Sprite1, Sprite Sprite2);
void game_over (BITMAP *Buffer, chien *chien , jennyfer *jenn, BITMAP *OVER,BITMAP *BLOOD, int *K);
void blood(BITMAP *BLOOD ,BITMAP *Buffer, jennyfer *jenn, int *k);




int main() {

	char chemin_background1[]="/home/fatma/jen/images/background1.bmp";
        char chemin_backgroundcol1[]="/home/fatma/jen/images/backgroundcol1.bmp";
        char chemin_background2[]="/home/fatma/jen/images/background2.bmp";
        char chemin_backgroundcol2[]="/home/fatma/jen/images/backgroundcol2.bmp";
        char chemin_background3[]="/home/fatma/jen/images/background3.bmp";
        char chemin_backgroundcol3[]="/home/fatma/jen/images/backgroundcol3.bmp";
          char chemin_background4[]="/home/fatma/jen/images/background4.bmp";
char chemin_backgroundcol4[]="/home/fatma/jen/images/backgroundcol4.bmp";
          
        
              
	BITMAP *Buffer;
        BITMAP *OVER=NULL;
        BITMAP *agenda=NULL;
         BITMAP *bulle=NULL ; 
        BITMAP *BLOOD=NULL;
         //SAMPLE *sound;
	BITMAP *Background=NULL;
         BITMAP *VIANDE=NULL;
           BITMAP *viande=NULL;
        
	BITMAP *Backgroundcol=NULL;
	jennyfer jenn;
        chien chien ;
         int CB=0; 
	int compteur;
         int k;
         int CO;
         int s=0;
          int collision;   
	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); 
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return (1);
	}

	Buffer = create_bitmap(SCREEN_W, SCREEN_H);
        VIANDE =load_bitmap("/home/fatma/jen/vie/viande.bmp",NULL);
       OVER =load_bitmap("/home/fatma/jen/vie/gameover.bmp",NULL);
         viande =load_bitmap("/home/fatma/jen/images/BV.bmp",NULL);
          agenda=load_bitmap("/home/fatma/jen/images/agendaJ.bmp",NULL);
           bulle=load_bitmap("/home/fatma/jen/images/bulle.bmp",NULL);
          BLOOD= jenn.blood[0];
	if(BLOOD == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image: BLOOD !\n%s\n", allegro_error);
	}
	load_chien(&chien);
	load_jennyfer(&jenn);
	Background=load_background_bmp(chemin_background1);
        Backgroundcol=load_background_bmp(chemin_backgroundcol1);
          k=0;
	compteur = 0;
        collision=0;
         CO=0;
if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);

	}
           init_chien(&chien);
	init_jennyfer(&jenn);
         
           
	while (!key[KEY_ESC]) 
	{
		
		clear_bitmap(Buffer);
                  //soundm(sound);
                 move_chien(&chien,&CO);
		
		move_jennyfer(&jenn,&compteur,Backgroundcol);
		
		draw_background(Background,Buffer);
		  
                 draw_chien(chien,Buffer,CO);
                  
		draw_jennyfer(jenn,Buffer,compteur);
               game_over (Buffer, &chien , &jenn, OVER,BLOOD,&k);
 textout_centre_ex(Buffer, font, "you are in jack's house ", 300,30, makecol(255, 249, 0), -1);
   textout_centre_ex(Buffer, font, "the one whose name is just before yours in the list !", 300,40, makecol(255,249,0), -1); 
   textout_centre_ex(Buffer, font, "discover the mystery !", 300,50, makecol(255, 249, 0), -1); 
           
		
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                
		if (compteur == 1000)
			compteur = 0;
                if ( CO==100)
                     CO=0;
			//printf("chien x %d      directon   :  %d\n  "  ,chien.x ,chien.direction);
	
               

              if ((jenn.x>560) && (jenn.y<266) && (CB==0))   
                                
                   { usleep(150000);
			CB=1;
                      jenn.x=0;
                      jenn.y=100;
                      jenn.direction=0;
                    Background=load_background_bmp(chemin_background2);
                    Backgroundcol=load_background_bmp(chemin_backgroundcol2);
                    
                 while (!key[KEY_ESC]) {
                          
		clear_bitmap(Buffer);
                  
		move_jennyfer(&jenn,&compteur,Backgroundcol);
		
		draw_background(Background,Buffer);
		  
                 draw_jennyfer(jenn,Buffer,compteur);
                  
                    blood(BLOOD ,Buffer,&jenn,&k); 
		   if ((jenn.x<100) && (jenn.y<266))
		   {textout_centre_ex(Buffer, font, " lire l'agenda", 200,60, makecol(255, 249, 0), -1); }
                       if ((jenn.x>560) && (jenn.y<266))

{
              draw_sprite(Buffer,bulle,380,80);
                draw_sprite(Buffer,agenda,20,10); 
}

		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                
		if (compteur == 10000)
			compteur = 0; 
               if ((jenn.x<40)&&(jenn.y>300) && (CB==1)) 
               {  usleep(150000);
			CB=2;
                      jenn.x=450;
                      jenn.y=343;
                      jenn.direction=0;
                    Background=load_background_bmp(chemin_background3);
                    Backgroundcol=load_background_bmp(chemin_backgroundcol3);
                    
                 while (!key[KEY_ESC]) {
		
		clear_bitmap(Buffer);
                  
		move_jennyfer(&jenn,&compteur,Backgroundcol);
		
		draw_background(Background,Buffer);
	       
                 draw_jennyfer(jenn,Buffer,compteur);
                     blood(BLOOD ,Buffer,&jenn,&k); 
                       if (key[KEY_A]) 
                          
                   {draw_sprite(Buffer, VIANDE,200,0);}
                         
                textout_centre_ex(Buffer, font, "pensez au chien", 500,300, makecol(131, 12, 28), -1);
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                             if (compteur == 10000)
			compteur = 0; 
                  

                 if ((jenn.x<40)&&(jenn.y>300) && (CB==2)) 
               {  usleep(150000);
			CB=3 ;
                      jenn.x=40;
                      jenn.y=350;
                      jenn.direction=0;
                    Background=load_background_bmp(chemin_background4);
                    Backgroundcol=load_background_bmp(chemin_backgroundcol4);
                    
                   while (!key[KEY_ESC])      {
		 
		clear_bitmap(Buffer);
                  
                move_chien(&chien,&CO);
		move_jennyfer(&jenn,&compteur,Backgroundcol);
		
		draw_background(Background,Buffer);
		  textout_centre_ex(Buffer, font, "key space", 300,60, makecol(255, 249, 0), -1);
                 draw_jennyfer(jenn,Buffer,compteur);
                      draw_chien(chien,Buffer,CO);
                         throw(&jenn, Buffer,viande,&s); 
                     game_over (Buffer, &chien , &jenn, OVER,BLOOD,&k);       
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                           if ( CO==100)
                     CO=0;
                     if (compteur == 10000)
			compteur = 0; 
                      if( s==1) { while (!key[KEY_ESC]) { draw_sprite(Buffer,viande,0,0); } }
                      
 
                 
} }} }} } }
      
        destroy_chien(&chien);
	destroy_jennyfer(&jenn);
	destroy_background(Background);
        destroy_background(Backgroundcol);
	destroy_bitmap(Buffer);
	return (0);
}
END_OF_MAIN();



 
/*void soundm(sound)
{

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
 {
allegro_message("Sound Error: %s", allegro_error);
 }
set_volume(100, 100); 
sound = load_sample("/home/fatma/jen/Dog Barking 01.wav");

play_sample(sound, 255, 128, 1000, 0);
if (!sound)
{
allegro_message("Error reading wav file");
}
}
*/




void throw (jennyfer *jenn, BITMAP *Buffer,BITMAP *viande,int *s)
{    
    
     if ((key[KEY_SPACE]))
         
     {draw_sprite(Buffer,viande,0,0);
               *s=1 ; }
      
}

BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
		if(Background == NULL){
			set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);



			allegro_message("Unable to load image !\n%s\n", allegro_error);
			return (NULL);
		}
return (Background);

}


void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP *Background)
{
	destroy_bitmap(Background);
}



void load_jennyfer(jennyfer *jenn)
{
	int i;
	char buf[50];
        //char buff[50];
	for (i = 0; i <3; i++) {
		sprintf(buf, "/home/fatma/jen/images/right%d.bmp", i);
		jenn->right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "/home/fatma/jen/images/left%d.bmp", i);
		jenn->left[i] = load_bitmap(buf, NULL); }
		jenn->bow[0]=load_bitmap("/home/fatma/jen/images/bowright1.bmp",NULL);
		jenn->bow[1]=load_bitmap("/home/fatma/jen/images/bowleft1.bmp",NULL);
                 
		/*for (i = 0; i <6; i++) {
		sprintf(buff, "/home/fatma/jen/vie/blood%d.bmp", i);
		jenn->blood[i] = load_bitmap(buff, NULL); }*/
                 jenn->blood[0]=load_bitmap("/home/fatma/jen/vie/blood0.bmp",NULL); 
		jenn->blood[1]=load_bitmap("/home/fatma/jen/vie/blood1.bmp",NULL); 
                 jenn->blood[2]=load_bitmap("/home/fatma/jen/vie/blood2.bmp",NULL);
		jenn->blood[3]=load_bitmap("/home/fatma/jen/vie/blood3.bmp",NULL);
               jenn->blood[4]=load_bitmap("/home/fatma/jen/vie/blood4.bmp",NULL);
		jenn->blood[5]=load_bitmap("/home/fatma/jen/vie/blood5.bmp",NULL);  
}

void destroy_jennyfer(jennyfer *jenn)
{
	int i;
	for (i = 0; i <3; i++) {
		destroy_bitmap(jenn->right[i]);
		destroy_bitmap(jenn->left[i]);
	}
    for (i=0;i<6;i++)
    {destroy_bitmap(jenn->blood[i]);}
   
    
}

void init_jennyfer(jennyfer *jenn)
{
	jenn->direction = 0;
	jenn->x = 20;
	jenn->y = 325;
}
void move_jennyfer_right(jennyfer *jenn,int *compteur,BITMAP * Background)
{     Sprite sprite1;
	sprite1.x=jenn->x;
	sprite1.y=jenn->y;
	sprite1.w=jenn->right[*compteur % 2]->w;
	sprite1.h=jenn->right[*compteur % 2]->h; 

	if (key[KEY_RIGHT] && (jenn->x < SCREEN_W - jenn->right[0]->w) && !collision_background_right(sprite1,Background))
 {
		*compteur+=1;

		(jenn->x)+=2;
		jenn->direction = 0; //right
	}

}

void move_jennyfer_left(jennyfer *jenn,int *compteur,BITMAP * Background)
{        Sprite sprite1;
	sprite1.x=jenn->x;
	sprite1.y=jenn->y;
	sprite1.w=jenn->left[*compteur % 2]->w;
	sprite1.h=jenn->left[*compteur % 2]->h; 

	if (key[KEY_LEFT] && (jenn->x > 7)&& !collision_background_left(sprite1,Background)) {
		*compteur+=1;


		(jenn->x)-=2;
		jenn->direction = 1; //left
	}
}
void jump_jennyfer(jennyfer *jenn,BITMAP * Background)
{       Sprite sprite1;
	sprite1.x=jenn->x;
	sprite1.y=jenn->y;
	sprite1.w=jenn->right[2]->w;
	sprite1.h=jenn->right[2]->h;

	if (key[KEY_UP] && (jenn->y > 0) && !collision_background_up(sprite1,Background) ) {
		(jenn->y)-=12;
		jenn->direction = 2;
	}
	


}
void bow_jennyfer(jennyfer *jenn,BITMAP *Background)
{
    if (key[KEY_DOWN] ){

		jenn->direction = 3;
	}
	

}


       



void move_jennyfer(jennyfer *jenn,int *compteur,BITMAP * Background)
{
	move_jennyfer_right(jenn,compteur,Background);
	move_jennyfer_left(jenn,compteur,Background);
	jump_jennyfer(jenn,Background);
        gravity_jennyfer(jenn,Background);
	bow_jennyfer(jenn,Background);
	
}
void draw_jennyfer(jennyfer jenn,BITMAP *Buffer, int compteur)
{
	static int last_direction;

	if (jenn.direction == 0){ //moving right

		draw_sprite(Buffer, jenn.right[compteur % (2) ],jenn.x, jenn.y);
		last_direction=0;
	}

	if(jenn.direction == 1){ //moving left

		draw_sprite(Buffer, jenn.left[compteur % (2) ],jenn.x, jenn.y);
		last_direction=1;
	}
	if (jenn.direction == 2 && last_direction==0) {	

		draw_sprite(Buffer, jenn.right[2],jenn.x, jenn.y);
	}
	if (jenn.direction == 2 && last_direction==1) {	//jumping left

		draw_sprite(Buffer, jenn.left[2],jenn.x, jenn.y);
	}
	if (jenn.direction == 3 && last_direction==0) {	

		draw_sprite(Buffer, jenn.bow[0],jenn.x, jenn.y);
	}
	if (jenn.direction == 3 && last_direction==1) {	

		draw_sprite(Buffer, jenn.bow[1],jenn.x, jenn.y);
	}
}
 // module chien 

 void load_chien(chien *chien)
{
	int i;
	char buf[50];
	for (i = 0; i <3; i++) {
		sprintf(buf, "/home/fatma/jen/chien/droite%d.bmp", i);
		chien->right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "/home/fatma/jen/chien/gauche%d.bmp", i);
		chien->left[i] = load_bitmap(buf, NULL); }
}

void destroy_chien(chien *chien)
{
	int i;
	for (i = 0; i <3; i++) {
		destroy_bitmap(chien->right[i]);
		destroy_bitmap(chien->left[i]);
	}

}
void init_chien(chien *chien)
{
	chien->direction = 2;
	chien->x = 100;
	chien->y = 400;
}
void move_chien_right(chien *chien,int *CO)
{  
   
  verifdir(chien);  
if (chien->direction==0 )
{    
         *CO+=1 ;
         (chien->x)+=1;
          
        
}
}



void move_chien_left(chien *chien,int *CO)
{  verifdir(chien);  
if (chien->direction==1 )
{
         *CO+=1 ;
         (chien->x)-=1;
	
        
}
}
void verifdir(chien *chien  )  
{if(chien->x>550)
	chien->direction=1 ;  
	else 
	if (chien->x<200) 
		chien->direction=0 ;  
}
void move_chien(chien *chien,int *CO)
{    	
	 	move_chien_left(chien,CO);
	
      		move_chien_right(chien,CO);
                
	
}


void draw_chien(chien chien,BITMAP *Buffer, int CO)
{
	

	if (chien.direction == 0){ 

		draw_sprite(Buffer, chien.right[CO % (2) ],chien.x, chien.y);
		
	}

	if(chien.direction == 1){ 

		draw_sprite(Buffer, chien.left[CO % (2)],chien.x,chien.y);
		

} }


// module collision


int bounding_box_collision(Sprite Sprite1,Sprite Sprite2)
{
	if ((Sprite1.x > Sprite2.x + Sprite2.w - 1) ||
			(Sprite1.y > Sprite2.y + Sprite2.h - 1) || 
			(Sprite2.x > Sprite1.x + Sprite1.w - 1) || 
			(Sprite2.y > Sprite1.y + Sprite1.h - 1)) 
	{
		// no collision
		return (0);
	}

	// collision

       return (1); }


void game_over (BITMAP *Buffer, chien *chien , jennyfer *jenn, BITMAP *OVER,BITMAP *BLOOD, int *k)
 { 
    
      

    Sprite Sprite1;
    Sprite Sprite2;

      Sprite1.x=jenn->x;
	Sprite1.y=jenn->y;
    Sprite1.w=jenn->right[0]->w;
	Sprite1.h=jenn->right[0]->h;

         Sprite2.x=chien->x;
	Sprite2.y=chien->y;
	Sprite2.w=chien->right[0]->w;
	Sprite2.h=chien->right[0]->h;


if (*k==5)
{
	draw_sprite(Buffer,OVER,0,0); 
}else{
if (bounding_box_collision(Sprite1,Sprite2)==0)
{   
	BLOOD=jenn->blood[*k % (5)];
	draw_sprite(Buffer, BLOOD,0,0 ) ;
}
else if (bounding_box_collision(Sprite1,Sprite2)==1)
{  
	*k+=1;
	BLOOD=jenn->blood[*k % (5)];
	draw_sprite(Buffer, BLOOD,0,0 );
	usleep(50000);

}}


}

void blood(BITMAP *BLOOD ,BITMAP *Buffer, jennyfer *jenn, int *k)
{
      BLOOD=jenn->blood[*k % (5)];
	draw_sprite(Buffer, BLOOD,0,0 ) ;


}
void gravity(int *y)

{
	*y+=1;
}
void gravity_jennyfer(jennyfer *jenn,BITMAP * Background)

{    Sprite sprite1;
	sprite1.x=jenn->x;
	sprite1.y=jenn->y;
	sprite1.w=jenn->right[0]->w;
	sprite1.h=jenn->right[0]->h;

	if(!collision_background_down(sprite1,Background))
		gravity(&(jenn->y));
}


int collision_background_left(Sprite sprite1,BITMAP* Backgroundcol)
{
	int x;
	int y;
	x=sprite1.x;
	y=sprite1.y + (sprite1.h /2);

	if( get_pixel(Backgroundcol,x,y)== 255)
		return (1); 

	return (0); }


int collision_background_right(Sprite sprite1,BITMAP* Backgroundcol)
{
	int x;
	int y;
	x=sprite1.x + sprite1.w;
	y=sprite1.y + (sprite1.h / 2);

	if( get_pixel(Backgroundcol,x,y)== 255)
		return (1);

	return (0);
}

int collision_background_down(Sprite sprite1,BITMAP* Backgroundcol)
{
	int x;
	int y;
	x=sprite1.x + (sprite1.w /2);
	y=sprite1.y + sprite1.h;

	if( get_pixel(Backgroundcol,x,y)== 255)
		return (1); 

	return (0); 

}

int collision_background_up(Sprite sprite1,BITMAP* Backgroundcol)
{
	int x;
	int y;
	x=sprite1.x + (sprite1.w /2);
	y=sprite1.y;

	if( get_pixel(Backgroundcol,x,y)== 255)
		return (1); 

	return (0); 

}
 int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}












