/**
* @file main.c
* @author fatma & omar <3
* @version 1.0
* @date 19 mai 2013
* @brief chapitre1 stage1
*/




#include <stdio.h>
#include <allegro.h>
#include <stdlib.h>
#include "background.h"
#include "chien.h"
#include "car.h"
#include "collision.h"
#include "jennyfer.h"
#include "perso.h"
#define GFX_W 642
#define GFX_H 450
int bounding_box_collision1(int sprite1_x, int sprite1_y, int sprite1_w,
                           int sprite1_h, int sprite2_x, int sprite2_y, int sprite2_w,
                           int sprite2_h);
typedef struct menu 
{ 
BITMAP *Background2;
int x; 
int y; 
int w; 
int h;

int a;
int b;

int e;
int f;

int l;
int m;

} menu;


int main() {
       /**
           *@brief chemain pour acceder au background 
        */     
	char chemin_background1[]="/home/fatma/jenny/images/background1.bmp";
        char chemin_backgroundcol1[]="/home/fatma/jenny/images/backgroundcol1.bmp";
        char chemin_background2[]="/home/fatma/jenny/images/background2.bmp";
        char chemin_backgroundcol2[]="/home/fatma/jenny/images/backgroundcol2.bmp";
        char chemin_background3[]="/home/fatma/jenny/images/background3.bmp";
        char chemin_backgroundcol3[]="/home/fatma/jenny/images/backgroundcol3.bmp";
        char chemin_background4[]="/home/fatma/jenny/images/background4.bmp";
        char chemin_backgroundcol4[]="/home/fatma/jenny/images/backgroundcol4.bmp";
         char chemin_background9[]="/home/fatma/jenny/back/background9.bmp";
        char chemin_backgroundcol9[]="/home/fatma/jenny/back/backgroundcol9.bmp";
        char chemin_background10[]="/home/fatma/jenny/back/background10.bmp";
        char chemin_backgroundcol10[]="/home/fatma/jenny/back/backgroundcol10.bmp";
        char chemin_background11[]="/home/fatma/jenny/back/background11.bmp";
        char chemin_backgroundcol11[]="/home/fatma/jenny/back/backgroundcol11.bmp";
          char chemin_background12[]="/home/fatma/jenny/back/background12.bmp";
       char chemin_backgroundcol12[]="/home/fatma/jenny/back/backgroundcol12.bmp";
        
               

         //SAMPLE *sound;
        BITMAP *buffer;
        BITMAP *image ;
        BITMAP *BULLE=NULL;
        BITMAP *gameover ;
	BITMAP *Buffer;
        BITMAP *OVER=NULL;
        BITMAP *agenda=NULL;
        BITMAP *bulle=NULL ;
        BITMAP *BLOOD=NULL;
        BITMAP *Background=NULL;
        BITMAP *VIANDE=NULL;
        BITMAP *viande=NULL;
        BITMAP *CL=NULL;
        BITMAP *NEWS=NULL;
        BITMAP *LETTRE=NULL;
        BITMAP *MIS=NULL;
        BITMAP *Backgroundcol=NULL;
        BITMAP *menu2;
        BITMAP *souris;
      BITMAP *Images;
      BITMAP *instructions;
      SAMPLE sound;
menu A;
A.w=120; 
A.h=20;
A.b=94;
A.f=66;
A.m=34;
A.x=100;
A.y=144;
A.a=193;
A.e=241;
A.l=283;
extern volatile int mouse_x;
extern volatile int mouse_y;
extern volatile int mouse_z;
extern volatile int mouse_b;
int mouse_w =45;
int mouse_h =55;
	jennyfer jenn;
        chien chien ;
        CAR c;

        rat rat ;
        barre barre ;
        barre1 barre1 ;
        clef clef ;
        file file ;
        barre2 barre2;
        BACKG b;
        int pas=0;
        int CB=0;
	int compteur=0;
        int k=0;
        int CO=0;
        int collision=0;
        int s=0;
        int v=0;
        int pix1, pixr,pixl,pixd ;
        int CLEE=0;
        int br=0;
         
         extern FONT *font;
          
	/**
           *@brief General initialization function 
         */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
  

      if(install_mouse() == -1) {
      allegro_message("Erreur ! %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);
	}
        load_jennyfer(&jenn);
	Buffer = create_bitmap(SCREEN_W, SCREEN_H);
        VIANDE =load_bitmap("/home/fatma/jenny/vie/viande.bmp",NULL);
        OVER =load_bitmap("/home/fatma/jenny/vie/gameover.bmp",NULL);
        viande =load_bitmap("/home/fatma/jenny/images/BV.bmp",NULL);
        agenda=load_bitmap("/home/fatma/jenny/images/agendaJ.bmp",NULL);
        bulle=load_bitmap("/home/fatma/jenny/images/bulle.bmp",NULL);
        buffer = create_bitmap(800, 453);
        c.car = load_bitmap("/home/fatma/jenny/essay/im/voit.bmp", NULL);
        b.backgroundprincipale = load_bitmap("/home/fatma/jenny/essay/im/s1ch2sc1QUALITY.bmp", NULL);
        b.backgroundCollision = load_bitmap("/home/fatma/jenny/essay/im/backgroundcol.bmp", NULL);
        gameover = load_bitmap("/home/fatma/jenny/essay/im/gameover.bmp",NULL) ;
        image=create_bitmap(225,90) ;  
        NEWS= load_bitmap("/home/fatma/jenny/images/news.bmp",NULL);
        BLOOD= jenn.blood[0];
        CL=load_bitmap("/home/fatma/jenny/images/cle.bmp",NULL);
        LETTRE=load_bitmap("/home/fatma/jenny/images/lettre.bmp",NULL); 
        BULLE=load_bitmap("/home/fatma/jenny/images/bulles.bmp",NULL);
        MIS=load_bitmap("/home/fatma/jenny/images/misacomplie.bmp",NULL);
        A.Background2 = load_bitmap("/home/fatma/jenny/menu2/images/menu.bmp", NULL);
        souris = load_bitmap("/home/fatma/jenny/menu2/images/souris.bmp", NULL);
        menu2 = load_bitmap("/home/fatma/jenny/menu2/images/menu2.bmp", NULL);
       instructions = load_bitmap("/home/fatma/jenny/menu2/images/instructions.bmp", NULL);
      blit(A.Background2, Buffer, 0, 0, 0, 0, A.Background2->w, A.Background2->h);
      Images = souris;
        draw_sprite(Buffer,Images, mouse_x, mouse_y);

       
       
         
	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);
        load_barre2(&barre2);
        load_clef(&clef);
        load_file(&file);
        load_barre(&barre);
	load_rat(&rat);
	load_barre1(&barre1);
	Background=load_background_bmp(chemin_background1);
        Backgroundcol=load_background_bmp(chemin_backgroundcol1);
       if(Background == NULL)
	{	set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);

	}
         
	
         
        /**
          *@brief iniatitation
        */c.x = 200;
          c.y = 200;
         b.backg_x=0 ;
         b.backg_y =0;
         b.backgc_x=0;
              
        init_chien(&chien);
	init_jennyfer(&jenn);
        init_clef(&clef);
        init_rat(&rat);
        init_file(&file);
        init_barre(&barre);
        init_barre1(&barre1);
        init_barre2(&barre2);
        

        /**
          *@brief boucle du jeu 
        */
while (!key[KEY_ESC]) { 

clear_bitmap(Buffer) ;
blit(A.Background2, Buffer, 0, 0, 0, 0, A.Background2->w, A.Background2->h);
blit(Buffer, screen, 0, 0, 0, 0, mouse_x, mouse_y);

draw_sprite(Buffer, Images, mouse_x, mouse_y);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H) ;

 if((mouse_b & 1) && (bounding_box_collision1(mouse_x,mouse_y,mouse_w,
                      mouse_h,A.x,A.y,A.w,A.h)==1)) 
{ 
  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;

       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);
        //textout_centre_ex(Buffer, font, "pensez au chien", 500,300, makecol(131, 12, 28), -1);
      if (jenn.x>400)
         { 
             draw_sprite(Buffer,BULLE,250,210);
         }

    if ((key[KEY_SPACE]) && (jenn.x>400) )
       { 
      v=1;
       }
    if (v==1) 
      { 
       draw_sprite(Buffer,VIANDE,200,0);
      } 
       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);
        
        draw_jennyfer(jenn,Buffer,compteur);
        draw_chien(chien,Buffer,CO);
        throw(&s);
     if (v==1) 
       { 
       draw_sprite(Buffer,VIANDE,200,0);
       textout_centre_ex(Buffer, font, "key space", 300,60, makecol(255, 249, 0), -1);
       } 
       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) && (jenn.x>100) && (v==1)) 
        { 
          draw_sprite(Buffer,viande,0,0); 
           blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
            usleep(5000000);
       while(!key[KEY_ESC])
          {

while (b.backg_x>(-10500))
{
b.backg_x-=8;
b.backgc_x-=8;
pix1=get_pixel(b.backgroundCollision,(c.x + c.car->w/2)-b.backgc_x,c.y + 15) ;
pixr=get_pixel(b.backgroundCollision, (c.x+c.car->w),c.car->h/2);
pixl=get_pixel(b.backgroundCollision, (c.x)+c.car->w ,c.y+c.car->h/2+40);
pixd=get_pixel(b.backgroundCollision, (c.x+c.car->w/2)-b.backgc_x ,c.y+60);

draw_sprite (buffer,b.backgroundprincipale,b.backg_x,b.backg_y) ;
blit (c.car,image,0,0,0,0,260,290) ;
draw_sprite (buffer,image,c.x,c.y) ;
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);



if (key[KEY_UP] && (pix1!=255))
{
moveup (&b,&c,buffer,image);
}


if (key[KEY_DOWN] && pixd!=255 )
{
movedown (&b,&c,buffer,image);
}

if (key[KEY_RIGHT]) 
{
moveright (&b,&c,buffer,image);
}


if (key[KEY_LEFT]) 
{
moveleft (&b,&c,buffer,image);
}

rest(10) ;

printf("%d\n" , pixl ) ;
}
if ((c.x=9980) && (c.y>305)) 
{ draw_sprite (buffer,MIS,250,150) ;
    pas=1;
    CB=8;
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
usleep(5000000);
}  
 clear_bitmap(buffer) ;
if ((pas==1)&&(c.x=10000)&&(CB==8))
  {                 CB=9;
                      jenn.x=0;
                      jenn.y=280;
                      jenn.direction=0;
                    Background=load_background_bmp(chemin_background9);
                    Backgroundcol=load_background_bmp(chemin_backgroundcol9);
         
   while (!key[KEY_ESC])
	{
           
		clear_bitmap(Buffer);
                  //soundm(sound);
                 move_rat(&rat);

		move_jennyfer(&jenn,&compteur,Backgroundcol);
                 move_jennyfer_gramp(&jenn,&compteur);
		draw_background(Background,Buffer);

                 draw_rat(rat,Buffer);

		draw_jennyfer(jenn,Buffer,compteur);


               game_over_rat(Buffer, &rat , &jenn, OVER,BLOOD,&k);
                  if((jenn.x>540) && (jenn.y<385) && (jenn.y> 211))
                     {

          draw_sprite(Buffer,NEWS,40,80);
                      }


          blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

		if (compteur == 1000)
			compteur = 0;

              if ((jenn.x<56) && (jenn.y<50) && (CB==9))

                   { usleep(150000);
			CB=10;
                      jenn.x=150;
                      jenn.y=100;
                      jenn.direction=0;
                    Background=load_background_bmp(chemin_background10);
                    Backgroundcol=load_background_bmp(chemin_backgroundcol10);

                 while (!key[KEY_ESC]) {

		clear_bitmap(Buffer);

		move_jennyfer(&jenn,&compteur,Backgroundcol);

                    barreD(&jenn,&br,Buffer);
		 if (br==1) {move_barre(&barre);}
		    draw_background(Background,Buffer);

                    draw_jennyfer(jenn,Buffer,compteur);
                    draw_barre(barre,Buffer);
                    draw_clef(clef,Buffer,&CLEE);
                    blood(BLOOD ,Buffer,&jenn,&k);

                      col_barre(Buffer, &barre, &jenn);
                       cle(&clef , &jenn , &CLEE);
                     if (CLEE==1) 
                              {
                               draw_sprite(Buffer,CL,200,5);
                               }
                    if ((jenn.x<320) && (jenn.x>287) && (jenn.y<225))
                        { textout_centre_ex(Buffer, font, " key space", 300,70, makecol(255, 249, 0), -1);
                         }
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

		if (compteur == 10000)
			compteur = 0;

               if ((jenn.x>600)&&(jenn.y<100) && (CB==10) )
               {  usleep(150000);
			CB=11;
                      jenn.x=0;
                      jenn.y=130;
                      jenn.direction=0;
                      Background=load_background_bmp(chemin_background11);
                      Backgroundcol=load_background_bmp(chemin_backgroundcol11);

                 while (!key[KEY_ESC]) {

		clear_bitmap(Buffer);

		move_jennyfer(&jenn,&compteur,Backgroundcol);
		move_barre1(&barre1);
                move_file(&file);
		draw_background(Background,Buffer);
	        draw_barre1(barre1,Buffer);
                draw_jennyfer(jenn,Buffer,compteur);
                draw_file(file,Buffer);
                blood(BLOOD ,Buffer,&jenn,&k);
                col_barre1(Buffer, &barre1, &jenn);
                 col_file(Buffer,&file, &jenn);
                     if (CLEE==1) 
                            {
                           draw_sprite(Buffer,CL,200,5);
                             }

                     if ((jenn.x<180) && (jenn.y<100))
                      { 
                         draw_sprite(Buffer,OVER,0,0);
                      }
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                             if (compteur == 10000)
			compteur = 0;


                 if ((jenn.x>535)&&(jenn.y>300) && (CB==11)&& (CLEE==1))
               {  usleep(150000);
			CB=12 ;
                      jenn.x=40;
                      jenn.y=280;
                      jenn.direction=0;
                      Background=load_background_bmp(chemin_background12);
                      Backgroundcol=load_background_bmp(chemin_backgroundcol12);

                   while (!key[KEY_ESC])      {

		clear_bitmap(Buffer);


		move_jennyfer(&jenn,&compteur,Backgroundcol);
		move_barre2(&barre2);
		draw_background(Background,Buffer);

                 draw_jennyfer(jenn,Buffer,compteur);
                 draw_barre2(barre2,Buffer);
                 col_barre2(Buffer, &barre2,&jenn);
            if ((jenn.x< 137) && (jenn.x>85) && (jenn.y>280))
                 {
                   draw_sprite(Buffer,OVER,0,0);
                  }
              if ((jenn.x< 375) && (jenn.x>251) && (jenn.y>250)&&(jenn.y<350))
                       {
                   draw_sprite(Buffer,LETTRE,0,0);
                        }
               blood(BLOOD ,Buffer,&jenn,&k);
	       blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);


               if (compteur == 10000)
			compteur = 0;

              
	
                      





} }} }} } }

}
}
}


} }} }} } } } 
if((mouse_b & 1) && (bounding_box_collision1(mouse_x,mouse_y,mouse_w,
                      mouse_h,A.x,A.a,A.b,A.h)==1)) 
{ while (!key[KEY_ESC])
   {
clear_bitmap(Buffer);
draw_sprite(Buffer,instructions, 0,0);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
} }

if((mouse_b & 1) && (bounding_box_collision1(mouse_x,mouse_y,mouse_w,
                      mouse_h,A.x,A.e,A.f,A.h)==1)) 
{ while (!key[KEY_ESC])
  {
clear_bitmap(Buffer);
draw_sprite(Buffer,menu2, 0,0);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
} }

 if((mouse_b & 1) && (bounding_box_collision1(mouse_x,mouse_y,mouse_w,
                      mouse_h,A.x,A.l,A.m,A.h)==1)) 
{ 
allegro_exit();
}
}

         
        destroy_file(&file);
        destroy_barre2(&barre2);
        destroy_barre1(&barre1);
        destroy_barre(&barre);
        destroy_rat(&rat);
	destroy_jennyfer(&jenn);
	destroy_background(Background);
        destroy_background(Backgroundcol);
	destroy_bitmap(Buffer);
        destroy_bitmap(image) ;
        destroy_bitmap(c.car) ;
        destroy_bitmap(b.backgroundCollision) ;
        destroy_bitmap(b.backgroundprincipale) ;
        destroy_chien(&chien);
	
	return (0);
}
END_OF_MAIN();

int bounding_box_collision1(int sprite1_x, int sprite1_y, int sprite1_w,int sprite1_h,
                           int sprite2_x, int sprite2_y, int sprite2_w,int sprite2_h)
{
    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;
}
