
PROGRAM OpenScreen;

{
    PCQ-Version des Picasso96-Demoprogrammes

    in Pascal übersetzt von Andreas Neumann
}

{$I "Include:exec/interrupts.i" }
{$I "Include:exec/libraries.i" }
{$I "Include:libraries/dos.i" }
{$I "Include:graphics/graphics.i" }
{$I "Include:graphics/pens.i" }
{$I "Include:intuition/intuition.i" }
{$I "Include:utils/random.i" }
{$I "Include:p96/Picasso96.i" }

Const
    gfxname :   String = "graphics.library";
    ScreenTitle :   String  =   "Picasso96 API Test";
    W1Title     :   String  =   "WritePixel";
    W2Title     :   String  =   "FillRect";
    Width   :   Integer =   640;
    Height  :   Integer =   480;
    Depth   :   Integer =   8;
    Pens    :   Integer =   0;

    W1NW    :   NewWindow = (0,0,200,300,-1,-1,RAWKEY_f+CLOSEWINDOW_f,
                             WINDOWDRAG+WINDOWDEPTH+WINDOWSIZING+GIMMEZEROZERO+ACTIVATE+SIMPLE_REFRESH+REPORTMOUSE_f+WINDOWCLOSE+RMBTRAP,
                             NIL,NIL,W1Title,NIL,NIL,0,0,0,0,CUSTOMSCREEN_f);
    W2NW    :   NewWindow = (0,0,200,300,-1,-1,RAWKEY_f+CLOSEWINDOW_f,
                             WINDOWDRAG+WINDOWDEPTH+WINDOWSIZING+GIMMEZEROZERO+ACTIVATE+SIMPLE_REFRESH+REPORTMOUSE_f+WINDOWCLOSE+RMBTRAP,
                             NIL,NIL,W2Title,NIL,NIL,0,0,0,0,CUSTOMSCREEN_f);

Var
    i       :   Integer;
    sc      :   ScreenPtr;
    ptags   :   Array [0..32] Of TagItem;
    w1,
    w2      :   WindowPtr;
    rp1,
    rp2     :   RastPortPtr;
    terminate   :   Boolean;
    signals     :   Integer;
    format      :   RGBFTYPE;
    x1, y1,
    x2, y2,
    x3, y3      :   Short;
    imsg        :   IntuiMessagePtr;
    msg         :   MessagePtr;

