{ Copyright (C) 1998 Peter Gerwinski, Essen, Germany
                     peter@gerwinski.de, http://www.gerwinski.de/

                2003-2004 Frank Heckenbach <frank@pascal.gnu.de>
                          (small cosmetic changes)

  All rights reserved except those explicitly granted below.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation, version 2.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING. If not, write to
  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA. }

program Sanduhr;

uses GPC, GRX;

const
  Count = 5000;
  Dump = 10000;

var
  h, m, x0, y0, x1, y1, i, Yellow: Integer;
  t: TimeStamp;
  StartTime, FinishTime, DelayTime: UnixTimeType;

procedure GrainBottom (Nr, Farbe: Integer);
var
  n, x, x0, y, dx: Integer;
begin
  n := 0;
  dx := 187;
  x0 := 0;
  x := x0;
  y := 0;
  while n < Nr do
    begin
      Inc (n);
      Inc (x);
      if x >= dx then
        begin
          x := x0;
          Inc (y);
          if Odd (y) then
            begin
              Inc (x0);
              Dec (dx);
            end
        end
    end;
  GrPlot (227 + x, GrScreenY - 1 - y, Farbe)
end;

procedure GrainTop (Nr, Farbe: Integer);
var
  n, x, x0, y, dx: Integer;
begin
  n := 0;
  dx := 1;
  x0 := 0;
  x := x0;
  y := 0;
  while n < Nr do
    begin
      Inc (n);
      Inc (x);
      if x >= dx then
        begin
          x := x0;
          Inc (y);
          if Odd (y) then
            begin
              Dec (x0);
              Inc (dx)
            end
        end
    end;
  GrPlot (320 + x, GrScreenY div 2  - GrScreenY div 10 - 5 - y, Farbe)
end;

begin
  Write ('Hour: ');
  ReadLn (h);
  Write ('Minute: ');
  ReadLn (m);
  GetTimeStamp (t);
  StartTime := TimeStampToUnixTime (t);
  t.Hour := h;
  t.Minute := m;
  t.Second := 0;
  FinishTime := TimeStampToUnixTime (t);
  DelayTime := ((FinishTime - StartTime) * UnixTimeType (1000)) div Count;
  if GrSetMode (Gr_Default_Graphics, 0, 0, 0, 0, 0) <> 1 then
    begin
      WriteLn (StdErr, 'could not initialize graphics');
      Halt (1)
    end;
  x0 := GrScreenX div 2;
  y0 := GrScreenY div 2 - GrScreenY div 10;
  x1 := GrScreenX div 2;
  y1 := GrScreenY div 2 + GrScreenY div 10;
  GrLine (x0, y0, x0 - 1000, y0 - 2000, GrAllocColor (255, 255, 255));
  GrLine (x0, y0, x0 + 1000, y0 - 2000, GrAllocColor (255, 255, 255));
  GrLine (x1, y1, x1 - 1000, y1 + 2000, GrAllocColor (255, 255, 255));
  GrLine (x1, y1, x1 + 1000, y1 + 2000, GrAllocColor (255, 255, 255));
  GrLine (x0, y0, x1, y1, GrAllocColor (255, 255, 255));
  Yellow := GrAllocColor (255, 200, 0);
  for i := 0 to Dump - 1 do
    GrainBottom (i, Yellow);
  for i := 0 to Count - 1 do
    GrainTop (i, Yellow);
  for i := 0 to Count - 1 do
    begin
      GrainTop (Count - 1 - i, GrBlack);
      GrainBottom (Dump + i, Yellow);
      Discard (GrKeyPressed);
      GrSleep (DelayTime)
    end;
  ReadLn;
  Discard (GrSetMode (Gr_Default_Text, 0, 0, 0, 0, 0))
end.
