;Amiga Byte numero 6, prog2.s ** screen scroll

;** Exec Offsets
ExecBase      EQU 4
Wait          EQU -$13e
GetMsg        EQU -$174
ReplyMsg      EQU -$17a
CloseLibrary  EQU -$19e
OpenLibrary   EQU -$228

;** Intuition Offsets
CloseScreen   EQU -$42
CloseWindow   EQU -$48
MoveScreen    EQU -$a2
OpenScreen    EQU -$c6
OpenWindow    EQU -$cc

;** Graphics Offsets
Text          EQU -$3c
Move          EQU -$f0


start:
    move.l  ExecBase,a6         ;a6 -> ExecBase

;apre libreria grafica
    lea     GfxName,a1          ;a1 -> 'graphics.library'
    moveq   #0,d0               ;versione = 0
    jsr     OpenLibrary(A6)     ;apre libreria grafica
    move.l  d0,GfxBase          ;d0 -> GfxBase
    beq     exit5               ;errore, esci

;apre libreria Intuition
    lea     IntuiName,a1        ;a1 -> 'intuition.library'
    moveq   #0,d0               ;versione = 0
    jsr     OpenLibrary(A6)     ;apre Intuition
    move.l  d0,IntuiBase        ;d0 -> IntuitionBase
    beq     exit4               ;errore, esci

;apre lo screen
    move.l  IntuiBase,a6        ;puntatore a IntuitionBase
    lea     NewScreen,a0        ;puntatore struttura Screen
    jsr     OpenScreen(A6)      ;apre il nuovo screen
    move.l  d0,MyScreen         ;e ne salva il puntatore 
    beq     exit3               ;errore, esci 
    move.l  d0,NScreen          ;d0 in struttura NewWindow

;apre la finestra
    lea     NuovaWindow,a0      ;puntatore a str. NewWindow 
    jsr     OpenWindow(a6)      ;apre la nuova finestra
    move.l  d0,MyWindow         ;e ne salva il puntatore
    beq     exit2               ;errore, esci

;sposta il cursore nella finestra
    move.l  GfxBase,a6          ;puntatore a GraphicsBase 
    move.l  MyWindow,a0         ;a0 -> MyWindow
    move.l  $32(a0),RPort       ;salva Rast Port
    move.l  RPort,a1            ;a1 -> RPort
    move.l  #20,d0              ;offset dal bordo sinistro
    move.l  #50,d1              ;offset dall'alto
    jsr     Move(A6)            ;muove il cursore

;stampa il testo
    move.l  RPort,a1            ;a1 -> RPort
    lea     MyText,a0           ;a0 -> testo da stampare 
    move.l  #33,d0              ;numero dei caratteri = 33
    jsr     Text(A6)            ;stampa il testo 
    
;attende un messaggio
attendi:    
    move.l  ExecBase,a6         ;puntatore ad ExecBase 
    move.l  MyWindow,a0         ;a0 -> MyWindow
    move.l  $56(a0),a0          ;a0 -> UserPort
    clr.l   d1                  ;pulisce d1
    move.b  $0f(a0),d1          ;setta MP_SIGBIT
    move.l  #1,d0               ;setta il bit di segnale
    lsl.l   d1,d0               ;e lo shifta
    jsr     Wait(a6)            ;attende un messaggio

;leggi il messaggio
leggi:
    move.l  MyWindow,a0         ;a0 -> MyWindow
    move.l  $56(a0),a0          ;a0 -> UserPort
    jsr     GetMsg(a6)          ;legge il messaggio
    tst.l   d0                  ;c'e' un messaggio?
    beq     attendi             ;no, attendi ancora
    move.l  d0,a1               ;si, a1 -> messaggio

;ci interessa il messaggio?
    move.l  $14(a1),d0          ;leggi la classe del msg
    btst.l  #9,d0               ;e' un CLOSEWINDOW?
    bne     scrolla             ;si, scrolla ed esci
    jsr     ReplyMsg(A6)        ;no, rispondi al messaggio
    bra     leggi               ;e leggi quello successivo

;muove lo screen  
scrolla:
    move.l  IntuiBase,a6        ;puntatore a IntuitionBase
    moveq   #1,d2               ;step di scroll iniziale
