#include "midasdll.h"
//#include "midas.h"
//#include "vga.h"
//#include "mconfig.h"

static int Asc_h, Asc_s, Asc_X, Asc_Y;

static int NumZic;
static MIDASmodule module1;
static MIDASmodule module2; 
static MIDASmodule module3; 
static MIDASmodule module4; 
static MIDASmodule module5; 

static unsigned refreshRate;        /* Display refresh rate */

static char *Vtmpbuf2;
static char *Vtmpbuf;
static char *Vbuffer;
static byte pal[768], palt2[768], palt3[768];
static int GammaCorrec;
static int GamMenu;
static byte TrueCol;
static byte far *lfb;
static word sel;

static int ForceV2;
static int Vesa12Beuark;
static int FadeOk;
static int WaitAtInit;
static int hidden;
static int hiddenEngaged;
static int Ascenseur;
static int DebugMode;
static int NotTestWin;
static int NoSound;

static int Clip;
volatile unsigned int frameCount;

// New Wait VBL (Synchro avec la Zic...)
void MIDAS_CALL prevr(void) { frameCount++; }

void WaitVbl2(void)
{ static unsigned int old;
  if (NoSound) WaitVbl();
  else
   { old=frameCount;
     while(old==frameCount);
   }
}

void JoueModule(int i)
{      if (i==1) MIDASplayModule(module1, TRUE);
  else if (i==2) MIDASplayModule(module2, TRUE);
  else if (i==3) MIDASplayModule(module3, TRUE);
  else if (i==4) MIDASplayModule(module4, TRUE);
  else if (i==5) MIDASplayModule(module5, TRUE);
  
}

void StopModule(int i)
{      if (i==1) MIDASstopModule(module1);
  else if (i==2) MIDASstopModule(module2);
  else if (i==3) MIDASstopModule(module3);
  else if (i==4) MIDASstopModule(module4);
  else if (i==5) MIDASstopModule(module5);
}


