name22

GDI+ Animation with motion timing-function

Nov 20th, 2013
124
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 $nPiHalf = ACos(-1) / 2
  11. Global $hWnd_GUI, $hDC_Window, $hDC_Bitmap, $hBitmap
  12. Global $nSleepTime, $nFPS_Average, $nFPS_Cur, $nFPS_Display, $tPrecSleep, $pPrecSleep, $vNTdll
  13. Global $hGraphics, $hBrush_Black, $hBrush_Red, $hStringFormat, $hFamily_FPS, $hFont_FPS, $aMeasure, $tLayout_FPS
  14. Global $nT_Sleep, $nT_UpdateFPS, $nT_Movement, $nMovementTime, $nMovementPhase, $nFrameTime
  15. Global $nX_Source, $nY_Source, $nX_Pos, $nY_Pos, $nX_Target, $nY_Target
  16.  
  17. ;###-v-SETTINGS-v-###
  18.  
  19. Global $iWidth = 600
  20. Global $iHeight = 400
  21. Global $iARGB_BG = 0xFFFFFFFF
  22. Global $nFPS = 60
  23.  
  24. Global $nMovementDuration = 2000
  25.  
  26. $nX_Target = Random(20, $iWidth - 20, 1)
  27. $nY_Target = Random(20, $iHeight - 20, 1)
  28.  
  29. ;###-^-SETTINGS-^-###
  30.  
  31. $nSleepTime = 1000 / $nFPS
  32.  
  33. $nFPS_Display = 0
  34. $nFPS_Average = $nFPS
  35.  
  36. $vNTdll = DllOpen("ntdll.dll")
  37.  
  38. $tPrecSleep = DllStructCreate("int64 time;")
  39. $pPrecSleep = DllStructGetPtr($tPrecSleep)
  40.  
  41. $hWnd_GUI = GUICreate("Motion along linear path", $iWidth, $iHeight)
  42. GUISetState()
  43.  
  44. $hDC_Window = _WinAPI_GetDC($hWnd_GUI)
  45. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  46. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iWidth, $iHeight)
  47. _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  48.  
  49. _GDIPlus_Startup()
  50.  
  51. $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC_Bitmap)
  52. _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
  53.  
  54. $hBrush_Black = _GDIPlus_BrushCreateSolid(0xFF000000)
  55. $hBrush_Red = _GDIPlus_BrushCreateSolid(0xFFFF0000)
  56.  
  57. $hStringFormat = _GDIPlus_StringFormatCreate()
  58. $hFamily_FPS = _GDIPlus_FontFamilyCreate("Arial")
  59. $hFont_FPS = _GDIPlus_FontCreate($hFamily_FPS, 6)
  60.  
  61. $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphics, "FPS: 0000", $hFont_FPS, _GDIPlus_RectFCreate(), $hStringFormat)
  62. $tLayout_FPS = $aMeasure[0]
  63. $aMeasure = ""
  64. DllStructSetData($tLayout_FPS, "X", $iWidth - DllStructGetData($tLayout_FPS, "Width") - 3)
  65. DllStructSetData($tLayout_FPS, "Y", $iHeight - DllStructGetData($tLayout_FPS, "Height"))
  66. DllStructSetData($tLayout_FPS, "Width", DllStructGetData($tLayout_FPS, "Width") + 3)
  67.  
  68. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
  69. OnAutoItExitRegister("_Shutdown")
  70.  
  71. $nT_UpdateFPS = TimerInit()
  72. $nT_Sleep = TimerInit()
  73. _SetNewTarget()
  74. While True
  75.     DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep)))
  76.     DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
  77.     $nFrameTime = TimerDiff($nT_Sleep)
  78.     $nT_Sleep = TimerInit()
  79.  
  80.     $nFPS_Cur = 1000 / $nFrameTime
  81.     If TimerDiff($nT_UpdateFPS) >= 1000 Then
  82.         $nFPS_Display = $nFPS_Cur
  83.         $nT_UpdateFPS = TimerInit()
  84.     EndIf
  85.  
  86.     If TimerDiff($nT_Movement) >= $nMovementDuration Then _SetNewTarget()
  87.  
  88.     $nMovementTime = TimerDiff($nT_Movement)
  89.     $nMovementPhase = _MovementTimingFunction($nMovementTime, $nMovementDuration)
  90.  
  91.     $nX_Pos = $nX_Source + $nMovementPhase * ($nX_Target - $nX_Source)
  92.     $nY_Pos = $nY_Source + $nMovementPhase * ($nY_Target - $nY_Source)
  93.  
  94.     _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  95.     _GDIPlus_GraphicsFillEllipse($hGraphics, $nX_Pos - 20, $nY_Pos - 20, 40, 40, $hBrush_Red)
  96.     _GDIPlus_GraphicsFillRect($hGraphics, $nX_Target - 2, $nY_Target - 2, 4, 4, $hBrush_Black)
  97.     _GDIPlus_GraphicsDrawStringEx($hGraphics, "FPS: " & Round($nFPS_Display, 1), $hFont_FPS, $tLayout_FPS, $hStringFormat, $hBrush_Black)
  98.     _WinAPI_BitBlt($hDC_Window, 0, 0, $iWidth, $iHeight, $hDC_Bitmap, 0, 0, $SRCCOPY)
  99. WEnd
  100.  
  101. Func _Close()
  102.     Exit
  103. EndFunc   ;==>_Close
  104.  
  105. Func _SetNewTarget()
  106.     $nX_Source = $nX_Target
  107.     $nY_Source = $nY_Target
  108.     $nX_Target = Random(20, $iWidth - 20, 1)
  109.     $nY_Target = Random(20, $iHeight - 20, 1)
  110.     $nT_Movement = TimerInit()
  111. EndFunc
  112.  
  113. Func _MovementTimingFunction($nTime, $nDuration)
  114.     Local $T = $nTime / $nDuration
  115.     Return $T ^ 2 * (3 - 2 * $T)
  116. EndFunc
  117.  
  118. Func _Shutdown()
  119.     _WinAPI_ReleaseDC($hWnd_GUI, $hDC_Window)
  120.     _WinAPI_DeleteDC($hDC_Bitmap)
  121.     _WinAPI_DeleteObject($hBitmap)
  122.  
  123.     _GDIPlus_GraphicsDispose($hGraphics)
  124.     _GDIPlus_BrushDispose($hBrush_Black)
  125.     _GDIPlus_BrushDispose($hBrush_Red)
  126.     _GDIPlus_StringFormatDispose($hStringFormat)
  127.     _GDIPlus_FontFamilyDispose($hFamily_FPS)
  128.     _GDIPlus_FontDispose($hFont_FPS)
  129.     _GDIPlus_Shutdown()
  130.  
  131.     DllClose($vNTdll)
  132. EndFunc   ;==>_Shutdown
RAW Paste Data