;  -------------------------------------------------------------------
;  A Hello World program based on the Blitz Basic original
;  by Carl Read. Assembler version by Matthew Goode 18/1/97
;
;  Feel free to use any part of this program in your own, and, although
;  you can claim it as your own, please be considerate and give credit
;  where credit is due.
;  -------------------------------------------------------------------

;  -------------------------------------------------------------------
;  ------------------- Environment Design ----------------------------

UseIntuitionLib =   1
UseGraphicsLib  =   1

;  -------------------------------------------------------------------
;  ------------------ Included (External) Constants ------------------

    IncDir "work:Assem/Include/"

;  ----- Executive Stuff ------
    Include "exec/Exec_lib.i"
    Include "exec/ExecBase.i"

;  ----- Graphics Stuff ------
    Include "Graphics/Graphics_lib.i"

;  ----- Intuition Stuff ------
    Include "Intuition/Intuition_lib.i"
    Include "Intuition/Intuition.i"

;  ----- WorkBench Stuff ------
    Include "WorkBench/WorkBench.i"

;  ----- Other Stuff ------
    Include "Libraries/DosExtens.i"

;  ----- My Stuff ------
    IncDir "work:Assem/MyIncludes/"
    Include "StandardMacros.i"
    Include "SimpleWorkbenchStart.i"
    Include "OpIntuitionMacros.i"

;  -------------------------------------------------------------------
;  ------------------- Internal Constants ----------------------------

screenWidth =   640
screenHeight    =   256
mainWindowWidth =   300
mainWindowHeight    =   200
mainWindowXPos  =   (screenWidth-mainWindowWidth)/2
mainWindowYPos  =   (screenHeight-mainWindowHeight)/2
mainWindowDetailPen =   2
mainWindowBlockPen  =   1
mainWindowIDCMP =   CLOSEWINDOW
mainWindowFlags =   WINDOWSIZING+WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+SMART_REFRESH+RMBTRAP+GIMMEZEROZERO
mainWindowMinWidth  =   40
mainWindowMinHeight =   40
mainWindowMaxWidth  =   -1
mainWindowMaxHeight =   -1

mainWindowType  =   WBENCHSCREEN

textCol =   1
backCol =   0

;  -------------------------------------------------------------------
;  --------------------- Internal Macros -----------------------------

;  -------------------------------------------------------------------
;  -------------------- Main Program ---------------------------------

TheBegining
    StartHerUp
OpenWindows
    Bsr OpenMainWindow
    beq WindowFailure

    Bsr MainSetup
    Bsr MainLoop

WindowFailure
    Bsr CloseMainWindow

    TidyUp  ;No need for a RTS


;  -------------------------------------------------------------------

MainSetup
    Move.w #0,scrollCount
    rts

MainLoop
.ll1
    ;CALLGRAF WaitTOF   ;Use this line for a smoother demo.

    move.l mainWindowHandle,a2
    move.l mainWindowRast,a3

    moveq #0,d2     ;Here we get the dimensions of the window
    moveq #0,d3     ;Note that since this is a GIMMEZEROZERO
                    ;Window we do not have to worry about the
                    ;borders. Usually it is better to use
                    ;Normal Windows as they can be faster.
    move.w wd_GZZWidth(a2),d4
    move.w wd_GZZHeight(a2),d5
    Sub.w #1,d4 ;Adjust for 'Of by One Errors
    Sub.w #1,d5

    tst.w scrollCount   ;Is it time to Reprint?
    bne .noReprintMessage
    GetFontHeight a3,d0 ;Get verticle displacement
    move.w d0,scrollCount

    move.w d5,d1
    sub.w d0,d1 ;Get YPos of text

    move.w d2,d0    ;Get XPos of text

    lea iTextStruct,a1
    lea message,a0
    move.l a0,textToPrint_p
    lea (a3),a0 ;Get RastPort
    CALLINT PrintIText

.noReprintMessage
    moveq #0,d0 ;No horizontal scroll
    moveq #1,d1
    lea (a3),a1 ;Get RastPort for scroll
    CALLGRAF ScrollRaster

    sub.w #1,scrollCount


    move.l mainWindowHandle,a3
    Bsr GetMessageFromWindow
    Beq .ll1
    Cmp.l #CLOSEWINDOW,d1   ;Not really necessary as this is the only
                ;message possible
    bne .ll1
    rts

;  -------------------------------------------------------------------

GetMessageFromWindow
    ;Waits for a message from the window in a3 and then procedes to
    ;get message.
    ;On Exiting: a0 - holds pointer to message
    ;            d1 - holds message class
    ;            z flag set if message Found
    move.l wd_UserPort(a3),a0
    CALLEXEC GetMsg
    move.l d0,a0
    move.l im_Class(a0),d1
    Tst.l d0
    rts

;  -------------------------------------------------------------------

OpenMainWindow
    ;Opens the main window
    ; D0 holds handle on exit (will be zero if failure)
    ; No need to test as Status Flags will already be set
    Lea newMainWindow,a0
    CALLINT OpenWindow
    Move.l d0,a0
    Move.l wd_RPort(a0),mainWindowRast
    Move.l d0,mainWindowHandle
    rts

CloseMainWindow
    ;Closes the Window if it was opened
    Move.l mainWindowHandle,d0
    Beq .noWindow
    Move.l d0,a0
    CALLINT CloseWindow
    Clr.l mainWindowHandle
.noWindow
    rts


;  -------------------------------------------------------------------
;  ---------------- Physical Constants -------------------------------

    Include "LibraryNames.i"
    even

newMainWindow
    dc.w mainWindowXPos,mainWindowYPos,mainWindowWidth,mainWindowHeight
    dc.b mainWindowDetailPen,mainWindowBlockPen
    dc.l mainWindowIDCMP,mainWindowFlags
    dc.l 0  ;Pointer to first Gadget
    dc.l 0,mainWindowTitle,0,0  ;CheckMark,title,screen,bitmap
    dc.w mainWindowMinWidth,mainWindowMinWidth,mainWindowMaxWidth,mainWindowMaxHeight
    dc.w 1  ;mainWindowType

mainWindowTitle
    dc.b "<- Click to exit",0
    Even

message
    dc.b " Hello World!  I'm an ASSEMBLER program!!!!!!!.",0

;  -------------------------------------------------------------------
;  ---------------- Variables ----------------------------------------

iTextStruct dc.b textCol,backCol,RP_JAM2,0
        dc.w 0,0
        dc.l 0
textToPrint_p   dc.l 0,0

    Include "StandardVaribles.i"

mainWindowHandle    ds.l 1
mainWindowRast      ds.l 1

scrollCount ds.w 1  ;The scroller counter

;  -------------------------------------------------------------------

