// Position des differents choix disponibles dans le MENU principal ...

static int PrincPosXY[10*2]={  0,  0, 105,130, 155,180, 205,200, 255,290,
                                      305,300, 355,320, 405,395,   0,  0 };

static int ProgPosXY[10*2]=  {  0,  0, 165,220, 215,295, 265,295, 315,300,
                                       365,325, 415,335,   0,  0 };

static int PartPosXY[10*2]=  {  0,  0, 160,262, 211,272, 260,312, 311,290,
                                       361,273 };

static int C2GPosXY[10*2]=   {  0,  0, 205,260, 255,260, 305,275, 355,325,
                                       405,365 };

static int DelirPosXY[10*2]= {  0,  0, 156,210, 206,250, 256,250, 306,270,
                                       356,280, 406,295 };

static int TruLifPosXY[10*2]={  0,  0, 210,175, 260,225, 310,265, 360,325,
                                       410,340, 000,000 };

   static unsigned int nblig, llen, psize, LpP;         // plein de variables
   static unsigned char* TEXTbuffer;                    // pour le texte...
   static unsigned int NumLig;
   static long TEXTlen;
   static word Xlig,Ylig,Colig;

void TestZic()           // test les touches F1-F5 pour le choix de la ZIC !
{if (!NoSound)
  {        if (WasKey(KeyF1)) { if (NumZic!=1)
                          {StopModule(NumZic); NumZic=1; JoueModule(NumZic);}}
      else if (WasKey(KeyF2)) { if (NumZic!=2) 
                          {StopModule(NumZic); NumZic=2; JoueModule(NumZic);}}
      else if (WasKey(KeyF3)) { if (NumZic!=3) 
                          {StopModule(NumZic); NumZic=3; JoueModule(NumZic);}}
      else if (WasKey(KeyF4)) { if (NumZic!=4) 
                          {StopModule(NumZic); NumZic=4; JoueModule(NumZic);}} /**/
      else if (WasKey(KeyF5)) { if (NumZic!=5) 
                          {StopModule(NumZic); NumZic=5; JoueModule(NumZic);}} /**/
  }
}


void TestColor()
{ static word i,t;

           if (WasKey(KeyPadPlus))
            { if (GammaCorrec<=150)
               { GammaCorrec+=10;
                 for (i=0; i<=768-3; i++)
                  { t=(pal[i]*GammaCorrec)/100;
                    if (t<=63) palt2[i]=t;
                    else palt2[i]=63;
                  }
                 SetPalette(palt2); 
               }
            }
      else if (WasKey(KeyPadMinus))
            { if (GammaCorrec>=50)
               { GammaCorrec-=10;
                 for (i=0; i<=768-3; i++)
                  { t=(pal[i]*GammaCorrec)/100;
                    if (t<=63) palt2[i]=t;
                    else palt2[i]=63;
                  }
                 SetPalette(palt2); 
               }
            }
      else if (WasKey(KeyPadStar))
            { if (GammaCorrec!=100)
                 { GammaCorrec=100;
                   SetPalette(pal);
                 }
            }
}

void TestSaveTxt(char *NameOfFile)
{ static unsigned char* Tbuftmp;
  static char nomfich[20];
  static word i;
  FILE *NOF;

  if (WasKey(KeyS))
      { KillKeyb();                     // vire l'iterruption clavier
        i=0;
        while (NameOfFile[i]!='.' && i<9)
         { nomfich[i]=NameOfFile[i];
           i++;
         }
        nomfich[i++]='.';
        nomfich[i++]='t';
        nomfich[i++]='x';
        nomfich[i++]='t';
        nomfich[i]='\0';


        Tbuftmp=TEXTbuffer;
        NOF=fopen(nomfich,"wt");

        for (i=0; i<nblig; i++)
         { if (*Tbuftmp!='\0') fputs(Tbuftmp,NOF);
           fputc('\n',NOF);
           Tbuftmp+=llen;
         }
        fclose(NOF);
        RaiseKeyb();                    // restaure l'interruption clavier
      }

}


void TestGamMenu()
{ if (WasKey(KeyN2))
   if (GamMenu) { GamMenu=0; pal[16*3]=20; SetColor(16,20,0,0);}
   else { GamMenu=1; pal[16*3]=40; SetColor(16,40,0,0);}
}

