-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
74 lines (63 loc) · 2.78 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local ffi = require('ffi')
function love.load()
ffi.cdef[[
typedef double CGFloat;
typedef struct CGPoint { CGFloat x; CGFloat y; } CGPoint;
typedef struct CGSize { CGFloat width; CGFloat height; } CGSize;
typedef struct CGRect { CGPoint origin; CGSize size; } CGRect;
]]
cg = ffi.load('/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics')
ffi.cdef[[
typedef uint32_t CGDirectDisplayID;
CGDirectDisplayID CGMainDisplayID(void);
typedef uint32_t CGError;
CGError CGDisplayMoveCursorToPoint(CGDirectDisplayID display, CGPoint point);
typedef struct CGImage* CGImageRef;
CGImageRef CGDisplayCreateImageForRect(CGDirectDisplayID display, CGRect rect);
typedef struct __CFData* CFDataRef;
typedef struct CGDataProvider* CGDataProviderRef;
CFDataRef CGDataProviderCopyData(CGDataProviderRef provider);
CGDataProviderRef CGImageGetDataProvider(CGImageRef image);
const uint8_t * CFDataGetBytePtr(CFDataRef theData);
typedef struct __CGEvent* CGEventRef;
typedef struct __CGEventSource* CGEventSourceRef;
CGEventRef CGEventCreate(CGEventSourceRef source);
CGPoint CGEventGetLocation(CGEventRef event);
size_t CGImageGetBytesPerRow(CGImageRef image);
size_t CGImageGetWidth(CGImageRef image); size_t CGImageGetHeight(CGImageRef image);
typedef const void *CFTypeRef; void CFRelease(CFTypeRef cf);
]]
display = cg.CGMainDisplayID()
end
local image, data, pixels
function love.update(dt)
-- hot reload support (when you edit this file, the new code swaps
-- into the running program w/o needing to restart love)
pcall(function() require('vendor.lurker').update() end)
-- get mouse location
local event = cg.CGEventCreate(nil)
local loc = cg.CGEventGetLocation(event)
ffi.C.CFRelease(event)
if image then ffi.C.CFRelease(image); image = nil end
if data then ffi.C.CFRelease(data); data = nil end
-- get screen pixels under mouse location
local rect = ffi.new('CGRect', {loc.x - 20, loc.y - 20}, {40, 40}) -- edit this!
image = cg.CGDisplayCreateImageForRect(display, rect)
data = cg.CGDataProviderCopyData(cg.CGImageGetDataProvider(image))
pixels = ffi.C.CFDataGetBytePtr(data)
end
function love.draw()
if not pixels then return end
local sw, sh = love.graphics.getWidth(), love.graphics.getHeight()
local cols, rows = tonumber(cg.CGImageGetWidth(image)), tonumber(cg.CGImageGetHeight(image))
local bytes_per_row = cg.CGImageGetBytesPerRow(image)
for y = 0, rows - 1 do
for x = 0, cols - 1 do
local k = bytes_per_row*y + x*4
love.graphics.setColor(pixels[k+2]/255, pixels[k+1]/255, pixels[k]/255)
love.graphics.rectangle('fill', x*(sw/cols), y*(sh/rows), sw/cols, sh/rows)
end
end
love.graphics.setColor(1, 1, 1)
-- pcall(function() love.graphics.print(require('memoryusage')(), 20, 20) end) -- to spot memory leaks
end