name22

GDI+ Snowfall (New Version)

Dec 7th, 2012
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GDIPlus.au3>
  4. #include <WinAPI.au3>
  5.  
  6. ; -Author: name22 (www.autoit.de)
  7.  
  8. Opt("GUIOnEventMode", 1)
  9.  
  10. Global Const $nTau = ACos(-1) * 2
  11. Global $vNTdll = DllOpen("ntdll.dll")
  12. Global $tPrecSleep = DllStructCreate("int64 time;")
  13. Global $pPrecSleep = DllStructGetPtr($tPrecSleep)
  14.  
  15. ;###-v-SETTINGS-v-###
  16. Global $iWidth = 400
  17. Global $iHeight = 400
  18. Global $nFPS = 60
  19.  
  20. Global $iARGB_BG = 0xFF505050
  21. Global $iARGB_Snow = 0xFFFFFFFF
  22.  
  23. Global $iFlakeCount = 60
  24. Global $iMinFallingSpeed = 20, $iMaxFallingSpeed = 50
  25. Global $iMinOscillations = 2, $iMaxOscillations = 5
  26. Global $iMinOscillationDist = 10, $iMaxOscillationDist = 50
  27. Global $iMinRadius = 1, $iMaxRadius = 3
  28.  
  29. Global $iBlastRadius = 200
  30. Global $iBlastAccell = 300
  31. Global $nAirFriction = 0.95
  32. ;###-^-SETTINGS-^-###
  33.  
  34. Global $aFlakes[$iFlakeCount + 1][8] = [[$iFlakeCount]]
  35. Global $hWnd, $hDC_Window, $hDC_Bitmap, $hBitmap, $hGraphics, $hOldObj, $hBrush_Snow
  36. Global $nSleepTime = 1000 / $nFPS
  37.  
  38. For $i = 1 To $aFlakes[0][0]
  39.     $aFlakes[$i][0] = Random(10, $iWidth - 10, 1)
  40.     $aFlakes[$i][1] = Random(-$iHeight, 0, 1)
  41.     $aFlakes[$i][2] = 0
  42.     $aFlakes[$i][3] = 0
  43.     $aFlakes[$i][4] = Random($iMinRadius, $iMaxRadius, 1)
  44.     $aFlakes[$i][5] = Random($iMinFallingSpeed, $iMaxFallingSpeed)
  45.     $aFlakes[$i][6] = Random($iMinOscillations, $iMaxOscillations, 1)
  46.     $aFlakes[$i][7] = Random($iMinOscillationDist, $iMaxOscillationDist) * (-1) ^ Random(1, 2, 1)
  47. Next
  48.  
  49. $hWnd = GUICreate("name22 - Snowflakes", $iWidth, $iHeight)
  50. GUISetState()
  51.  
  52. $hDC_Window = _WinAPI_GetDC($hWnd)
  53. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  54. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iWidth, $iHeight)
  55. $hOldObj = _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  56.  
  57. _GDIPlus_Startup()
  58.  
  59. $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC_Bitmap)
  60. _GDIPlus_GraphicsSetClipRect($hGraphics, 0, 0, $iWidth, $iHeight)
  61. _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
  62.  
  63. $hBrush_Snow = _GDIPlus_BrushCreateSolid($iARGB_Snow)
  64.  
  65. _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  66. _Redraw()
  67.  
  68. OnAutoItExitRegister("_Shutdown")
  69. GUIRegisterMsg($WM_PAINT, "_Redraw")
  70. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
  71. GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_Click")
  72.  
  73. $nT_Sleep = TimerInit() + $nSleepTime
  74.  
  75. While True
  76.     DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep)))
  77.     DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
  78.     $nFrameTime = TimerDiff($nT_Sleep)
  79.     $nT_Sleep = TimerInit()
  80.  
  81.     $nFPS_Cur = 1000 / $nFrameTime
  82.  
  83.     _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  84.     For $i = 1 To $aFlakes[0][0]
  85.         $aFlakes[$i][1] += ($aFlakes[$i][5] + $aFlakes[$i][3]) / $nFPS_Cur
  86.         $aFlakes[$i][0] += (Sin($aFlakes[$i][1] / $iHeight * $nTau * $aFlakes[$i][6]) * $aFlakes[$i][7] + $aFlakes[$i][2]) / $nFPS_Cur
  87.         $aFlakes[$i][2] *= $nAirFriction
  88.         $aFlakes[$i][3] *= $nAirFriction
  89.         If $aFlakes[$i][1] > $iHeight Or $aFlakes[$i][0] < -20 Or $aFlakes[$i][0] > $iWidth + 20 Then
  90.             $aFlakes[$i][0] = Random(10, $iWidth - 10, 1)
  91.             $aFlakes[$i][1] = Random(-$iHeight, 0, 1)
  92.             $aFlakes[$i][2] = 0
  93.             $aFlakes[$i][3] = 0
  94.             $aFlakes[$i][4] = Random($iMinRadius, $iMaxRadius, 1)
  95.             $aFlakes[$i][5] = Random($iMinFallingSpeed, $iMaxFallingSpeed)
  96.             $aFlakes[$i][6] = Random($iMinOscillations, $iMaxOscillations, 1)
  97.             $aFlakes[$i][7] = Random($iMinOscillationDist, $iMaxOscillationDist)
  98.         EndIf
  99.         _GDIPlus_GraphicsFillEllipse($hGraphics, $aFlakes[$i][0] - $aFlakes[$i][4] / 2, $aFlakes[$i][1] - $aFlakes[$i][4] / 2, $aFlakes[$i][4] * 2, $aFlakes[$i][4] * 2, $hBrush_Snow)
  100.     Next
  101.     _WinAPI_BitBlt($hDC_Window, 0, 0, $iWidth, $iHeight, $hDC_Bitmap, 0, 0, $SRCCOPY)
  102. WEnd
  103.  
  104. Func _Click()
  105.     Local $tMousePos = _WinAPI_GetMousePos(True, $hWnd)
  106.     For $i = 1 To $aFlakes[0][0]
  107.         $iX_Dist = ($aFlakes[$i][0] - $aFlakes[$i][4]) - DllStructGetData($tMousePos, "X")
  108.         $iY_Dist = ($aFlakes[$i][1] - $aFlakes[$i][4]) - DllStructGetData($tMousePos, "Y")
  109.         $iDist = Sqrt($iX_Dist ^ 2 + $iY_Dist ^ 2)
  110.         If $iDist = 0 Then ContinueLoop
  111.         If $iDist < $iBlastRadius Then
  112.             $aFlakes[$i][2] = (1 - $iDist / $iBlastRadius) * ($iX_Dist / $iDist) * $iBlastAccell
  113.             $aFlakes[$i][3] = (1 - $iDist / $iBlastRadius) * ($iY_Dist / $iDist) * $iBlastAccell
  114.         EndIf
  115.     Next
  116. EndFunc
  117.  
  118. Func _Redraw()
  119.     _WinAPI_BitBlt($hDC_Window, 0, 0, $iWidth, $iHeight, $hDC_Bitmap, 0, 0, $SRCCOPY)
  120. EndFunc
  121.  
  122. Func _Close()
  123.     Exit
  124. EndFunc
  125.  
  126. Func _Shutdown()
  127.     _WinAPI_SelectObject($hDC_Bitmap, $hOldObj)
  128.     _WinAPI_ReleaseDC($hWnd, $hDC_Window)
  129.     _WinAPI_DeleteDC($hDC_Bitmap)
  130.     _WinAPI_DeleteObject($hBitmap)
  131.  
  132.     _GDIPlus_GraphicsDispose($hGraphics)
  133.     _GDIPlus_BrushDispose($hBrush_Snow)
  134.     _GDIPlus_Shutdown()
  135.  
  136.     DllClose($vNTdll)
  137. EndFunc
  138.  
  139.  
  140. ; #FUNCTION# ====================================================================================================================
  141. ; Name...........: _GDIPlus_GraphicsSetClipRect
  142. ; Description ...: Updates the clipping region of a Graphics object to a region that is the combination of itself and a rectangle
  143. ; Syntax.........: _GDIPlus_GraphicsSetClipRect($hGraphics, $nX, $nY, $nWidth, $nHeight[, $iCombineMode = 0])
  144. ; Parameters ....: $hGraphics - Pointer to a Graphics object
  145. ;                  $nX        - X coordinate of the upper-left corner of the rectangle
  146. ;                  $nY        - Y coordinate of the upper-left corner of the rectangle
  147. ;                  $nWidth    - Width of the rectangle
  148. ;                  $nHeight   - Height of the rectangle
  149. ;                  $iCombineMode - Regions combination mode:
  150. ;                  |0 - The existing region is replaced by the new region
  151. ;                  |1 - The existing region is replaced by the intersection of itself and the new region
  152. ;                  |2 - The existing region is replaced by the union of itself and the new region
  153. ;                  |3 - The existing region is replaced by the result of performing an XOR on the two regions
  154. ;                  |4 - The existing region is replaced by the portion of itself that is outside of the new region
  155. ;                  |5 - The existing region is replaced by the portion of the new region that is outside of the existing region
  156. ; Return values .: Success      - True
  157. ;                  Failure      - False and either:
  158. ;                  |@error and @extended are set if DllCall failed
  159. ;                  |$GDIP_STATUS contains a non zero value specifying the error code
  160. ; Remarks .......: None
  161. ; Related .......: None
  162. ; Link ..........; @@MsdnLink@@ GdipSetClipRect
  163. ; Example .......; No
  164. ; ===============================================================================================================================
  165. Func _GDIPlus_GraphicsSetClipRect($hGraphics, $nX, $nY, $nWidth, $nHeight, $iCombineMode = 0)
  166.     Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetClipRect", "hwnd", $hGraphics, "float", $nX, "float", $nY, "float", $nWidth, "float", $nHeight, "int", $iCombineMode)
  167.  
  168.     If @error Then Return SetError(@error, @extended, False)
  169.     $GDIP_STATUS = $aResult[0]
  170.     Return $aResult[0] = 0
  171. EndFunc   ;==>_GDIPlus_GraphicsSetClipRect
RAW Paste Data