BEGIN
 GFXBase:=OpenLibrary (gfxname,0);
 IF GFXBase<>Nil Then
 Begin
  P96Base:=OpenLibrary (P96NAME,0);
  If P96Base<>Nil Then
  Begin
   ptags[0].ti_Tag:=P96SA_Width;
   ptags[0].ti_Data:=Width;
   ptags[1].ti_Tag:=P96SA_Height;
   ptags[1].ti_Data:=Height;
   ptags[2].ti_Tag:=P96SA_Depth;
   ptags[2].ti_Data:=Depth;
   ptags[3].ti_Tag:=P96SA_AutoScroll;
   ptags[3].ti_Data:=Integer(TRUE);
   ptags[4].ti_Tag:=P96SA_Pens;
   ptags[4].ti_Data:=Pens;
   ptags[5].ti_Tag:=P96SA_Title;
   ptags[5].ti_Data:=Integer(ScreenTitle);
   ptags[6].ti_Tag:=TAG_DONE;

   sc:=p96OpenScreenTagList (Adr(ptags));
   If sc=Nil Then
    Writeln ("Kann Screen nicht öffnen.")
   Else
   Begin
    W1NW.Screen:=sc;
    W1NW.LeftEdge:=((sc^.Width DIV 2)-200)+(sc^.Width DIV 2);
    W1NW.TopEdge:=((sc^.Height-sc^.BarHeight-300) DIV 2);
    W1NW.MinHeight:=sc^.BarHeight+1;
    W1NW.MaxWidth:=sc^.Width;
    W1NW.MaxHeight:=sc^.Height-sc^.BarHeight-1;
    w1:=OpenWindow (Adr(W1NW));
    If w1<>Nil Then
    Begin
     W2NW.Screen:=sc;
     W2NW.LeftEdge:=((sc^.Width DIV 2)-200);
     W2NW.TopEdge:=((sc^.Height-sc^.BarHeight-300) DIV 2);
     W2NW.MinHeight:=sc^.BarHeight+1;
     W2NW.MaxWidth:=sc^.Width;
     W2NW.MaxHeight:=sc^.Height-sc^.BarHeight-1;

     w2:=OpenWindow (Adr(W2NW));
     If w2<>Nil Then
     Begin
      rp1:=w1^.RPort;
      rp2:=w2^.RPort;
      terminate:=False;
      signals:=((1 shl w1^.UserPort^.mp_SigBit) or (1 shl w2^.UserPort^.mp_SigBit));
      format:=RGBFTYPE (p96GetBitMapAttr (sc^.SRastPort.BitMap, P96BMA_RGBFORMAT));

      SelfSeed;

      Repeat
       x1:=RangeRandom (w2^.Width);
       y1:=RangeRandom (w2^.Height);
       x2:=RangeRandom (w2^.Width);
       y2:=RangeRandom (w2^.Height);
       If x2<x1 Then
       Begin
        x3:=x2;
        x2:=x1;
        x1:=x3;
       End;
       If y2<y1 Then
       Begin
        y3:=y2;
        y2:=y1;
        y1:=y3;
       End;

       x3:=RangeRandom (w1^.Width);
       y3:=RangeRandom (w1^.Height);

       If format=RGBFB_CLUT Then
       Begin
        SetAPen (rp2, RangeRandom (255));
        RectFill (rp2,x1,y1,x2,y2);

        SetAPen (rp1, RangeRandom (255));
        WritePixel (rp1,x3,y3);
       End
       Else
       Begin
        p96RectFill (rp2, x1, y1, x2, y2,(RangeRandom(255) shl 16)+(RangeRandom(255) shl 8)+(RangeRandom (255)));


        p96WritePixel (rp1, x3, y3, ((RangeRandom(255)) shl 16)+((RangeRandom(255)) shl 8)+(RangeRandom(255)));
       End;

       Repeat
        imsg:=Address(GetMsg (w1^.UserPort));
        If imsg<>Nil Then
        Begin
         If ((imsg^.Class=CLOSEWINDOW_f) Or ((imsg^.Class=RAWKEY_f) And ((imsg^.Code=$40) or (imsg^.Code=$45)))) Then
          terminate:=True;
         ReplyMsg (Address(imsg));
        End;
       Until imsg=Nil;
       Repeat
        imsg:=Address(GetMsg (w2^.UserPort));
        If imsg<>Nil Then
        Begin
         If ((imsg^.Class=CLOSEWINDOW_f) Or ((imsg^.Class=RAWKEY_f) And ((imsg^.Code=$40) or (imsg^.Code=$45)))) Then
          terminate:=True;
         ReplyMsg (Address(imsg));
        End;
       Until imsg=Nil;

      Until terminate;

      Forbid;
      Repeat
       msg:=GetMsg (w1^.UserPort);
       If msg<>Nil Then
        ReplyMsg (msg);
      Until msg=Nil;
      Repeat
       msg:=GetMsg (w2^.UserPort);
       If msg<>Nil Then
        ReplyMsg (msg);
      Until msg=Nil;
      Permit;

      CloseWindow (w2);
     End
     Else
      Writeln ("Kann W2 nicht öffnen.");
     CloseWindow (w1);
    End
    Else
     Writeln ("Kann W1 nicht öffnen.");

    p96CloseScreen (sc);
   End;

   CloseLibrary (GfxBase);
   CloseLibrary (P96Base);
  End
  Else
  Begin
   CloseLibrary (GfxBase);
   Writeln ("Kann P96-Library nicht öffnen.");
  End;
 End
 Else
  Writeln ("Kann Gfx-Library nicht öffnen.");
END.

