// File: finish.agc
// Created: 20-08-14


function		Settings(env ref as tEnvironment)
	bg 		as integer
	i 		as integer
	id 		as integer	
	name	as string
	spos 	as integer[3,2]
	cpos 	as integer[2,2]	
	scursor as integer
	ccursor as integer	
	button	as integer
	scurr	as integer
	ccurr	as integer
	focus	as integer
	
	SetSpriteVisible(env.loading, 0)
	SetSpriteVisible(env.canvas, 0)
	SetSpriteVisible(env.place, 0)
	SetTextVisible(env.place, 0)	
	SetTextVisible(env.lap, 0)	
	
	
	spos.fromJSON("[[0, 26], [21, 26], [43, 26]]")
	cpos.fromJSON("[[19, 44], [43, 44]]")	
	
	scursor = CreateSprite(LoadImage("texture/scursor.png"))
	ccursor = CloneSprite(scursor)
	
	select env.scale
		case 256
			scurr = 0
		endcase
		case 512
			scurr = 1
		endcase
		case 1024
			scurr = 2
		endcase
	endselect		
	
	bg = CreateSprite(LoadImage("texture/settings.png"))
	SetSpriteDepth(bg, 90)
			
	SetGlobal3DDepth(50)	
		
	SetCursors(focus, scursor, ccursor, scurr, ccurr, spos, cpos)
		
	Transition(env)
		
	SetMusicVolumeOGG(env.theme, 10)
	
	do	
		button = AnyJoystickButton(FALSE)
		
		SetCursors(focus, scursor, ccursor, scurr, ccurr, spos, cpos)		
		
		if GetRawKeyPressed(KEY_UP) or button = JOYSTICK_DPAD_UP
			if focus > 0
				dec focus
				PlaySound(env.coin.sound)						
			endif
		endif
		
		if GetRawKeyPressed(KEY_DOWN) or button = JOYSTICK_DPAD_DOWN
			if focus < 1
				inc focus
				PlaySound(env.coin.sound)				
			endif
		endif
		
		if GetRawKeyPressed(KEY_RIGHT) or button = JOYSTICK_DPAD_RIGHT
			if focus = 0
				if scurr < 2
					PlaySound(env.coin.sound)					
					inc scurr
				endif
			else
				if ccurr < 2
					PlaySound(env.coin.sound)					
					inc ccurr
				endif				
			endif
		endif
		
		if GetRawKeyPressed(KEY_LEFT) or button = JOYSTICK_DPAD_LEFT
			if focus = 0
				if scurr > 0
					PlaySound(env.coin.sound)					
					dec scurr
				endif
			else
				if ccurr > 0
					PlaySound(env.coin.sound)
					dec ccurr
				endif				
			endif
		endif
		
		
		if GetRawKeyPressed(KEY_ESCAPE) or GetJoystickBack() or GetRawKeyPressed(KEY_ENTER) or GetRawKeyPressed(KEY_SPACE)
			gGameState = GAMESTATE_SPLASH
			exit
		endif

		if ( scurr = 0 and env.scale <> 256 ) or ( scurr = 1 and env.scale <> 512 ) or ( scurr = 2 and env.scale <> 1024 )
			ChangeResolution(scurr, env)
		endif


		LowRez(env)		
		sync()
	loop
	
	LowRez(env)		
	sync()

			
	PlaySound(env.coin.sound)
	
	DestroySprite(bg)
	DestroySprite(scursor)	
	DeleteSprite(ccursor)		
	SetMusicVolumeOGG(env.theme, 30)

endfunction


function SetCursors(focus as integer, scursor as integer, ccursor as integer, scurr as integer, ccurr as integer, spos as integer[][], cpos as integer[][])
	if focus = 0
		SetSpriteColor(ccursor, 0x33, 0, 0, 0xff)
		SetSpriteColor(scursor, 0xff, 0xff, 0xff, 0xff)			
	else
		SetSpriteColor(scursor, 0x33, 0, 0, 0xff)
		SetSpriteColor(ccursor, 0xff, 0xff, 0xff, 0xff)						
	endif
	
	SetSpritePosition(scursor, spos[scurr,0], spos[scurr,1])
	SetSpritePosition(ccursor, cpos[ccurr,0], cpos[ccurr,1])		
endfunction

function ChangeResolution(rez as integer, env ref as tEnvironment)
	select rez
		case 0
			env.scale = 256
		endcase
		case 1
			env.scale = 512
		endcase
		case 2
			env.scale = 1024
		endcase		
	endselect

 	SetWindowSize(env.scale, env.scale, 0)
 	SetVirtualResolution(env.scale, env.scale) 	
 	SetSpriteSize(env.canvas, env.scale,  env.scale)
 	sync()

endfunction