Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
I am having issues trying to use imgui: player crashes — Gideros Forum

I am having issues trying to use imgui: player crashes

piepie Member
edited November 2022 in Plugins
Hi guys, it's been a long time I am not using Gideros (but always lurking) and I am happy to see that the community is still thriving! :smiley:
Now I forgot almost everything I learned in the past years of using gideros, and something is probably changed, but I hope I will recover something on the way..

I came back to see if I can make use of ImGui: it's the first time I am using it and I am moving my first steps.
I basically copied the first example found on github and it crashes my windows player. Shall I do something to enable the plugin in the windows player? Am I doing something unexpected?

This is my code so far, in main.lua, thank you!
 require "ImGui"
 
 
function SetMainUI()
local UI = ImGui.new()
local IO = UI:getIO()
local FontAtlas = IO:getFonts()
local Font = FontAtlas:addFonts{ {"fonts/CharisSIL-Regular.ttf", 18}, {"fonts/CharisSIL-Bold.ttf", 18},  {"fonts/CharisSIL-Italic.ttf", 18} }
 
 
 
IO:setFontDefault(Font[1])
FontAtlas:build()
 
	UI:pushFont(Font[1])
	UI:text("Font1")
	UI:popFont()
 
 
	return UI
 
end
 
ui = SetMainUI()
stage:addChild(ui)
edit: I forgot the basic info. Windows10, Gideros 2022.9
Tagged:

