Skip to content

Commit

Permalink
Automatically dereference struct fields
Browse files Browse the repository at this point in the history
This is an experiment to see if we can avoid having to call unsafe_load() all
the time.
  • Loading branch information
JamesWrigley committed Jul 6, 2024
1 parent 41313a4 commit 7b9f143
Show file tree
Hide file tree
Showing 26 changed files with 15,779 additions and 773 deletions.
8 changes: 4 additions & 4 deletions demo/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function official_demo(; engine=nothing)

# Enable docking and multi-viewport
io = CImGui.GetIO()
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_DockingEnable
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_ViewportsEnable
io.ConfigFlags = io.ConfigFlags | CImGui.ImGuiConfigFlags_DockingEnable
io.ConfigFlags = io.ConfigFlags | CImGui.ImGuiConfigFlags_ViewportsEnable

# Setup Dear ImGui style
CImGui.StyleColorsDark()
Expand All @@ -30,7 +30,7 @@ function official_demo(; engine=nothing)
# - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling `CImGui.Build()`/`GetTexDataAsXXXX()``, which `ImGui_ImplXXXX_NewFrame` below will call.
# - Read 'fonts/README.txt' for more instructions and details.
fonts_dir = joinpath(@__DIR__, "..", "fonts")
fonts = unsafe_load(CImGui.GetIO().Fonts)
fonts = CImGui.GetIO().Fonts
# default_font = CImGui.AddFontDefault(fonts)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Cousine-Regular.ttf"), 15)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "DroidSans.ttf"), 16)
Expand Down Expand Up @@ -65,7 +65,7 @@ function official_demo(; engine=nothing)

CImGui.SameLine()
CImGui.Text("counter = $counter")
CImGui.Text(@sprintf("Application average %.3f ms/frame (%.1f FPS)", 1000 / unsafe_load(CImGui.GetIO().Framerate), unsafe_load(CImGui.GetIO().Framerate)))
CImGui.Text(@sprintf("Application average %.3f ms/frame (%.1f FPS)", 1000 / CImGui.GetIO().Framerate, CImGui.GetIO().Framerate))

