GDI+ Animation with motion timing-function


SUBMITTED BY: Guest

DATE: Jan. 4, 2015, 1:55 p.m.

FORMAT: Text only

SIZE: 5.0 kB

HITS: 695

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

comments powered by Disqus