#include <allegro.h>                                                                      // Include Allegra
#include <alfont.h>
#include <almp3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>                                                                        // Include klasickeho stdio.h
#include <iostream>
#include <time.h>
#include "button.h"                                                                       // Include triedy tlacitka

#define RES640x480 1
#define RES800x600 2
#define RES1024x768 3
#define RES1154x864 4
#define RES1280x960 5
#define RES1280x1024 6

#define DATASZ  (1<<15) /* (32768) amount of data to read from disk each time */
#define BUFSZ   (1<<16) /* (65536) size of audiostream buffer */

#define NO_LANG_STRINGS 10

using namespace std;

FILE *language=NULL;

char s1[20];
char s2[20];
char s3[20];
char s4[20];
char s5[20];
char s6[20];
char s7[20];
char s8[60];
char s9[60];
char s10[20];

int i;
volatile int ms=0;
volatile bool credits_bool=FALSE;
volatile int sekundy=0;
volatile int outgame_sekundy=0;
int width,height;                                                                         // Deklaracia premennych urcujucich sirku a vysku okna
bool done=FALSE;                                                                          // Premenna signalizujuca ukoncenie hlavneho cyklu
bool fullscreen=FALSE;
bool change_function=FALSE;

typedef struct riadok
{
  bool existence;
  char s[40];
  int vyska;  
}RIADOK;

typedef struct 
{
  PACKFILE *f;
  ALMP3_MP3STREAM *s;
} MP3FILE;

/*typedef struct retazec
{
  char s[];
}RETAZEC;*/

ALMP3_MP3STREAM *s = NULL;
MP3FILE *baywatch = NULL;
MP3FILE *eso = NULL;
MP3FILE *music = NULL;

int GetLanguage();
void SetLanguage();
void language_launcher();
void launcher();
void intro();
void GetResolution();
void credits_timer();
void intro_timer();
void Options();
void resize();
void HighScore();
void NewGame();
void LoadGame();
void MultiPlayer();
void QuitGame();
void Credits();
void ChuckFacts();
void briefing(ALFONT_FONT *beznytext,int farba,char text[]);

MP3FILE *open_mp3_file(char *filename) 
{
  MP3FILE *p = NULL;
  PACKFILE *f = NULL;
  char data[DATASZ];
  int len;

  if (!(p = (MP3FILE *)malloc(sizeof(MP3FILE))))
    goto error;
  if (!(f = pack_fopen(filename, F_READ)))
    goto error;
  if ((len = pack_fread(data, DATASZ, f)) <= 0)
    goto error;
  if (len < DATASZ) {
    if (!(s = almp3_create_mp3stream(data, len, TRUE)))
      goto error;
  }
  else 
  {
    if (!(s = almp3_create_mp3stream(data, DATASZ, FALSE)))
      goto error;
  }
  p->f = f;
  p->s = s;
  return p;

  error:
  pack_fclose(f);
  free(p);
  return NULL;
}


int play_mp3_file(MP3FILE *mp3, int buflen, int vol, int pan) 
{
  return almp3_play_mp3stream(mp3->s, buflen, vol, pan);
}

void close_mp3_file(MP3FILE *mp3) 
{
  if (mp3) 
  {
    pack_fclose(mp3->f);
    almp3_destroy_mp3stream(mp3->s);
    free(mp3);
  }
}

int poll_mp3_file(MP3FILE *mp3) 
{
  char *data;
  long len;
  data = (char *)almp3_get_mp3stream_buffer(mp3->s);
  if (data) 
  {
    len = pack_fread(data, DATASZ, mp3->f);
    if (len < DATASZ)
      almp3_free_mp3stream_buffer(mp3->s, len);
    else
      almp3_free_mp3stream_buffer(mp3->s, -1);
  }

  return almp3_poll_mp3stream(mp3->s);
}

void sleep (unsigned int mseconds) 
{     
     clock_t goal = mseconds + clock();
     while(goal > clock());     
}

int GetLanguage()
{
  int i;
  FILE *lang;
  printf("Choosing language...\n");
  lang=fopen("cfg/language.cfg","r");
  fscanf(lang,"%d",&i);
  printf("Language sucesfully chosen!\n");
  fclose(lang);
  return i;
}

