!  Dotty Window
!
!  Demonstration of simple graphics, windows, and events using the
!  Developer's Toolkit libraries.
!
!  DON'T HIT BREAK TO STOP THIS PROGRAM.  Just use the mouse to close
!  the dotty window.
!
LIBRARY "libs/intuition*","libs/graphics*","libs/exec*","libs/amiga*"

DECLARE DEF Nullt$, Addr, InitNewWindow$, wd_UserPort, wd_RPort
DECLARE DEF wd_GZZWidth, wd_GZZHeight, im_Class
DECLARE DEF OpenWindow, CloseWindow, GetMsg, ReplyMsg, SetAPen, WritePixel

!
!
! Open the window.
!
LET IDCMP = 2+512                 ! SIZEWINDOW and CLOSEWINDOW
LET flags = 1 + 2                 ! WINDOWSIZING, WINDOWDRAG
LET flags = flags + 8 + 16 +64    ! WINDOWCLOSE, SIZEBRIGHT, SIMPLE_REFRESH
LET flags = flags + 1024          ! GIMMEZEROZERO
LET title$ = Nullt$("TB Dotty Window")
LET NewWindow$ = InitNewWindow$(100,50,100,50,IDCMP,flags,Addr(title$))
LET MyWindow = OpenWindow(Addr(NewWindow$))

!
!
! Extract IDCMP pointer, RastPort pointer, width and height.
!
LET MyIDCMP = wd_UserPort(MyWindow)
LET MyRPort = wd_RPort(MyWindow)
LET width = wd_GZZWidth(MyWindow)
LET height = wd_GZZHeight(MyWindow)

!
!
! Main loop -- check for events, draw three dots.
!
DO
   LET im = GetMsg(MyIDCMP)
   IF im = 0 then                 ! if no event
      FOR i = 1 to 3
          LET xxx = SetAPen(MyRPort,i)
          LET xxx = WritePixel(MyRPort,rnd*width,rnd*height)
      NEXT i
   ELSE                           ! if there is an event
      SELECT CASE im_Class(im)    ! branch on the class
      CASE 2                      ! SIZEWINDOW
           LET width = wd_GZZWidth(MyWindow)
           LET height = wd_GZZHeight(MyWindow)
      CASE 512                    ! CLOSEWINDOW
           LET xxx = CloseWindow(MyWindow)
           EXIT DO
      END SELECT
      LET xxx = ReplyMsg(im)
   END IF
LOOP

END

DEF InitNewWindow$(left,top,width,height,IDCMP,flags,title)
    CALL packb(s$,maxnum,16,left)      ! LeftEdge
    CALL packb(s$,maxnum,16,top)  ! TopEdge
    CALL packb(s$,maxnum,16,width)     ! Width
    CALL packb(s$,maxnum,16,height)    ! Height
    CALL packb(s$,maxnum,8,2)     ! DetailPen
    CALL packb(s$,maxnum,8,1)     ! BlockPen
    CALL packb(s$,maxnum,32,IDCMP)     ! IDCMPFlags
    CALL packb(s$,maxnum,32,flags)     ! Flags
    CALL packb(s$,maxnum,32,0)    ! FirstGadget
    CALL packb(s$,maxnum,32,0)    ! CheckMark
    CALL packb(s$,maxnum,32,title)     ! Title
    CALL packb(s$,maxnum,32,0)    ! Screen
    CALL packb(s$,maxnum,32,0)    ! BitMap
    CALL packb(s$,maxnum,16,30)   ! MinWidth
    CALL packb(s$,maxnum,16,30)   ! MinHeight
    CALL packb(s$,maxnum,16,3000)      ! MaxWidth
    CALL packb(s$,maxnum,16,3000)      ! MaxHeight
    CALL packb(s$,maxnum,16,1)    ! Type (WBENCHSCREEN)
    LET InitNewWindow$ = s$
END DEF

DEF wd_UserPort(wd)
    DECLARE DEF upeekl
    LET wd_UserPort = upeekl(wd+86)
END DEF

DEF wd_RPort(wd)
    DECLARE DEF upeekl
    LET wd_RPort = upeekl(wd+50)
END DEF

DEF wd_GZZWidth(wd)
    DECLARE DEF peekw
    LET wd_GZZWidth = peekw(wd+112)
END DEF

DEF wd_GZZHeight(wd)
    DECLARE DEF peekw
    LET wd_GZZHeight = peekw(wd+114)
END DEF

DEF im_Class(im)
    DECLARE DEF upeekl
    LET im_Class = upeekl(im+20)
END DEF
