apicici / cimgui-love

LÖVE module for Dear ImGui obtained by wrapping cimgui with LuaJIT FFI.
MIT License
76 stars 6 forks source link

Support UserCallback draw cmd #6

Closed xiejiangzhi closed 2 years ago

xiejiangzhi commented 2 years ago

For AddCallback feature

--luajit will not GC this callback,  save it by you way, and call free if you don't use it, more see luajit callback
local draw_cb = ffi.cast('ImDrawCallback', function(parent_draw_list, cmd)
   -- love 2d draw
end)

-- NOTE: Don't directly use lua function for every draw, you will get "too many callbacks" error. 
-- because lua function will auto create a C callback and not free.
drawlist:AddCallback(draw_cb)
apicici commented 2 years ago

I like the idea of adding support for user callbacks, but I don't think using FFI callbacks is the best approach. The callbacks are not called by imgui, but by the rendering function which is written in Lua—it doesn't make much sense to create a C function pointer of a lua function just to pass it a lua function and call it from there.

I'll look into options. I could either add a imgui.love.AddCallback function that takes Lua functions as inputs (instead of C function pointers) or do something similar to the way textures are passed to imgui.AddImage.

apicici commented 2 years ago

I added support for AddCallback. Lua functions can be passed direct;y, but FFI callbacks are also accepted. Thanks for the suggestion!

xiejiangzhi commented 2 years ago

Cool, directly use Lua function is easier.