;credits scroll text code

.Credits_Scroll
	ld A,(credit_scroll_mode)
	cp #FF
	jr z,Credits_ScrollReverse	
	
	call CreditEraseCurrentLine

	ld A,(credit_scroll_offset)
	or A
	jp z,Credits_ScrollDo
	cp 4
	jp z,Credits_ScrollDo
	
	halt
	ret
.Credits_ScrollReverse

	call CreditEraseCurrentLineReverse

	ld A,(credit_scroll_offset)
	cp 7
	jp z,Credits_ScrollDoReverse
	cp 3
	jp z,Credits_ScrollDoReverse
	
	halt
	ret
	
;control chars used to draw graphics
	
.Credits_ScrollDo_ControlChar
	cp 9
	jr nz,Credits_ScrollDo_ControlNotStop
	xor A
	ld (credit_scroll_mode),A
	halt
	ret
	
.Credits_ScrollDo_ControlNotStop	
	ld HL,Credits_ee0
	cp 2
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee1
	cp 3
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee2
	cp 4
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee3
	cp 5
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee4
	cp 6
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee5
	cp 7
	jr z,Credit_ScrollDo_DrawLogo
	ld HL,Credits_ee6

.Credit_ScrollDo_DrawLogo

	push HL
;compute screen address
	call CreditComputeScreenAddress
	ld A,24
	add L
	ld L,A
	ex DE,HL
	
	pop HL
	
	ld B,4
.Credit_ScrollDo_DrawLogo_loop
	push DE
	push BC
	ld BC,16
	ldir
	pop BC
	pop DE
	ld A,8
	add D
	ld D,A
	djnz Credit_ScrollDo_DrawLogo_loop


	halt
	ld A,(Credits_Scroll_CurrentScrOffset)
	xor 32
	ld (Credits_Scroll_CurrentScrOffset),A
	ret
.Credits_ScrollDoReverse
	ld HL,(Credits_Scroll_Current_ptr)
	ld (Credits_Scroll_Old_ptr),HL
	dec HL
	ld D,(HL)
	dec HL
	ld E,(HL)
	jr Credit_ScrollDo_Normal
	
.Credits_ScrollDo
	ld A,(credit_scroll_mode)
	or A
	jr nz,Credits_ScrollDoNotStopped
	halt
	ret
	
.Credits_ScrollDoNotStopped
;next half line

	ld HL,(Credits_Scroll_Current_ptr)
	ld (Credits_Scroll_Old_ptr),HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL

.Credit_ScrollDo_Normal
	
	;ld A,D
	;or A
	;jump here to stop scroll
	
	ld (Credits_Scroll_Current_ptr),HL

	ex DE,HL
	ld (Credit_Scroll_Current_String),HL
	
	;ld A,(HL)
	;or A
	;jump here to fill a half char line
	
	push HL
	call CreditComputeScreenAddress
	
	;call CreditEraseHalfLine
	pop DE
	ld A,(DE)
	or A
	jr nz,Credits_ScrollDo_NotEmptyLine
	
	halt
	
	ld A,(Credits_Scroll_CurrentScrOffset)
	xor 32
	ld (Credits_Scroll_CurrentScrOffset),A
	ret
	
.Credits_ScrollDo_NotEmptyLine	
	cp 32
	jp c,Credits_ScrollDo_ControlChar


	call CreditComputeStringCentering
;break	
	halt
	call CreditDrawHalfLine
	
	;ld HL,(Credit_Scroll_Current_String)
	;or A
	;ret z
	
	ld A,(Credits_Scroll_CurrentScrOffset)
	xor 32
	ld (Credits_Scroll_CurrentScrOffset),A
	
	ld A,(Credit_Scroll_CurrentPart)
	or A
	jr z,Credits_Scroll_PrepareNextHalf
	
	xor A
	ld (Credit_Scroll_CurrentPart),A
	ld (Credits_Scroll_CurrentFontOffset),A
	;ld A,(Credits_Scroll_CurrentFontOffset)
	;xor 8
	ld (Credits_Scroll_CurrentFontOffset),A

	ret
.Credits_Scroll_PrepareNextHalf
	inc A
	ld (Credit_Scroll_CurrentPart),A
	ld A,(Credits_Scroll_CurrentFontOffset)
	xor 8
	ld (Credits_Scroll_CurrentFontOffset),A
	
	;ld HL,(Credits_Scroll_Current_ptr)
	;dec HL
	;dec HL
	ld HL,(Credits_Scroll_Old_ptr)
	ld (Credits_Scroll_Current_ptr),HL
	ret