void SetLanguage(int number)
{
  if(number==1)//Slovencina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"Nova Hra",19);
    strncpy(s2,"Nahrat hru",19);
    strncpy(s4,"Najvyssie skore",19);
    strncpy(s5,"Nastavenia",19);
    strncpy(s6,"Autori hry",19);
    strncpy(s7,"Koniec hry",19);
    strncpy(s8,"Najlepsi cas dosiahnuty v hre ChuckMarine",59);
    strncpy(s9,"Hra este nebola prejdena",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");
  }
  if(number==2)//Cestina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"Nova Hra",19);
    strncpy(s2,"Nahrat hru",19);
    strncpy(s4,"Nejvyssi skore",19);
    strncpy(s5,"Nastaveni",19);
    strncpy(s6,"Autori hry",19);
    strncpy(s7,"Konec hry",19);
    strncpy(s8,"Nejlepsi cas dosahnuty ve hre ChuckMarine",59);
    strncpy(s9,"Hra jeste nebyla pokorena",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");    
  }
  if(number==3)//Anglictina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"New Game",19);
    strncpy(s2,"Load Game",19);
    strncpy(s4,"High Score",19);
    strncpy(s5,"Options",19);
    strncpy(s6,"Credits",19);
    strncpy(s7,"Quit Game",19);
    strncpy(s8,"Best time, that was gained in ChuckMarine",59);
    strncpy(s9,"The game is not completed yet",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");
  }
  if(number==4)//Nemcina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"Neues Spiel",19);
    strncpy(s2,"Einspielen das Spiel",19);
    strncpy(s4,"Hochstes Torverhaltnis",19);
    strncpy(s5,"Die Anstellungen",19);
    strncpy(s6,"Spielautoren",19);
    strncpy(s7,"Ende",19);
    strncpy(s8,"Bestes Zeit im ChuckMarine Spiel erreicht",59);
    strncpy(s9,"Das Spiel ist nicht durchgegangen",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");
  }
  if(number==5)//Spanielcina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"Nuevo juego",19);
    strncpy(s2,"Caldear juego",19);
    strncpy(s4,"Soberano score",19);
    strncpy(s5,"Reglaje",19);
    strncpy(s6,"Autoros",19);
    strncpy(s7,"Fin juego",19);
    strncpy(s8,"Optimo tiempo alcanzado de juego ChuckMarino",59);
    strncpy(s9,"Juego sin cesar no estar pasar",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");    
  }
  if(number==6)//Madarcina
  {
    printf("Opening language file...\n");
    printf("Language file sucesfully opened!\n");
    printf("Loading text...\n");
    strncpy(s1,"Uj Jatek",19);
    strncpy(s2,"Foljatszani",19);
    strncpy(s4,"Csucs pontszam",19);
    strncpy(s5,"Bealitasok",19);
    strncpy(s6,"Jatek szerzok",19);
    strncpy(s7,"Jatek vege",19);
    strncpy(s8,"A jatekban ChuckMarine elert legjobb ido",59);
    strncpy(s9,"A jatek meg nincs kesz",59);
    strncpy(s10,"Soundtrack",19);
    printf("Text loaded!\n");
  }
}

// HODNE primitivne intro
void intro()
{
  
  int delay;
  bool end=FALSE;
  BITMAP *karel;
  BITMAP *logo;
  SAMPLE *sound;
  sound=load_sample("sfx/intro/water.wav");
  karel=load_bitmap("media/karel.tga",NULL);
  logo=load_bitmap("media/logo.tga",NULL);
  
  sekundy=0;
  install_int(intro_timer,1000);
  
  while(sekundy!=1)
    clear_to_color(screen,makecol(0,0,0)); 
  remove_int(intro_timer);
  
  sekundy=0;
  install_int(intro_timer,1000);
  
  clock_t stop = clock() + 3000;
  delay = 800;
  while(stop > clock())
  {
    //cout << "\a";
    if (delay > 1) sleep(delay);
    //delay = delay - 100;
    delay = delay / 2;
    stretch_blit(karel,screen,0,0,1280,1024,0,0,width,height);
    if(key[KEY_ENTER]||key[KEY_SPACE])
      sekundy=3;
  }
  remove_int(intro_timer);
  
  sekundy=0;
  install_int(intro_timer,1000);
  while(sekundy!=1)
    clear_to_color(screen,makecol(0,0,0));
  remove_int(intro_timer);
  
  
  int x,y,sirka,vyska;
  x=(int)((width/2)-((width/2)/1.6));
  y=(int)((height/2)-((height/2)/5.12));
  sirka=(int)(2*((width/2)/1.6));
  vyska=(int)(2*((height/2)/5.12));
  
  clear_to_color(screen,makecol(102,125,255));
  sekundy=0;
  install_int(intro_timer,1000);
  play_sample(sound,255,128,1000,FALSE);
  while(sekundy!=3)
  {
    stretch_sprite(screen,logo,x,y,sirka,vyska);
    if(key[KEY_ENTER]||key[KEY_SPACE])
      sekundy=3;
  }
  remove_int(intro_timer);
  
  destroy_bitmap(karel);
  destroy_bitmap(logo);
  destroy_sample(sound);  
}

void GetResolution()                                                                      // Funkcia nacitavajuca rozlisenie zo suboru
{
  FILE *cfg;                                                                              // Smernik na strukturu typu FILE
  cfg=fopen("cfg/menu.cfg","r");                                                          // Otvaranie suboru
  fscanf(cfg,"%d\n%d\n%d",&width,&height,&i);                                             // Nacitavanie rozlisenia
  if(i==1) {fullscreen=TRUE;}
  fclose(cfg);                                                                            // Uzatvaranie suboru
}

void briefing_timer() 
{
  ms++;
}

void credits_timer()
{
  credits_bool=TRUE;
}
END_OF_FUNCTION(credits_timer);

void intro_timer()
{
  sekundy++;
}
END_OF_FUNCTION(intro_timer);

void outgame_timer()
{
  outgame_sekundy++;
}
END_OF_FUNCTION(intro_timer);

