Unit gameClassTimer

The code of gameClassTimer

unit gameClassTimer;
{
    Copyright (c) 2011 Christopher Winward
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sdl, gameVariables;

type TTimer = class(TObject)
  private
    startTicks, pausedTicks : U32;
    paused, started : boolean;
  public
    procedure Init;
    procedure Start;
    procedure Stop;
    procedure Pause;
    procedure Unpause;
    function GetTicks : U32;
    function IsPaused : boolean;
    function IsStarted : boolean;
end;

implementation

procedure TTimer.Init;
begin
  startTicks := 0;
  pausedTicks := 0;
  paused := false;
  started := false;
end;

procedure TTimer.Start;
begin
  started := true;
  paused := false;
  startTicks := SDL_GetTicks;
end;

procedure TTimer.Stop;
begin
  started := false;
  paused := false;
  startTicks := 0;
end;

function TTimer.GetTicks : U32;
begin
  if (started = true) then
    begin
      if (paused = true) then
        begin
          result := pausedTicks;
        end
      else
        begin
          result := SDL_GetTicks - startTicks;
        end;
    end
  else
    begin
      writeln('Trying to get ticks from a timer that isn''t running');
      result := 0;
    end;
end;

procedure TTimer.Pause;
begin
  if ((started = true) and (paused = false)) then
    begin
      paused := true;
      pausedTicks := SDL_GetTicks - StartTicks;
    end;
end;

procedure TTimer.Unpause;
begin
  if ((started = true) and (paused = true)) then
    begin
      paused := false;
      StartTicks := SDL_GetTicks - pausedTicks;
      pausedTicks := 0;
    end;
end;

function TTimer.isPaused : boolean;
begin
  result := paused;
end;

function TTimer.isStarted : boolean;
begin
  result := started;
end;

end.

Remarks

Could you write a game that uses routines in these SpaceShooter units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16