Skip to content

Commit

Permalink
Merge pull request #87 from JamesWrigley/v1.89.4-update
Browse files Browse the repository at this point in the history
Update wrapper for v1.89.4
  • Loading branch information
Gnimuc authored Sep 22, 2023
2 parents 33c8de8 + b5cd513 commit 2883aac
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 143 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ julia> include(joinpath(pathof(CImGui), "..", "..", "demo", "demo.jl"))
julia> using CImGui
julia> include(joinpath(pathof(CImGui), "..", "..", "examples", "demo.jl"))
```
[All of these examples](https://github.com/Gnimuc/CImGui.jl/tree/master/examples) are one-to-one ported from [Dear ImGui's C++ examples](https://github.com/ocornut/imgui/blob/master/imgui_demo.cpp) and there is an [interactive manual](https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html) for quickly locating the code. You could also run `? CImGui.xxx` to retrieve docs:
[All of these examples](examples/) (except for
[large_meshes.jl](examples/large_meshes.jl), which is a port of a
[stress-test][stress-test-comment]) are one-to-one ported from [Dear ImGui's C++
examples][upstream-examples] and there is an [interactive
manual][interactive-manual] for quickly locating the code. You could also run `?
CImGui.xxx` to retrieve docs:
```
help?> CImGui.Button
Button(label) -> Bool
Expand Down Expand Up @@ -83,3 +88,8 @@ The default backend is based on [ModernGL](https://github.com/JuliaGL/ModernGL.j

## License
Only the Julia code in this repo is released under MIT license. Other assets such as those fonts in the `fonts` folder are released under their own license.


[stress-test-comment]: https://github.com/ocornut/imgui/issues/2591#issuecomment-496954460
[upstream-examples]: https://github.com/ocornut/imgui/blob/master/imgui_demo.cpp
[interactive-manual]: https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html
19 changes: 10 additions & 9 deletions examples/demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Recursive Sans Casual-Regu
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Recursive Sans Linear-Regular.ttf"), 16)
# @assert default_font != C_NULL

# creat texture for image drawing
# img_width, img_height = 256, 256
# image_id = ImGui_ImplOpenGL3_CreateImageTexture(img_width, img_height)
# create texture for image drawing
img_width, img_height = 256, 256
image_id = ImGuiOpenGLBackend.ImGui_ImplOpenGL3_CreateImageTexture(img_width, img_height)

# setup Platform/Renderer bindings
ImGuiGLFWBackend.init(window_ctx)
Expand All @@ -88,12 +88,13 @@ try

demo_open && @c CImGui.ShowDemoWindow(&demo_open)

# # show image example
# CImGui.Begin("Image Demo")
# image = rand(GLubyte, 4, img_width, img_height)
# ImGui_ImplOpenGL3_UpdateImageTexture(image_id, image, img_width, img_height)
# CImGui.Image(Ptr{Cvoid}(image_id), (img_width, img_height))
# CImGui.End()
# show image example
if CImGui.Begin("Image Demo")
image = rand(GLubyte, 4, img_width, img_height)
ImGuiOpenGLBackend.ImGui_ImplOpenGL3_UpdateImageTexture(image_id, image, img_width, img_height)
CImGui.Image(Ptr{Cvoid}(image_id), CImGui.ImVec2(img_width, img_height))
CImGui.End()
end

# rendering
CImGui.Render()
Expand Down
152 changes: 152 additions & 0 deletions examples/large_meshes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# This is a port of a C++ stress-test of the renderer: https://github.com/ocornut/imgui/issues/2591#issuecomment-496954460

# It basically draws a bunch of squares and text to force ImGui to create lots of
# vertices, which should result in visual artifacts unless vertex offsets are
# enabled (or ImGui was built with 32-bit indices).

using CImGui
using CImGui.ImGuiGLFWBackend
using CImGui.ImGuiGLFWBackend.LibCImGui
using CImGui.ImGuiGLFWBackend.LibGLFW
using CImGui.ImGuiOpenGLBackend
using CImGui.ImGuiOpenGLBackend.ModernGL

# create contexts
imgui_ctx = CImGui.CreateContext()

window_ctx = ImGuiGLFWBackend.create_context()
window = ImGuiGLFWBackend.get_window(window_ctx)

gl_ctx = ImGuiOpenGLBackend.create_context()

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

# set style
CImGui.StyleColorsDark()

# init
ImGuiGLFWBackend.init(window_ctx)
ImGuiOpenGLBackend.init(gl_ctx)

mutable struct GuiState
vtx_count::Cint
vtx_count2::Cint
end

function generate_widget_properties(n)
off_x = (n % 100) * 3f0
off_y = (n % 100) * 1f0

color::ImU32 = ((n * 17) & 255)
color |= ((n * 59) & 255) << 8
color |= ((n * 83) & 255) << 16
color |= 255 << 24

off_x, off_y, color
end

function gui(state)
if CImGui.Begin("Dear ImGui Backend Checker")
version = unsafe_string(CImGui.GetVersion())
io_ref = unsafe_load(io)
platform = unsafe_string(io_ref.BackendPlatformName)
renderer = unsafe_string(io_ref.BackendRendererName)

CImGui.Text("Dear ImGui $(version) Backend Checker")
CImGui.Text("Platform: $(platform)")
CImGui.Text("Renderer: $(renderer)")

CImGui.Separator()

if CImGui.TreeNode("0001: Renderer: Large Mesh Support")
drawlist_ptr = CImGui.GetWindowDrawList()

let vtx_count = Ref(state.vtx_count)
CImGui.SliderInt("VtxCount##1", vtx_count, 0, 100_000)
pos = CImGui.GetCursorScreenPos()

for n = 0:(vtx_count[] ÷ 4 - 1)
off_x, off_y, color = generate_widget_properties(n)
CImGui.AddRectFilled(drawlist_ptr,
ImVec2(pos.x + off_x, pos.y + off_y),
ImVec2(pos.x + off_x + 50, pos.y + off_y + 50),
color)
end

CImGui.Dummy(350, 150)
drawlist = unsafe_load(drawlist_ptr)
CImGui.Text("VtxBuffer.Size = $(drawlist.VtxBuffer.Size)")

state.vtx_count = vtx_count[]
end

let vtx_count2 = Ref(state.vtx_count2)
CImGui.SliderInt("VtxCount##2", vtx_count2, 0, 100_000)
pos = CImGui.GetCursorScreenPos()

for n = 0:(vtx_count2[] ÷ 40 - 1)
off_x, off_y, color = generate_widget_properties(n)
CImGui.AddText(drawlist_ptr,
ImVec2(pos.x + off_x, pos.y + off_y),
color, "ABCDEFGHIJ")
end

CImGui.Dummy(350, 120)
drawlist = unsafe_load(drawlist_ptr)
CImGui.Text("VtxBuffer.Size = $(drawlist.VtxBuffer.Size)")

state.vtx_count2 = vtx_count2[]
end

CImGui.TreePop()
end

CImGui.End()
end
end

try
state = GuiState(60_000, 60_000)

while glfwWindowShouldClose(window) == GLFW_FALSE
glfwPollEvents()
# new frame
ImGuiOpenGLBackend.new_frame(gl_ctx)
ImGuiGLFWBackend.new_frame(window_ctx)
CImGui.NewFrame()

# UIs
gui(state)

# rendering
CImGui.Render()
glfwMakeContextCurrent(window)
w_ref, h_ref = Ref{Cint}(0), Ref{Cint}(0)
glfwGetFramebufferSize(window, w_ref, h_ref)
display_w, display_h = w_ref[], h_ref[]
glViewport(0, 0, display_w, display_h)
glClearColor(0.45, 0.55, 0.60, 1.00)
glClear(GL_COLOR_BUFFER_BIT)
ImGuiOpenGLBackend.render(gl_ctx)

if unsafe_load(CImGui.GetIO().ConfigFlags) & ImGuiConfigFlags_ViewportsEnable == ImGuiConfigFlags_ViewportsEnable
backup_current_context = glfwGetCurrentContext()
LibCImGui.igUpdatePlatformWindows()
GC.@preserve gl_ctx LibCImGui.igRenderPlatformWindowsDefault(C_NULL, pointer_from_objref(gl_ctx))
glfwMakeContextCurrent(backup_current_context)
end

glfwSwapBuffers(window)
end
catch e
@error "Error in renderloop!" exception=e
Base.show_backtrace(stderr, catch_backtrace())
finally
ImGuiOpenGLBackend.shutdown(gl_ctx)
ImGuiGLFWBackend.shutdown(window_ctx)
CImGui.DestroyContext(imgui_ctx)
glfwDestroyWindow(window)
end
Loading

0 comments on commit 2883aac

Please sign in to comment.