;clear last raster line
.CreditEraseCurrentLineReverse
	ld HL,(credit_reg12_offset)
	add HL,HL	;CRTC chars so *2 for bytes
	ld DE,30*64	;31 line lower !
	add HL,DE

	ld A,(credit_scroll_offset)
	add A
	add A
	add A
	ld C,A
	jr CreditEraseCurrentLineEntry


.CreditEraseCurrentLine
	ld HL,(credit_reg12_offset)
	add HL,HL	;CRTC chars so *2 for bytes
	;ld DE,32*64	;31 line lower !
	;add HL,DE

	ld A,(credit_scroll_offset)
	add A
	add A
	add A
	ld C,A
	
.CreditEraseCurrentLineEntry
	ld A,H
	and %00000111							;cycling
	or #C0
	or C
	ld H,A


	xor A
repeat 64
	ld (HL),A
	inc L
rend

	ret	
	
	
;draw halt part of the string
.CreditDrawHalfLine

	ex DE,HL

	ld HL,(Credit_Scroll_Current_String)
.CreditDrawHalfLineLoop
	ld A,(HL)
	or A
	ret z
;control char 1 , half char	
	dec A
	jr nz,CreditDrawHalfLineLoopNotHalfChar
	
	dec DE	;skip one byte / half char
	inc HL	;next char
	jr CreditDrawHalfLineLoop
.CreditDrawHalfLineLoopNotHalfChar
	
	sub 31
	jr nz,CreditDrawHalfLineLoopNotSpace
	inc DE
	inc HL
	jr CreditDrawHalfLineLoop
	
.CreditDrawHalfLineLoopNotSpace		
	push HL
	push DE
	ld L,A
	ld H,0
	add HL,HL	;2
	add HL,HL	;4
	add HL,HL	;8
	add HL,HL	;16
	ld A,(Credits_Scroll_CurrentFontOffset)
	add L
	ld L,A
	ld BC,Credits_Scroll_Font
	add HL,BC
	
	ld C,8
	
	;now HL points on half part of the char and DE on screen
;*** line 1 ***
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A
	inc L
	dec E	
	;next line
	ld A,C
	add D
	ld D,A
;*** line 2 ***
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A
	inc L
	dec E	
	;next line
	ld A,C
	add D
	ld D,A
;*** line 3 ***	
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A
	inc L
	dec E	
	;next line
	ld A,C
	add D
	ld D,A
;*** line 4 ***
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A


	pop DE
	inc E	;next char on screen
	inc E
	pop HL
	inc HL	;next char on string
	
	jr CreditDrawHalfLineLoop
	
;compute offset for string centering , return in B
.CreditComputeStringCentering
	push HL
	call CreditComputeStringGetLen
	pop HL
	
	ld A,B
	ld (Credits_Scroll_CharCount),A
	
	;pos = 32-(len/2)
	srl B
	ld A,32
	sub B
	
	;add to current address
	add L
	ld L,A
	
	ret

.CreditComputeStringGetLen
	ld HL,(Credit_Scroll_Current_String)
	ld B,0				;B
.CreditComputeStringCenteringLoop
	ld A,(HL)
	or A
	ret z
	inc HL
;control char 1 , increment only 1 byte
	cp 1
	jr nz,CreditComputeStringCenteringLoop_NotHalfChar
	dec B
	jr CreditComputeStringCenteringLoop
.CreditComputeStringCenteringLoop_NotHalfChar
	cp 32
	jr z,CreditComputeStringCentering_space	;space is half char
	jr c,CreditComputeStringCenteringLoop
	inc B
.CreditComputeStringCentering_space
	inc B
	jr CreditComputeStringCenteringLoop
	
.CreditComputeScreenAddress
	ld HL,(credit_reg12_offset)
	add HL,HL	;CRTC chars so *2 for bytes
	ld DE,31*64	;31 line lower !
	add HL,DE
	
	ld A,H
	and %00000111							;cycling
	ld H,A

	ld A,(Credits_Scroll_CurrentScrOffset)	;offset for half line
	or H
	or #C0									;base address
	ld H,A
	
	;ld (HL),#FF
	;push HL
	;ld A,63
	;add L
	;ld L,A
	;ld (HL),#FF
	;pop HL
	ret
.Credit_Scroll_CurrentPart					;current part of the line
	db 0
.Credit_Scroll_Current_String
	dw 0
.Credits_Scroll_Current_ptr
	dw scroll_text_list ;reverse_scroll_tst ;
.Credits_Scroll_Old_ptr
	dw 0
.Credits_Scroll_CurrentScrOffset		;screen offset	(4*8) or 0
	db 0
.Credits_Scroll_CurrentFontOffset		;offset in font graphics 8 or 0
	db 0
.Credits_Scroll_CharCount
	db 0