CImGui.End()
end
Expand Down
6 changes: 3 additions & 3 deletions examples/app_constrained_resize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Create a window with custom resize constraints.
"""
global function ShowExampleAppConstrainedResize(p_open::Ref{Bool})
function Square(data::Ptr{ImGuiSizeCallbackData})::Cvoid
desired_size = unsafe_load(data.DesiredSize)
desired_size = data.DesiredSize
max_size = max(desired_size.x, desired_size.y)
data.DesiredSize = ImVec2(max_size, max_size)
end

function Step(data::Ptr{ImGuiSizeCallbackData})::Cvoid
desired_size = data.DesiredSize |> unsafe_load
step::Cfloat = data.UserData |> unsafe_load |> Int
desired_size = data.DesiredSize
step::Cfloat = data.UserData |> Int
size_x = trunc(desired_size.x / step + 0.5) * step
size_y = trunc(desired_size.y / step + 0.5) * step
data.DesiredSize = ImVec2(size_x, size_y)
Expand Down
4 changes: 2 additions & 2 deletions examples/app_custom_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool})

adding_preview = false
CImGui.InvisibleButton("canvas", canvas_size)
mouse_x = unsafe_load(CImGui.GetIO().MousePos.x)
mouse_y = unsafe_load(CImGui.GetIO().MousePos.y)
mouse_x = CImGui.GetIO().MousePos.x
mouse_y = CImGui.GetIO().MousePos.y
mouse_pos_in_canvas = ImVec2(mouse_x - canvas_pos.x, mouse_y - canvas_pos.y)
if adding_line
adding_preview = true
Expand Down
6 changes: 3 additions & 3 deletions examples/app_simple_overlay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function ShowExampleAppSimpleOverlay(p_open::Ref{Bool})
io = CImGui.GetIO()
@cstatic corner=Cint(0) begin
if corner != -1
window_pos_x = corner & 1 != 0 ? unsafe_load(io.DisplaySize).x - DISTANCE : DISTANCE
window_pos_y = corner & 2 != 0 ? unsafe_load(io.DisplaySize).y - DISTANCE : DISTANCE
window_pos_x = corner & 1 != 0 ? io.DisplaySize.x - DISTANCE : DISTANCE
window_pos_y = corner & 2 != 0 ? io.DisplaySize.y - DISTANCE : DISTANCE
window_pos = (window_pos_x, window_pos_y)
window_pos_pivot = (corner & 1 != 0 ? 1.0 : 0.0, corner & 2 != 0 ? 1.0 : 0.0)
CImGui.SetNextWindowPos(window_pos, CImGui.ImGuiCond_Always, window_pos_pivot)
Expand All @@ -28,7 +28,7 @@ function ShowExampleAppSimpleOverlay(p_open::Ref{Bool})
CImGui.Text("Simple overlay\n in the corner of the screen.\n (right-click to change position)")
CImGui.Separator()
if CImGui.IsMousePosValid()
CImGui.Text(@sprintf("Mouse Position: (%.1f,%.1f)", unsafe_load(io.MousePos).x, unsafe_load(io.MousePos).y))
CImGui.Text(@sprintf("Mouse Position: (%.1f,%.1f)", io.MousePos.x, io.MousePos.y))
else
CImGui.Text("Mouse Position: <invalid>")
end
Expand Down
8 changes: 4 additions & 4 deletions examples/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function julia_demo(; engine=nothing)

# enable docking and multi-viewport
io = CImGui.GetIO()
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_DockingEnable
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_ViewportsEnable
io.ConfigFlags = io.ConfigFlags | CImGui.ImGuiConfigFlags_DockingEnable
io.ConfigFlags = io.ConfigFlags | CImGui.ImGuiConfigFlags_ViewportsEnable

# setup Dear ImGui style
CImGui.StyleColorsDark()
Expand All @@ -25,7 +25,7 @@ function julia_demo(; engine=nothing)

# When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
style = Ptr{ImGuiStyle}(CImGui.GetStyle())
if unsafe_load(io.ConfigFlags) & ImGuiConfigFlags_ViewportsEnable == ImGuiConfigFlags_ViewportsEnable
if io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable == ImGuiConfigFlags_ViewportsEnable
style.WindowRounding = 5.0f0
col = CImGui.c_get(style.Colors, CImGui.ImGuiCol_WindowBg)
CImGui.c_set!(style.Colors, CImGui.ImGuiCol_WindowBg, ImVec4(col.x, col.y, col.z, 1.0f0))
Expand All @@ -38,7 +38,7 @@ function julia_demo(; engine=nothing)
# - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling `CImGui.Build()`/`GetTexDataAsXXXX()``, which `ImGui_ImplXXXX_NewFrame` below will call.
# - Read 'fonts/README.txt' for more instructions and details.
fonts_dir = joinpath(@__DIR__, "..", "fonts")
fonts = unsafe_load(CImGui.GetIO().Fonts)
fonts = CImGui.GetIO().Fonts
# default_font = CImGui.AddFontDefault(fonts)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Cousine-Regular.ttf"), 15)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "DroidSans.ttf"), 16)
Expand Down
56 changes: 28 additions & 28 deletions examples/demo_helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function ShowUserGuide()
CImGui.BulletText("Click and drag on any empty space to move window.")
CImGui.BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields.")
CImGui.BulletText("CTRL+Click on a slider or drag box to input value as text.")
unsafe_load(CImGui.GetIO().FontAllowUserScaling) && CImGui.BulletText("CTRL+Mouse Wheel to zoom window contents.")
CImGui.GetIO().FontAllowUserScaling && CImGui.BulletText("CTRL+Mouse Wheel to zoom window contents.")
CImGui.BulletText("Mouse Wheel to scroll.")
CImGui.BulletText("While editing text:\n")
CImGui.Indent()
Expand Down Expand Up @@ -63,43 +63,43 @@ function ShowAboutWindow(p_open::Ref{Bool})
CImGui.Separator()
CImGui.Text("sizeof(size_t): $(sizeof(Csize_t)), sizeof(ImDrawIdx): $(sizeof(CImGui.ImDrawIdx)), sizeof(ImDrawVert): $(sizeof(CImGui.ImDrawVert))")
CImGui.Separator()
CImGui.Text(@sprintf("io.BackendPlatformName: %s", unsafe_load(io.BackendPlatformName) != C_NULL ? unsafe_string(unsafe_load(io.BackendPlatformName)) : "NULL"))
CImGui.Text(@sprintf("io.BackendRendererName: %s", unsafe_load(io.BackendRendererName) != C_NULL ? unsafe_string(unsafe_load(io.BackendRendererName)) : "NULL"))
CImGui.Text(@sprintf("io.ConfigFlags: 0x%08X", unsafe_load(io.ConfigFlags)))
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NavEnableKeyboard != 0 && CImGui.Text(" NavEnableKeyboard")
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NavEnableGamepad != 0 && CImGui.Text(" NavEnableGamepad")
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NavEnableSetMousePos != 0 && CImGui.Text(" NavEnableSetMousePos")
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NavNoCaptureKeyboard != 0 && CImGui.Text(" NavNoCaptureKeyboard")
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NoMouse != 0 && CImGui.Text(" NoMouse")
unsafe_load(io.ConfigFlags) & CImGui.ImGuiConfigFlags_NoMouseCursorChange != 0 && CImGui.Text(" NoMouseCursorChange")
unsafe_load(io.MouseDrawCursor) && CImGui.Text("io.MouseDrawCursor")
unsafe_load(io.ConfigMacOSXBehaviors) && CImGui.Text("io.ConfigMacOSXBehaviors")
unsafe_load(io.ConfigInputTextCursorBlink) && CImGui.Text("io.ConfigInputTextCursorBlink")
unsafe_load(io.ConfigWindowsResizeFromEdges) && CImGui.Text("io.ConfigWindowsResizeFromEdges")
unsafe_load(io.ConfigWindowsMoveFromTitleBarOnly) && CImGui.Text("io.ConfigWindowsMoveFromTitleBarOnly")
CImGui.Text(@sprintf("io.BackendFlags: 0x%08X", unsafe_load(io.BackendFlags)))
unsafe_load(io.BackendFlags) & ImGuiBackendFlags_HasGamepad != 0 && CImGui.Text(" HasGamepad")
unsafe_load(io.BackendFlags) & ImGuiBackendFlags_HasMouseCursors != 0 && CImGui.Text(" HasMouseCursors")
unsafe_load(io.BackendFlags) & ImGuiBackendFlags_HasSetMousePos != 0 && CImGui.Text(" HasSetMousePos")
CImGui.Text(@sprintf("io.BackendPlatformName: %s", io.BackendPlatformName != C_NULL ? unsafe_string(io.BackendPlatformName) : "NULL"))
CImGui.Text(@sprintf("io.BackendRendererName: %s", io.BackendRendererName != C_NULL ? unsafe_string(io.BackendRendererName) : "NULL"))
CImGui.Text(@sprintf("io.ConfigFlags: 0x%08X", io.ConfigFlags))
io.ConfigFlags & CImGui.ImGuiConfigFlags_NavEnableKeyboard != 0 && CImGui.Text(" NavEnableKeyboard")
io.ConfigFlags & CImGui.ImGuiConfigFlags_NavEnableGamepad != 0 && CImGui.Text(" NavEnableGamepad")
io.ConfigFlags & CImGui.ImGuiConfigFlags_NavEnableSetMousePos != 0 && CImGui.Text(" NavEnableSetMousePos")
io.ConfigFlags & CImGui.ImGuiConfigFlags_NavNoCaptureKeyboard != 0 && CImGui.Text(" NavNoCaptureKeyboard")
io.ConfigFlags & CImGui.ImGuiConfigFlags_NoMouse != 0 && CImGui.Text(" NoMouse")
io.ConfigFlags & CImGui.ImGuiConfigFlags_NoMouseCursorChange != 0 && CImGui.Text(" NoMouseCursorChange")
io.MouseDrawCursor && CImGui.Text("io.MouseDrawCursor")
io.ConfigMacOSXBehaviors && CImGui.Text("io.ConfigMacOSXBehaviors")
io.ConfigInputTextCursorBlink && CImGui.Text("io.ConfigInputTextCursorBlink")
io.ConfigWindowsResizeFromEdges && CImGui.Text("io.ConfigWindowsResizeFromEdges")
io.ConfigWindowsMoveFromTitleBarOnly && CImGui.Text("io.ConfigWindowsMoveFromTitleBarOnly")
CImGui.Text(@sprintf("io.BackendFlags: 0x%08X", io.BackendFlags))
io.BackendFlags & ImGuiBackendFlags_HasGamepad != 0 && CImGui.Text(" HasGamepad")
io.BackendFlags & ImGuiBackendFlags_HasMouseCursors != 0 && CImGui.Text(" HasMouseCursors")
io.BackendFlags & ImGuiBackendFlags_HasSetMousePos != 0 && CImGui.Text(" HasSetMousePos")
CImGui.Separator()
# CImGui.Text("io.Fonts: %d fonts, Flags: 0x%08X, TexSize: %d,%d", io.Fonts->Fonts.Size, io.Fonts->Flags, io.Fonts->TexWidth, io.Fonts->TexHeight);
CImGui.Text(@sprintf("io.DisplaySize: %.2f,%.2f", unsafe_load(io.DisplaySize).x, unsafe_load(io.DisplaySize).y))
CImGui.Text(@sprintf("io.DisplayFramebufferScale: %.2f,%.2f", unsafe_load(io.DisplayFramebufferScale).x, unsafe_load(io.DisplayFramebufferScale).y))
CImGui.Text(@sprintf("io.DisplaySize: %.2f,%.2f", io.DisplaySize.x, io.DisplaySize.y))
CImGui.Text(@sprintf("io.DisplayFramebufferScale: %.2f,%.2f", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y))
CImGui.Separator()

window_padding = unsafe_load(style.WindowPadding)
window_padding = style.WindowPadding
CImGui.Text(@sprintf("style.WindowPadding: %.2f,%.2f", window_padding.x, window_padding.y))
CImGui.Text(@sprintf("style.WindowBorderSize: %.2f", unsafe_load(style.WindowBorderSize)))
CImGui.Text(@sprintf("style.WindowBorderSize: %.2f", style.WindowBorderSize))

frame_padding = unsafe_load(style.FramePadding)
frame_padding = style.FramePadding
CImGui.Text(@sprintf("style.FramePadding: %.2f,%.2f", frame_padding.x, frame_padding.y))
CImGui.Text(@sprintf("style.FrameRounding: %.2f", unsafe_load(style.FrameRounding)))
CImGui.Text(@sprintf("style.FrameBorderSize: %.2f", unsafe_load(style.FrameBorderSize)))
CImGui.Text(@sprintf("style.FrameRounding: %.2f", style.FrameRounding))
CImGui.Text(@sprintf("style.FrameBorderSize: %.2f", style.FrameBorderSize))

item_spacing = unsafe_load(style.ItemSpacing)
item_spacing = style.ItemSpacing
CImGui.Text(@sprintf("style.ItemSpacing: %.2f,%.2f", item_spacing.x, item_spacing.y))

item_inner_spacing = unsafe_load(style.ItemInnerSpacing)
item_inner_spacing = style.ItemInnerSpacing
CImGui.Text(@sprintf("style.ItemInnerSpacing: %.2f,%.2f", item_inner_spacing.x, item_inner_spacing.y))

copy_to_clipboard && CImGui.LogFinish()
Expand Down
26 changes: 13 additions & 13 deletions examples/demo_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ function ShowDemoWindowMisc()
if CImGui.CollapsingHeader("Inputs, Navigation & Focus")
io = CImGui.GetIO()

CImGui.Text("WantCaptureMouse: $(unsafe_load(io.WantCaptureMouse))")
CImGui.Text("WantCaptureKeyboard: $(unsafe_load(io.WantCaptureKeyboard))")
CImGui.Text("WantTextInput: $(unsafe_load(io.WantTextInput))")
CImGui.Text("WantSetMousePos: $(unsafe_load(io.WantSetMousePos))")
CImGui.Text("NavActive: $(unsafe_load(io.NavActive)), NavVisible: $(unsafe_load(io.NavVisible))")
CImGui.Text("WantCaptureMouse: $(io.WantCaptureMouse)")
CImGui.Text("WantCaptureKeyboard: $(io.WantCaptureKeyboard)")
CImGui.Text("WantTextInput: $(io.WantTextInput)")
CImGui.Text("WantSetMousePos: $(io.WantSetMousePos)")
CImGui.Text("NavActive: $(io.NavActive), NavVisible: $(io.NavVisible)")

if CImGui.TreeNode("Keyboard, Mouse & Navigation State")
if CImGui.IsMousePosValid()
CImGui.Text("Mouse pos: ($(unsafe_load(io.MousePos).x), $(unsafe_load(io.MousePos).y))")
CImGui.Text("Mouse pos: ($(io.MousePos.x), $(io.MousePos.y))")
else
CImGui.Text("Mouse pos: <INVALID>")
end
CImGui.Text("Mouse delta: ($(unsafe_load(io.MouseDelta).x), $(unsafe_load(io.MouseDelta).y))")
CImGui.Text("Mouse delta: ($(io.MouseDelta.x), $(io.MouseDelta.y))")
CImGui.Text("Mouse down:")
for i = 0:4
dur = CImGui.c_get(io.MouseDownDuration, i)
Expand All @@ -62,13 +62,13 @@ function ShowDemoWindowMisc()
CImGui.SameLine()
CImGui.Text("b$i")
end
CImGui.Text(@sprintf("Mouse wheel: %.1f", unsafe_load(io.MouseWheel)))
CImGui.Text(@sprintf("Mouse wheel: %.1f", io.MouseWheel))

key_range = CImGui.ImGuiKey_NamedKey_BEGIN:CImGui.ImGuiKey_NamedKey_END - 1

CImGui.Text("Keys down:")
for i = key_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
key_data = CImGui.GetKeyData(CImGui.ImGuiKey(i))
dur = key_data.DownDuration
dur 0 || continue
CImGui.SameLine()
Expand All @@ -87,7 +87,7 @@ function ShowDemoWindowMisc()
CImGui.SameLine()
CImGui.Text("$i")
end
CImGui.Text(@sprintf("Keys mods: %s%s%s%s", unsafe_load(io.KeyCtrl) ? "CTRL " : "", unsafe_load(io.KeyShift) ? "SHIFT " : "", unsafe_load(io.KeyAlt) ? "ALT " : "", unsafe_load(io.KeySuper) ? "SUPER " : ""))
CImGui.Text(@sprintf("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""))

navinput_range = CImGui.ImGuiKey_GamepadStart:CImGui.ImGuiKey_GamepadRStickDown

Expand All @@ -99,15 +99,15 @@ function ShowDemoWindowMisc()
end
CImGui.Text("NavInputs pressed:")
for i = navinput_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
key_data = CImGui.GetKeyData(CImGui.ImGuiKey(i))
dur = key_data.DownDuration
dur == 0.0 || continue
CImGui.SameLine()
CImGui.Text("[$i]")
end
CImGui.Text("NavInputs duration:")
for i = navinput_range
key_data = unsafe_load(CImGui.GetKeyData(CImGui.ImGuiKey(i)))
key_data = CImGui.GetKeyData(CImGui.ImGuiKey(i))
dur = key_data.DownDuration
dur 0.0 || continue
CImGui.SameLine()
Expand Down Expand Up @@ -199,7 +199,7 @@ function ShowDemoWindowMisc()
# you can request a lower or higher threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta()
value_raw = CImGui.GetMouseDragDelta(0, 0.0)
value_with_lock_threshold = CImGui.GetMouseDragDelta(0)
mouse_delta = unsafe_load(io.MouseDelta)
mouse_delta = io.MouseDelta
CImGui.SameLine()
txt = @sprintf "Raw (%.1f, %.1f), WithLockThresold (%.1f, %.1f), MouseDelta (%.1f, %.1f)" value_raw.x value_raw.y value_with_lock_threshold.x value_with_lock_threshold.y mouse_delta.x mouse_delta.y
CImGui.Text(txt)
Expand Down
Loading

0 comments on commit 7b9f143

Please sign in to comment.