;credits

.TITLE_Credits
;wait for end of screen
	;ld A,(int_counter)
	;cp 5
	;jr nz,TITLE_Credits

	ei
	
	call MUSIC_Stop

;force return directly to options	
	ld A,2
	ld (menu_force),A

;clear all
	call GFX_Cls
	
	change_mode 2
	
	
;change name is bonus still locked
	ld HL,credit_music_hidden
;chek rtype 2 first
	ld A,(level_list)
	bit 0,A	;is level 0 has been finished
	jr nz,credits_rtype2_hiddennot
	ld (hidden_rtype2_zikA),HL
	ld (hidden_rtype2_zikB),HL
;cybernoid level	
.credits_rtype2_hiddennot
	ld A,(level_list+1)
	bit 7,A
	jr nz,credits_cyber2_hiddennot
	ld (hidden_cyber2_zik),HL
.credits_cyber2_hiddennot

.credits_waitfor_crtcconfig	
	ld A,(int_counter)
	cp 5
	jr nz,credits_waitfor_crtcconfig
	
	ld HL,crtc_config_credits
	call GFX_SetCRTC
;32 char high split
	ld A,32
	call GFX_SendR6
;for debug , red border	
	;ld BC,#7F10
	;ld A,#4C	
	;out (C),C
	;out (C),A
	
;default colors
	ld HL,credits_pal
	call GFX_SetColors

;save interrupt vector	
	ld HL,(#38)
	ld (credits_int_restore),HL
;setup new one
	ld HL,#C9FB
	ld (#38),HL	
	
	;ranking music
	ld A,12
	call MUSIC_Play
	
	ei	;need ints to synchronise splits
	
	;ld HL,#C000
	;ld (HL),#FF

	;default values
	ld A,1 ;#FF ;1
	ld (credit_scroll_mode),A
	
	xor A
	ld (extra_phase),A
	ld (Credit_Scroll_CurrentPart),A
	ld (Credits_Scroll_CurrentScrOffset),A
	ld (Credits_Scroll_CurrentFontOffset),A
	
	ld HL,credit_reg12_offset
	ld (HL),A
	inc HL
	ld (HL),A
	
	ld HL,scroll_text_list
	ld (Credits_Scroll_Current_ptr),HL

	ld A,7
	ld (credit_scroll_offset),A

	
	ld HL,6000 ;= 2 minutes
	ld (extra_counter),HL
	
	

;break
	
.credit_scroll_loop
	call GFX_WaitVbl

	;halt
	;break
	
	repeat 16
	call credit_wait_line
	rend

	ld B,#BC
	ld DE,#07FF		;3	31		R7 = #FF , no VBL
	out (C),D		;4	35
	inc B			;1	36
	out (C),E		;4	40
	dec B			;1	41

;screen 1	
	ld DE,#0404-1
	out (C),D
	inc B
	out (C),E
	dec B

scroll_offset1 EQU $+1
	ld DE,#0500
	out (C),D
	inc B
	out (C),E
	dec B
	
	ld B,#7F

	repeat 23
	call credit_wait_line
	rend
	
	ds 51,0 ;now on border
	
	ld DE,#4454
	call credit_change_color
	ld DE,#4654
	call credit_change_color

	call credit_wait_line
	ld DE,#4B40
	call credit_change_color

	;repeat 8-3
	;call credit_wait_line
	;rend
	
	;break

	halt	;here we must be on 2nd int	
	
	;screen 2
	ld B,#BC
	
	ld DE,#0422-1
	out (C),D
	inc B
	out (C),E
	dec B

scroll_offset2 EQU $+1
	ld DE,#0500
	out (C),D
	inc B
	out (C),E
	dec B	
	
	ld BC,#BC07		;3	3	finish the job
	ld A,0			;2	5
	out (C),C		;4	9
	inc B			;1	10
	out (C),A		;4	14
	dec B			;1	15
	
	
	call CreditUpdateRegisters	

	halt
	
	;must fit in two halts !
	
	;ld BC,#7F10
	;ld A,#4C
	;out (C),C
	;out (C),A

	call Credits_Scroll
	
	;ld BC,#7F10
	;ld A,#54
	;out (C),C
	;out (C),A
	
	;ld A,(Credits_Scroll_CharCount)
	;cp 40
	;jr nc,Credit_Scroll_IsSlow
	

	halt
	
	call KEY_Update
	call KEY_UpdateState
	ld A,(keys+4)
	cp 1
	jp z,Credits_End
	
	call extra_stuff
	
	
	call MUSIC_PlayerEntry
	
	halt ;last halt , wait a bit and do the raster
	
	repeat 18+8-2
	call credit_wait_line
	rend
	
	ld B,#7F
	ds 28,0 ;synchro with right border
	;break
	
	;break
	
	;ld DE,#5354
	;call credit_change_color
	ld DE,#4654
	call credit_change_color
	call credit_wait_line
	ld DE,#4454
	call credit_change_color
	ld DE,#5454
	call credit_change_color
	
	
	;jp credit_scroll_loop

	
	jp credit_scroll_loop
.Credits_End
	;break
	
credits_int_restore	equ $+1
	ld HL,0
	ld (#38),HL
	
	;hide screen
	xor A
	call GFX_SendR6
	
	call GFX_Cls
	change_mode 1
	
	ld HL,crtc_config_options
	call GFX_SetCRTC
	
	ld HL,menu_int_table3
	ld (int_table),HL
	
	ld HL,default_pal
	call GFX_SetColors
	
	xor A
	ld (sound_test_zik),A
	
	jp GAME_Enter
	
.CreditUpdateRegisters
	ld A,(credit_scroll_mode)
	or A
	ret z
	
	cp #FF
	jr z,CreditUpdateRegistersReverse

	ld A,(credit_scroll_offset)
	inc A
	and 7
	call z,credit_scroll_char_down
	ld (credit_scroll_offset),A
	jr CreditUpdateRegistersDone
	
.CreditUpdateRegistersReverse
	ld A,(credit_scroll_offset)
	dec A
	and 7
	cp 7
	call z,credit_scroll_char_up
	ld (credit_scroll_offset),A
	
.CreditUpdateRegistersDone
	
	ld (scroll_offset2),A
	
	xor 7
	inc A
	ld (scroll_offset1),A

	ld HL,(credit_reg12_offset)
	ld A,%00110000
	or H
	ld H,A
	ld BC,#BC00+12
	out (C),C
	inc B
	out (C),H
	dec B
	inc C
	out (C),C
	inc B
	out (C),L
	
	ret
	
.credit_scroll_char_down
	push AF
	ld HL,(credit_reg12_offset)
	ld DE,32
	add HL,DE
	ld A,H
	and %00000011
	ld H,A
	ld (credit_reg12_offset),HL
	pop AF
	ret	
.credit_scroll_char_up
	push AF
	ld HL,(credit_reg12_offset)
	ld DE,-32
	add HL,DE
	ld A,H
	and %00000011
	ld H,A
	ld (credit_reg12_offset),HL
	pop AF
	ret	
	
;wait a complete scanline , 64 nops call inclued
.credit_wait_line
	ds 64-8,0
	ret
	
;change colors on 1 line , 64 nops , DE = color 1/2 ld DE,XX inclued
.credit_change_color
	ld BC,#7F01	;3	3
	out (C),C	;4	7
	out (C),D	;4	11
	inc C		;1	12
	out (C),C	;4	16
	out (C),E	;4	20
	

	ds 64-28-3		;add 8 for call/ret and 3 for ld DE,XX
	ret
	
.extra_stuff
	ld A,(credit_scroll_mode)
	or A
	ret nz
	
	ld A,(extra_phase)
	or A
	ret nz
	
	ld HL,(extra_counter)
	dec HL
	ld (extra_counter),HL
	
	ld A,L
	or H
	ret nz
	
	ld A,1
	ld (extra_phase),A
	ld (credit_scroll_mode),A
	
	ld A,32
	ld (Credits_Scroll_CurrentScrOffset),A
	ret
	
.extra_counter
	dw 0
.extra_phase
	db 0
	
.credit_scroll_mode
	db 0
	
.credit_reg12_offset
	dw 0
.credit_scroll_offset
	db 7
	
.crtc_config_credits
	db #01,#20,#02,#2A,4,38,5,0,#06,30,#07,38,#0C,#30,#0D,#00,9,7,#FF
.credits_pal
	db 0,0,0,6
	db #FF
	
	
read "credits_scroll_text.asm"
read "credits_scroll_text_code.asm"
;.Credits_BG1
	;incbin "gfx/bg2.raw"
	
align 16
.Credits_Scroll_Font	
	incbin "gfx/credits_font.pm2"
.Credits_ee0
	incbin "gfx/ee_line0.pm2"
.Credits_ee1
	incbin "gfx/ee_line1.pm2"
.Credits_ee2
	incbin "gfx/ee_line2.pm2"
.Credits_ee3
	incbin "gfx/ee_line3.pm2"
.Credits_ee4
	incbin "gfx/ee_line4.pm2"	
.Credits_ee5
	incbin "gfx/ee_line5.pm2"
.Credits_ee6
	incbin "gfx/ee_line6.pm2"