int Selection(int TabPos[],int nb,int na)    // Petit Morphing :) du selecteur du MENU ...
{static int i;
 static int len,pos, tpos, tlen;
 pos=TabPos[nb*2]; len=TabPos[nb*2+1];
 for(i=0; i<=16; i++)
 { Restore(pos,len,34,Vbuffer);
   tpos=pos; tlen=len;
   pos = TabPos[nb*2  ] + ((TabPos[na*2  ] - TabPos[nb*2  ])*i) / 16;
   len = TabPos[nb*2+1] + ((TabPos[na*2+1] - TabPos[nb*2+1])*i) / 16;
   Selector(pos,len,34,254);
   WaitVbl2();
   if (Vesa12Beuark)                 // si Vesa 1.2 Compatible
      BufferCopyVesa12(Vtmpbuf);
   else
     { ConfigR2B(0, tpos, tlen, tpos+34); SwpFBuf(Vtmpbuf);
       ConfigR2B(0,  pos,  len,  pos+34); SwpFBuf(Vtmpbuf);
     }
 } return na;
}

void ChargeTEXT(char* nomfichier,unsigned int tLpP)     // charge le texte 
{ KillKeyb();
  TEXTlen = EDsearch(nomfichier);                       // d'une rubrique.
  TEXTbuffer = malloc(TEXTlen);
  EDload(TEXTbuffer);      
  RaiseKeyb();
  LpP=tLpP;
  llen=0; while ((char)TEXTbuffer[llen]!='\0') llen++;
  while ((char)TEXTbuffer[llen]=='\0') llen++;
  nblig=TEXTlen/llen;  psize=LpP*llen;
}

void InitDefil(int XX, int YY, int HH)
{ Asc_Y=YY; 
  Asc_h=HH;
  Asc_X=XX;
  Asc_s=(Asc_h*LpP)/nblig;
}


void AfficheTxt(int Xp, int Yp, unsigned int Pos)       // Affiche le texte
{ static int ii,PosA, Asclen;
  static char* txtptr;                  // d'une rubrique
  static char* tempPTR;
  static unsigned int LpPTemp;

if (DebugMode)
  CopyBuf2Buf(Vbuffer,Vtmpbuf);
else
  Restore2Buf(Vbuffer,Vtmpbuf);


if (Ascenseur)
{   PosA=Asc_Y+((Pos/llen)*(Asc_h-Asc_s)/(nblig-LpP));
    if (PosA+Asc_s>Asc_Y+Asc_h) Asclen=Asc_Y+Asc_h-PosA+1;
    else Asclen=Asc_s;

 if (!Clip)
  { VlineBuf(Asc_X,  Asc_Y,  Asc_h+1,255);
    VlineBuf(Asc_X+1,Asc_Y,  Asc_h+1,0);
    VlineBuf(Asc_X+2,Asc_Y,  Asc_h+1,0);
    VlineBuf(Asc_X+3,Asc_Y,  Asc_h+1,255);
    VlineBuf(Asc_X+1,PosA , Asclen,255);
    VlineBuf(Asc_X+2,PosA , Asclen,255);
  }
 else
  { VlineBuf(Asc_X,  20+Asc_Y,  Asc_h+1,255);
    VlineBuf(Asc_X+1,20+Asc_Y,  Asc_h+1,0);
    VlineBuf(Asc_X+2,20+Asc_Y,  Asc_h+1,0);
    VlineBuf(Asc_X+3,20+Asc_Y,  Asc_h+1,255);
    VlineBuf(Asc_X+1,20+PosA , Asclen,255);
    VlineBuf(Asc_X+2,20+PosA , Asclen,255);
  }

if (DebugMode)
{  // bah, on va voir
 BufPutStr(0, 0, 255, "AscL:"); BufPutStr(45, 0, 255, ToChar(Asclen));
 BufPutStr(0,20, 255, "PosY:"); BufPutStr(45,20, 255, ToChar(Asc_Y));
 BufPutStr(0,40, 255, "AscH:"); BufPutStr(45,40, 255, ToChar(Asc_h));
 BufPutStr(0,60, 255, "PosA:"); BufPutStr(45,60, 255, ToChar(PosA));
}  // bah on a vu!!

}
   txtptr=TEXTbuffer+Pos; ii=Yp; LpPTemp=Yp+(LpP*18);
   tempPTR=TEXTbuffer+TEXTlen;

   while (txtptr<tempPTR && ii<LpPTemp)
     { BufPutStr(Xp, ii, 255, txtptr); ii+=18; txtptr+=llen;}

   WaitVbl2();
   if (Vesa12Beuark)                 // si Vesa 1.2 Compatible
      BufferCopyVesa12(Vtmpbuf);
   else
      SwpFBuf(Vtmpbuf);               //VSwpscr2(Vtmpbuf); VSwpscr(Vtmpbuf);
}

