;*****************
;*** LEVEL 1 BG ***
;*****************
;BG L1 element
;0 = base sprite , if NULL element is dead
;1 = x , 2=y
.L1BG_Init

	xor A
	ld (BG_StopCommand),A
	ld A,(FRAME)
	inc A
	ld (BG_Counter),A
	
	ld B,12
	ld HL,BG_Buffer
	ld DE,3
	ld IX,L1BG_Table
	
.L1BG_InitLoop
	ld A,(IX)
	ld (HL),A
	inc L
	ld A,(IX+1)	;x
	;add 32		;to be out of screen for first run
	add 16
	ld (HL),A
	inc L
	ld A,(IX+2)
	ld (HL),A
	inc L
	add IX,DE
	djnz L1BG_InitLoop
	
	ret
;init table
.L1BG_Table	
;screen 1
	db 110,0,5
	db 118,0,15
	db 114,8,10
	db 122,16,5
	db 110,16,15
	db 114,24,10	
;screen 2
	db 118,32,5
	db 122,32,15
	db 110,32+8,10
	db 114,32+16,5
	db 110,32+16,15
	db 118,32+24,10
	
.L1BG_Stop
	ld A,1
	ld (BG_StopCommand),A
	ret
	
;****************
;*** DRAW BG ***
;****************
.L1BG_SpriteOffset
	db 0
.L1BG_Run
	ld A,(BG_Counter)
	inc A
	ld (BG_Counter),A
	
	and 15
	srl A
	srl A
	;and 7
	;srl A
	ld (L1BG_SpriteOffset),A

	ld BC,#0C03
	ld HL,BG_Buffer
	
.L1BG_RunLoop
	ld A,(HL)
	or A
	jr z,L1BG_RunLoop_EmptySlot
	
	push HL
	push BC
	
	inc L
	ld C,(HL)
	
	push AF
	
	ld A,(BG_Counter)
	and 15
	;and 7
	jr nz,L1BG_RunLoop_NoMove
	
	dec C
	ld A,C 
	cp -3
	
	jr nz,L1BG_RunLoop_ValidateMove
	
	ld A,(BG_StopCommand)
	or A
	jr nz,L1BG_RunLoop_Kill
	
	ld C,64
.L1BG_RunLoop_ValidateMove
	ld (HL),C
.L1BG_RunLoop_NoMove

	ld A,(L1BG_SpriteOffset)
	ld B,A
	
	pop AF
	add B
	
	inc L
	ld B,(HL)
	inc L
	
	call TSPGADD
	call SPNS	
	
	pop BC
	pop HL	
	
.L1BG_RunLoop_EmptySlot
	ld A,C
	add L
	ld L,A
	djnz L1BG_RunLoop
	ret
.L1BG_RunLoop_Kill
	pop AF
	pop BC
	pop HL
	ld (HL),0
	jr L1BG_RunLoop_EmptySlot
	
