{ÛßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßÛ}
{Û                                                       Û}
{Û      Virtual Pascal Examples  Version 2.1             Û}
{Û      DIVE example                                     Û}
{Û      ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÛ}
{Û      Copyright (C) 1996-2000 vpascal.com              Û}
{Û                                                       Û}
{ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß}

program TestDive;

{$PMTYPE PM}

uses
  Os2Def, Os2Dive, Os2PmApi, SysUtils;

// Sample routine for updating buffer content
procedure MyUpdate( Dive: tDiveWindow );
const
  Count: longint = 0;
  First: Boolean = True;

var
  i : integer;
  Col: Integer;
  p1, p2, p3, p4 : Pixel;
  s: String;

begin
  With Dive do
    try
      If First then
        begin
          // Initialisation code goes here
          First := False;
        end;

      // Change between linemode and filled graphics
      If Random > 0.9 then
        LineMode := Not LineMode;

      // Sometimes flip the image
      If ( Random > 0.92 ) and ( Count > 1 ) then
        Flip( 10 + random(50) );

      Col := Count mod Colours;

      // Put some objects on screen
      DrawStr( 10, 10, 15, -1, 'Dive Sample' );

      DrawLine( Random( xsize ), Random( ySize ),
                Random( xsize ), Random( Ysize ), Col );

      p1.x := Random( xsize ); p1.y := Random( ySize );
      DrawBox( p1.x, p1.y, p1.x+Random(100), p1.y+Random(100), Col );

      p1.x := Random( xsize ); p1.y := Random( ySize );
      i := Random( 250 );
      DrawPieSlice( p1, Random( 100 ), i, Random( 360-i ), Col );

      p1.x := Random( xsize ); p1.y := Random( ySize );
      p2.x := p1.x+Random( 100 ); p2.y := p1.y+Random( 100 );
      p3.x := p1.x-Random( 100 ); p3.y := p1.y-Random( 100 );
      DrawTriangle( p1, p2, p3, col );

      p1.x := Random( xsize ); p1.y := Random( ySize );
      p2.x := p1.x+Random( 100 ); p2.y := p1.y+Random( 100 );
      p3.x := p2.x-Random( 100 ); p3.y := p2.y-Random( 100 );
      p4.x := p3.x-Random( 100 ); p4.y := p3.y-Random( 100 );
      DrawQuad( p1, p2, p3, p4, col );

      Inc( Count );
    except
      // Handle exceptions gracefully
    end;
end;

// Sample user message handler
function MyWindowProc(Window: HWnd; Msg: ULong; Mp1,Mp2: MParam): MResult;cdecl;
begin
  case msg of
    wm_char :
      // Add ESC as exit-key to default handling
      if (CharMsgMp1(Mp1).fs and kc_KeyUp) = 0 then { Key is Down }
        case uShort(mp2 shr 16) of
          VK_ESC :
            begin
              WinPostMsg ( Window, WM_QUIT, 0, 0 );
              Result := 1;   // Do not pass message to default handler
              Exit;
            end;
        end;
  end;
  Result := 0;  // Message not handled
end;

{$R TestDive.Res}

var
  Dive : tDiveWindow;
  rc   : ApiRet;
  Code : Longint;

begin
  try
    // Create main DIVE window without menu
    // Dive := tDiveWindow.Create( 640, 480, 8 );
    // Create main DIVE window with default menu
    Dive := tDiveWindow.CreateRes( 640, 480, 8, FCF_MENU, ID_DIVEMENU );
    With Dive do
      begin
        Statistics := True;                // Enable title bar statistics
        UpdateBuffer := MyUpdate;          // Set function to update buffer
        MessageHandler := MyWindowProc;    // Set message handler hook
        Run;                               // Run!
        Destroy;
      end;
  except
    rc := WinGetLastError( Code );         // Grab error for further processing
  end;
end.
