name22

GDI32 - Forest Fire

Feb 16th, 2013
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <WinAPI.au3>
  4.  
  5. ; -Author: name22 (www.autoit.de)
  6.  
  7. Global $hDll_GDI32 = DllOpen("gdi32.dll")
  8.  
  9. Global $iLength = 100
  10. Global $iColor_Tree = 0x00FF00, $iColor_Fire = 0x0000FF, $iColor_Empty = 0
  11. Global $fIgnition = 0.0001, $fGrowth = 0.01, $fTree = 0.55
  12.  
  13. Opt("GUIOnEventMode", 1)
  14.  
  15. $hWnd = GUICreate("Forest Fire", $iLength, $iLength)
  16. GUISetState()
  17.  
  18. $hDC_Window = _WinAPI_GetDC($hWnd)
  19. $hDC_BitmapTmp = _WinAPI_CreateCompatibleDC($hDC_Window)
  20. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  21. $hBitmapTmp = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iLength, $iLength)
  22. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iLength, $iLength)
  23. $hObj_Old1 = _WinAPI_SelectObject($hDC_BitmapTmp, $hBitmapTmp)
  24. $hObj_Old2 = _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  25.  
  26. OnAutoItExitRegister("_Shutdown")
  27. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
  28. GUISetOnEvent($GUI_EVENT_RESTORE, "_DrawBuffer")
  29. GUIRegisterMsg($WM_PAINT, "_DrawBuffer")
  30.  
  31. _WinAPI_SetBkColor($hDC_Bitmap, $iColor_Empty)
  32.  
  33. For $iY = 0 To $iLength - 1
  34.     For $iX = 0 To $iLength - 1
  35.         If Random(0, 1) < 0.4 Then _SetPixel($hDC_Bitmap, $iX, $iY, $iColor_Tree)
  36.     Next
  37. Next
  38.  
  39. _BitBlt($hDC_Bitmap, $hDC_BitmapTmp)
  40. _BitBlt($hDC_Bitmap, $hDC_Window)
  41.  
  42. While True
  43.     For $iY = 0 To $iLength - 1
  44.         For $iX = 0 To $iLength - 1
  45.             Switch _GetPixel($hDC_BitmapTmp, $iX, $iY)
  46.                 Case $iColor_Fire
  47.                     _SetPixel($hDC_Bitmap, $iX, $iY, $iColor_Empty)
  48.                 Case $iColor_Empty
  49.                     If Random(0, 1) < $fGrowth Then _SetPixel($hDC_Bitmap, $iX, $iY, $iColor_Tree)
  50.                 Case $iColor_Tree
  51.                     For $iX_Off = -1 To 1
  52.                         For $iY_Off = -1 To 1
  53.                             If $iX_Off = 0 And $iY_Off = 0 Then ContinueLoop
  54.                             If _GetPixel($hDC_BitmapTmp, $iX + $iX_Off, $iY + $iY_Off) = $iColor_Fire Then
  55.                                 _SetPixel($hDC_Bitmap, $iX, $iY, $iColor_Fire)
  56.                                 ContinueLoop 3
  57.                             EndIf
  58.                         Next
  59.                     Next
  60.                     If Random(0, 1) < $fIgnition Then _SetPixel($hDC_Bitmap, $iX, $iY, $iColor_Fire)
  61.             EndSwitch
  62.         Next
  63.         _BitBlt($hDC_Bitmap, $hDC_Window)
  64.     Next
  65.     _BitBlt($hDC_Bitmap, $hDC_BitmapTmp)
  66. WEnd
  67.  
  68. Func _SetPixel(ByRef $hDC, $iX, $iY, $iColor)
  69.     DllCall($hDll_GDI32, 'INT', 'SetPixelV', 'HWND', $hDC, 'INT', $iX, 'INT', $iY, 'DWORD', $iColor)
  70. EndFunc   ;==>_SetPixel
  71.  
  72. Func _GetPixel(ByRef $hDC, $iX, $iY)
  73.     $aRet = DllCall($hDll_GDI32, 'DWORD', 'GetPixel', 'HWND', $hDC, 'INT', $iX, 'INT', $iY)
  74.     Return $aRet[0]
  75. EndFunc   ;==>_GetPixel
  76.  
  77. Func _DrawBuffer()
  78.     _BitBlt($hDC_Bitmap, $hDC_Window)
  79. EndFunc
  80.  
  81. Func _BitBlt(ByRef $hDC_Source, ByRef $hDC_Target)
  82.     DllCall($hDll_GDI32, "BOOL", "BitBlt", "HANDLE", $hDC_Target, "INT", 0, "INT", 0, "INT", $iLength, "INT", $iLength, "HANDLE", $hDC_Source, "INT", 0, "INT", 0, "DWORD", $SRCCOPY)
  83. EndFunc   ;==>_DrawBuffer
  84.  
  85. Func _Shutdown()
  86.     _WinAPI_SelectObject($hDC_BitmapTmp, $hObj_Old1)
  87.     _WinAPI_SelectObject($hDC_Bitmap, $hObj_Old2)
  88.     _WinAPI_ReleaseDC($hWnd, $hDC_Window)
  89.     _WinAPI_DeleteDC($hDC_BitmapTmp)
  90.     _WinAPI_DeleteDC($hDC_Bitmap)
  91.     _WinAPI_DeleteObject($hBitmapTmp)
  92.     _WinAPI_DeleteObject($hBitmap)
  93.  
  94.     DllClose($hDll_GDI32)
  95. EndFunc   ;==>_Shutdown
  96.  
  97. Func _Close()
  98.     Exit
  99. EndFunc   ;==>_Close
RAW Paste Data