void language_launcher()
{
	allegro_init();
	install_mouse();
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 300, 150, 0, 0);
	
	FILE *cfg;
	
	BITMAP *sk, *cz, *en, *ge, *es, *hu;
	sk = load_bitmap("gfx/language/sk.tga", NULL);
	cz = load_bitmap("gfx/language/cz.tga", NULL);
	en = load_bitmap("gfx/language/en.tga", NULL);
	ge = load_bitmap("gfx/language/ge.tga", NULL);
	es = load_bitmap("gfx/language/es.tga", NULL);
	hu = load_bitmap("gfx/language/hu.tga", NULL);
	
	int flags_width = 75;
	int flags_height = 50;
	int flags_start_x = 22;
	int flags_start_y = 10;
	int flags_hspace = 30;
	int flags_wspace = 15;
	
	clear_to_color(screen, makecol(180, 180, 180));
	
	int x = flags_start_x;
	int y = flags_start_y;
	
	blit(sk, screen, 0, 0, x, y, flags_width, flags_height);
	y += flags_height + flags_hspace;
	
	blit(cz, screen, 0, 0, x, y, flags_width, flags_height);

	x += flags_width + flags_wspace;	
	y = flags_start_y;
	
	blit(en, screen, 0, 0, x, y, flags_width, flags_height);
	y += flags_height + flags_hspace;
	
	blit(ge, screen, 0, 0, x, y, flags_width, flags_height);
	
	x += flags_width + flags_wspace;	
	y = flags_start_y;
	
	blit(es, screen, 0, 0, x, y, flags_width, flags_height);
	y += flags_height + flags_hspace;
	
	blit(hu, screen, 0, 0, x, y, flags_width, flags_height);
	
	show_mouse(screen);
	
	int lang = 0;
	
	while (TRUE) {
		
		if (mouse_b & 1) {
			
			if (mouse_x >= 22 && mouse_x <= 277 && mouse_y >= 10 && mouse_y <= 140) {
				
				if (mouse_y <= 60) {
					
					if (mouse_x >= 22 && mouse_x <= 97) lang = 1;
					else if (mouse_x >= 113 && mouse_x <= 188) lang = 3;
					else if (mouse_x >= 203 && mouse_x <= 278) lang = 5;
					
				} else if (mouse_y >= 90) {
					
					if (mouse_x >= 22 && mouse_x <= 97) lang = 2;
					else if (mouse_x >= 113 && mouse_x <= 188) lang = 4;
					else if (mouse_x >= 203 && mouse_x <= 278) lang = 6;
					
				}
				
				if (lang != 0) break;
				
			}
			
		}
		
	}
	
	cfg = fopen("cfg/language.cfg", "w");
	fprintf(cfg,"%d\n",lang);
	fclose(cfg);
	allegro_exit();
}

void launcher()
{
  allegro_init();
  install_mouse();
  set_gfx_mode(GFX_AUTODETECT_WINDOWED,200,300,0,0);
  
  int i,j;
  bool launcher_done=FALSE;
  int selected=0;
  int sel_res_x,sel_res_y;
  
  FILE *settings;
  FILE *menu;
  
  BITMAP *vysledok;
  vysledok=create_bitmap(300,400);
  
  while(!launcher_done)
  {
    text_mode(-1);
    clear_to_color(vysledok,makecol(180,180,180));
    textout_centre(vysledok,font,"SELECT SCREEN RESOLUTION",100,20,makecol(0,0,0));
    textout_centre(vysledok,font,"640x480",100,60,makecol(0,0,0));
    textout_centre(vysledok,font,"800x600",100,90,makecol(0,0,0));
    textout_centre(vysledok,font,"1024x768",100,120,makecol(0,0,0));
    textout_centre(vysledok,font,"1154x864",100,150,makecol(0,0,0));
    textout_centre(vysledok,font,"1280x960",100,180,makecol(0,0,0));
    textout_centre(vysledok,font,"1280x1024",100,210,makecol(0,0,0));
    circlefill(vysledok,35,62,4,makecol(255,255,255));
    circlefill(vysledok,35,92,4,makecol(255,255,255));
    circlefill(vysledok,35,122,4,makecol(255,255,255));
    circlefill(vysledok,35,152,4,makecol(255,255,255));
    circlefill(vysledok,35,182,4,makecol(255,255,255));
    circlefill(vysledok,35,212,4,makecol(255,255,255));
    if(mouse_b&1)
    {
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=46 && mouse_y <=74 ))
        selected=RES640x480;
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=76 && mouse_y <=104 ))
        selected=RES800x600;
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=106 && mouse_y <=134 ))
        selected=RES1024x768;
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=136 && mouse_y <=164 ))
        selected=RES1154x864;
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=166 && mouse_y <=194 ))
        selected=RES1280x960;
      if((mouse_x>=20 && mouse_x<=180 )&&(mouse_y>=196 && mouse_y <=224 ))
        selected=RES1280x1024;
      
      if((mouse_x>=40 && mouse_x<=160 )&&(mouse_y>=240 && mouse_y <=280 ))
      {
        if(selected!=0)
        {
          launcher_done=TRUE;
        } 
      }
    }
    if(selected==1)
      circlefill(vysledok,35,62,2,makecol(0,0,0));
    if(selected==2)
      circlefill(vysledok,35,92,2,makecol(0,0,0));
    if(selected==3)
      circlefill(vysledok,35,122,2,makecol(0,0,0));
    if(selected==4)
      circlefill(vysledok,35,152,2,makecol(0,0,0));
    if(selected==5)
      circlefill(vysledok,35,182,2,makecol(0,0,0));
    if(selected==6)
      circlefill(vysledok,35,212,2,makecol(0,0,0));
      
    rect(vysledok,40,240,160,280,makecol(0,0,0));
    rect(vysledok,41,241,159,279,makecol(0,0,0));
    textout_centre(vysledok,font,"OK",100,257,makecol(0,0,0));
                                                                                   
    show_mouse(vysledok);
    blit(vysledok,screen,0,0,0,0,300,400);
  }
  settings=fopen("cfg/menu.cfg","w");
  if(selected==1)
  {
    sel_res_x=640;
    sel_res_y=480;
  }
  if(selected==2)
  {
    sel_res_x=800;
    sel_res_y=600;
  }
  if(selected==3)
  {
    sel_res_x=1024;
    sel_res_y=768;
  }
  if(selected==4)
  {
    sel_res_x=1154;
    sel_res_y=864;
  }
  if(selected==5)
  {
    sel_res_x=1280;
    sel_res_y=960;
  }
  if(selected==6)
  {
    sel_res_x=1280;
    sel_res_y=1024;
  } 
  fprintf(settings,"%d\n",sel_res_x); 
  fprintf(settings,"%d\n",sel_res_y);
  fprintf(settings,"1\n");  
  fclose(settings);
  
  allegro_exit();
}

