home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT05.ZIP / temp / techtut / tut5 / Tech05.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-09-10  |  8.1 KB  |  264 lines

  1. {
  2.  
  3.         TechTutor5
  4.         Demo Recording/playing (ripped from SuperFX Engine)
  5.  
  6.         Coding by P.Bestebroer
  7.         FreeWare
  8.  
  9.         Source does NOT work on its own, it was merely used for showing
  10.         howto, instead of doing it foryou.
  11.         The code DOES work when put inside the SuperFX engine (wich is
  12.         allready done).
  13.  
  14.         Contacting:
  15.  
  16.          HTTP://people.zeelandnet.nl/rpb/
  17.         EMAIL:just4fun@zeelandnet.nl
  18.  
  19. }
  20. PROGRAM Tech05;
  21.  
  22. USES CRT;
  23. {─────────────────────────────────────────────────────────────────────────────}
  24. {
  25.         First create a few variables
  26.  
  27. }
  28. VAR DEMrecord   : boolean;               { recording demo?                   }
  29.     DEMplay     : boolean;               { playing a demo?                   }
  30.     DEMfile     : file;                  { filename of the .DEM file         }
  31.  
  32. CONST DEMID     : string = 'SFX_DEMOv1'; { ID of the SuperFX Demo files      }
  33.  
  34. VAR DEMleft     : boolean;               { left key is pressed?              }
  35.     DEMright    : boolean;               { right key is pressed?             }
  36.     DEMup       : boolean;               { up key is pressed?                }
  37.     DEMdown     : boolean;               { down key is pressed?              }
  38.     DEMfire     : boolean;               { fire key is pressed?              }
  39.     DEMfire2    : boolean;               { support for 4 joystick buttons    }
  40.     DEMfire3    : boolean;
  41.     DEMfire4    : boolean;
  42.     DEMpress    : longint;               { time the keys are pressed         }
  43. {─────────────────────────────────────────────────────────────────────────────}
  44. {
  45.         This procedure will create a new DEMO file
  46.  
  47.         Expects: Filename of the Demo
  48.         Returns: Nothing
  49. }
  50. FUNCTION _NewDemo(filename:string):boolean;
  51. BEGIN
  52.  Halt_Proc:='_NewDemo';
  53.  assign(DEMfile,filename);
  54.  {$I-}
  55.  rewrite(DEMfile,1);
  56.  {$I+}
  57.  if IoResult<>0 then begin
  58.     _NewDemo:=false;
  59.     halt_Error:='Unable to assign demoname ('+filename+')';
  60.     exit;
  61.  end;
  62.  
  63.  
  64.  blockwrite(DEMfile,DEMID[1],sizeof(DEMID));    { write DEMO ID }
  65.  blockwrite(DEMfile,MapFile[1],32);             { write map-filename }
  66.  blockwrite(DEMfile,MapLib,1);                  { write map-using lib? }
  67.  
  68.  DEMleft:=false;        { not pressed,}
  69.  DEMright:=false;       { not pressed neither, }
  70.  DEMup:=false;          { definetly not pressed, }
  71.  DEMdown:=false;        { maybe...NAH not pressed! }
  72.  DEMfire:=false;        { ...don't shoot!, don't shoot! }
  73.  DEMfire2:=false;       { support for 4 joystick buttons }
  74.  DEMFire3:=false;
  75.  DEMfire4:=false;
  76.  
  77.  DEMpress:=0;           { the pressing-time of the keys    }
  78.  DEMrecord:=true;       { silence please, we're recording! }
  79.  {$IFDEF PLUGIN_SHOWEVENTS}
  80.    _Ucase(Filename);
  81.    _AddEvent('RECORDING DEMO ('+filename+')');
  82.  {$ENDIF}
  83.  
  84.  _NewDemo:=true;
  85. END;
  86. {─────────────────────────────────────────────────────────────────────────────}
  87. {
  88.         This procedure will OPEN a DEMO for playing
  89.  
  90.         Expects: Filename of the Demo
  91.         Returns: Nothing
  92. }
  93. FUNCTION _OpenDemo(filename:string):Boolean;
  94. VAR testID  : string[24];
  95.     tmpFile : string;
  96. BEGIN
  97.  Halt_Proc:='_OpenDemo';
  98.  tmpFile:=filename;
  99.  assign(DEMfile,filename);
  100.  {$I-}
  101.  reset(DEMfile,1);
  102.  {$I+}
  103.  if IoResult<>0 then begin
  104.      _OpenDemo:=false;
  105.      Halt_error:='Couldn''t spawn demo ('+tmpFile+')';
  106.      exit;
  107.  end;
  108.  
  109.  blockread(DEMfile,testID[1],sizeof(DEMID));
  110.  testID[0]:=DEMid[0];
  111.  if testID<>DEMid then begin
  112.     halt_Error:='Not a valid DEMO file ('+filename+')';
  113.     halt_proc:='_PlayDemo';
  114.     halt;
  115.  end;
  116.  
  117.  blockread(DEMfile,MapFile[1],32);             { read map-filename }
  118.  blockread(DEMfile,MapLib,1);                  { read map-using lib? }
  119.  LoadMap(mapLib,MapFile);
  120.  
  121.  DEMpress:=0;
  122.  DEMplay:=true;
  123.  
  124.  {$IFDEF PLUGIN_SHOWEVENTS}
  125.    _Ucase(tmpFile);
  126.    _AddEvent('PLAYING DEMO ('+tmpFile+')');
  127.  {$ENDIF}
  128.  _OpenDemo:=true;
  129. END;
  130. {─────────────────────────────────────────────────────────────────────────────}
  131. {
  132.         This will END the DEMO file, and close it...
  133.  
  134.         Expects: Nothing
  135.         Returns: Nothing
  136. }
  137. PROCEDURE _EndDemo;
  138. BEGIN
  139.   {$I-}
  140.    Close(DEMfile);
  141.   {$I+}
  142. END;
  143. {─────────────────────────────────────────────────────────────────────────────}
  144. {
  145.         This will handle the recording of key-presses, and save
  146.         it to the DEMO file.
  147.  
  148.         Expects: all directions, and the fire  presses
  149.         Returns: Nothing
  150. }
  151. PROCEDURE _RecordDemo;
  152.  
  153.   PROCEDURE _ADDtoDEMObyte(code:byte);
  154.   BEGIN
  155.     blockwrite(DEMfile,code,1);
  156.   END;
  157.  
  158.   PROCEDURE _ADDtoDEMO(code:longint);
  159.   BEGIN
  160.     blockwrite(DEMfile,code,4);
  161.   END;
  162.  
  163. BEGIN
  164.   if (joystickleft<>demleft) or (joystickright<>demright) or
  165.      (joystickup<>demup) or (joystickdown<>demdown) or (joystickA<>demfire) or
  166.      (joystickB<>demfire2) or (joystickC<>demfire3) or (joystickD<>demfire4) then begin
  167.          if demleft then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  168.          if demright then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  169.          if demup then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  170.          if demdown then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  171.          if demfire then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  172.          if demfire2 then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  173.          if demfire3 then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  174.          if demfire4 then _ADDtoDEMObyte(1) else _ADDtoDEMObyte(0);
  175.  
  176.          _ADDtoDEMO(dempress);
  177.  
  178.          DEMleft:=joystickleft;
  179.          DEMright:=joystickright;
  180.          DEMup:=joystickup;
  181.          DEMdown:=joystickdown;
  182.          DEMfire:=joystickA;
  183.          DEMfire2:=joystickB;
  184.          DEMfire3:=joystickC;
  185.          DEMfire4:=joystickD;
  186.  
  187.          DEMpress:=0;
  188.   end else inc(DemPress);
  189. END;
  190. {─────────────────────────────────────────────────────────────────────────────}
  191. {
  192.         This will handle the PLAYING the demo
  193.  
  194.         Expects: all directions + fire keys (VARS to be returned)
  195.         Returns: the boolean filled with TRUE or FALSE
  196. }
  197. PROCEDURE _PlayDemo;
  198.  
  199. VAR i : byte;
  200.  
  201.   PROCEDURE _GETfromDEMObyte(var code:byte);
  202.   BEGIN
  203.     blockread(DEMfile,code,1);
  204.   END;
  205.  
  206.   PROCEDURE _GETfromDEMO(var code:longint);
  207.   BEGIN
  208.     blockread(DEMfile,code,4);
  209.   END;
  210.  
  211. BEGIN
  212.   Joystickleft:=DEMleft;
  213.   JoystickRight:=DEMright;
  214.   JoystickUp:=DEMup;
  215.   JoystickDown:=DEMdown;
  216.   JoystickA:=DEMfire;
  217.   JoystickB:=DEMfire2;
  218.   JoystickC:=DEMfire3;
  219.   JoystickD:=DEMfire4;
  220.  
  221.   if DemPress>0 then dec(DemPress) else begin
  222.      if eof(DEMfile) then DEMplay:=false else begin
  223.         _GetFromDemoByte(i); if i=1 then DemLeft:=true else DemLeft:=false;
  224.         _GetFromDemoByte(i); if i=1 then DemRight:=true else DemRight:=false;
  225.         _GetFromDemoByte(i); if i=1 then DemUp:=true else DemUp:=false;
  226.         _GetFromDemoByte(i); if i=1 then DemDown:=true else DemDown:=false;
  227.         _GetFromDemoByte(i); if i=1 then DemFire:=true else DemFire:=false;
  228.         _GetFromDemoByte(i); if i=1 then DemFire2:=true else DemFire2:=false;
  229.         _GetFromDemoByte(i); if i=1 then DemFire3:=true else DemFire3:=false;
  230.         _GetFromDemoByte(i); if i=1 then DemFire4:=true else DemFire4:=false;
  231.         _GetFromDemo(DEMpress);
  232.      end;
  233.   end;
  234. END;
  235. {$ENDIF}
  236.  
  237. {─────────────────────────────────────────────────────────────────────────────}
  238. BEGIN
  239.  
  240.   while port[$60]<>156 do ;
  241.   textcolor(7); textbackground(0); clrscr;
  242.   writeln('TechTutor #5');
  243.   writeln('written by P.Bestebroer, Just4Fun Productions');
  244.   writeln('');
  245.   writeln('A great source code showing how you COULD implement demo-playing');
  246.   writeln('and demo recording. The source does not work stand-alone, but it');
  247.   writeln('can be used when some tweaking is done, and you have a game to put');
  248.   writeln('it in.');
  249.   writeln('The source code was taken from the SuperFX engine.');
  250.   writeln;
  251.   writeln('Watch out for the other techtutors...');
  252.   writeln;
  253.   writeln('Press a key');
  254.  
  255.   writeln;
  256.   writeln;
  257.   writeln;
  258.   writeln('----------------------------------');
  259.   writeln('Contacting: just4fun@zeelandnet.nl');
  260.   writeln('http://people.zeelandnet.nl/rpb   ');
  261.   writeln('----------------------------------');
  262.   repeat until port[$60]<>156;
  263. END.
  264.