#Macro GetInput()
	SDL_PumpEvents
	If SDL_PollEvent(@event) Then
		Select Case As Const event.type
			Case SDL_QUIT_
				cpurunning = 0
			Case SDL_KEYDOWN
				For checkkey = 0 To 7
					If keymap1(checkkey) = event.key.keysym.sym Then
						joypad1(checkkey) = 1
					End If
				Next checkkey
			Case SDL_KEYUP
				For checkkey = 0 To 7
					If keymap1(checkkey) = event.key.keysym.sym Then
						joypad1(checkkey) = 0
					End If
				Next checkkey
		End Select
	End If
#EndMacro

Sub SDLrender()
	SDL_LockSurface (video)
	For drawy = 0 To 479
		For drawx = 0 To 511
			sdlpixel = video->pixels + drawy * video->pitch + drawx * len( integer )
			*sdlpixel = paletteNES(outputNES(drawy Shr 1, drawx Shr 1))
		Next drawx
	Next drawy
	SDL_UnlockSurface (video)
	SDL_Flip video
End Sub

Sub SetupKeys()
	Dim keynames(7) As String => { "A", "B", "select", "start", "up", "down", "left", "right" }
	Dim cfgevent as SDL_Event
	TextDraw(0, 0, Space$(960))
	realpos = 0
	'Dim ctrlpix As UByte
	'Open "controller.raw" For Binary As #1
	'Open "controller.txt" For Output As #2
	'Print #2, "Dim Shared controlpic(25343) as UByte => {_"
	For dry = 141 To 239
		For drx = 0 To 255
			'Get #1, , ctrlpix
			outputNES(dry, drx) = controlpic(realpos)
			'Print #2, " "+Str$(ctrlpix)+",";
			'If (realpos Mod 32) = 31 Then
			'	Print #2, "_"+Chr$(13)+Chr$(10)
			'End If
			realpos = realpos + 1
		Next drx
	Next dry
	'Print #2, "}"
	'Close #1, #2
	TextDraw(0, 0, "Player 1 controller configure")
	For n = 0 To 7
		TextDraw(0, 2+n, "Press new key for " + keynames(n))
		SDLrender()
		Do
			SDL_PumpEvents
			If SDL_PollEvent(@cfgevent) Then
				Select Case As Const cfgevent.type
					Case SDL_QUIT_
						SDL_Quit()
						End
					Case SDL_KEYDOWN
						keymap1(n) = cfgevent.key.keysym.sym
						Exit Do
				End Select
			End If
			#Ifdef __FB_WIN32__
				SwitchToThread()
			#EndIf
		Loop
	Next n
	'Print "Saving controller configuration to keymap.cfg...";
	Open "keymap.cfg" For Binary As #1
	For n = 0 To 7
		Put #1, , keymap1(n)
	Next n
	Close #1
	'Print "OK!"
	'Print
End Sub