void Options()
{
  int x,y;
  int full;
  FILE *settings;
  bool change=FALSE;
  int selection=0;
  int nadpis_size=(int)(height/10);
  int nadpis2_size=(int)(height/20);
  int text_size=(int)(height/36.923076);
   
  int text_color=makecol(128,128,128);
  int nadpis_color=makecol(76,76,255);
  int nadpis2_color=makecol(76,255,128);
      
  BITMAP *buffer;
  buffer=create_bitmap(width,height);
  ALFONT_FONT *text,*nadpis,*nadpis2;
  text=alfont_load_font("data/fonts/credits.ttf");
  nadpis=alfont_load_font("data/fonts/formal.ttf");
  nadpis2=alfont_load_font("data/fonts/credits.ttf");
  
  alfont_set_font_size(text,text_size);
  alfont_set_font_size(nadpis,nadpis_size);
  alfont_set_font_size(nadpis2,nadpis2_size);
  
  clear_to_color(buffer,makecol(0,0,0));
  
  bool options_done=FALSE;
  while(!options_done)
  {
    poll_mp3_file(baywatch);
    if(key[KEY_ESC])
    {
      selection=0;
      options_done=TRUE;
    }
    if(key[KEY_F1])
    {
      selection=1;
      options_done=TRUE;
    }
    if(key[KEY_F2])
    {
      selection=2;
      options_done=TRUE;
    }
    if(key[KEY_F3])
    {
      selection=3;
      options_done=TRUE;
    }
    if(key[KEY_F4])
    {
      selection=4;
      options_done=TRUE;
    }
    if(key[KEY_F5])
    {
      selection=5;
      options_done=TRUE;
    }
    if(key[KEY_F6])
    {
      selection=6;
      options_done=TRUE;
    }
    if(key[KEY_F7])
    {
      selection=7;
      options_done=TRUE;
    }
    if(key[KEY_F8])
    {
      selection=8;
      options_done=TRUE;
    }
    if(key[KEY_F9])
    {
      selection=9;
      options_done=TRUE;
    }
    if(key[KEY_F10])
    {
      selection=10;
      options_done=TRUE;
    }
    if(key[KEY_F11])
    {
      selection=11;
      options_done=TRUE;
    }
    if(key[KEY_F12])
    {
      selection=12;
      options_done=TRUE;
    }
    
    clear_to_color(buffer,makecol(0,0,0));    
    
    alfont_textout_centre_aa(buffer,nadpis,s5,width/2,(nadpis_size/2)+(height/20),nadpis_color);
    alfont_textout_aa(buffer,nadpis2,"Zmen rozlisenie:",width/5,nadpis_size+(height/20) + nadpis2_size + 10,nadpis2_color);
    alfont_textout_aa(buffer,text,"[F1] 640x480",width/5,nadpis_size+(height/20) + nadpis2_size + 2*text_size + 20,text_color);
    alfont_textout_aa(buffer,text,"[F2] 800x600",width/5,nadpis_size+(height/20) + nadpis2_size + 3*text_size + 30,text_color);
    alfont_textout_aa(buffer,text,"[F3] 1024x768",width/5,nadpis_size+(height/20) + nadpis2_size + 4*text_size + 40,text_color);
    alfont_textout_aa(buffer,text,"[F4] 1152x864",width/5,nadpis_size+(height/20) + nadpis2_size + 5*text_size + 50,text_color);
    alfont_textout_aa(buffer,text,"[F5] 1280x960",width/5,nadpis_size+(height/20) + nadpis2_size + 6*text_size + 60,text_color);
    alfont_textout_aa(buffer,text,"[F6] 1280x1024",width/5,nadpis_size+(height/20) + nadpis2_size + 7*text_size + 70,text_color);
    alfont_textout_aa(buffer,text,"[F7] 640x480 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 8*text_size + 80,text_color);
    alfont_textout_aa(buffer,text,"[F8] 800x600 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 9*text_size + 90,text_color);
    alfont_textout_aa(buffer,text,"[F9] 1024x768 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 10*text_size + 100,text_color);
    alfont_textout_aa(buffer,text,"[F10] 1152x864 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 11*text_size + 110,text_color);
    alfont_textout_aa(buffer,text,"[F11] 1280x960 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 12*text_size + 120,text_color);
    alfont_textout_aa(buffer,text,"[F12] 1280x1024 v okne",width/5,nadpis_size+(height/20) + nadpis2_size + 13*text_size + 130,text_color);
    alfont_textout_aa(buffer,text,"[ESC] Spat",width/5,nadpis_size+(height/20) + nadpis2_size + 15*text_size + 150,text_color);
    
    blit(buffer,screen,0,0,0,0,width,height);
  }
  if(selection!=0)
  {
    switch (selection)
    {
      case 1:
            x=640,y=480,full=1;break;
      case 2:
            x=800,y=600,full=1;break;
      case 3:
            x=1024,y=768,full=1;break;
      case 4:
            x=1152,y=864,full=1;break;
      case 5:
            x=1280,y=960,full=1;break;
      case 6:
            x=1280,y=1024,full=1;break;
      case 7:
            x=640,y=480,full=0;break;
      case 8:
            x=800,y=600,full=0;break;
      case 9:
            x=1024,y=768,full=0;break;
      case 10:
            x=1152,y=864,full=0;break;
      case 11:
            x=1280,y=960,full=0;break;
      case 12:
            x=1280,y=1024,full=0;
    }
    printf("%d %d %d\n",x,y,full);
    settings=fopen("cfg/menu.cfg","w");
    fprintf(settings,"%d\n%d\n%d\n",x,y,full);
    fclose(settings);
    close_mp3_file(baywatch);
    atexit(resize);
    exit(0);
  }
  destroy_bitmap(buffer);
  alfont_destroy_font(text);
}

void resize()
{
  allegro_exit();
  system("ChuckMarine.exe");
}

void HighScore()
{ 
  int time=0;
  int minuty,sekundy;
  FILE *score;
  bool existence;
  int nadpis_size=(int)(height/10);
  int text_size=(int)(height/36.923076);
  int skore_size=(int)(height/20);
  
  int nadpis_color=makecol(227,152,230);
  int text_color=makecol(128,128,128);
  int skore_color=makecol(137,219,179);
  
  if((score=fopen("cfg/score.cfg","r"))==NULL)
  {
    existence=FALSE;
  }
  else
  {
    existence=TRUE;
    score=fopen("cfg/score.cfg","r");
    fscanf(score,"%d",&time);
  }

  sekundy=time%60;
  minuty=(time-sekundy)/60;
    
  BITMAP *buffer;
  buffer=create_bitmap(width,height);
  ALFONT_FONT *nadpis,*skore,*text;
  nadpis=alfont_load_font("data/fonts/formal.ttf");
  skore=alfont_load_font("data/fonts/copperlate.ttf");
  text=alfont_load_font("data/fonts/credits.ttf");
  
  alfont_set_font_size(nadpis,nadpis_size);
  alfont_set_font_size(text,text_size);
  alfont_set_font_size(skore,skore_size);
  
  clear_to_color(buffer,makecol(0,0,0));
  
  bool highscore_done=FALSE;
  while(!highscore_done)
  {
    poll_mp3_file(baywatch);
    if(key[KEY_ESC]) highscore_done=TRUE;
    
    clear_to_color(buffer,makecol(0,0,0));
    alfont_textout_centre_aa(buffer,nadpis,s4,width/2,(nadpis_size/2)+(height/20),nadpis_color);
    alfont_textout_centre_aa(buffer,text,s8,width/2,(int)(7*height/10),text_color);
    
    if(existence)
    {
      alfont_textprintf_centre_aa(buffer,skore,width/2,height/2,skore_color,"%d : %d",minuty,sekundy);
    }
    else
    {
      alfont_textprintf_centre_aa(buffer,skore,width/2,height/2,skore_color,s9);
    }
    
    blit(buffer,screen,0,0,0,0,width,height);
  }
  if(existence) fclose(score);
  destroy_bitmap(buffer);
  alfont_destroy_font(nadpis);
  alfont_destroy_font(text);
  alfont_destroy_font(skore);
}

void levels()
{
  allegro_exit();
  system("levels.exe");
}

void NewGame()
{
  close_mp3_file(baywatch);
  FILE *level;
  level=fopen("cfg/level.cfg","w");
  fprintf(level,"1");
  atexit(levels);
  exit(0);
}

void SoundTrack()
{
  close_mp3_file(baywatch);
  bool playing=FALSE;
  bool change=FALSE;
  bool soundtrack_done=FALSE;
  int nadpis_size=(int)(height/20);
  int nadpis2_size=(int)(height/30);
  int text_size=(int)(height/36.923076);
  int nadpis_color=makecol(176,128,76); 
  int nadpis2_color=makecol(76,128,255);
  int text_color=makecol(128,128,128);
      
  BITMAP *buffer;
  buffer=create_bitmap(width,height);
  ALFONT_FONT *text,*nadpis,*nadpis2;
  text=alfont_load_font("data/fonts/credits.ttf");
  nadpis=alfont_load_font("data/fonts/formal.ttf");
  nadpis2=alfont_load_font("data/fonts/formal.ttf");
  alfont_set_font_size(text,text_size);
  alfont_set_font_size(nadpis,nadpis_size);
  alfont_set_font_size(nadpis2,nadpis2_size);
  
  clear_to_color(buffer,makecol(0,0,0));
  
  while(!soundtrack_done)
  {
    if(key[KEY_ESC]) soundtrack_done=TRUE;

    if(key[KEY_1] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/music/menu.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(key[KEY_2] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/credits/eso.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(key[KEY_3] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/music/mix1.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(key[KEY_4] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/music/mix2.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(key[KEY_5] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/music/mix3.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(key[KEY_6] && !change)
    {
      if(playing) 
      {
        close_mp3_file(music);
        playing=FALSE;
      }
      music=open_mp3_file("sfx/music/wine.mp3");
      change=TRUE;
      play_mp3_file(music, BUFSZ, 255, 128);      
      playing=TRUE;
    }
    if(!key[KEY_1] && !key[KEY_2] && !key[KEY_3] && !key[KEY_4] && !key[KEY_5] && !key[KEY_6])
      change=FALSE;
      
    if(playing)
      poll_mp3_file(music);

    
    clear_to_color(buffer,makecol(0,0,0));    
    
    alfont_textout_centre_aa(buffer,nadpis,s10,width/2,(nadpis_size/2)+(height/20),nadpis_color);
    alfont_textout_aa(buffer,nadpis2,"Stlac na klavesnici cislo skladby, ktoru si chces vypocut:",width/10,height/5,nadpis2_color);
    
    alfont_textout_aa(buffer,text,"[1] Survivor",width/10,height/5 + nadpis2_size + 10,text_color);
    alfont_textout_aa(buffer,text,"[2] Eso`s beat",width/10,height/5 + nadpis2_size + text_size + 20,text_color);
    alfont_textout_aa(buffer,text,"[3] Mix 1",width/10,height/5 + nadpis2_size + 2*text_size + 30,text_color);
    alfont_textout_aa(buffer,text,"[4] Mix 2",width/10,height/5 + nadpis2_size + 3*text_size + 40,text_color);
    alfont_textout_aa(buffer,text,"[5] Mix 3",width/10,height/5 + nadpis2_size + 4*text_size + 50,text_color);
    alfont_textout_aa(buffer,text,"[6] Sound of Wine (nenachadza sa v hre)",width/10,height/5 + nadpis2_size + 5*text_size + 60,text_color);
    
    blit(buffer,screen,0,0,0,0,width,height);
  }
  if(playing) close_mp3_file(music);
  destroy_bitmap(buffer);
  alfont_destroy_font(text);
  alfont_destroy_font(nadpis);
  change_function=TRUE;
}

void LoadGame()
{
  close_mp3_file(baywatch);
  atexit(levels);
  exit(0);
}

void QuitGame()
{
  done=TRUE;
}

void ChuckFacts()
{
  close_mp3_file(baywatch);
  ALFONT_FONT *trebuchet;
  trebuchet=alfont_load_font("data/fonts/trebuchet.ttf");
  briefing(trebuchet,makecol(128,182,182),"Chuck Norris facts:\n\nGuns don't kill people.\nChuck Norris kills People.\n\nChuck Norris can slam a revolving door.\n\nChuck Norris doesn't give Christmas presents.\nIf you live to see Christmas,\nthat is your Christmas present from Chuck.\n\nChuck Norris always gets blackjack.\nEven when he's playing poker.\n\nThere is no theory of evolution.\nThere is just list of animals, Chuck Norris\nallows to live.\n\nChuck Norris can draw concave triangle.");
  change_function=TRUE;
}

void Credits()
{
  close_mp3_file(baywatch);
  bool done_credits=FALSE;
  int y=height;
  int velkost_textu;
  int col_bg,col_txt;
  int pocet_riadkov;
  int r,g,b;
 
  BITMAP *buffer; 
  ALFONT_FONT *cr_font;
  FILE *credits;
  
  eso = open_mp3_file("sfx/credits/eso.mp3");
  
  install_int(credits_timer,25);
  LOCK_VARIABLE(credits_bool);
  LOCK_FUNCTION(credits_timer);
  
  buffer=create_bitmap(width,height);
  cr_font=alfont_load_font("data/fonts/credits.ttf");  
  credits=fopen("cfg/credits.cfg","r");
  fscanf(credits,"%d\n",&r);
  fscanf(credits,"%d\n",&g);
  fscanf(credits,"%d\n",&b);
  col_bg=makecol(r,g,b);
  fscanf(credits,"%d\n",&r);
  fscanf(credits,"%d\n",&g);
  fscanf(credits,"%d\n",&b);
  col_txt=makecol(r,g,b);
  fscanf(credits,"%d\n",&pocet_riadkov);
  fscanf(credits,"%d\n",&velkost_textu);
  
  alfont_set_font_size(cr_font,velkost_textu);
  
  RIADOK riadok[pocet_riadkov+1];
  
  for(int loop=0;loop<pocet_riadkov+1;loop++,y+=(velkost_textu+(height/50)))
  {
    riadok[loop].existence=FALSE;
    riadok[loop].vyska=y;
  }
  
  strncpy(riadok[0].s,"ChuckMarine",39); riadok[0].existence=TRUE;
  strncpy(riadok[1].s,"",39); riadok[1].existence=TRUE;
  strncpy(riadok[2].s,"",39); riadok[2].existence=TRUE;
  strncpy(riadok[3].s,"Programmers",39); riadok[3].existence=TRUE;
  strncpy(riadok[4].s,"Peter \"5Hz\" Csiba",39); riadok[4].existence=TRUE;
  strncpy(riadok[5].s,"Ladislav \"lacop\" Papay",39); riadok[5].existence=TRUE;
  strncpy(riadok[6].s,"Rastislav \"BiXXX\" Tisovcik",39); riadok[6].existence=TRUE;
  strncpy(riadok[7].s,"",39); riadok[7].existence=TRUE;
  strncpy(riadok[8].s,"",39); riadok[8].existence=TRUE;
  strncpy(riadok[9].s,"Graphics designer",39); riadok[9].existence=TRUE;
  strncpy(riadok[10].s,"Barbora \"gabriel\" Holla",39); riadok[10].existence=TRUE;
  strncpy(riadok[11].s,"Kristina \"lorelai\" Jablonicka",39); riadok[11].existence=TRUE;
  strncpy(riadok[12].s,"",39); riadok[12].existence=TRUE;
  strncpy(riadok[13].s,"",39); riadok[13].existence=TRUE;
  strncpy(riadok[14].s,"Music & Audio by",39); riadok[14].existence=TRUE;
  strncpy(riadok[15].s,"DJ Michael \"Majkl\" Kasarda",39); riadok[15].existence=TRUE;
  strncpy(riadok[16].s,"",39); riadok[16].existence=TRUE;
  strncpy(riadok[17].s,"",39); riadok[17].existence=TRUE;
  strncpy(riadok[18].s,"Special Thanks to",39); riadok[18].existence=TRUE;
  strncpy(riadok[19].s,"Emil \"Eso\" Smoliga",39); riadok[19].existence=TRUE;
  strncpy(riadok[20].s,"",39); riadok[20].existence=TRUE;
  strncpy(riadok[21].s,"",39); riadok[21].existence=TRUE;
  strncpy(riadok[22].s,"",39); riadok[22].existence=TRUE;
  strncpy(riadok[23].s,"",39); riadok[23].existence=TRUE;
  strncpy(riadok[24].s,"Dedicated to Chuck",39); riadok[24].existence=TRUE;
  
  show_mouse(NULL);
  clear_to_color(buffer,col_bg);  
  play_mp3_file(eso, BUFSZ, 255, 128);
  while(!done_credits)
  {
    poll_mp3_file(eso);
  
    vsync();
    if(credits_bool)
    {
      clear_to_color(buffer,col_bg);
      for(int loop=0;loop<=pocet_riadkov;loop++)
      { 
        riadok[loop].vyska-=(int)((float)height/480);
        if(riadok[loop].vyska>=-velkost_textu && riadok[loop].vyska<=height+velkost_textu);
        {
           alfont_textprintf_centre_aa(buffer,cr_font,width/2,riadok[loop].vyska,col_txt,"%s",riadok[loop].s);
        }
      }
      credits_bool=FALSE;
    }
    blit(buffer,screen,0,0,0,0,width,height);
    if(riadok[pocet_riadkov].vyska<=0-velkost_textu) done_credits=TRUE;
    
    if(key[KEY_ESC]||key[KEY_SPACE]||key[KEY_ENTER])
    {
      done_credits=TRUE;
    }
  }
  close_mp3_file(eso);
  free((void *)riadok);
  show_mouse(screen);
  destroy_bitmap(buffer);
  change_function=TRUE;  
}

void outgame()
{
  BITMAP *thanks;
  thanks=load_bitmap("gfx/thanks.tga",NULL);
  bool outgame_done=FALSE;
  install_int(outgame_timer,1000);
  while(!outgame_done)
  {
    stretch_blit(thanks,screen,0,0,1280,1024,0,0,width,height);
    if(outgame_sekundy==3)
    {
      outgame_done=TRUE;
    }
  }
  destroy_bitmap(thanks);
}

int main()                                                                                // Main :-)
{ 
  FILE *first_run;
  if((first_run=fopen("cfg/run.cfg","r"))==NULL)
  {
    first_run=fopen("cfg/run.cfg","w");
    fprintf(first_run," $0xff#h *& @");
    launcher();
    language_launcher();
    fclose(first_run);
  }
  GetResolution();
  
  SetLanguage(GetLanguage());  
  
  int ideal_height=height/32;
  if(ideal_height<=15) {ideal_height=16;}
  
  void (*p_ng)();
  void (*p_lg)();
  void (*p_hs)();
  void (*p_sndtrck)();
  void (*p_opt)();                                                                         // Deklaracia smernikov na funkcie ktore sa maju vykonat pri stlaceni tlacitka
  void (*p_cr)();
  void (*p_qg)();
  void (*p_chuck)();
  
  p_ng = NewGame;
  p_lg = LoadGame;
  p_hs = HighScore;
  p_sndtrck = SoundTrack;
  p_opt = Options;
  p_cr = Credits;                                                                        // Definicia smernikov na funkcie
  p_qg = QuitGame;
  p_chuck = ChuckFacts;
  
  allegro_init();                                                                         // Inicializacia Allegra
  alfont_init();
  install_keyboard();                                                                     // Inicializacia klavesnice
  install_mouse();                                                                        // Inicializacia mysi
  install_timer();                                                                        // Inicializacia timerov
  install_sound(DIGI_AUTODETECT,MIDI_NONE,NULL);                                          // Inicializacia zvuku
  set_color_depth(16);                                                                    // Nastavovanie farebnej hlbky
  if(!fullscreen) set_gfx_mode(GFX_AUTODETECT_WINDOWED,width,height,0,0);                 // Nastavovanie grafickeho rezimu
  if(fullscreen) set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,width,height,0,0);                // Nastavovanie grafickeho rezimu
  
  intro();
  
  baywatch = open_mp3_file("sfx/music/menu.mp3");
  
  BITMAP *pozadie;                                                                        // Smernik na pozadie nacitane zo suboru
  BITMAP *mouse_cursor;                                                                   // Smernik na kurzor mysi nacitany zo suboru
  BITMAP *buffer;                                                                         // Vytvorenie smernika na buffer
  SAMPLE *snd_b1,*snd_b1_c;
  mouse_cursor=load_bitmap("gfx/menu/cursor.bmp",NULL);                                   // Nacitavanie kurzora
  pozadie=load_bitmap("gfx/menu/background.tga",NULL);                                    // Nacitavanie pozadia
  buffer=create_bitmap(width,height);                                                     // Vytvaranie buffera
  snd_b1=load_sample("sfx/menu/b1.wav");
  snd_b1_c=load_sample("sfx/menu/b1_c.wav");
  set_mouse_sprite(mouse_cursor);                                                         // Nastavenie kurzoru mysi 
  
  stretch_blit(pozadie,buffer,0,0,1280,1024,0,0,width,height);                            // Nahravanie pozadia do bufferu
  
  ALFONT_FONT *f1,*f2;
  f1=alfont_load_font("data/fonts/copperlate.ttf");
  alfont_set_font_size(f1,ideal_height);                                                   

  Gombik new_game(s1,s1,p_ng,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)-5*(height/20),alfont_text_length(f1,s1),ideal_height,width/2,(height/2)-5*(height/20));                 
  Gombik load_game(s2,s2,p_lg,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)-4*(height/20),alfont_text_length(f1,s2),ideal_height,width/2,(height/2)-4*(height/20));                
  Gombik high_score(s4,s4,p_hs,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)-3*(height/20),alfont_text_length(f1,s4),ideal_height,width/2,(height/2)-3*(height/20));                
  Gombik soundtrack(s10,s10,p_sndtrck,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)-2*(height/20),alfont_text_length(f1,s10),ideal_height,width/2,(height/2)-2*(height/20));                
  Gombik options(s5,s5,p_opt,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)-(height/20),alfont_text_length(f1,s5),ideal_height,width/2,(height/2)-(height/20));               
  Gombik credits(s6,s6,p_cr,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,height/2,alfont_text_length(f1,s6),ideal_height,width/2,height/2);               
  Gombik quit_game(s7,s7,p_qg,NULL,NULL,buffer,snd_b1,snd_b1_c,f1,f1,makecol(128,128,128),makecol(182,182,182),width/2,(height/2)+(height/20),alfont_text_length(f1,s7),ideal_height,width/2,(height/2)+(height/20));               
  Gombik chuck("","",p_chuck,NULL,NULL,buffer,NULL,NULL,f1,f1,makecol(128,128,128),makecol(182,182,182),(int)(width/1.163636),(int)(height/2.048),(int)(width/7.111111),(int)(height/4.096),0,0);               
  
  play_mp3_file(baywatch, BUFSZ, 255, 128);

  while(!done)                                                                            // Hlavny cyklus menu
  {
    
    new_game.Krok();
    load_game.Krok();
    high_score.Krok();
    credits.Krok();                                                                      // Vykreslenie tlacitok
    options.Krok();
    soundtrack.Krok();
    quit_game.Krok();
    chuck.Krok();
    
    if(change_function)
    {
      change_function=FALSE;
      baywatch = open_mp3_file("sfx/music/menu.mp3");
      play_mp3_file(baywatch, BUFSZ, 255, 128);      
    }
    if ((baywatch) && (poll_mp3_file(baywatch) != ALMP3_OK)) 
    {
      close_mp3_file(baywatch);
    }        
    show_mouse(buffer);                                                                   // Aktualizacia mysi v bufferi
    blit(buffer,screen,0,0,0,0,width,height);                                             // Vykreslenie bufferu na obrazovku 
  }
  close_mp3_file(baywatch);
  outgame();                                                                              // Koniec hlavneho cyklu
                                                                                          // Uvolnovanie obrazkov z pamati
  destroy_bitmap(mouse_cursor);
  destroy_bitmap(pozadie);
  destroy_sample(snd_b1);
  destroy_sample(snd_b1_c);
}
END_OF_MAIN();                                                                            // Koniec programu

void briefing(ALFONT_FONT *beznytext,int farba,char text[])
{
  int bezny_size=(int)(height/36.923);
  alfont_set_font_size(beznytext,bezny_size);
  int text_x=(int)(width/1.82),text_y=(int)(height/5.12);
  int font_size=(int)(height/21.333333);
  int pocet_vypisanych=0;
  int r=0,g=0,b=0;
  bool raise=TRUE;
  BITMAP *chuck;
  BITMAP *buffer;
  SAMPLE *beat;
  ALFONT_FONT *copper;
  copper=alfont_load_font("data/fonts/copperlate.ttf");
  alfont_set_font_size(copper,font_size);
  
  beat=load_sample("sfx/briefing/beat.wav");
  
  buffer=create_bitmap(width,height);
  chuck=load_bitmap("gfx/briefing/chuck.tga",NULL);
  
  clear_to_color(buffer,makecol(0,0,0));
  
  install_int(briefing_timer,8);
  play_sample(beat,128,128,1000,TRUE);
  while(!key[KEY_ENTER]&&!key[KEY_ENTER_PAD])
  {
    if(ms!=0)
    {
      if(raise)
      {
        r++;
        g++;
        b++;
      }
      if(!raise)
      {
        r--;
        g--;
        b--;
      }
      if(r>=128) raise=FALSE;
      if(r<=0) raise=TRUE;
      ms=0;
      if(pocet_vypisanych<strlen(text))
        pocet_vypisanych++;
    }
    stretch_sprite(buffer,chuck,(int)(width/12.8),(int)(height/6.4),(int)(width/2.31),(int)(height/1.47));
    for(int loop=0;loop<pocet_vypisanych;loop++)
    {
      char c=text[loop];
      if(c=='\n')
      {
        text_x=(int)(width/1.82);
        text_y+=bezny_size+2;
      }
      else
      {
        if(text_x>=width-(2*bezny_size))
        {
          text_x=(int)(width/1.82);
          text_y+=bezny_size+2;
        }
        alfont_textprintf_aa(buffer,beznytext,text_x,text_y,farba,"%c",c);
        char retazec[2];
        retazec[0]=c;
        retazec[1]='\0';
        int j=alfont_text_length(beznytext,retazec);
        text_x+=j;
      }
    }
    alfont_textout_centre_aa(buffer,copper,"Press ENTER to continue",width/2,(height-(height/10)),makecol(r,g,b));
    blit(buffer,screen,0,0,0,0,width,height);
    text_x=(int)(width/1.82);
    text_y=(int)(height/5.12);
  }
  remove_int(briefing_timer);
  alfont_destroy_font(copper);
  destroy_bitmap(buffer);
  destroy_bitmap(chuck);
  destroy_sample(beat);
}

