#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)

Global $iWidth = 600
Global $iHeight = 400

;Global Const $iMovers = 3
;Global Const $iFollowers = 15
Global Const $iMovers = 5
Global Const $iFollowers = 8

Global Const $fSpeedMin = 1
Global Const $fSpeedMax = 4
Global Const $iMoveXMin = -20
Global Const $iMoveXMax = $iWidth + 20
Global Const $iMoveYMin = -20
Global Const $iMoveYMax = $iHeight + 20

Global $iHUE, $iRed, $iGreen, $iBlue

Global $aMovers[$iMovers + 1][4]
Global $aFollowers[$iFollowers + 1][$iMovers + 1][4]
Global $tPnt = DllStructCreate("float[" & $iMovers * 2 & "]")
Global $pPnt = DllStructGetPtr($tPnt)

_GDIPlus_Startup()

Global $hGui = GUICreate("Moving Curve", $iWidth, $iHeight)
GUISetOnEvent(-3, "_Exit")

Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
Global $hBmpBuffer = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
Global $hGfxBuffer = _GDIPlus_ImageGetGraphicsContext($hBmpBuffer)
_GDIPlus_GraphicsSetSmoothingMode($hGfxBuffer, 2)
_GDIPlus_GraphicsClear($hGfxBuffer, 0xFF000000)

Global $hBmpScale = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
Global $hGfxScale = _GDIPlus_ImageGetGraphicsContext($hBmpScale)
_GDIPlus_GraphicsSetSmoothingMode($hGfxScale, 2)
DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGfxScale, "float", -($iWidth / 2 * 0.005), "float", -($iHeight / 2 * 0.005), "int", 0)
DllCall($ghGDIPDll, "uint", "GdipScaleWorldTransform", "hwnd", $hGfxScale, "float", 1.005, "float", 1.005, "int", 0)
DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "hwnd", $hGfxScale, "int", 0)

Global $hPen = _GDIPlus_PenCreate(0xFFAA0000, 1)
DllCall($ghGDIPDll, "uint", "GdipSetPenLineJoin", "hwnd", $hPen, "int", 2)
Global $hPen1 = _GDIPlus_PenCreate(0x40000000, 1)
DllCall($ghGDIPDll, "uint", "GdipSetPenLineJoin", "hwnd", $hPen1, "int", 2)

Global $hBrush = _GDIPlus_BrushCreateSolid(0x25000000)

GUIRegisterMsg($WM_PAINT, "WM_PAINT")
GUISetState()

_Init()
While 1
	_Calc()
	_Draw()
	;Sleep(10)
WEnd


Func _Draw()
	_GDIPlus_GraphicsDrawImage($hGfxBuffer, $hBmpScale, 0, 0)
	_GDIPlus_GraphicsFillRect($hGfxBuffer, 0, 0, $iWidth, $iHeight, $hBrush)
	Local $fSize
	Local $aPnt[$iMovers + 1][2] = [[$iMovers]]
	For $j = $iFollowers To 1 Step -1
		For $i = 1 To $iMovers
			DllStructSetData($tPnt, 1, $aFollowers[$j][$i][0], ($i - 1) * 2 + 1)
			DllStructSetData($tPnt, 1, $aFollowers[$j][$i][1], ($i - 1) * 2 + 2)
		Next
		$fSize = 10 - ($j * 8 / $iFollowers)
		_GDIPlus_PenSetWidth($hPen, $fSize)
		_GDIPlus_PenSetWidth($hPen1, $fSize + 2)
		_GDIPlus_PenSetColor($hPen, _ARGBColor(($iFollowers - $j + 1) * 0xFF / $iFollowers, $iRed, $iGreen, $iBlue))
		DllCall($ghGDIPDll, "uint", "GdipDrawClosedCurve2", "hwnd", $hGfxBuffer, "hwnd", $hPen1, "ptr", $pPnt, "int", $iMovers, "float", 0.5)
		DllCall($ghGDIPDll, "uint", "GdipDrawClosedCurve2", "hwnd", $hGfxBuffer, "hwnd", $hPen, "ptr", $pPnt, "int", $iMovers, "float", 0.5)
	Next
	_GDIPlus_GraphicsDrawImage($hGfxScale, $hBmpBuffer, 0, 0)
	_GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