int InitAll(int SSarg)
{     int error, err2;

      GammaCorrec=100; GamMenu=0;
      NumZic=1+(Rnd() % 5);

      if (!NotTestWin)
       if (IsWin())
       { printf("\nHeeuuu... How Can I Say... we have some problems ..."
                "\nDon't be afraid but It seems that You heva a BIG Virus"
                "\nOr perhaps you are runnningthe DefcoN DiskMag"
                "\nunder the shitty Windows System !!!"
                "\nThis Mag is only available under Dos System ...");
         return(0);
       }

    VESAInfo.VersionVESAHi=1;  // je fixe une valeur !!! au cas ou la carte
                               // reponde pas ....
       readVESA(&VESAInfo);
       readINFO(&MODEInfo,0x101);

    if (!Vesa12Beuark)
     if (!ForceV2)
       if(VESAInfo.VersionVESAHi<2)
        {     printf("\n==> Warning! <==> Warning! <==> Warning! <==> Warning! <==> Warning! <=="
                     "\nVotre carte est en VESA %d.%d",VESAInfo.VersionVESAHi,VESAInfo.VersionVESALo);
              printf("\nCe programme utilise le LFB support‚ uniquement avec Vesa 2.0+!!"
                     "\nUtilisez donc un driver VESA 2.0 du type UNIVBE.\n"
                     "\nEt si vous avez une carte video merdique qui ne gere pas le LFB meme"
                     "\navec un driver utiliser l'option /O (Vesa 1.2) et penser … en changer");
              return(0);
        } 


  if (!NoSound)
   { MIDASstartup();
     if ( (SSarg > 1) || (!MIDASdetectSoundCard()) )
       if ( !MIDASconfig() )
           { printf("\nAbort by User..."); return(0); }
   }

   if (Vesa12Beuark) setVESA(0x101);
   else
    { sel=0; lfb=MK_FP((sel=ptrLFB(MODEInfo.PhysBasePtr)),0);
      setVESA(0x4101);
    }

 if (!NoSound)
  { refreshRate = MIDASgetDisplayRefreshRate();
    if ( refreshRate == 0 )
    {
        printf("Warning! Unable to synchronize to display refresh!\n"
               "This can lead to problems in music output and flickering.\n"
               "This usually happens only when running under Windows 95 - "
               "please consider\nrunning DOS programs in DOS instead.\n"
               "Press Esc to quit or any other key to continue\n");
        if ( getch() == 27 )
            return 0;
   refreshRate = 60000; /* default 70Hz */
    }
  }

  if (!Vesa12Beuark)
    if (sel==0)
        {     printf("\n==> Warning! <==> Warning! <==> Warning! <==> Warning! <==> Warning! <=="
                     "\nVotre carte est en VESA 2.0 certes mais ce programme utilise le LFB "
                     "\nUtilisez donc un driver VESA 2.0 du type UNIVBE. qui emule le LFB\n"
                     "\nEt si vous avez une carte video merdique qui ne gere pas le LFB meme"
                     "\navec un driver utiliser l'option /O (Vesa 1.2) et penser … en changer");
              return(0);
        } /**/
    Mode3();
    NewKeyb();
    printf("\t- DefCoN Mag System Initialisation - Final Version (V1.0) -\n\n");
    if (!Vesa12Beuark)
      printf("VESA 2.0 Found! The Linear Frame Buffer is Engaged\n");

 if (!NoSound)
   {MIDASinit();
    printf("Initializing The Pro Of The Pot: MIDAS Digital Audio System V0.7b1\n");

    printf("\nLoading Modules:\n"
           "   ž \"Mangoose Ultimatum\" by ToAlnkor / RealTech\n");
        module1 = MIDASloadModule("ZIC\\mangoose.s3m");

    printf("   ž \"Artichok\" by Maf / Cocoon & Silicon\n");
        module2 = MIDASloadModule("ZIC\\artichok.mod");

    printf("   ž \"DefCoN Theme\" by PLi$CaNN / K!Prod\n");
        module3 = MIDASloadModule("ZIC\\defcon.xm"); 

    printf("   ž \"Escape From Earth\" by PLi$CaNN / K!Prod\n");
        module4 = MIDASloadModule("ZIC\\ESCAPE.xm");

    printf("   ž \"SNY\" by Scorpik / Pulse\n");
        module5 = MIDASloadModule("ZIC\\SNY.mod");
    }
   else
    printf("You Select NoSound... So we don't load any modules ...\n");

    printf("\nInitializing The 3D Vector System...\n");
        Vbuffer =(char *)malloc(640*480);
        Vtmpbuf2=(char *)malloc(640*(480+40));
        Vtmpbuf =(char *)AddPTR(Vtmpbuf2,640*20);
        SetBuf(Vtmpbuf2,640*20);

    if (Ascenseur)     printf("\nAscenseurs Enclench‚!");

    if (Vesa12Beuark)  printf("\nMode Vesa 1.2 Enclench‚!");
    else if (ForceV2)  printf("\nMode Vesa 2.0 Forc‚!");

    if (hiddenEngaged) printf("\nHidden Part Forc‚!");

    if (DebugMode)     printf("\nDebug Mode Enclench‚!");

    printf("\n\nAll is Ok!  I want to believe!\n");


      if (WaitAtInit) { InitKey();
                        printf("\n<< Press A Key to Start >>\n");
                        do {} while (!KeyPressed()); }

        if (Vesa12Beuark) setVESA(0x101);
                     else setVESA(0x4101);



 if (!NoSound) MIDASsetTimerCallbacks(refreshRate, TRUE, &prevr, NULL, NULL);
        BlackPal();
        InitKey();
 if (!NoSound) JoueModule(NumZic);
        return 1;
}

void DeInitAll()
{
        OldKeyb();
       if (!NoSound)
        { StopModule(NumZic);
          MIDASremoveTimerCallbacks();
        }
        free(Vbuffer); free(Vtmpbuf);/**/ // pas de free(buffer)
       if (!NoSound)
        { MIDASfreeModule(module1);
          MIDASfreeModule(module2);
          MIDASfreeModule(module3);
          MIDASfreeModule(module4); 
          MIDASfreeModule(module5); /**/  // pas de free(music)
          MIDASclose();
        }

        Mode3();
}
