Re: CIMGUI Wrapper Help
- Posted by ghaberek (admin) May 30, 2023
- 680 views
I am trying to get a basic example going, but can't figure out how to wrap this one part.
Wow that ImGuiIO structure is huge! Good thing I'm working on named members for the standard library version of libffi. There's no way you could build sane code around index-counting one hundred members.
That seems like an unfortunate side-effect of the complexities introduced by trying to "flatten" a C++ library (Dear ImGui) down to plain C, but I think I can help with what I've already got in libffi-euphoria.
One thing that's confusing about this code is that they're storing the values in a local struct and then assigning the whole thing to the member at once:
ImVec2 display_size; display_size.x = 1920; display_size.y = 1080; io->DisplaySize = display_size; io->DeltaTime = 1.0f / 60.0f;
That code could be written differently, which helps us understand that we're storing the values in the DisplaySize member of the ImGuiIO structure:
io->DisplaySize.x = 1920; // poke( io + offsetof(DisplaySize) + offsetof(x), 1920 ) io->DisplaySize.y = 1080; // poke( io + offsetof(DisplaySize) + offsetof(y), 1080 )
I added some pseudo code as a comment, to indicate what's basically happening in the background. Hope that makes sense.
You can use poke_member to write the values in one go:
sequence display_size = {1920,1080} poke_member( io, ImGuiIO, 3, display_size ) -- 3 = DisplaySize poke_member( io, ImGuiIO, 4, 1.0 / 60.0 ) -- 4 = DeltaTime
Or simply:
poke_member( io, ImGuiIO, 3, {1920,1080} ) -- 3 = DisplaySize poke_member( io, ImGuiIO, 4, 1.0 / 60.0 ) -- 4 = DeltaTime
For this case, you need to pass a pointer to the variable (&f means "the address of f") so you have to allocate the memory, poke the value, and then pass the address to the function.
One thing to note, however, is that this value is marked as static which means it effectively exists "outside" the for loop or even the main function; it's a part of the program itself.
static float f = 0.0f; igSliderFloat("float", &f, 0.0f, 1.0f, "%.3f", 0);
procedure main() -- allocate this for the whole run atom f = allocate_data( sizeof(C_FLOAT) ) poke_type( f, C_FLOAT, 0.0 ) -- (other code here) -- for i = 1 to 20 do -- (other code here) -- igSliderFloat("float", f, 0.0, 1.0, "%.3f", 0) -- here, f is a pointer to a value in memory -- (more code here) -- end for -- free the memory before we exit free( f ) end procedure
Similar to the DisplaySize and DeltaTime values above, you can use peek_member here:
gText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate);
In this case I'm storing the value in a local variable to clean up the code and avoid multiple peeks to memory.
atom framerate = peek_member( io, ImGuiIO, 57 ) -- 57 = Framerate gText("Application average %.3f ms/frame (%.1f FPS)", {1000.0 / framerate,framerate})
Hope that helps!
-Greg