home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D11 / CHESSDLL.ZIP / LTIMER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  2.6 KB  |  131 lines

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