apicici / cimgui-love

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

Silent crash with DrawList.AddImage() #11

Closed Froyok closed 2 years ago

Froyok commented 2 years ago

Hi,

Love2D crash as soon as I try to draw an image via a DrawList in a window. Regular widgets (text, image, separator, etc) work fine.

Maybe I'm doing something wrong and there is actually no bugs, but without documentation regarding DrawList it's a bit hard to be sure if there should be specific setup compared to normal Imgui commands. Usually an incorrect setup lead to an Imgui assert or similar.

System and version:

Steps to reproduce:

With the following script, Love2D starts but freeze before drawing anything and then crash without any callstack/imgui error message:

---------------------------------------------------------
-- Pre-Imgui setup
local lib_ext   = ""
local lib_path  = love.filesystem.getSource() .. "bin/"

if jit.os == "Windows" then
    lib_ext = "dll"
    lib_path = lib_path .. "win64"
else
    lib_ext = "so"
    lib_path = lib_path .. "linux"
end

package.cpath = string.format( "%s;%s/?.%s", package.cpath, lib_path, lib_ext )
---------------------------------------------------------
local IMGUI = require( "lib.cimgui" )
IMGUI.love.RevertToOldNames()

local CANVAS = -1

function love.load( Args )
    IMGUI.Init()

    local CanvasSettings = {
        ["type"] = "2d",
        ["format"]= "rgba16f",
        ["readable"] = true,
        ["msaa"] = 0,
        ["dpiscale"] = 1.0,
        ["mipmaps"] = "none"
    }

    CANVAS = love.graphics.newCanvas( 256, 32, CanvasSettings )
end

----------------------------------------
function love.update( DeltaTime )
    IMGUI.Update( DeltaTime )
    IMGUI.NewFrame()

    local Flags = IMGUI.WindowFlags(
        "NoMove",
        "NoResize",
        "NoTitleBar",
        "NoBackground",
        "NoScrollbar",
        "NoNav"
    )

    IMGUI.SetNextWindowPos( IMGUI.ImVec2_Float( 0.0, 0.0 ) )
    IMGUI.SetNextWindowSize( IMGUI.ImVec2_Float( 600, 300 ) )
    IMGUI.Begin( "Picker", nil, Flags )

    local DrawList = IMGUI.GetWindowDrawList()

    DrawList.AddImage(
        CANVAS,
        IMGUI.ImVec2_Float( 0.0, 0.0 ), -- Start
        IMGUI.ImVec2_Float( 256.0, 32.0 ), -- End
        IMGUI.ImVec2_Float( 0.0, 0.0 ), -- UV min
        IMGUI.ImVec2_Float( 1.0, 1.0 )  -- UV max
    )

    IMGUI.End()
end

----------------------------------------
function love.draw()
    love.graphics.setColor( 1, 1, 1, 1 )
    love.graphics.setShader()

    IMGUI.Render()
    IMGUI.RenderDrawLists()
end

function love.quit()
    IMGUI.Shutdown()
end

function love.keypressed( Key, ScanCode )
    IMGUI.KeyPressed( Key )
    if Key == "escape" then
        love.event.quit()
    end
end
apicici commented 2 years ago

The crash is happening because AddImage is a method of the ImDrawList, so it should be called with a colon

DrawList:AddImage(
    CANVAS,
    IMGUI.ImVec2_Float( 0.0, 0.0 ), -- Start
    IMGUI.ImVec2_Float( 256.0, 32.0 ), -- End
    IMGUI.ImVec2_Float( 0.0, 0.0 ), -- UV min
    IMGUI.ImVec2_Float( 1.0, 1.0 )  -- UV max
)
Froyok commented 2 years ago

That fixed it, thx ! :)

apicici commented 2 years ago

Other than that you are using the DrawList correctly, just passing a LÖVE texture where ImGui expects a ImTextureID should work with all functions.