Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G33kDude/aa97dfeb5b1729f269ce to your computer and use it in GitHub Desktop.
Save G33kDude/aa97dfeb5b1729f269ce to your computer and use it in GitHub Desktop.
/r/DailyProgrammer
Size := 100
PixSize := 5
GuiSize := Size * PixSize
Stack := []
Loop, % Size
Stack.Insert(Rand(0.0, 1.0))
Gui, +hWndhWnd -Caption
hDC := DllCall("GetDC", "UPtr", hWnd, "UPtr")
hMemDC := DllCall("CreateCompatibleDC", "UPtr", hDC, "UPtr")
hBitmap := DllCall("CreateCompatibleBitmap", "UPtr", hDC, "Int", GuiSize, "Int", GuiSize, "UPtr")
DllCall("SelectObject", "UPtr", hMemDC, "UPtr", hBitmap)
OnExit, ExitSub
OnMessage(0xF, "WM_PAINT")
Gui, Show, w%GuiSize% h%GuiSize%
Sorted := False
Loop
{
Sorted := True
SetRectangle(hMemDC, 0, 0, GuiSize, GuiSize, 0)
Draw(Stack, hMemDC, PixSize)
Loop, % Size - A_Index
{
if (Stack[A_Index] > Stack[A_Index + 1])
{
Tmp := Stack[A_Index]
Stack[A_Index] := Stack[A_Index+1]
Stack[A_Index+1] := tmp
Sorted := False
}
SetRectangle(hMemDC, (A_Index-1)*PixSize, 0, PixSize*2, GuiSize, 0) ; Reset background of the two columns
SetRectangle(hMemDC, (A_Index)*PixSize, 0, PixSize, GuiSize, 0xFFFFFF) ; Draw the column pointer
; Draw the two points
SetRectangle(hMemDC, (A_Index-1)*PixSize, Stack[A_Index]*(GuiSize-PixSize)
, PixSize, PixSize, Stack[A_Index]*0xFFFFFF)
SetRectangle(hMemDC, A_Index*PixSize, Stack[A_Index]*(GuiSize-PixSize)
, PixSize, PixSize, Stack[A_Index+1]*0xFFFFFF)
; 1/8 chance of blitting (blitting is waaay more expensive than random, and random makes it look nicer)
if !Rand(0, 7)
BitBlt(hDC, x, y, GuiSize, GuiSize, hMemDC)
}
; SetRectangle(hMemDC, (Size-A_Index)*PixSize, 0, PixSize, GuiSize, 0xFFFFFF)
}
until Sorted
SetRectangle(hMemDC, 0, 0, GuiSize, GuiSize, 0)
Draw(Stack, hMemDC, PixSize)
BitBlt(hDC, x, y, GuiSize, GuiSize, hMemDC)
MsgBox, Done.
ExitApp
return
GuiEscape:
GuiClose:
ExitApp
return
ExitSub:
DllCall("SelectObject", "UPtr", hMemDC, "UPtr", hBitmap)
DllCall("DeleteObject", "UPtr", hBitmap)
DllCall("DeleteObject", "UPtr", hMemDC)
DllCall("ReleaseDC", "UPtr", hWnd, "UPtr", hDC)
ExitApp
return
Draw(Stack, hDC, PixSize)
{
Size := Stack.MaxIndex()
for Index, Number in Stack
{
x := (Index-1)*PixSize
y := Number*PixSize*(Size-1) ; -1 to leave room for the extra height
SetRectangle(hDC, x, y, PixSize, PixSize, Number*0xFFFFFF)
}
}
WM_PAINT(wParam, lParam, Msg, hWnd)
{
global hDC, Size, hMemDC
BitBlt(hDC, 0, 0, Size, Size, hMemDC)
}
SetPixel(hDC, x, y, Color)
{
DllCall("SetPixelV", "UPtr", hDC, "Int", x, "Int", y, "UInt", Color)
}
SetRectangle(hDC, x, y, w, h, Color)
{
static Pens:=[], Brushes:=[]
; If this.hPen && !DllCall("DeleteObject","UPtr",this.hPen)
; If this.hBrush && !DllCall("DeleteObject","UPtr",this.hBrush)
if !Pens[Color]
Pens[Color] := DllCall("CreatePen", "Int", 0, "Int", 0, "UInt", Color, "UPtr") ;PS_SOLID
if !Brushes[Color]
Brushes[Color] := DllCall("CreateSolidBrush", "UInt", Color, "UPtr")
; Replace the original pen and brush with our own
hOriginalPen := DllCall("SelectObject", "UPtr", hDC, "UPtr", Pens[Color], "UPtr")
hOriginalBrush := DllCall("SelectObject", "UPtr", hDC, "UPtr", Brushes[Color], "UPtr")
DllCall("Rectangle", "UPtr", hDC, "Int", x, "Int", y, "Int", x+w, "Int", y+h)
; Reselect the original pen and brush
DllCall("SelectObject", "UPtr", hDC, "UPtr", hOriginalPen, "UPtr")
DllCall("SelectObject", "UPtr", hDC, "UPtr", hOriginalBrush, "UPtr")
}
BitBlt(hDC, x, y, w, h, hSrcDC)
{
DllCall("BitBlt", "UPtr", hDC, "Int", x, "Int", y, "Int", w, "Int", h, "UPtr", hSrcDC, "Int", 0, "Int", 0, "UInt", 0xCC0020) ;SRCCOPY
}
Rand(Min, Max)
{
Random, Rand, Min, Max
return Rand
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment