{$N+,A+,G+,F+,D-,E-,L-,R-,S-,Q-,Y-,I-}
{DEFINE release_mode_compile}

uses global, utils, xms2, resource, timer,s3m, midas, mfile, mplayer, errors, mconfig,
screen, memory, palette,

loadscr1, boxcube3, cubwate7, proja, tunnel, roundte5, penrose, bump, balls0,
ball64_5, ball7, plasma, iferenc, hexfall, floor_m7, dodec3, wizard4, shadbobs,
sandb_tc, wirth, text1, ansi;

const
g_path='c:\bp\demoded\'; {for development (if General_datafile_mode = 0) }
{$I filreg_p.pas} {<--PACK-version! procedure for registration all data-files
                                    in resources array}


procedure preVR;
begin
    mtimer:=mtimer+1;
end;

procedure set_music_pos(p:byte);
begin
MP^.SetPosition(p); Midas_WaitVR;
end;

label exit_to_dos, cubewater, projection, tunnele, credits, title, balls1, balls2,
plasmas, hexfloor, dodecahedron, wizard, shadebobs, sandbox, niklauswirth, endscroll;

label exit_from_demo;
var
n:word;
str_filename: array [0..12] of char;
s3m_filename:string[12];
numparam:byte;
start_from_part, MidasForceDisableEMS, MidasSetupMenuOn:byte;

begin
if release_mode=0 then begin  {CHECK GLOBAL.PAS for status DEFINE directive}
Debug_mode:=1;
UseXMS:=0;
FPS_mode:=1;
Vsync_mode:=1;
General_datafile_mode:= 0;
General_datafile_name:='demoded.dat';
S3M_filename:='demoded.s3m';
S3MParam_mode:=0;
MidasSetupMenuOn:=0;
MidasForceDisableEMS:=1;
end
else begin
   Debug_mode:=0;    {Release config}
   UseXMS:=1;
   FPS_mode:=0;
   Vsync_mode:=1;
   General_datafile_mode:= 1;
   General_datafile_name:='demoded.dat';
   S3M_filename:='demoded.dat';
   S3MParam_mode:=0;
   MidasSetupMenuOn:=1;
   MidasForceDisableEMS:=0;
end;

if checkparam('debug') then debug_mode:=1;
if checkparam('noxms') then UseXMS:=0;
if checkparam('fps')   then FPS_mode:=1;
if checkparam('novsync') then Vsync_mode:=0;


{------------Allocate all memory in base memory-------------}
SetUpVirtual(scr,vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr3, vaddr3);
SetUpVirtualPalette(pale, seg_palitra, ofs_palitra);
if FPS_mode=1 then SetUpVirtualFont6x6_d(fonte, seg_font6x6_d, ofs_font6x6_d);
{-----------------------------------------------------------}