Comments

  • MoKaLuxMoKaLux Member
    edited November 2022
    welcome back pie :) I am not an imgui gideros expert but I tested your code and it indeed crashes, I suspect IO and the font stuff. Here is some code that doesn't crash on me:
    require "ImGui"
     
    application:setBackgroundColor(0x323232)
     
    local imgui = ImGui.new()
    stage:addChild(imgui)
     
    local window01 = true
     
    function onEnterFrame(e)
    	-- 1 we start ImGui
    	imgui:newFrame(e.deltaTime)
    	-- 2 the main window
    	if window01 then -- if window exists
    		local windowdrawn = false
    		window01, windowdrawn = imgui:beginWindow(
    			"Hello Dear ImGui", -- window title
    			window01 -- is window expanded
    		)
    		if (windowdrawn) then -- the variable is false when main window is collapsed
    			imgui:text("Hello Dear ImGui!") -- we add some element to our GUI
    			imgui:textWrapped("lorem ipsum, dolorem")
    		end
    		imgui:endWindow()
    	end
    	-- 3 we end the frame and render to screen
    	imgui:endFrame()
    	imgui:render()
    end
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    There are some imgui threads which can help as well: https://forum.gideros.rocks/discussion/8399/imgui-new-thread and there is of course the wiki :) https://wiki.gideros.rocks/index.php/Dear_ImGui

    PS: I added the imgui plugin in the Plugins

    Likes: pie

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • perhaps instead of
    UI:pushFont(Font[1])
    you should use
    UI:pushFont(FontAtlas:getFont(1))
    ?
    see https://github.com/MultiPain/Gideros_ImGui/ for docs

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • FYI the wiki may not be up to date because imgui was changing a lot during its implementation. If you see this in the wiki just comment or delete this line from the examples:
    --	window01 = imgui:beginWindow("Window 01") -- no close button (X)

    Likes: pie

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you @MoKaLux at least your Hello World works out of the box :smiley: it's a starting point.

    @keszegh unfortunately not: I was trying to follow the examples on github but without much success: the example I slightly modified above comes from the official github, but it is some years I don't use gideros and I forgot a lot of the basics, maybe I did something stupid "before" even using imgui :disappointed: looking at what I wrote 4 years ago there is a black hole :neutral:
    anyway I will try to unrust some knowledge, thank you again!
  • my font related imgui code is as follows (after which UI:text() works):
    local FontAtlas = IO:getFonts()
      local Roboto_font = FontAtlas:addFont(_resources.."Roboto-Regular.ttf",26, {  
          oversampleH = 2,
          oversampleV = 2,
          --glyphs = {
          --ranges = {ImGui.GlyphRanges_Cyrillic}
          --}
        })
      Roboto_font:setScale(0.5)
      IO:setFontDefault(Roboto_font)
      FontAtlas:build()
  • hgy29hgy29 Maintainer
    edited November 2022
    @pie, I traced your crash with a debugger and it seems that your call to PushFont crashes because Begin() wasn't called beforehand, ImGui being an immediate GUI, you should draw it every frame, like this:
    require "ImGui"
    function SetMainUI()
    	local UI = ImGui.new()
    	local IO = UI:getIO()
    	local FontAtlas = IO:getFonts()
    	local Font = FontAtlas:addFonts{ {"DejaVuSans.ttf", 18}, {"OpenSans-ExtraBold.ttf", 18},  {"OpenSans-ExtraBold.ttf", 18} }
    	IO:setFontDefault(Font[1])
    	FontAtlas:build()
    	stage:addEventListener(Event.ENTER_FRAME, function (e)
    		UI:newFrame(e.deltaTime)
    		UI:pushFont(Font[1])
    		UI:text("Font1")
    		UI:popFont()
    		UI:endFrame()
    		UI:render()
    	end)
    	return UI
    end
     
    ui = SetMainUI()
    stage:addChild(ui)

    Likes: keszegh, MoKaLux, pie

    +1 -1 (+3 / -0 )Share on Facebook
  • hgy29 said:

    PushFont crashes because Begin() wasn't called beforehand

    Thats true, you should use pushFont/popFont between ImGui:newFrame() and ImGui:endFrame() + ImGui:render()

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • pie said:

    I was trying to follow the examples on github but without much success

    Well, it is not 100% clear, but 100% working example :neutral:
    https://github.com/MultiPain/Gideros_ImGui/#minimal-example
  • Thank you guys! as always the best community on the Internet!! :blush:

    hgy29 said:

    PushFont crashes because Begin() wasn't called beforehand

    I don't know if it's possible but I would add an error catch to avoid player crashing. It's not critical but it makes it a lot more user friendly. Should I open a github issue? on gideros or Imgui-gideros?

    As a side question: I can't draw a gui and avoid continuously rendering(updating) it even if there is nothing happening/nothing clicking it? (an enterframe event is mandatory for imgui to work)?

    Coming from a knowledge hole, I feel there might be useful a set of "hello world" examples for imgui because it is really powerful, but at the same time looks overly complicated for someone who is approaching it like a virgin :smiley:
    The demo window, and the project examples are so much feature packed that it feels like going to rocket science when you just need to learn the basic operations :smile:

    thank you again!
  • rrraptorrrraptor Member
    edited November 2022 Accepted Answer
    pie said:

    I don't know if it's possible but I would add an error catch to avoid player crashing.

    I dont think it is possible. I have only 1 solution: redefine ImGui assert macro. But it completely depends on the ImGui code.
    pie said:

    As a side question: I can't draw a gui and avoid continuously rendering(updating) it even if there is nothing happening/nothing clicking it? (an enterframe event is mandatory for imgui to work)?

    technically it is possible, but you have to do it yourself. For example, you can remove listener (sprite:removeEventListener(...)) if there is no interractions. Check the discussion: https://github.com/ocornut/imgui/pull/5599
    pie said:

    Coming from a knowledge hole, I feel there might be useful a set of "hello world" examples for imgui because it is really powerful, but at the same time looks overly complicated for someone who is approaching it like a virgin :smiley:
    The demo window, and the project examples are so much feature packed that it feels like going to rocket science when you just need to learn the basic operations :smile:

    The only thing I have is a demo converted from C++ to LUA:
    https://github.com/MultiPain/Gideros_examples/tree/master/ImGuiTablesDemo
    It is probably a bit outdated, but still very useful :D

    I guess you can use it as a "hello world" example, but instead of "showDemoWindowTables" function call write your own code...

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited November 2022
    rrraptor said:

    @pie you can ask me any question about imgui here: https://forum.giderosmobile.com/discussion/8399/imgui-new-thread no matter how hard/simle it is :smile:

    i've learnt imgui using the above 'method', see for yourself the outcome:
    https://longtitle-productions.itch.io/fragmenter

    Likes: pie, MoKaLux

    +1 -1 (+2 / -0 )Share on Facebook
  • @rrraptor thank you a lot :) I will move to the other thread for the next questions, but in the meanwhile I have to point out that tablesdemo doesn't work out of the box: this is the output.
    Uploading finished.
    [string "luabinding/compatibility.lua"]:79: Module TablesDemo not found
    stack traceback:
    	[string "luabinding/compatibility.lua"]:79: in function require
    	main.lua:3: in function <main.lua:1>
    If I comment require("TablesDemo") and showDemoWindowTables() out it is indeed a good "empty hello world project" thank you :)

    Anyway if it is easy to fix it, it would be cool to have access to a working tables example too!

    Thank you!
  • rrraptorrrraptor Member
    edited November 2022
    @pie it should work. I did a quick test before posting here.
    You can remove first line: --!NOEXEC in the TablesDemo file and remove require call, maybe there is an error in TablesDemo.lua, which is why require function fails
  • @rraptor removing the --NOEXEC line the require works (and here I wonder how a commented line can break things :o ).
    Anyway, unfortunately it still breaks on the first lines of TablesDemo.lua (it seems to be a list of macros to me, and even gideros editor displays the same "warning").
    TablesDemo.lua:2: Incomplete statement: expected assignment or a function call; 
    line 2 is: CT_Text @ 0



    Thank you again :)
  • MoKaLuxMoKaLux Member
    edited November 2022
    pie said:

    here I wonder how a commented line can break things :o

    see this https://wiki.gideros.rocks/index.php/Loading_Order_of_Lua_Files :)

    ps: I have opened rrraptor's imgui table demo and it works for me with his latest comit.
    EDIT to ps :) : except the very last table example (Sorting) where it crashes the demo (not the gideros player) with this message:
    [2652] ../Common/imgui_src/imgui.cpp: table->RowPosY1 == StartPosY && table->RowPosY2 == window->DC.CursorPos.y
    stack traceback:
    	TablesDemo.lua:1481: in function showDemoWindowTables
    	main.lua:19: in function onDrawGui

    Likes: pie

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited November 2022
    pie said:

    (it seems to be a list of macros to me, and even gideros editor displays the same "warning").

    TablesDemo.lua:2: Incomplete statement: expected assignment or a function call; 
    line 2 is: CT_Text @ 0
    Macros have been removed since Gideros switch to Luau. You should replace all the @ with the usual = sign. See this https://wiki.gideros.rocks/index.php/Lua_to_Luau_conversion_guide

    Like the terminator, they may be back one day :)

    Likes: pie

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLux said:

    Macros have been removed since Gideros switch to Luau. You should replace all the @ with the usual = sign. See this https://wiki.gideros.rocks/index.php/Lua_to_Luau_conversion_guide

    Like the terminator, they may be back one day :)

    I fixed it yesterday, but it seems @pie took an older version :)


    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLux said:

    pie said:

    here I wonder how a commented line can break things :o

    see this https://wiki.gideros.rocks/index.php/Loading_Order_of_Lua_Files :)

    ps: I have opened rrraptor's imgui table demo and it works for me with his latest comit.
    EDIT to ps :) : except the very last table example (Sorting) where it crashes the demo (not the gideros player) with this message:
    [2652] ../Common/imgui_src/imgui.cpp: table->RowPosY1 == StartPosY && table->RowPosY2 == window->DC.CursorPos.y
    stack traceback:
    	TablesDemo.lua:1481: in function showDemoWindowTables
    	main.lua:19: in function onDrawGui
    Fixed

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • OK now it works :smile: Thank you a lot for the updates!

    You can now implement Gideros Code Dependency by code instead of Gideros project setting.


    I am sorry I've lost track of changes: does this mean that Code dependency settings through project properties is no longer supported? Shall I stick to this method instead?

    Since we are already here the demo works but breaks on
    TablesDemo.lua:1178: invalid argument #1 to 'colorConvertRGBtoHEX' (number expected, got table)
    When I press the "open all" button, or "background color" group



  • pie said:

    You can now implement Gideros Code Dependency by code instead of Gideros project setting.


    I am sorry I've lost track of changes: does this mean that Code dependency settings through project properties is no longer supported? Shall I stick to this method instead?
    You can use whatever method you like
    pie said:

    Since we are already here the demo works but breaks on
    TablesDemo.lua:1178: invalid argument #1 to 'colorConvertRGBtoHEX' (number expected, got table)
    When I press the "open all" button, or "background color" group

    Use dot instead of colon for the color covertion functions

    https://github.com/MultiPain/Gideros_ImGui#color-convert

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • I'm sorry I think I am stupid :| : I'd say that my code looks like yours. Where should I put dots instead of commas?



    As a side question, I noticed that you sometimes refer to ImGui using "ImGui" and sometimes using "ui" which is its instance name. Since every instance of ImGui should be equal to its source object, is there any reason to refer to it as the "parent instance ("imGui")"?
    For example in the picture you have just posted I see that row_bg_color uses a method from ImGui, but I believe it could use the same method from ui (as it happens in my file). Am I right?

    Thank you, I am sorry for wasting your time o:)
  • rrraptorrrraptor Member
    edited November 2022
    pie said:

    I'm sorry I think I am stupid :| : I'd say that my code looks like yours. Where should I put dots instead of commas?

    pie said:

    As a side question, I noticed that you sometimes refer to ImGui using "ImGui" and sometimes using "ui" which is its instance name. Since every instance of ImGui should be equal to its source object, is there any reason to refer to it as the "parent instance ("imGui")"?

    You can use whichever you like, they are the same
    ui.colorConvertRGBtoHEX(...)
    ImGui.colorConvertRGBtoHEX(...)
    But it only applies to color converters
    pie said:

    For example in the picture you have just posted I see that row_bg_color uses a method from ImGui, but I believe it could use the same method from ui (as it happens in my file). Am I right?

    In this case "colorConvertRGBtoHEX" is just a function. LUA alternative:
    ImGui = {}
    function ImGui.colorConvertRGBtoHEX(r, g, b)
          --------^
    end
    But for "text" function it will be:
    ImGui = {}
    function ImGui.text(self, text)
          --------^
    end
    -- OR:
    function ImGui:text(text)
          --------^
    end

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • OK, I AM stupid. Thank you! :)
Sign In or Register to comment.