apicici / cimgui-love

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

How to use PushFont? #4

Closed xiejiangzhi closed 2 years ago

xiejiangzhi commented 2 years ago

I saw the README that telling me how to change the global default font.

If I just want to change a button font, I didn't find a way.

I have tried use PushFont, but it waill directly crash my game.


some crash log

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00000000000002b8
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [2617]

VM Regions Near 0x2b8:
--> 
    __TEXT                 000000010d26e000-000000010d272000 [   16K] r-x/r-x SM=COW  /Applications/love.app/Contents/MacOS/love

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   cimgui.dylib                    0x0000000113331a93 ImGui::PushFont(ImFont*) + 83
1   cimgui.dylib                    0x00000001132febc5 igPushFont + 21
2   LuaJIT.LuaJIT                   0x000000010d82ed4b 0x10d828000 + 27979
3   LuaJIT.LuaJIT                   0x000000010d88e775 0x10d828000 + 419701

append font code to readme example , 'SFNS.ttf' is a MacOS font I copied from system Fonts folder

love.load = function()
    imgui.love.Init()

    local io = imgui.GetIO()
    local font = io.Fonts:AddFontFromFileTTF('SFNS.ttf', 16)
    imgui.love.BuildFontAtlas()
    imgui.PushFont(font)
end

ENV: cimgui-love 1.87-1 love2d: 11.4

xiejiangzhi commented 2 years ago

I try call PushFont without params, it also crash. In the source code https://github.com/ocornut/imgui/blob/1ee252772ae9c0a971d06257bb5c89f628fa696a/imgui.cpp#L7370

if not font, it will use default font.

xiejiangzhi commented 2 years ago

image

It also working in the debugger page. but the PushFont function will crash.

xiejiangzhi commented 2 years ago

https://github.com/ocornut/imgui/issues/3621

Oh, shit, because I call PushFont before NewFrame.

cimgui-love is a great project.

apicici commented 2 years ago

I'm glad you figured it out!