Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created August 7, 2018 19:37
Show Gist options
  • Select an option

  • Save SimonDanisch/b4f82db5af88b2cf2a764521dd545673 to your computer and use it in GitHub Desktop.

Select an option

Save SimonDanisch/b4f82db5af88b2cf2a764521dd545673 to your computer and use it in GitHub Desktop.
#Get a console handle
GetConsoleWindow() = ccall((:GetConsoleWindow, :kernel32), Ptr{Void}, ())
GetDC(win) = ccall((:GetDC, :user32), Ptr{Void}, (Ptr{Void},), win)
ReleaseDC(win, dc) = ccall((:ReleaseDC, :user32), Cint, (Ptr{Void}, Ptr{Void}), win, dc)
function GetCursorPos() # mouse position
pos = Ref{Tuple{Int32, Int32}}()
ccall((:GetCursorPos, :user32), UInt8, (Ptr{Tuple{Int32, Int32}},), pos)
Int64.(pos[])
end
using Colors, FixedPointNumbers
SetPixel(dc, x, y, color) = ccall(
(:SetPixel, :Gdi32),
RGB24, (Ptr{Void}, Cint, Cint, RGB24), dc, x, y, color
)
const COORD = NTuple{2, Cshort}
struct SMALL_RECT
Left::Cshort
Top::Cshort
Right::Cshort
Bottom::Cshort
end
struct CONSOLE_SCREEN_BUFFER_INFO
dwSize::COORD
dwCursorPosition::COORD
wAttributes::UInt16
srWindow::SMALL_RECT
dwMaximumWindowSize::COORD
end
GetStdHandle(x = 10) = ccall((:GetStdHandle, :kernel32), Ptr{Void}, (UInt32,), x)
function GetConsoleScreenBufferInfo()
info = Ref{CONSOLE_SCREEN_BUFFER_INFO}()
handle = GetStdHandle()
ccall((:GetCursorPos, :user32), UInt8, (Ptr{Void}, Ptr{CONSOLE_SCREEN_BUFFER_INFO},), handle, info)
info[]
end
t = GetConsoleScreenBufferInfo()
handle = GetStdHandle(3)
function draw_image2_console(image::Matrix{<: Colorant})
myconsole = GetConsoleWindow()
#Get a handle to device context
mydc = GetDC(myconsole)
for i in 1:size(image, 1), j in 1:size(image, 2)
SetPixel(mydc, i - 1, j - 1, convert(RGB24, image[i, j]))
end
ReleaseDC(myconsole, mydc)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment