;Idea taken from http://js1k.com/demo/213
;Ported to AutoIt by UEZ 2010
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
#AutoIt3Wrapper_Run_After=del /f /q "Twister WinAPI_Obfuscated.au3"
#AutoIt3Wrapper_Run_After=upx.exe --ultra-brute "%out%"
;~ #AutoIt3Wrapper_Run_After=upx.exe --best "%out%"

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)
Local $hWnd, $hGraphic, $hBitmap, $hBackbuffer, $hBrush
Local $hFormat, $hFamily, $hFont, $tLayout, $aInfo, $hBrush_Font
Local $fps = 0, $fps_maintimer, $fps_timer, $fps_diff

Local $width = 0x000050, $height = 0x000100
Local $gui_text = "Twister"
Local $pi = ACos(-1)

Local $win_x = 3 * $width
Local $win_x_d = $win_x / 3
$hWnd = GUICreate($gui_text, $win_x, $height)
GUISetBkColor(0x000000)
Local $hdc_gui = _WinAPI_GetDC($hWnd) ;DeviceContext der Gui
GUISetState()

Local $ptr_Bitmap, $hbmp_Bitmap ;Pointer auf die Bitmapdaten, Handle für GDI+
Local $hdc_Bitmap = _CreateNewBmp32($width, $height, $ptr_Bitmap, $hbmp_Bitmap) ;DC
Global $struct = DllStructCreate("dword[" & $width * $height & "]", $ptr_Bitmap) ;32Bit-bitmap mit der Größe der GUI
Global $tRECT = DllStructCreate($tagRect)
DllStructSetData($tRECT, "Left", 0)
DllStructSetData($tRECT, "Top", 0)
DllStructSetData($tRECT, "Right", $width)
DllStructSetData($tRECT, "Bottom", $height)
$hBrush = _WinAPI_CreateSolidBrush(0x000000)

Local $pi2 = 0x000002 * $pi
Local $STEPS = 0x00000F
Local $iteration = 0
Local $yMin[$height +  1], $yMax[$height + 1]
Local $i, $min, $max, $previousX, $twist, $step, $angle, $radius, $x, $r

Local $aSTEPS[$STEPS + 1]
For $i = 0 To $STEPS
	$aSTEPS[$i] = $pi * $i / $STEPS
Next
Local $aRadius[$STEPS + 1]
For $i = 0 To $STEPS
	$aRadius[$i] = Cos($aSTEPS[$i])
	$aRadius[$i] = (0x000001 - 0.4 * $aRadius[$i] * $aRadius[$i])
Next
Local $aTwist[$height]
For $i = 0 To $height - 1
	$aTwist[$i] = $i / $height
Next

For $i = 0 To $height - 1
	$yMin[$i] = $width
	$yMax[$i] = 0
Next

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$fps_maintimer = TimerInit()
Do
	$fps_timer = TimerInit()

	_WinAPI_FillRect($hdc_Bitmap, DllStructGetPtr($tRECT), $hBrush)
	$iteration += 0.05
	For $y = 0 To $height - 1 Step 4
;~ 		$min = $width
;~ 		$max = 0
;~ 		$previousX = $width
		$twist = $iteration + 0x00000A * Cos($iteration) * $aTwist[$y]
		For $step = 0x000000 To $STEPS
			$angle = $aSTEPS[$step]
			$radius = $aRadius[$step]
			$x = ($width + $width * Cos($angle + $twist) * $radius) * 0.5
			If $previousX < $x Then
				$r = 0x000000FF * $radius
;~ 				If Mod($step, 2) Then
;~ 					Line($previousX, $x, $y, $r, $r)
;~ 				Else
					Line($previousX, $x, $y, $r)
;~ 				EndIf
			EndIf
			$previousX = $x
			If $x < $min Then $min = $x
			If $x > $max Then $max = $x
		Next
	Next
	_WinAPI_BitBlt($hdc_gui, $win_x_d, 0, $width, $height, $hdc_Bitmap, 0, 0, $SRCCOPY)

	$fps_diff = TimerDiff($fps_timer)
	If TimerDiff($fps_maintimer) > 999 Then ;calculate FPS
		$fps = Round(1000 / $fps_diff, 2)
		$fps_maintimer = TimerInit()
	EndIf
	WinSetTitle($hWnd, "", $gui_text & "   /   FPS: " & StringFormat("%.2f", $fps))
Until 0

Func Line($x1, $x2, $y, $r = 0x000000, $gb = 0x000000)
	Local $z = 0x000000, $c, $g

	While $x1 < $x2
		$c = $x1 + $y * $width
		If $z < 0x000003 Then
			DllStructSetData($struct, 1, 0x000000, $c)
		Else
			DllStructSetData($struct, 1, $r * 0x10000 + $gb * 0x100 + $gb, $c)
;~ 			$g = Hex($gb, 2)
;~ 			DllStructSetData($struct, 1, "0xFF" & Hex($r, 2) & $g & $g, $c)
		EndIf
		$z += 1
		$x1 += 1
	WEnd
EndFunc

Func _Exit()
	_WinAPI_DeleteObject($hBrush)
	_WinAPI_DeleteObject($hbmp_Bitmap)
	$struct = 0
	$tRECT = 0
	GUIDelete($hWnd)
    Exit
EndFunc   ;==>_Exit


Func _CreateNewBmp32($iwidth, $iheight, ByRef $ptr, ByRef $hbmp) ;erstellt leere 32-bit-Bitmap; Rückgabe DC und ptr und handle auf die Bitmapdaten
	;by Andy
	Local $hcdc = _WinAPI_CreateCompatibleDC(0) ;Desktop-Kompatiblen DeviceContext erstellen lassen
	Local $tBMI = DllStructCreate($tagBITMAPINFO) ;Struktur der Bitmapinfo erstellen und Daten eintragen
	DllStructSetData($tBMI, "Size", DllStructGetSize($tBMI) - 4);Structgröße abzüglich der Daten für die Palette
	DllStructSetData($tBMI, "Width", $iwidth)
	DllStructSetData($tBMI, "Height", -$iheight) ;minus =standard = bottomup
	DllStructSetData($tBMI, "Planes", 1)
	DllStructSetData($tBMI, "BitCount", 32) ;32 Bit = 4 Bytes => AABBGGRR
	Local $adib = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBMI), 'uint', 1, 'ptr*', 0, 'ptr', 0, 'uint', 0)
	$hbmp = $adib[0] ;hbitmap handle auf die Bitmap, auch per GDI+ zu verwenden
	$ptr = $adib[4] ;pointer auf den Anfang der Bitmapdaten, vom Assembler verwendet
	;_arraydisplay($adib)
	_WinAPI_SelectObject($hcdc, $hbmp) ;objekt hbitmap in DC
	Return $hcdc ;DC der Bitmap zurückgeben
EndFunc   ;==>_CreateNewBmp32