Advertisement
Alan72104

autoit Static experiment(bubble sort visualizer)

Feb 3rd, 2021
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.96 KB | None | 0 0
  1. #include <GDIPlus.au3>
  2. #include <GUIConstantsEx.au3>
  3.  
  4. Global Const $width = 960
  5. Global Const $height = $width / 16 * 9
  6. Global $g_bPaused = False
  7. Global $hGui
  8. Global Const $title = "Sorting - Alan72104"
  9. GlobaL $hGraphics
  10. Global Const $bgColorARGB = 0xFF000000
  11. Global $frameBuffer
  12. Global $hFrameBuffer
  13. Global $brushRed
  14. Global $brushWhite
  15. Global $hTimerFrame
  16. Global $nTimerFrame
  17. Global $hTimerDraw
  18. Global $smoothedFrameTime
  19. Global Const $tps = 60
  20. Global Const $frameTimeSmoothingRatio = 0.3
  21. Global Const $arrayLength = 500
  22. Global $array[$arrayLength]
  23. HotKeySet("{F6}", "TogglePause")
  24. HotKeySet("{F7}", "Terminate")
  25.  
  26. Func CreateArray()
  27.     For $i = 0 To $arrayLength - 1
  28.         $array[$i] = Random(0, $height, 1)
  29.     Next
  30. EndFunc
  31.  
  32. Func Update()
  33.     Local Static $i =1
  34.     ; For $i = 1 To $arrayLength - 1
  35.     If $i < $arrayLength Then
  36.         For $j = 0 To $arrayLength - $i - 1
  37.             If $array[$j] > $array[$j + 1] Then
  38.                 Swap($array[$j], $array[$j + 1])
  39.             EndIf
  40.         Next
  41.         $i += 1
  42.     EndIf
  43.     ; Next
  44. EndFunc
  45.  
  46. Func Draw()
  47.     _GDIPlus_GraphicsClear($hFrameBuffer, $bgColorARGB)
  48.     For $i = 0 To $arrayLength - 1
  49.         _GDIPlus_GraphicsFillRect($hFrameBuffer, 0 + $i * $width / $arrayLength, $height - $array[$i], _
  50.                                                 $width / $arrayLength, $array[$i], $brushWhite)
  51.     Next
  52. EndFunc
  53.  
  54. Func FrameBufferTransfer()
  55.     _GDIPlus_GraphicsDrawImage($hGraphics, $frameBuffer, 0, 0)
  56. EndFunc
  57.  
  58. Func Main()
  59.     _DebugOn()
  60.     _GDIPlus_Startup()
  61.     $hGui = GUICreate($title, $width, $height)
  62.     ; $hGui = GUICreate($title, $width, $height, Default, Default, Default, $WS_EX_TOPMOST)
  63.     GUISetBkColor($bgColorARGB - 0xFF000000, $hGui)
  64.     $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
  65.     $frameBuffer = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
  66.     $hFrameBuffer = _GDIPlus_ImageGetGraphicsContext($frameBuffer)
  67.     $brushRed = _GDIPlus_BrushCreateSolid(0xFFFF0000)
  68.     $brushWhite = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
  69.     CreateArray()
  70.     GUISetState(@SW_SHOW)
  71.     While 1
  72.         $hTimerFrame = TimerInit()
  73.         Update()
  74.         If TimerDiff($hTimerDraw) >= 1000 / $tps Then
  75.             Draw()
  76.             FrameBufferTransfer()
  77.             $hTimerDraw = TimerInit()
  78.         EndIf
  79.         $nTimerFrame = TimerDiff($hTimerFrame)
  80.         $smoothedFrameTime = ($smoothedFrameTime * (1 - $frameTimeSmoothingRatio)) + $nTimerFrame * $frameTimeSmoothingRatio
  81.         Switch GUIGetMsg()
  82.             Case $GUI_EVENT_CLOSE
  83.                 Terminate()
  84.         EndSwitch
  85.     WEnd
  86. EndFunc
  87.  
  88. Main()
  89.  
  90. Func Swap(ByRef $a, ByRef $b)
  91.     Local $t = $a
  92.     $a = $b
  93.     $b = $t
  94. EndFunc
  95.  
  96. Func GdiPlusClose()
  97.     _GDIPlus_BrushDispose($brushRed)
  98.     _GDIPlus_BrushDispose($brushWhite)
  99.     _GDIPlus_BitmapDispose($frameBuffer)
  100.     _GDIPlus_GraphicsDispose($hGraphics)
  101.     _GDIPlus_Shutdown()
  102. EndFunc
  103.  
  104. Func Terminate()
  105.     GdiPlusClose()
  106.     GUIDelete($hGui)
  107.     Exit 0
  108. EndFunc
  109.  
  110. Func TogglePause()
  111.     $g_bPaused = Not $g_bPaused
  112.     While $g_bPaused
  113.         Sleep(500)
  114.         ToolTip('Script is "Paused"', @desktopWidth / 2, @desktopHeight / 2, Default, Default, $TIP_CENTER)
  115.     WEnd
  116.     ToolTip("")
  117. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement