;********************************
;ATALAN nes sprite asm functions
;********************************
; created by Marcel Cevani 31.01.2011
; tnx to Atalan the snake & rudla :)

;_TEMPB1 equ 54  array offset
;_TEMPB2 equ 55  array offset

;  ATALAN code	 
;  PPU_DATA_ADR = $3F
;  PPU_DATA_ADR = $10
;  for i:0..15
;        PPU_DATA  = spritePal(i)
			    
sprLoadPal .proc
			sta _TEMPADR
			stx _TEMPADR+1
			ldx #$10
			ldy #$00
			lda #$3F
            sta $2006
			stx $2006
cpyloop_sr: lda (_TEMPADR),y
			sta $2007
			iny
			cpy #$10
			bne cpyloop_sr 
			rts
			.endp


;5060
setMultiSprite_1 .proc
		    sta _TEMPADR     ; save spr array adr to zerro page
			stx _TEMPADR+1   ; 
		
			lda _SPR_NR
			asl
			tay

			lda  (_TEMPADR),y  
			sta _TEMPADRSPR
			iny
			
			lda  (_TEMPADR),y  
			sta _TEMPADRSPR+1
		
			lda _SPR_ANIM_NR
			asl
			tay

			lda  (_TEMPADRSPR),y  
			sta _TEMPADR
			iny
			
			lda  (_TEMPADRSPR),y  
			sta _TEMPADR+1
		

			ldy #$00		 ; set y pointer = 0
			ldx _OAM_INDEX   ; load back oam index of last write
		
			; put x & y size to loop var's
			lda  (_TEMPADR),y  
			sta _SPR_YSIZE
			sta _SPR_Y
			iny
			lda  (_TEMPADR),y  
			sta _SPR_XSIZE
			iny	
copy_row:	lda _SPR_XPOS
			sta _SPR_TMP_XPOS

			lda _SPR_XSIZE
			sta _SPR_X
			
copy_tile:
								  ; copy a tile to ram
			lda  (_TEMPADR),y     ; get byte out of sprite array
			beq notCopy
								  ; copy data to OAM
			sta $0301 , x		  ; store tile nr
		
			lda _SPR_YPOS			  
			sta $0300 , x		  ; y pos
		
			iny
		
			lda  (_TEMPADR),y     ; get attribute byte 
			sta $0302 , x		  ; store attribute byte 
		
			lda  _SPR_TMP_XPOS; 
			sta $0303 , x		  ; store x pos
			
			inx 
			inx 
			inx 
			inx 
notCopy:
			dec	_SPR_X				  ; xsize = xsize -1
			iny
	
			lda	_SPR_TMP_XPOS
			clc					  ; shift xpos + 8
			adc #$08
			sta _SPR_TMP_XPOS
					
			lda _SPR_X		  ; check if we need to copy more row tiles
			bne copy_tile

			dec	_SPR_Y		  ; ysize = ysize -1
			beq copy_end
								  ; row complete, now ypos +8 and set back startx
								  ; ypos + 8
			lda	_SPR_YPOS
			clc	
			adc #$08
			sta _SPR_YPOS
			jmp copy_row
copy_end:			
								  ; save x pointer to _OAM_INDEX
			txa
			sta	_OAM_INDEX	
			rts
			.endp



















setMultiSprite .proc
		    sta _TEMPADR     ; save spr array adr to zerro page
			stx _TEMPADR+1   ; 
		
			lda _SPR_NR
			asl
			tay

			lda  (_TEMPADR),y  
			sta _TEMPADRSPR
			iny
			
			lda  (_TEMPADR),y  
			sta _TEMPADRSPR+1
		
			lda _SPR_ANIM_NR
			asl
			tay

			lda  (_TEMPADRSPR),y  
			sta _TEMPADR
			iny
			
			lda  (_TEMPADRSPR),y  
			sta _TEMPADR+1
		

			ldy #$00		 ; set y pointer = 0
			ldx _OAM_INDEX   ; load back oam index of last write
		
			; put x & y size to loop var's
			lda  (_TEMPADR),y  
			sta _SPR_YSIZE
			sta _SPR_Y
			iny
			lda  (_TEMPADR),y  
			sta _SPR_XSIZE
			iny	
copy_row:	lda _SPR_XPOS
			sta _SPR_TMP_XPOS

			lda _SPR_XSIZE
			sta _SPR_X
			
copy_tile:
								  ; copy a tile to ram
			lda  (_TEMPADR),y     ; get byte out of sprite array
			beq notCopy
								  ; copy data to OAM
			sta $0301 , x		  ; store tile nr
		
			lda _SPR_YPOS			  
			sta $0300 , x		  ; y pos
		
			iny
		
			lda  (_TEMPADR),y     ; get attribute byte 
			sta $0302 , x		  ; store attribute byte 
		
			lda  _SPR_TMP_XPOS; 
			sta $0303 , x		  ; store x pos
			
			inx 
			inx 
			inx 
			inx 
notCopy:
			dec	_SPR_X				  ; xsize = xsize -1
			iny
	
			lda	_SPR_TMP_XPOS
			clc					  ; shift xpos + 8
			adc #$08
			sta _SPR_TMP_XPOS
					
			lda _SPR_X		  ; check if we need to copy more row tiles
			bne copy_tile

			dec	_SPR_Y		  ; ysize = ysize -1
			beq copy_end
								  ; row complete, now ypos +8 and set back startx
								  ; ypos + 8
			lda	_SPR_YPOS
			clc	
			adc #$08
			sta _SPR_YPOS
			jmp copy_row
copy_end:			
								  ; save x pointer to _OAM_INDEX
			txa
			sta	_OAM_INDEX	
			rts
			.endp


resetUnusedSprites .proc
			ldx _OAM_INDEX
	fill:	lda #$FF 
			sta $0300 , x		  ; y pos
			inx 
			inx 
			inx 
			inx
			cpx _LAST_FRAME_OAM_INDEX
			bne fill 
			rts
			.endp