.GAME_Run	;enter game here !

;init extra life
	call XLIFE_Init


	call GAME_InitStart	;init game here
	call GAME_Init_CheckCheats	;cheats ?
	call GAME_Init_CheckCasual	;casual mode ?

	;ld A,1
	;ld (CASUAL_RespawnWeapon),A
	;ld (CASUAL_RespawnImmediate),A
		
	;save stack to return here after
	ld (Stack_Save),SP

.GAME_StartLevel
	;init SFX
	call SND_SfxStart

	call GFX_HideScreen
	;break
	
	;be sure to restore hud on level start
	xor A
	ld (no_hud),A
	
	di
	call FS_Enter
	;load level here
	call GAME_InitLoadLevel
	call_indirect lvl_init
	call FS_Exit
	
	IF save_snap
		break
	ENDIF
	
	;call GFX_HideScreen

	call GFX_Cls
	
	ld HL,game_pal ;default_pal
	call GFX_SetColors
	
	ld HL,crtc_config_game ;crtc_config_options
	call GFX_SetCRTC
	
	;ld BC,#7F8C
	;out (C),C
	;change_mode 0



	call BG_Init	;re init BG man

	
.GAME_Restart	;restart here
	call SND_SfxStopAll ;stop all SFX (beam bug)
	call GFX_HideScreen
	call GFX_Cls	;clear all
	;init current game
	call GAME_Init0
	call GAME_Init1
	call GAME_Init2
	call LASER_Init	;reset lasers
	call BRINGON
	
	;setup weapon sprites
	call GAME_InitWeapons

	call REND_Init
	
	call GAME_Init_CheckCheats
	

	
	;reload BG event
	ld DE,BG_Buffer
	ld HL,save_BG_Buffer
	ld BC,64
	ldir
		
	call KEY_Update
	
	;save BG color and set to black
	ld HL,BG_COLOR
	ld A,(HL)
	push AF
	ld (HL),#54
	
	ei
	
	;full mode to be sure to have music in GO/CONTINUE
	xor A
	call SND_SetMode
	
	call GFX_ShowScreen
	call GAME_IsOver ;is game over ?
	
	call GAME_WaitReady
	
	;adapt to selected mode 
	ld A,(option_menu_sound)
	call SND_SetMode
	
	call GAME_StartMusic
	
	;re-init lvl
	call_indirect lvl_reinit
	
	pop AF
	ld (BG_COLOR),A
	
	call GAME_Init_Inv
	
	;player immortality
	ld A,immort_duration
	ld (PLAYER_Immortal),A
	
.GAME_MainLoop

	;ld A,5
	;call SCORE_Add

;check immortality
	ld A,(PLAYER_Immortal)
	or A
	jr z,GAME_MainLoop_NotImmortal
	
	dec A
	ld (PLAYER_Immortal),A

.GAME_MainLoop_NotImmortal


	call BIG_EXPLO_Run ;destroy all , yeah !
	call SOFT_END_Run ;cleanup level
	call END_LEVEL	;show end level (win!))
	call PLAYER_End ;check for end game
	
.GAME_LoopRetFromImmediate

	call REND_DoScroll	;replace PUTTILE

	call THEM
	call GETSYNC
	
	call MMI
	call URSHIP
	
	call URBALL
	call PLASMA

	call JEW_Update
	
	;update blue laser
	ld HL,laserA
	call LAZER_Update	
	ld HL,laserB
	call LAZER_Update	
	ld HL,laserC
	call LAZER_Update
	;update circular laser
	call CIRC_Update
	call CIRC_Update
	
	;update fire
	call FIRE_Run	
	call LAZER_Draw
	call CIRC_Draw
	
	;wave
	call WAVE_Run
	
	;missiles
	call MOVEMISS
	

	
	;call SHOOT	

	;call MOVEBULLS obsolete
	call BULLETS_Move	;new version
	
	
	call ALIENATX
	
	call EXPLODE
	
BG_Effect_Addr	EQU $+1
	call BG_EffectNull

;.GAME_MainLoopWaitSync
	
	;ld B,#F5
	;in A,(C)
	;rra
	;jr nc,GAME_MainLoopWaitSync
	
	;ld A,(int_counter)
	;cp 1
	;jr nz,GAME_MainLoopWaitSync

	call GAME_LoopManageFade ;manage fade before rendering !
	
	call REND_Udpate
	call SCORE_Update
	
	;ld A,(TILENO)
	;ld BC,tile_no_digit
	;call FONT_GetHexFromByte
	
	;ld A,(TILESCROLL)
	;ld BC,tile_scr_digit
	;call FONT_GetHexFromByte
	
	;LD A,(TILEPOS)
	;ld BC,tile_pos_digit
	;call FONT_GetHexFromByte
	
	;ld A,(PLASPOWER)
	;ld BC,plascounter_digit+3
	;call FONT_GetHexFromByte
	
	;ld A,(PLASCOUNT)
	;ld BC,plascounter_digit
	;call FONT_GetHexFromByte
	
	;ld A,(THEM_Count)
	;ld BC,dhem_digit
	;call FONT_GetHexFromByte
	
	;ld DE,#C5C0
	;ld HL,dhem
	;ld BC,color_table_options_normal
	;call FONT_DrawStringEX	



	call GAME_Loop_Manage_Music

IF perf_cap	
	;cap speed
	call GAME_ManagePerfomance
ENDIF

IF perf_show
	;show speed
	ld BC,perf_count_digit1	
	ld A,(frame)
	and 1
	jr z,perf_count_show_pair
	ld BC,perf_count_digit2
.perf_count_show_pair
	ld A,(perf_counter)
	call FONT_GetHexFromByte
	
	ld DE,#C600-#100
	ld HL,perf_count_str
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
ENDIF

	call GAME_CheckPause
	
	xor A
	ld (perf_counter),A
	
	jp GAME_MainLoop
	
IF perf_show	

.perf_count_str
	db "P "
.perf_count_digit1
	db "00 / "
.perf_count_digit2
	str "00 "
	
ENDIF
	
.GAME_ManagePerfomance
	ld A,(FRAME)
	and 1
	jr z,GAME_ManagePerfomance_frame0

.GAME_ManagePerfomanceSync	
frame0_perf EQU $+1
	ld B,0
	ld A,(perf_counter)
	add B
	
	cp 36		;minimal frame rate 36 = 16.xfps like Spectrum
				;50/((frame0+frame1)/12)
	jr c,GAME_ManagePerfomanceSync	

.GAME_ManagePerfomance_done

	ret
.GAME_ManagePerfomance_frame0
	ld A,(perf_counter)
	ld (frame0_perf),A
	jr GAME_ManagePerfomance_done

	
.GAME_Loop_Manage_Music

	ld A,(FRAME)	;half speed
	and 1
	ret z

	ld A,(LVL_NextMusik)
	or A
	ret z
	
	ld A,(LVL_FadeVolume)
	push AF
	call MUSIC_SetVolume
	
	pop AF
	inc A
	ld (LVL_FadeVolume),A
	
	cp 17
	ret c

	ld A,(LVL_NextMusik)
music_alt	EQU $+1
	ld HL,0				; in case of music = #FE
	call MUSIC_Play

	xor A
	ld (LVL_NextMusik),A
	call MUSIC_SetVolume

	ret
	
.GAME_LoopManageFade

	ld A,(level_fade)
	cp #FF
	ret z
	
	and 3
	ld HL,lvl_pal	
	call GFX_SetupLevelPalette
	
	ld A,(FRAME)
	and 7
	ret nz
	
	ld HL,level_fade
	dec (HL)
	ret
	

	