labA:    
    move.l  #256,d3             ;linee dello schermo
    divu.w  d2,d3               ;linee/step = contatore
    move.w  d3,Cont1            ;salva contatore
labB:    
    move.l  MyScreen,a0         ;puntatore a MyScreen
    moveq   #0,d0               ;DeltaX = 0
    move.l  d2,d1               ;DeltaY = step 
    jsr     MoveScreen(a6)      ;scrolla lo schermo in giu'
    subi.w  #1,d3               ;decrementa contatore
    bne     labB                ;e continua fino a zero
    
    move.w  Cont1,d3            ;recupera contatore
labC:
    move.l  MyScreen,a0         ;puntatore a MyScreen
    moveq   #0,d0               ;DeltaX = 0
    move.l  d2,d1               ;DeltaY = step
    neg.l   d1                  ;rende il numero negativo
    jsr     MoveScreen(a6)      ;scrolla lo schermo in su'
    subi.w  #1,d3               ;decrementa contatore
    bne     labC                ;e continua fino a zero
   
    addi.b  #1,d2               ;aumenta step di scroll
    cmpi.b  #$30,d2             ;fino a $30 poi smetti
    bne     labA                ;scrolla con nuovo step
    
;termine del programma
exit1:
    move.l  IntuiBase,a6        ;puntatore a IntuitionBase
    move.l  MyWindow,a0         ;puntatore alla finestra
    jsr     CloseWindow(a6)     ;chiude la finestra
exit2:
    move.l  MyScreen,a0         ;puntatore allo screen
    jsr     CloseScreen(a6)     ;chiude lo screen
exit3:
    move.l  ExecBase,a6         ;puntatore ad ExecBase
    move.l  IntuiBase,a1        ;puntatore ad IntuitionBase
    jsr     CloseLibrary(a6)    ;chiude Intuition 
exit4:
    move.l  GfxBase,a6          ;puntatore a GraphicsBase 
    jsr     CloseLibrary(a6)    ;chiude libreria grafica
exit5:
    rts                         ;esci 
    

;dati del programma
IntuiBase:  DC.L  0
GfxBase:    DC.L  0
MyScreen:   DC.L  0
MyWindow:   DC.L  0
RPort:      DC.L  0
Cont1:      DC.W  0

;struttura Screen NewScreen
NewScreen:
    DC.W  0                     ns_LeftEdge
    DC.W  0                     ns_TopEdge
    DC.W  320                   ns_Width
    DC.W  256                   ns_Height
    DC.W  2                     ns_Depth
    DC.B  0                     ns_DetailPen
    DC.B  1                     ns_BlockPen
    DC.W  0                     ns_ViewModes
    DC.W  $0F                   ns_Type
    DC.L  0                     ns_Font
    DC.L  STitolo               ns_DefaultTitle
    DC.L  0                     ns_Gadgets
    DC.L  0                     ns_CustomBitMap

;struttura NewWindow NuovaWindow
NuovaWindow:  
    DC.W  10                    ;nw_LeftEdge
    DC.W  100                   ;nw_TopEdge
    DC.W  300                   ;nw_Width
    DC.W  156                   ;nw_Height
    DC.B  0                     ;nw_DetailPen
    DC.B  1                     ;nw_BlockPen
    DC.L  $200                  ;nw_IDCMPFlags
    DC.L  $2101F                ;nw_Flags
    DC.L  0                     ;nw_FirstGadget
    DC.L  0                     ;nw_CheckMark
    DC.L  WTitolo               ;nw_Title
NScreen: DC.L  0                     ;nw_Screen
    DC.L  0                     ;nw_Bitmap
    DC.W  100                   ;nw_MinWidth
    DC.W  25                    ;nw_MinHeight
    DC.W  320                   ;nw_MaxWidth
    DC.W  200                   ;nw_MaxHeight
    DC.W  $0F                   ;nw_Type

IntuiName:  DC.B  'intuition.library',0
GfxName:    DC.B  'graphics.library',0
STitolo:    DC.B  'MyScreen',0
WTitolo:    DC.B  'MyWindow',0
MyText:     DC.B  'Prova a chiudere questa finestra!',0
