Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Last active April 20, 2016 07:31
Show Gist options
  • Save stuartpb/4676983 to your computer and use it in GitHub Desktop.
Save stuartpb/4676983 to your computer and use it in GitHub Desktop.
An old experiment for getting to know Cairo bindings to Lua.
local iup = require "iuplua"
local cairo = require "lcairo"
local dlg
local sqx, sqy = 0, 0
local dragx, dragy
local mx, my = 0, 0
local sqsize = 50
local can = iup.canvas{}
---[[--- Double buffering
local canw, canh = 0, 0
local first_hdc
local back_sfc
local back_lud
local back_cr
local function makebackbuffer()
--todo: destroy any originals
back_sfc = cairo.win32_surface_create_with_ddb(
first_hdc, cairo.FORMAT_RGB24, canw, canh)
back_lud = cairo.create(back_sfc)
back_cr = cairo.ContextFromLUD(back_lud)
end
function can:resize_cb(x,y)
canw, canh = x, y
--todo: remake backbuffer?
end
---]]--- Double buffering
function can:action()
local hdc = iup.GetAttributeData(self,"HDC_WMPAINT")
-- This is how I had it before I added double buffering:
--cr = cairo.ContextFromLUD(cairo.CreateContext(hdc))
-- This is what I had after:
---[[--- Double buffering
if not first_hdc then first_hdc = hdc end
if not back_cr then makebackbuffer() end
local cr = back_cr
---]]--- Double buffering
cr:set_source_rgb(1,1,1)
cr:paint()
cr:set_source_rgb(1,0,0)
local function squircle(size, xoff, yoff)
local hsize = size/2
cr:save()
cr:translate(xoff,yoff)
cr:move_to(hsize,0)
cr:curve_to(0,0,0,0,0,hsize);
cr:curve_to(0,size,0,size,hsize,size);
cr:curve_to(size,size,size,size,size,hsize);
cr:curve_to(size,0,size,0,hsize,0);
cr:restore()
end
squircle(sqsize, sqx, sqy)
cr:fill()
cr:move_to(20,20)
cr:show_text(string.format(
"sq: (%i, %i)",sqx, sqy))
cr:move_to(20,30)
cr:show_text(string.format(
"m: (%i, %i)",mx, my))
cr:move_to(20,40)
cr:show_text(string.format(
"drag: (%s, %s)", tostring(dragx), tostring(dragy)))
---[[--- Double buffering
local cancr = cairo.CreateContext(hdc)
cairo.set_source_surface(cancr, back_sfc, 0, 0)
cairo.paint(cancr)
---]]--- Double buffering
end
function can:button_cb(but, pressed, x, y, status)
mx, my = x, y
if but == iup.BUTTON1 then
if pressed == 1 then
local h = sqsize/2
if x < sqx+sqsize and x > sqx
and y < sqy+sqsize and y > sqy then
dragx = x-sqx
dragy = y-sqy
end
else
dragx, dragy = nil, nil
end
end
end
function can:motion_cb(x,y,status)
mx, my = x, y
if dragx and dragy then
if iup.isbutton1(status) then
sqx = x - dragx
sqy = y - dragy
iup.Update(self)
else
dragx, dragy = nil, nil
end
end
iup.Update(self)
end
dlg = iup.dialog{
title = "Getting to know LuaCairo",
size = "HALFxHALF";
can
}
dlg:show()
iup.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment