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

struct jennifer
{
int x ; int y;
BITMAP *right[4];
BITMAP *left[4];
};
typedef struct jennifer jennifer;

void aller (dog D , BITMAP *Buffer , BITMAP *Background);
void retour (dog D , BITMAP *Buffer , BITMAP *Background); 
void jenn (jennifer M,dog D,BITMAP *Buffer , BITMAP *Background);


int main() 
{
BITMAP *Buffer;
BITMAP *Background;
dog D;
char chemin_background[30] = "./Images/background.bmp";
allegro_init();
if (install_keyboard() == -1) {
allegro_message("Error! %s", allegro_error);
return -1;
}

set_color_depth(16);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 643, 453, 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(643, 453);
Background = load_background_bmp(chemin_background);
menue(Buffer,Background,D);

return 0;
}
END_OF_MAIN();


void aller (dog D , BITMAP *Buffer , BITMAP *Background) 
{
  BITMAP *Image;
  int i=0;
  int z=0;

D.right[0] = load_bitmap("./Images/R1.bmp", NULL);
D.right[1] = load_bitmap("./Images/R2.bmp", NULL);
D.right[2] = load_bitmap("./Images/R3.bmp", NULL);
D.right[3] = load_bitmap("./Images/R4.bmp", NULL);
D.x=SCREEN_W/2; D.y=500;

if (Image == NULL && Background == NULL)
{
allegro_message("could not load images");
allegro_exit();
}

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
z =400;i=0;
Image = D.right[0];

draw_sprite(Buffer,Image, z, D.y);

while (!key[KEY_ESC]) {

 do  {

clear_bitmap(Buffer);
i++;
z=z+1;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
blit(Buffer, screen, 0, 0, 0, 0, z, D.y);

//clear_bitmap(Image);
Image = D.right[(i-1)%4];
draw_sprite(Buffer,Image, z, D.y);

} while (z>700);

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
//readkey();
}


destroy_bitmap(Image);
destroy_bitmap(Buffer);

}



void retour (dog D , BITMAP *Buffer , BITMAP *Background)
{ 
  BITMAP *Image;
  int i=0;
  int z;

D.left[0] = load_bitmap("./Images/R5.bmp", NULL);
D.left[1] = load_bitmap("./Images/R6.bmp", NULL);
D.left[2] = load_bitmap("./Images/R7.bmp", NULL);
D.left[3] = load_bitmap("./Images/R8.bmp", NULL);
D.x=SCREEN_W/2; D.y=500;

if (Image == NULL && Background == NULL)
{
allegro_message("could not load images");
allegro_exit();
}

 blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
z=700;i=0;
Image = D.left[0];

draw_sprite(Buffer,Image, z, D.y);

while (!key[KEY_ESC]) {

 do  {

clear_bitmap(Buffer);
i++;
z=z-1;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
blit(Buffer, screen, 0, 0, 0, 0, z, D.y);

//clear_bitmap(Image);
Image = D.left[(i-1)%4];
draw_sprite(Buffer,Image, z, D.y);

} while (z<400);


blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
//readkey();
}


destroy_bitmap(Image);
destroy_bitmap(Buffer);
}

void jenn (jennifer M,dog D,BITMAP *Buffer , BITMAP *Background)
{ 
  BITMAP *Image;
  BITMAP *Image2;
  int i=0;
  int z;
  int k;
  int j=0;
M.right[0] = load_bitmap("./images/jenn1.bmp", NULL);
M.right[1] = load_bitmap("./images/jenn2.bmp", NULL);
M.right[2] = load_bitmap("./images/jenn3.bmp", NULL);
M.right[3] = load_bitmap("./images/jenn4.bmp", NULL);
M.left[0] = load_bitmap("./images/jenn5.bmp", NULL);
M.left[1] = load_bitmap("./images/jenn6.bmp", NULL);
M.left[2] = load_bitmap("./images/jenn7.bmp", NULL);
M.left[3] = load_bitmap("./images/jenn8.bmp", NULL);
M.x=SCREEN_W/2; M.y=355;
 
D.right[0] = load_bitmap("./Images/R1.bmp", NULL);
D.right[1] = load_bitmap("./Images/R2.bmp", NULL);
D.right[2] = load_bitmap("./Images/R3.bmp", NULL);
D.right[3] = load_bitmap("./Images/R4.bmp", NULL);
D.x=SCREEN_W/2; D.y=500;

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
z =0;i=0;k=400;j=0;
//Image = M.right[0];
//draw_sprite(Buffer,Image, z, M.y);

Image2 = D.right[0];
draw_sprite(Buffer,Image2, k, D.y);


while (!key[KEY_ESC]) {

do  { 
//clear_bitmap(Buffer);
j++;
k=k+1;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
//blit(Buffer, screen, 0, 0, 0, 0, k, D.y);

//clear_bitmap(Image);
Image2 = D.right[(j-1)%4];
draw_sprite(Buffer,Image2, k, D.y);
blit(Buffer, screen, 0, 0, 0, 0, k, D.y);
} while (k>700);

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


Image = M.right[0];
draw_sprite(Buffer,Image, z, M.y);

while (!key[KEY_ESC]) {
do {
j++;
k=k+1;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
blit(Buffer, screen, 0, 0, 0, 0, k, D.y);
Image2 = D.right[(j-1)%4];
draw_sprite(Buffer,Image2, k, D.y);
} while (k>700);

if (key[KEY_RIGHT])
{ if (z<730)
   {

//clear_bitmap(Buffer);
i++;
z=z+3;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
blit(Buffer, screen, 0, 0, 0, 0, z, M.y);

//clear_bitmap(Image);
Image = M.right[(i-1)%4];
draw_sprite(Buffer,Image, z, M.y);

} }


if (key[KEY_LEFT])
{ if (z>2)
 {
clear_bitmap(Buffer);
i++;
z=z-5;
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);
blit(Buffer, screen, 0, 0, 0, 0, z, M.y);
//clear_bitmap(Image);
Image = M.left[(i-1)%4];
draw_sprite(Buffer,Image, z, M.y);

} }

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
//readkey();
}
}
destroy_bitmap(Image2);
destroy_bitmap(Buffer);
destroy_bitmap(Image);
destroy_bitmap(Buffer);

}