if debug_mode=1 then begin
WriteLn(MemAvail,' byte base memory avail');
WriteLn;
WriteLn('Select start part (input and press Enter):');
WriteLn(
'0.  exit', #10#13,
'1.  boxcube',#10#13,
'2.  cubewater',#10#13,
'3.  projection',#10#13,
'4.  tunnele',#10#13,
'5.  credits',#10#13,
'6.  title',#10#13,
'7.  balls1',#10#13,
'8.  balls2',#10#13,
'9.  plasmas',#10#13,
'10. hexfloor',#10#13,
'11. dodecahedron',#10#13,
'12. wizard',#10#13,
'13. shadebobs',#10#13,
'14. sandbox',#10#13,
'15. niklauswirth',#10#13,
'16. endscroll',#10#13);
read(start_from_part);
if start_from_part=0 then halt;
end;

{registration file-resources}
reg_resource_files;
if (General_datafile_mode = 1) then setup_df_offsetes; {find and setup offsets in general datafile}

{---------------------------Registration generation resources----------------}

res_reg(1, 'glitch_table2',  '!',  16*256,  0);
res_reg(2, 'cubwater2',      '!',  65536,  0);
res_reg(2, 'cubwater3',      '!',  65536,  0);
res_reg(3, 'tunnel3',        '!',  8192,   0);
res_reg(9, 'floor_1',        '!',  1536,   0);



{-------------------------MIDAS CONFIG-------------------------------------}
{$IFNDEF release_mode_compile}

    if (MidasSetupMenuOn=0) then begin
    { Check that the configuration file exists: }
         error := fileExists('MIDAS.CFG', @isConfig);
         if error <> OK then midasError(error);
         if isConfig <> 1 then
             begin
                WriteLn('Configuration file not found - run MSETUP.EXE');
                Halt;
             end;
    end;
{$ENDIF}

    midasSetDefaults;  { set MIDAS defaults }

{$IFNDEF release_mode_compile}
    if (MidasSetupMenuOn=0) then midasLoadConfig('MIDAS.CFG'); { load configuration }
{$ENDIF}

    if (MidasSetupMenuOn=1) then
       begin

           if midasConfig=0 then begin {<------ MIDAS-setup menu call}
                                    asm mov ax, 03h; int 10h end;
                                    goto exit_to_dos;
                                 end;

           if debug_mode=1 then asm mov ax, 3; int 10h; end
              else begin {fade screen}

                palette2memW(palette1,seg_palitra,ofs_palitra);
                for n:=0 to 63 do setonecolor2memw(n, 0,0,0, palette2, seg_palitra,ofs_palitra);
                init_pal2pal(seg_palitra,ofs_palitra);
                palette_process[0]:=1;
                repeat {fade in}
                    WaitVR;
                    if palette_process[0]=1 then Pal2Pal_asm(palette_process[0], 0, 63, 4, seg_palitra, ofs_palitra);
                until palette_process[0]=0;
                for n:=1 to 60 do WaitVR; {delay}
              end;
       end;

    If (MidasForceDisableEMS=1) then MidasDisableEMS:=1;

    MidasAmplification:=192;{}

{-----------------------------------------------------------------------}

if FPS_mode=1 then LoadBinFromFile('font6_d.bin', seg_font6x6_d,  ofs_font6x6_d);


if debug_mode=0 then begin
asm
mov ax, 13h
int 10h
end;
Set60hz;

LoadBinFromFile('loadscr.bin',  vaddr2,  0);
DecompressRLE(vaddr2, vaddr);
LoadBinFromFile('loadscr.pal', seg_palitra,  ofs_palitra+palette2);
for n:=1 to 60*3 do WaitVR; {delay}
loadscreen_open;
end;


If UseXMS=1 then begin
    if debug_mode=1 then WriteLn('XMS memory avail: ', XMSMemAvail);

    AllocateXMSforAll;

    if debug_mode=1 then begin
                         for n:=0 to 159 do waitVR;
                         WriteLn('Loading files:');
                         end;

    LoadFilesToXMS(vaddr);

    if debug_mode=1 then begin
                         WriteLn;
                         WriteLn('Precalculate:');
                         end;

    init_cubwater;{1}
    init_tunnel;  {4}
    init_balls0;  {6}
    init_floor;   {9}


end;


if debug_mode=1 then begin
asm
mov ax, 13h
int 10h
end;
Set60hz;
for n:=1 to 60 do WaitVR;{}
end;


tmrGetScrSync(@scrSync);

midasInit; { initialize MIDAS Sound System }

if S3MParam_mode=1 then begin
   numparam:=check_s3m_in_params;
   if numparam > 0 then   toASCIIZ(str_filename, ParamStr(numparam))
                     else toASCIIZ(str_filename, s3m_filename);
   module := midasLoadModule(str_filename, @mps3m, NIL);
end
else begin
toASCIIZ(str_filename, s3m_filename);
module := midasLoadModule(str_filename, @mps3m, NIL);
end;

if debug_mode=0 then begin
    FlipScreen(vga, vaddr);
    for n:=0 to 31 do loadscreen_progressbar(1);
    loadscreen_close; {}
end;
{-----------------------------------------------------------------------}


tmrSyncScr(scrSync, @preVR, nil, nil);

init_boxcube;

midasPlayModule(module, 0);         { start playing }
MusicStart:=1;

MP := midasMP;

{===========================================================}
case start_from_part of
2: begin set_music_pos(10); goto cubewater; end;
3: begin set_music_pos(13); goto projection; end;
4: begin set_music_pos(17); goto tunnele; end;
5: begin set_music_pos(20); goto credits; end;
6: begin set_music_pos(27); goto title; end;
7: begin set_music_pos(32); goto balls1; end;
8: begin set_music_pos(36); goto balls2; end;
9: begin set_music_pos(38); goto plasmas; end;
10:begin set_music_pos(46); goto hexfloor; end;
11:begin set_music_pos(50); goto dodecahedron; end;
12:begin set_music_pos(56); goto wizard; end;
13:begin set_music_pos(58); goto shadebobs; end;
14:begin set_music_pos(65); goto sandbox; end;
15:begin set_music_pos(72); goto niklauswirth; end;
16:begin set_music_pos(74); goto endscroll; end;
else
end;

scene_boxcube;  if ESC_PRESSED=1 then goto exit_from_demo; {}

cubewater:
scene_cubwater; if ESC_PRESSED=1 then goto exit_from_demo; {}

projection:
scene_project;  if ESC_PRESSED=1 then goto exit_from_demo;{}

tunnele:
scene_tunnel;   if ESC_PRESSED=1 then goto exit_from_demo;{}

credits:
scene_credits;  if ESC_PRESSED=1 then goto exit_from_demo;{}

title:
scene_penrose;  if ESC_PRESSED=1 then goto exit_from_demo;{}
scene_bump;     if ESC_PRESSED=1 then goto exit_from_demo;{}
scene_balls0;   if ESC_PRESSED=1 then goto exit_from_demo;{}

balls1:
scene_balls1;   if ESC_PRESSED=1 then goto exit_from_demo;{}
balls2:
scene_balls2;   if ESC_PRESSED=1 then goto exit_from_demo;{}

plasmas:
scene_plasma;   if ESC_PRESSED=1 then goto exit_from_demo;{}
scene_iferenc;  if ESC_PRESSED=1 then goto exit_from_demo;{}

scene_hexfall;  if ESC_PRESSED=1 then goto exit_from_demo;{}

hexfloor:
scene_floor;    if ESC_PRESSED=1 then goto exit_from_demo;{}

dodecahedron:
scene_dodec;    if ESC_PRESSED=1 then goto exit_from_demo;{}

wizard:
scene_wizard;   if ESC_PRESSED=1 then goto exit_from_demo;{}

shadebobs:
scene_shadbobs; if ESC_PRESSED=1 then goto exit_from_demo;{}

sandbox:
scene_sandbox;  if ESC_PRESSED=1 then goto exit_from_demo;{}

niklauswirth:
scene_wirth;    if ESC_PRESSED=1 then goto exit_from_demo;{}

endscroll:
scene_endscroll;{}

exit_from_demo:

{============================================================}


tmrStopScrSync;

asm
mov ax, 03h
int 10h
end;


if debug_mode=1 then begin
{   WriteLn(Debug_tmp);
   WriteLn(Debug_single :1:2);}


   WriteLn(MemAvail,' byte base memory avail');

end;

    midasStopModule(module);            { stop playing }
    midasFreeModule(module);            { deallocate module }
    midasClose;                         { uninitialize MIDAS }


if UseXMS=1 then FreeXMS;

if debug_mode=0 then SHOW_ANSI;

exit_to_dos:
asm
 {empy keyboard buffer}
 xor ax, ax
 @l:
    mov ah, 1
    int 16h
    jz @done
    mov ah, 0
    int 16h
    jmp @l
 @done:

 {set cursor position}
 xor bx, bx
 mov ax, 0200h
 mov dx, 1701h {24 string, 1 column}
 int 10h
end;


end.