!zone sprite_scroller
VIC_BANK             = $0000
SCREEN_MEM1          = (VIC_BANK)+$0800
SPRITE_DATA_0_MEM1   = (SCREEN_MEM1 + $03f8)

sprites_start       = ((sprite_data-$0000)/64)

VIC_SPRITE_SWITCH   = $d015 ;Each bit in $d015 controls whether the corresponding sprite is displayed or not. The lowest bit controls Sprite0 and so on.
VIC_SPRITE_MODE     = $d01c ;Each bit in $d01c controls whether the corresponding sprite is displayed in multicolor mode or not. The lowest bit controls Sprite0 and so on.
VIC_SPRITE_PRIORITY = $d01b
VIV_SPRITE_STETCH_V = $d017
VIV_SPRITE_STETCH_H = $d01d

;;The X coordinates are 9 bit long, and the 9th bit of all sprites are collected at $d010, similarly as $d015 works.
SPRITE_H_POSITION_0 = $d000
;SPRITE_V_POSITION_0 = $d001
;SPRITE_H_POSITION_1 = $d002
;SPRITE_V_POSITION_1 = $d003
;SPRITE_H_POSITION_2 = $d004
;SPRITE_V_POSITION_2 = $d005
;SPRITE_H_POSITION_3 = $d006
;SPRITE_V_POSITION_3 = $d007
;SPRITE_H_POSITION_4 = $d008
;SPRITE_V_POSITION_4 = $d009
;SPRITE_H_POSITION_5 = $d00A
;SPRITE_V_POSITION_5 = $d00B
;SPRITE_H_POSITION_6 = $d00C
;SPRITE_V_POSITION_6 = $d00D
;SPRITE_H_POSITION_7 = $d00E
;SPRITE_V_POSITION_7 = $d00F
SPRITE_H_MSB        = $d010

SPRITE_COLOR_0      = $d027
;SPRITE_COLOR_1      = $d028
;SPRITE_COLOR_2      = $d029
;SPRITE_COLOR_3      = $d02A
;SPRITE_COLOR_4      = $d02B
;SPRITE_COLOR_5      = $d02C
;SPRITE_COLOR_6      = $d02D
;SPRITE_COLOR_7      = $d02E


;scroller_init_sprites:
;          jsr .empty_sprites
          ;jsr .init_x_position
          ;
          ;lda #%00000000
          ;sta VIC_SPRITE_MODE
          ;lda #%11111111
          ;sta VIV_SPRITE_STETCH_H
          
scroller_reinit_sprites:
          jsr .reset_x_position
          lda #$00; #$07
          ;set_spritecolors_to_a
!For ROW = 0 TO 7
          sta SPRITE_COLOR_0+ROW
!end                  
;          lda #%00000000
;          sta VIC_SPRITE_MODE
          sta VIC_SPRITE_PRIORITY

;          lda #%11111111
;          sta VIC_SPRITE_SWITCH
          sta VIV_SPRITE_STETCH_V
          lda #%11111111
;          sta VIV_SPRITE_STETCH_H
          sta VIC_SPRITE_SWITCH
          sta VIV_SPRITE_STETCH_H
          rts

;.empty_sprites:
;          lda #.sprites_start
;.set_all_sprites_to_a:
;!For ROW = 0 TO 7
;          sta SPRITE_DATA_0_MEM1+ROW
;!End
;          rts

.init_x_position:
          lda #%11100000
          sta .OLD_SPRITE_H_MSB
;          sta SPRITE_H_MSB
          lda #$00
          sta .current_offset
;load spriteposition          
;!For ROW = 0 TO 15
;          lda sprite_init_positions+ROW
;          sta sprite_positions+ROW
;;          sta SPRITE_H_POSITION_0+ROW
;!end                    
          ldx #15
-          
          lda sprite_init_positions-1,x
          sta sprite_positions-1,x
          dex
          bne -
          rts

.reset_x_position:          
!For ROW = 0 TO 15
          lda sprite_positions+ROW
          sta SPRITE_H_POSITION_0+ROW
!end          
          lda .OLD_SPRITE_H_MSB
          sta SPRITE_H_MSB
          
;load spritepointer
!For ROW = 0 TO 7
          lda old_spritepointer+ROW
          sta SPRITE_DATA_0_MEM1+ROW
!end          
          rts

.current_offset:
          !byte $00
.move_sprites:
!For ROW = 0 TO 7
          lda sprite_positions+(ROW*2)
          clc
          adc #$fc          
          sta sprite_positions+(ROW*2)
!end     
          inc .current_offset

          lda .current_offset
          cmp #$02
          bne +

          lda #%11000000
          sta .OLD_SPRITE_H_MSB

+         lda .current_offset
          cmp #$06
          bne ++

          lda #%11000001
          sta .OLD_SPRITE_H_MSB
          ;lda sprite_positions
          lda #247
          ;clc
          ;adc #$f8
ntsc_sprite_position_patch = *
          sta sprite_positions

++        lda .current_offset
          cmp #12
          bne +++
          inc .scrollflag
          jsr .init_x_position
+++       rts

scroll_sprite_text:
          jsr .move_sprites
          lda .scrollflag
          cmp #$01
          bne +
          dec .scrollflag
          jsr .reposition_sprite_pointers
+         
          rts
           
.reposition_sprite_pointers
!For ROW = 0 TO 7          
          lda old_spritepointer+ROW+1
          sta old_spritepointer+ROW
!End          
.re_enter:
          lda .space_after_comma
          cmp #$01
          beq ++++
.pointer_hi = *+2
.pointer_lo = *+1
          lda scrolltext
          cmp #34
          bne +
          inc .space_after_comma   
+         tax
          clc
          ;adc #((SPRITE_data-BORDER_VIC_BANK)/64)
          adc #(sprites_start)
          sta old_spritepointer+7
          cpx #$ff
          bne ++
          lda #<scrolltext
          sta .pointer_lo
          lda #>scrolltext
          sta .pointer_hi
          jmp .re_enter:
++        inc .pointer_lo
          bne +++
          inc .pointer_hi
+++
          rts
++++
          lda #sprites_start
          sta old_spritepointer+7
          dec .space_after_comma
          rts
.space_after_comma:
  !byte $00          

;old_spritepointer:
;        !byte .sprites_start, .sprites_start, .sprites_start, .sprites_start
;        !byte .sprites_start, .sprites_start, .sprites_start, .sprites_start
;          
.OLD_SPRITE_H_MSB:
        !byte %11100000

.scrollflag:
        !byte $00

;sprite_positions:
;        !byte $17,$d1,$47,$d1,$77,$d1,$A7,$d1
;        !byte $D7,$d1,$07,$d1,$37,$d1,$67,$d1
;sprite_init_positions:
;        !byte $17,$d1,$47,$d1,$77,$d1,$A7,$d1
;        !byte $D7,$d1,$07,$d1,$37,$d1,$67,$d1           