EndFunc   ;==>_Draw


Func _Calc()
	Local $fDX, $fDY
	For $i = 1 To $iMovers
		$aMovers[$i][0] += $aMovers[$i][2]
		If $aMovers[$i][0] <= $iMoveXMin Or $aMovers[$i][0] >= $iMoveXMax Then $aMovers[$i][2] *= -1
		$aMovers[$i][1] += $aMovers[$i][3]
		If $aMovers[$i][1] <= $iMoveYMin Or $aMovers[$i][1] >= $iMoveYMax Then $aMovers[$i][3] *= -1

		$aFollowers[1][$i][0] = $aMovers[$i][0]
		$aFollowers[1][$i][1] = $aMovers[$i][1]
		For $j = 2 To $iFollowers
			$fDX = $aFollowers[$j][$i][0] - $aFollowers[$j - 1][$i][0]
			$fDX /= 10
			If $fDX > $fSpeedMax Then $fDX = $fSpeedMax
			If $fDX < -$fSpeedMax Then $fDX = -$fSpeedMax
			$aFollowers[$j][$i][0] -= $fDX

			$fDY = $aFollowers[$j][$i][1] - $aFollowers[$j - 1][$i][1]
			$fDY /= 10
			If $fDY > $fSpeedMax Then $fDY = $fSpeedMax
			If $fDY < -$fSpeedMax Then $fDY = -$fSpeedMax
			$aFollowers[$j][$i][1] -= $fDY
		Next
	Next

	$iHUE += 0.1
	If $iHUE >= 100 Then $iHUE = 0
	_HSLAtoARGB($iHUE / 100, 1, 0.5, $iRed, $iGreen, $iBlue)
EndFunc   ;==>_Calc


Func _HSLAtoARGB($nH, $nS, $nL, ByRef $iR, ByRef $iG, ByRef $iB)
	Local $nValB = $nL * ($nS + 1)
	Local $nValA = 2 * $nL - $nValB

	$iR = _HueToRGB($nValA, $nValB, $nH + 1 / 3) * 0xFF
	$iG = _HueToRGB($nValA, $nValB, $nH) * 0xFF
	$iB = _HueToRGB($nValA, $nValB, $nH - 1 / 3) * 0xFF
EndFunc   ;==>_HSLAtoARGB


Func _HueToRGB($nA, $nB, $nH)
	If $nH < 0 Then $nH += 1
	If $nH > 1 Then $nH -= 1

	If (6 * $nH) < 1 Then Return $nA + ($nB - $nA) * 6 * $nH
	If (2 * $nH) < 1 Then Return $nB
	If (3 * $nH) < 2 Then Return $nA + ($nB - $nA) * 6 * (2 / 3 - $nH)
	Return $nA
EndFunc   ;==>_HueToRGB


Func _Init()
	For $i = 1 To $iMovers
		$aMovers[$i][0] = Random(-20, $iWidth + 20)
		$aMovers[$i][1] = Random(-20, $iHeight + 20)
		$aMovers[$i][2] = Random($fSpeedMin, $fSpeedMax)
		$aMovers[$i][3] = Random($fSpeedMin, $fSpeedMax)
	Next
	For $j = 1 To $iFollowers
		For $i = 1 To $iMovers
			$aFollowers[$j][$i][0] = $aMovers[$i][0]
			$aFollowers[$j][$i][1] = $aMovers[$i][1]
		Next
	Next
	$iHUE = Random(0, 100, 1)
EndFunc   ;==>_Init


Func _ARGBColor($iA, $iR, $iG, $iB)
	Return BitOR(BitShift($iA, -24), BitShift($iR, -16), BitShift($iG, -8), $iB)
EndFunc   ;==>_ARGBColor


Func WM_PAINT($hWnd, $uMsgm, $wParam, $lParam)
	_GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_PAINT


Func _Exit()
	_GDIPlus_PenDispose($hPen)
	_GDIPlus_PenDispose($hPen1)
	_GDIPlus_BrushDispose($hBrush)
	_GDIPlus_GraphicsDispose($hGfxBuffer)
	_GDIPlus_BitmapDispose($hBmpBuffer)
	_GDIPlus_GraphicsDispose($hGraphics)
	_GDIPlus_Shutdown()
	Exit
EndFunc   ;==>_Exit