home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 10.ddi / CHESS.ZIP / LTIMER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  2.7 KB  |  134 lines

  1. {************************************************}
  2. {                                                }
  3. {   Chess - Shared DLL Example                   }
  4. {   CHESS.DLL Game timers.                       }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit LTimer;
  10.  
  11. {$R-,Q-,S-,W-}
  12.  
  13. interface
  14.  
  15. uses Objects;
  16.  
  17. { The Clock procedures provides a module for measuring Time.
  18.   Think of the module as a Stop watch. InitTime resets a
  19.   Clock completely. StartTime starts the Clock Running,
  20.   StopTime stops it again, and TotalTime then contains the
  21.   elapsed Time in seconds. You can use StartTime and StopTime
  22.   again and have the elapsed Time added to the TotalTime, and
  23.   you can use StopTime to get an intervening Time without
  24.   stoping the Clock itself }
  25.  
  26. type
  27.  
  28.   TStopWatch = object(TObject)
  29.     StartTime: Longint;
  30.     ElapsedTime : Longint;    { base unit is bios tics }
  31.     constructor Init;
  32.     procedure Start;
  33.     procedure Resume;
  34.     procedure Stop;
  35.     procedure Reset;
  36.     function  GetString: String;
  37.     function  GetElapsedTime: Longint;
  38.     procedure Update;
  39.     function  Running: Boolean;
  40.   end;
  41.  
  42.   PTaskTimer = ^TTaskTimer;
  43.   TTaskTimer = object(TStopWatch)
  44.     TimeLimit: Longint;
  45.     procedure SetLimit( A: Longint);
  46.     function  TimeExpired: Boolean;
  47.     function  TimeRemaining: Longint;
  48.   end;
  49.  
  50.  
  51. implementation
  52.  
  53. {$IFDEF WINDOWS}
  54. procedure __0040H;  far; external 'Kernel' index 193;
  55. const
  56.   Seg0040: Word = Ofs(__0040H);
  57. {$ENDIF}
  58.  
  59. constructor TStopWatch.Init;
  60. begin
  61.   Reset;
  62. end;
  63.  
  64. procedure TStopWatch.Reset;
  65. begin
  66.   StartTime := 0;
  67.   ElapsedTime := 0;
  68. end;
  69.  
  70. procedure TStopWatch.Start;
  71. begin
  72.   StartTime := MemL[Seg0040:$6C];
  73. end;
  74.  
  75. procedure TStopWatch.Resume;
  76. begin
  77.   StartTime:= MemL[Seg0040:$6C];
  78. end;
  79.  
  80. procedure TStopWatch.Stop;
  81. begin
  82.   Update;
  83.   StartTime := 0;
  84. end;
  85.  
  86. procedure TStopWatch.Update;
  87. begin
  88.   if LongBool(StartTime) then
  89.   begin
  90.     ElapsedTime := ElapsedTime + MemL[Seg0040:$6C] - StartTime;
  91.     Resume;
  92.   end;
  93. end;
  94.  
  95. function TStopWatch.GetString: String;
  96. var
  97.   Temp : String[20];
  98. begin
  99.   Update;
  100.   Str(ElapsedTime, Temp);
  101.   GetString := Temp;
  102. end;
  103.  
  104. function TStopWatch.GetElapsedTime: Longint;
  105. begin
  106.   Update;
  107.   GetElapsedTime := ElapsedTime;
  108. end;
  109.  
  110. function TStopWatch.Running: Boolean;
  111. begin
  112.   Running := StartTime <> 0;
  113. end;
  114.  
  115.  
  116. procedure TTaskTimer.SetLimit(A: Longint);
  117. begin
  118.   TimeLimit := A;
  119. end;
  120.  
  121. function TTaskTimer.TimeExpired: boolean;
  122. begin
  123.   Update;
  124.   TimeExpired := ElapsedTime >= TimeLimit;
  125. end;
  126.  
  127. function TTaskTimer.TimeRemaining: Longint;
  128. begin
  129.   Update;
  130.   TimeRemaining := TimeLimit - ElapsedTime;
  131. end;
  132.  
  133.  
  134. end.