void TxtUp(int Xp, int Yp, unsigned int Pos)
 { static int jj;
 if (Vesa12Beuark)
     for(jj=17; jj>=0; jj-=2) AfficheTxt(Xp,Yp+jj, Pos);
 else
   { SetClip(ON); Clip=1; LpP++;
     for(jj=2; jj<=18; jj+=2) AfficheTxt(Xp,20+Yp-jj, Pos-llen);
     SetClip(OFF); Clip=0;  LpP--;
   }
      
 }

void TxtDown(int Xp, int Yp, unsigned int Pos)
 { static int jj;
 if (Vesa12Beuark)
   { for(jj=16; jj>=2; jj-=2) AfficheTxt(Xp,Yp+18-jj, Pos+llen);
     AfficheTxt(Xp,Yp, Pos);
   }
 else
   { SetClip(ON); Clip=1; LpP++;
     for(jj=16; jj>=0; jj-=2) AfficheTxt(Xp,20+Yp-jj, Pos);
     SetClip(OFF); Clip=0; LpP--;
   }
 }

void TestTEXT(int Xp, int Yp)
{              if (IsKey(KeyDown))
                      { if ((NumLig+llen)<TEXTlen)
                         { NumLig+=llen; TxtUp(Xp, Yp, NumLig); }
                      }
          else if (IsKey(KeyUp))
                      { if(NumLig>=llen)
                         { NumLig-=llen; TxtDown(Xp, Yp, NumLig); }
                      }
          else if (WasKey(KeyPageDown))
                    { if ((NumLig+psize)<TEXTlen)
                         { NumLig+=psize; AfficheTxt(Xp, Yp, NumLig); }
                    }        
          else if (WasKey(KeyPageUp))
                    { if(NumLig>=psize)
                         { NumLig-=psize; AfficheTxt(Xp, Yp, NumLig); }
                 else if (NumLig>0)
                         { NumLig=0; AfficheTxt(Xp, Yp, NumLig); }
                    }       
          else if (WasKey(KeyEnd))
                    { if((NumLig+psize)<TEXTlen)
                         { NumLig=TEXTlen-psize; AfficheTxt(Xp, Yp, NumLig); }
                    }
          else if (WasKey(KeyHome))
                    { if (NumLig>0)
                         { NumLig=0; AfficheTxt(Xp, Yp, NumLig); }
                    }
}

void CopyColors(byte *Ps,byte *Pd)
{ static word i;
  GammaCorrec=100;
  for (i=0; i<=768; i++) Pd[i]=Ps[i];
}


void KText(byte num)
{ static byte i,Kpos,KposBk;
  static char tmp1[40],tmp2[40];

  Kpos=0;i=0;
  while ((char)TEXTbuffer[num*llen+Kpos]!='#' && Kpos<=30)
  { tmp1[i]=TEXTbuffer[num*llen+Kpos];
    Kpos++; i++;
  }
  tmp1[i]='\0'; Kpos++;

  i=0; KposBk=Kpos;
  do
  { tmp2[i]=TEXTbuffer[num*llen+Kpos];
    Kpos++; i++;
  } while ((char)TEXTbuffer[num*llen+Kpos]!='\0' && Kpos<=30);
  tmp2[i]='\0';


//  ConfigR2B(0, 225, 640,18); Restore2Buf(Vbuffer,Vtmpbuf);
  CopyBuf2Buf(Vbuffer,Vtmpbuf);

  BufPutStr(220-(KposBk*8),225, 255,tmp1);
  BufPutStr(320,225, 255,tmp2);

  if (Vesa12Beuark)                 // si Vesa 1.2 Compatible
     BufferCopyVesa12(Vtmpbuf);
  else
     VSwpscr(Vtmpbuf);                // sinon on l'affiche directos

}

