;Idea taken from http://js1k.com/demo/304
;Ported to AutoIt by UEZ, modified by Andy
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
#AutoIt3Wrapper_Run_After=del /f /q "Tramp of Particles_Obfuscated.au3"
#AutoIt3Wrapper_Run_After=upx.exe --ultra-brute "%out%"
;~ #AutoIt3Wrapper_Run_After=upx.exe --best "%out%"
;#include "QuickDraw.au3"
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <winapi.au3>
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)
Local $hWnd, $hGraphic, $hBitmap, $hBackbuffer, $hPen
Local $width = 640, $height = 480
Local $max_particle = 500
Local $aParticles[$max_particle + 1][5] ;x,y,vx,vy,color
Local $i, $x, $pi = ACos(-1), $pi180 = $pi / 180, $r, $g, $b
Local $gui_text = "Tramp of " & $max_particle & " Particles using BitBlt :o)"
Local $fps = 0, $fps_maintimer, $fps_timer, $fps_diff



$hWnd = GUICreate($gui_text, $width, $height) ;"normale" GUI
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
Local $struct = DllStructCreate("dword[" & $width * $height & "]", $ptr_Bitmap) ;32Bit-bitmap mit der Größe der GUI

Dim $background[$max_particle + 1] ;"buffer" für die Pixel


For $i = 0 To UBound($aParticles) - 1
	$aParticles[$i][0] = Random(0, $width, 1)
	$aParticles[$i][1] = Random(0, $height, 1)
	$aParticles[$i][2] = 0
	$aParticles[$i][3] = 0
	$aParticles[$i][4] = 0xFFFFFFFF
;~ 	$r = 0x10 + Int(0xD0 * Abs(Sin($i * 0.05) * 2))
;~ 	$g = 0x10 + Int(0xD0 * Abs(Sin($i * 0.05) * 2))
;~ 	$b = 0x10 + Int(0xD0 * Abs(Sin($i * 0.05) * 2))
;~ 	$aParticles[$i][4] = "0xFF" & Hex($r, 2) & Hex($g, 2) & Hex($b, 2)
Next

;_QuickDraw_Enable($gui_text, $width, $height, 0, 0, 0)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$fps_maintimer = TimerInit()
Do
	$fps_timer = TimerInit()
	;_QuickDraw_ClearBuffer(0xFF000000)

	For $x = 0 To $max_particle
		;local $t=timerinit()
		$aParticles[$x][0] += $aParticles[$x][2]
		$aParticles[$x][1] += $aParticles[$x][3]
		If $aParticles[$x][0] < 0 Then $aParticles[$x][0] += $width
		If $aParticles[$x][0] > $width Then $aParticles[$x][0] -= $width
		If $aParticles[$x][1] < 0 Then $aParticles[$x][1] += $height
		If $aParticles[$x][1] > $height Then $aParticles[$x][1] -= $height
		;local $m=timerinit()
		$aParticles[$x][3] = Cos($aParticles[$x][0] * 0.02) + 0.25
		$aParticles[$x][2] = Sin($aParticles[$x][1] * 0.025) + 0.5
		Local $v = TimerInit()
		;msgbox(0,"time",$m-$t&@crlf&$v-$m)  ;ticks zählen
		If $aParticles[$x][0] > 0 And $aParticles[$x][0] < $width And $aParticles[$x][1] > 0 And $aParticles[$x][1] < $height Then
			;_QuickDraw_Point($aParticles[$x][0], $aParticles[$x][1], $aParticles[$x][4])
			;local $m=timerinit()
			DllStructSetData($struct, 1, 0, $background[$x]) ;ehemaliges pixel löschen
			DllStructSetData($struct, 1, $aParticles[$x][4], Int($aParticles[$x][0]) + Int($aParticles[$x][1]) * $width)
			$background[$x] = Int($aParticles[$x][0]) + Int($aParticles[$x][1]) * $width
			;local $v=timerinit()
			;msgbox(0,"time",@crlf&$v-$m)   ;"Ticks" zählen
			;dllstruct ist 4x schneller als _quickdraw_point
		EndIf
	Next
	;local $m=timerinit()
	_WinAPI_BitBlt($hdc_gui, 0, 0, $width, $height, $hdc_Bitmap, 0, 0, $SRCCOPY)
	;local $v=timerinit()
	;msgbox(0,"time",@crlf&$v-$m)  ;"ticks" zählen
	;bitblt ist 10x LANGSAMER als _QuickDraw_SwapBuffers()

	;_QuickDraw_SwapBuffers()
	$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 _Exit()
	$aParticles = 0
	;_QuickDraw_Disable()
	_WinAPI_DeleteObject($hbmp_Bitmap)
	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