;**** original functions on sprite management****

;get sprite functions
.TSPGADD		;get a level sprite	,bank C6
if debug
	;cp 53
	;jr c,TSPGADD_OK
	;ld A,1
	;break
endif
.TSPGADD_OK
	push BC
	ld C,#C6
	jr USPGADD1
.USPGADD		;get a common sprite , bank C0
	ld (SPRITE),A	; saved in globals
	push BC
	ld C,#C0
.USPGADD1
	ld B,#7F
	out (C),C
	
	;dec A	originaly that was need as sprites start at 1 , now sprite 0 is bankID
	ld L,A
	ld H,0
	add HL,HL	;*2
	add HL,HL	;*4
	set 6,H		;add #4000
	
	ld D,(HL)	;must E/D reversed ?
	inc HL
	ld E,(HL)
	inc HL
	ld A,(HL)
	inc HL
	ld H,(HL)
	ld L,A
	pop BC
;if debug
;	ld A,D
;	or E
;	ret nz
;	break
;endif
	ret
	
;************
;*** SPNS ***
;************
;put a sprite on screen buffer
;HL = sprite
;DE = W,H
;BC = X,Y

.SPNS
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPNS_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPNS_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPNS_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPNS_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPNS_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPNS_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPNS_Clip
	inc B
	exx
	
	ld A,(HL)
	
	rra		;if target not empty , skip
	jr c,SPNS_empty
	
	ld A,(DE)
	or A
	jr z,SPNS_empty
	
	inc DE
	ld (HL),A
	
	ld A,H
	ld (SPNS_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;master
	ld A,(MASTER)
	ld (HL),A
	
SPNS_saveH EQU $+1
	ld H,0
	inc L
	
.SPNS_Done
	djnz SPNS_xloop
	


	exx
	pop BC
.SPNS_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPNS_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPNS_yloop
	
	pop IX
	ret

.SPNS_Clip
	inc B
	exx
.SPNS_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPNS_Done
	
	
;************
;*** SPMS ***
;************
;put a sprite on screen buffer
;reversed on X axis
;HL = sprite
;DE = W,H
;BC = X,Y

.SPMS
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	;add W to X
	ld A,B
	add D
	dec A
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPMS_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPMS_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPMS_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPMS_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPMS_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPMS_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPMS_Clip
	dec B
	exx
	
	ld A,(HL)
	
	rra		;if target not empty , skip
	jr c,SPMS_empty
	
	ld A,(DE)
	or A
	jr z,SPMS_empty
	
	inc DE
	or 8	;xflip
	ld (HL),A
	
	ld A,H
	ld (SPMS_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPMS_saveH EQU $+1
	ld H,0
	dec L
	
.SPMS_Done
	djnz SPMS_xloop
	


	exx
	pop BC
.SPMS_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPMS_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPMS_yloop
	
	pop IX
	
	ret

.SPMS_Clip
	dec B
	exx
.SPMS_Empty
	inc DE
	inc DE
	inc DE
	dec L
	jr SPMS_Done
	
;************
;*** SPFS ***
;************
;put a sprite on screen buffer
;reversed on Y axis
;HL = sprite
;DE = W,H
;BC = X,Y

.SPFS
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
;add H to Y
	ld A,C
	add E
	dec A
	ld C,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPFS_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPFS_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPFS_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPFS_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPFS_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPFS_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPFS_Clip
	inc B
	exx
	
	ld A,(HL)
	
	rra		;if target not empty , skip
	jr c,SPFS_empty
	
	ld A,(DE)
	or A
	jr z,SPFS_empty
	
	inc DE
	or 16 ;Y flip
	ld (HL),A
	
	ld A,H
	ld (SPFS_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPFS_saveH EQU $+1
	ld H,0
	inc L
	
.SPFS_Done
	djnz SPFS_xloop

	exx
	pop BC
.SPFS_skipLine
	dec C
	exx
	
;add sprite width to HL
	pop HL
SPFS_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPFS_yloop
	
	pop IX
	
	ret

.SPFS_Clip
	inc B
	exx
.SPFS_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPFS_Done
	
;************
;*** SPBS ***
;************
;X and Y axis reversed
;put a sprite on screen buffer
;HL = sprite
;DE = W,H
;BC = X,Y

.SPBS
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A

;add W to X and H to Y
	ld A,B
	add D
	dec A
	ld B,A
	ld A,C
	add E
	dec A
	ld C,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPBS_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPBS_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPBS_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPBS_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPBS_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPBS_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPBS_Clip
	dec B
	exx
	
	ld A,(HL)
	
	rra		;if target not empty , skip
	jr c,SPBS_empty
	
	ld A,(DE)
	or A
	jr z,SPBS_empty
	
	or 24;x/Y flip
	inc DE
	ld (HL),A
	
	ld A,H
	ld (SPBS_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPBS_saveH EQU $+1
	ld H,0
	dec L
	
.SPBS_Done
	djnz SPBS_xloop
	


	exx
	pop BC
.SPBS_skipLine
	dec C
	exx
	
;add sprite width to HL
	pop HL
SPBS_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPBS_yloop
	
	pop IX
	
	ret

.SPBS_Clip
	dec B
	exx
.SPBS_Empty
	inc DE
	inc DE
	inc DE
	dec L
	jr SPBS_Done

;*******************
;*** SPNS_Static ***
;*******************
;put a static sprite on screen buffer
;HL = sprite
;DE = W,H
;BC = X,Y

.SPNS_Static
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPNS_Static_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPNS_Static_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPNS_Static_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPNS_Static_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPNS_Static_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPNS_Static_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPNS_Static_Clip
	inc B
	exx
	
	ld A,(HL)
	
	and 3 ;check if it is a block
	cp 3
	jr z,SPNS_Static_empty
	
	ld A,(DE)
	or A
	jr z,SPNS_Static_CheckOld
	
	inc DE
	ld (HL),5+128 ;magic static flag 
	
	ld A,H
	ld (SPNS_Static_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPNS_Static_saveH EQU $+1
	ld H,0
	inc L
	
.SPNS_Static_Done
	djnz SPNS_Static_xloop
	


	exx
	pop BC
.SPNS_Static_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPNS_Static_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPNS_Static_yloop
	
	pop IX
	ret
	
.SPNS_Static_CheckOld ;check if we have an old block 13 here
	ld A,(HL)
	and 63
	cp 13
	jr nz,SPNS_Static_Empty
	ld (HL),4 ;so auto erase !
	jr SPNS_Static_Empty	

.SPNS_Static_Clip
	inc B
	exx
.SPNS_Static_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPNS_Static_Done
	
	
;*********************
;*** SPNS_CallBack  ***
;*********************
;put a sprite on screen buffer
;HL = sprite
;DE = W,H
;BC = X,Y

.SPNS_CallBack
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPNS_CallBack_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPNS_CallBack_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPNS_CallBack_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPNS_CallBack_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPNS_CallBack_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPNS_CallBack_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPNS_CallBack_Clip
	inc B
	exx
	
	ld A,(DE)	;check before is sprite is not empty
	or A
	jr z,SPNS_CallBack_empty

	ld A,(HL)	
	and %10000001
SPNS_CallBack_CallBack EQU $+1
	call nz,SPNS_CallBack_NullCallBack
	rra		;if target not empty , skip
	jr c,SPNS_CallBack_empty
	
	ld A,(DE)
	inc DE
	ld (HL),A
	
	ld A,H
	ld (SPNS_CallBack_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;master
	ld A,(MASTER)
	ld (HL),A
	
SPNS_CallBack_saveH EQU $+1
	ld H,0
	inc L
	
.SPNS_CallBack_Done
	djnz SPNS_CallBack_xloop
	


	exx
	pop BC
.SPNS_CallBack_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPNS_CallBack_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPNS_CallBack_yloop
	
	pop IX
.SPNS_CallBack_NullCallBack
	ret

.SPNS_CallBack_Clip
	inc B
	exx
.SPNS_CallBack_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPNS_CallBack_Done
	
;*********************
;*** SPMS_CallBack ***
;*********************
;put a sprite on screen buffer
;reversed on X axis
;HL = sprite
;DE = W,H
;BC = X,Y

.SPMS_CallBack
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	;add W to X
	ld A,B
	add D
	dec A
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPMS_CallBack_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPMS_CallBack_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPMS_CallBack_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPMS_CallBack_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPMS_CallBack_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPMS_CallBack_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPMS_CallBack_Clip
	dec B
	exx
	
	ld A,(DE)
	or A
	jr z,SPMS_CallBack_empty
	
	ld A,(HL)
	and %10000001
SPMS_CallBack_CallBack EQU $+1
	call nz,SPMS_NullCallBack
	
	rra		;if target not empty , skip
	jr c,SPMS_CallBack_empty

	ld A,(DE)	
	inc DE
	or 8	;xflip
	ld (HL),A
	
	ld A,H
	ld (SPMS_CallBack_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPMS_CallBack_saveH EQU $+1
	ld H,0
	dec L
	
.SPMS_CallBack_Done
	djnz SPMS_CallBack_xloop
	


	exx
	pop BC
.SPMS_CallBack_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPMS_CallBack_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPMS_CallBack_yloop
	
	pop IX
.SPMS_NullCallBack	
	ret

.SPMS_CallBack_Clip
	dec B
	exx
.SPMS_CallBack_Empty
	inc DE
	inc DE
	inc DE
	dec L
	jr SPMS_CallBack_Done

;********************
;*** SPFS_CallBack ***
;********************
;put a sprite on screen buffer
;reversed on Y axis
;HL = sprite
;DE = W,H
;BC = X,Y

.SPFS_CallBack
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
;add H to Y
	ld A,C
	add E
	dec A
	ld C,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPFS_CallBack_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPFS_CallBack_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPFS_CallBack_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPFS_CallBack_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPFS_CallBack_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPFS_CallBack_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPFS_CallBack_Clip
	inc B
	exx
	
	ld A,(DE)
	or A
	jr z,SPFS_CallBack_empty
	
	ld A,(HL)
	and %10000001
SPFS_CallBack_CallBack EQU $+1
	call nz,SPFS_CallBack_NullCallback
	
	rra		;if target not empty , skip
	jr c,SPFS_CallBack_empty

	ld A,(DE)	
	inc DE
	or 16 ;Y flip
	ld (HL),A
	
	ld A,H
	ld (SPFS_CallBack_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPFS_CallBack_saveH EQU $+1
	ld H,0
	inc L
	
.SPFS_CallBack_Done
	djnz SPFS_CallBack_xloop

	exx
	pop BC
.SPFS_CallBack_skipLine
	dec C
	exx
	
;add sprite width to HL
	pop HL
SPFS_CallBack_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPFS_CallBack_yloop
	
	pop IX
.SPFS_CallBack_NullCallback	
	ret

.SPFS_CallBack_Clip
	inc B
	exx
.SPFS_CallBack_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPFS_CallBack_Done
	
;********************
;*** SPBS_CallBack ***
;********************
;X and Y axis reversed
;put a sprite on screen buffer
;HL = sprite
;DE = W,H
;BC = X,Y

.SPBS_CallBack
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A

;add W to X and H to Y
	ld A,B
	add D
	dec A
	ld B,A
	ld A,C
	add E
	dec A
	ld C,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPBS_CallBack_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPBS_CallBack_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPBS_CallBack_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPBS_CallBack_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPBS_CallBack_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPBS_CallBack_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPBS_CallBack_Clip
	dec B
	exx
	
	ld A,(DE)
	or A
	jr z,SPBS_CallBack_empty
	
	ld A,(HL)
	and %10000001
SPBS_CallBack_CallBack EQU $+1
	call nz,SPBS_CallBack_NullCallBack
	
	rra		;if target not empty , skip
	jr c,SPBS_CallBack_empty

	ld A,(DE)	
	or 24;x/Y flip
	inc DE
	ld (HL),A
	
	ld A,H
	ld (SPBS_CallBack_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld A,(MASTER)
	ld (HL),A	
	
SPBS_CallBack_saveH EQU $+1
	ld H,0
	dec L
	
.SPBS_CallBack_Done
	djnz SPBS_CallBack_xloop
	


	exx
	pop BC
.SPBS_CallBack_skipLine
	dec C
	exx
	
;add sprite width to HL
	pop HL
SPBS_CallBack_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPBS_CallBack_yloop
	
	pop IX
.SPBS_CallBack_NullCallBack	
	ret

.SPBS_CallBack_Clip
	dec B
	exx
.SPBS_CallBack_Empty
	inc DE
	inc DE
	inc DE
	dec L
	jr SPBS_CallBack_Done
	
;****************
;*** SPNS_BG ***
;****************
;put a sprite on BG as block ,replace only BG
;HL = sprite
;DE = W,H
;BC = X,Y

.SPNS_BG
	ld A,(HL)
	or A
	ret z
	inc HL
	
	push IX
	
	;exchange x and Y
	ld A,C
	ld C,B
	ld B,A
	
	push DE
;save X,Y in BC' for clipping
	push BC
	exx
	pop BC
	exx
	
;save line larg
	ld A,D
	add A
	add D
	ld (SPNS_BG_saveWidth),A
	
;attr address base
	ld IXH,ATTR_Addr_high
;save original xpos	
	ld A,B
	ld (SPNS_BG_Xpos),A
	
	pop BC
	
	ex DE,HL
	
.SPNS_BG_yloop
	push BC
	push DE
	exx
	ld A,C
	cp 20
	jr nc,SPNS_BG_skipLine
	push BC
	exx
	;A contains Y pos
	add A	;*2
	add ATTR_Addr_low
	ld IXL,A
	ld L,(IX+0)
SPNS_BG_Xpos EQU $+1
	ld A,0
	add L
	ld L,A	
	ld H,(IX+1)	
	
.SPNS_BG_xloop
	exx
	ld A,B
	cp 32
	jr nc,SPNS_BG_Clip
	inc B
	exx
	
	;ld A,(HL)
	
	;rra		;if target not empty , skip
	;jr c,SPNS_BG_empty
	;and 3
	;cp 1
	;jr z,SPNS_BG_empty
	
	ld A,(DE)
	or A
	jr z,SPNS_BG_empty
	
	inc DE
	ld (HL),5+128 ;write a special static !
	
	ld A,H
	ld (SPNS_BG_saveH),A
	
	inc H
	
	ld A,(#4000)	;bank
	ld (HL),A
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;master
	ld A,(MASTER)
	ld (HL),A
	
SPNS_BG_saveH EQU $+1
	ld H,0
	inc L
	
.SPNS_BG_Done
	djnz SPNS_BG_xloop
	


	exx
	pop BC
.SPNS_BG_skipLine
	inc C
	exx
	
;add sprite width to HL
	pop HL
SPNS_BG_saveWidth EQU $+1
	ld BC,0
	add HL,BC
	ex DE,HL

	;inc IX
	;inc IX
	pop BC
	dec C

	jr nz,SPNS_BG_yloop
	
	pop IX
	ret

.SPNS_BG_Clip
	inc B
	exx
.SPNS_BG_Empty
	inc DE
	inc DE
	inc DE
	inc L
	jr SPNS_BG_Done
	
;**********************
;*** REND_EraseZone ***
;**********************
;Erase a D*E zone from C,B
;
.REND_EraseZone
;clip X first !
	ld A,C
	cp 32
	jr c,REND_EraseZoneXok

	ld A,D	;w=w-(+x)
	add C
	ret z		
	cp 32
	ret nc
	ld D,A
	ld C,0

.REND_EraseZoneXok

	ld A,C	;save X
	ld (REND_EraseZone_saveX),A
	ld IXH,ATTR_Addr_high
	
.REND_EraseZoneYloop
	ld A,B
	cp 20
	jr nc,REND_EraseZoneYloopSkip

	push DE
	push BC
	
;compute current address line
	ld A,B
	add A
	add ATTR_Addr_Low
	ld IXL,A
	ld A,(IX)
REND_EraseZone_saveX	EQU $+1
	add 0
	ld L,A
	ld H,(IX+1)

.REND_EraseZoneXloop
	ld A,C
	cp 32
	jr nc,REND_EraseZoneXloopSkip
	ld (HL),4	;auto erase this !
	inc L
	inc C
	dec D
	jr nz,REND_EraseZoneXloop
	
.REND_EraseZoneXloopSkip
	pop BC
	pop DE
.REND_EraseZoneYloopSkip	
	inc B
	dec E
	jr nz,REND_EraseZoneYloop
	
	ret


	
;***************************
;*** REND_SprSetCallBack ***
;***************************
;
;set callback for SP?S_CallBack functions
;HL = callback address
;	
.REND_SprSetCallBack
	ld (SPNS_CallBack_CallBack),HL
	ld (SPMS_CallBack_CallBack),HL
	ld (SPFS_CallBack_CallBack),HL
	ld (SPBS_CallBack_CallBack),HL	
	ret