*
* QUIX  Simple line drawing example of the Absoft interface to the Amiga
*       ROM routines. This version uses the Amiga graphics primitives,
*       taking over the entire display
*

      program quix

      implicit none                     ! keeps us out of trouble

      include graph.inc

      integer*4 DEPTH  ; parameter (DEPTH=3)
      integer*4 WIDTH  ; parameter (WIDTH=320)
      integer*4 HEIGHT ; parameter (HEIGHT=200)

      character*40 command_line         ! command line buffer

      integer*4 amiga,loc               ! define the interface functions
      integer*4 color                   ! current color
      integer*4 delay                   ! main loop delay factor
      integer*4 i,j                     ! working variable

      integer*4 displaymem              ! to access plane memory

      integer*4 oldview                 ! save pointer to current view

      integer*4 RASSIZE,w,h             ! for the RASSIZE statement function

* line end points

      integer*4 x1(8),y1(8),x2(8),y2(8)

* line end point increments

      integer*4 x1inc,y1inc,x2inc,y2inc


* (defined as a macro in C)

      RASSIZE(w,h) = h * (shift(w+15,-3) .and. z'fffe')


* get delay factor from command line

      delay = 0
      call args(command_line)
      read (command_line,*) delay
      delay = 10**delay

* get a pointer the current view so that we can restore it, otherwise
* when the program exits so does the Amiga

      i = amiga(GfxBase)                ! get pointer to "graphics.library"
      oldview = long(i+gb_ActiView)     ! save current view pointer

* initialize the a view and one viewport

      call amiga(InitView,View)         ! initialize View
      call amiga(InitVPort,ViewPort)    ! initialize ViewPort
      v_ViewPort = loc(ViewPort)        ! link View into ViewPort

* init BitMap (for RasInfo and RastPort)

      call amiga(InitBitMap,BitMap,DEPTH,WIDTH,HEIGHT)

* init RasInfo

      ri_BitMap   = loc(BitMap)
      ri_RxOffset = 0
      ri_RyOffset = 0
      ri_Next     = 0

* specify critical characteristics

      vp_DWidth  = WIDTH
      vp_DHeight = HEIGHT
      vp_RasInfo = loc(RasInfo)

* allocate space for bitmaps

        do (i=1,DEPTH)
          bm_Planes(i) = amiga(AllocRaster,WIDTH,HEIGHT)
          if (bm_Planes(i) = 0) stop "Not Enough Memory"
        repeat

* contruct copper instr (prelim) list
* merge prelim lists together into a real
* copper list in the view structure

      call amiga(MakeVPort,View,ViewPort)
      call amiga(MrgCop,View)

* zero all bytes of the display area

        do (i=1,DEPTH)
          displaymem = bm_Planes(i)
            do (j=0,RASSIZE(WIDTH,HEIGHT)-1)
              byte(displaymem+j) = 0
            repeat
        repeat

* init the raster port

      call amiga(InitRastPort,RastPort)
      rp_BitMap = loc(BitMap)

* let 'er rip

      call amiga(LoadView,loc(View))

      color = 0                         ! reset pen color

      x1inc =  3
      y1inc =  7
      x2inc = -9
      y2inc = -5

      x1(1) = WIDTH/2
      y1(1) = HEIGHT/2
      x2(1) = x1(1) + 10
      y2(1) = y1(1) + 20

        do (i=1,7)
          x1(i+1) = x1(i) + x1inc
          y1(i+1) = y1(i) + y1inc
          x2(i+1) = x2(i) + x2inc
          y2(i+1) = y2(i) + y2inc
        repeat

        do (5000 times)

* erase last line

          call amiga(SetAPen,loc(RastPort),0)
          call amiga(Move,loc(RastPort),x1(8),y1(8))
          call amiga(Draw,loc(RastPort),x2(8),y2(8))

* set new pen color

          color = color+1; if (color=32) color = 1
          call amiga(SetAPen,loc(RastPort),color)

* shift lines up

            do (i=7,1,-1)
              x1(i+1) = x1(i)
              y1(i+1) = y1(i)
              x2(i+1) = x2(i)
              y2(i+1) = y2(i)
            repeat

* increment the first line

          x1(1) = x1(1) + x1inc
            if (x1inc<0) then
              if (x1(1)<0) then
                x1(1) = 0
                x1inc = 0-x1inc
              end if
            else
              if (x1(1)>WIDTH-1) then
                x1(1) = WIDTH-1
                x1inc = 0-x1inc
              end if
            end if

          y1(1) = y1(1) + y1inc
            if (y1inc<0) then
              if (y1(1)<0) then
                y1(1) = 0
                y1inc = 0-y1inc
              end if
            else
              if (y1(1)>HEIGHT-1) then
                y1(1) = HEIGHT-1
                y1inc = 0-y1inc
              end if
            end if

          x2(1) = x2(1) + x2inc
            if (x2inc<0) then
              if (x2(1)<0) then
                x2(1) = 0
                x2inc = 0-x2inc
              end if
            else
              if (x2(1)>WIDTH-1) then
                x2(1) = WIDTH-1
                x2inc = 0-x2inc
              end if
            end if

          y2(1) = y2(1) + y2inc
            if (y2inc<0) then
              if (y2(1)<0) then
                y2(1) = 0
                y2inc = 0-y2inc
              end if
            else
              if (y2(1)>HEIGHT-1) then
                y2(1) = HEIGHT-1
                y2inc = 0-y2inc
              end if
            end if

          call amiga(Move,loc(RastPort),x1(1),y1(1))
          call amiga(Draw,loc(RastPort),x2(1),y2(1))

          do (i=1,delay); repeat

        repeat

      call amiga(LoadView,oldview)      ! put back the old view

* return user and system-allocated memory to sys manager

        do (i=1,DEPTH)
          call amiga(FreeRaster,bm_Planes(i),WIDTH,HEIGHT)
        repeat

      call amiga(FreeVPortCopLists,ViewPort)
      call amiga(FreeCprList,v_LOFCprList)

      end
