Skip to content

Commit

Permalink
fixup! Deprecate our own backends in favour of the official backends
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWrigley committed Jun 16, 2024
1 parent 864abcb commit 456fd8e
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 196 deletions.
15 changes: 8 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
name = "CImGui"
uuid = "5d785b6c-b76f-510e-a07c-3070796c7e87"
authors = ["Yupei Qi <qiyupei@gmail.com>"]
version = "1.89.1"
version = "2"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
CSyntax = "ea656a56-6ca6-5dda-bba5-7b6963a5f74c"
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
ImGuiOpenGL2Backend = "c105bd9b-9289-4129-acf4-4c081b46d160"
LibCImGui = "9be01004-c4f5-478b-abeb-cb32b114cf5e"
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"

[weakdeps]
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"

[extensions]
GlfwOpenGLBackend = ["GLFW", "ModernGL"]

[compat]
CEnum = "0.4, 0.5"
CSyntax = "0.4"
GLFW = "3.4.1"
ImGuiOpenGL2Backend = "0.1"
LibCImGui = "~1.90"
ModernGL = "1.1.7"
Preferences = "1"
julia = "1.6"

Expand Down
151 changes: 43 additions & 108 deletions demo/demo.jl
Original file line number Diff line number Diff line change
@@ -1,49 +1,28 @@
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
import CImGui.LibCImGui as lib
import GLFW
import ModernGL as GL
using Printf

GLFW.Init()

@static if Sys.isapple()
# OpenGL 3.2 + GLSL 150
const glsl_version = 150
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # 3.2+ only
GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
# OpenGL 3.0 + GLSL 130
const glsl_version = 130
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
# GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # 3.2+ only
# GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
end
using CImGui
import CImGui.CSyntax: @c, @cstatic
import CImGui.LibCImGui as lib

# create window
window = GLFW.CreateWindow(1280, 720, "Demo")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1) # enable vsync
# Load deps for the GLFW/OpenGL backend
import GLFW
import ModernGL

# setup Dear ImGui context
# Setup Dear ImGui context
CImGui.backend = :GlfwOpenGL
ctx = CImGui.CreateContext()

# enable docking and multi-viewport
# 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

# setup Dear ImGui style
# Setup Dear ImGui style
CImGui.StyleColorsDark()
# CImGui.StyleColorsClassic()
# CImGui.StyleColorsLight()

# load Fonts
# Load fonts
# - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use `CImGui.PushFont/PopFont` to select them.
# - `CImGui.AddFontFromFileTTF` will return the `Ptr{ImFont}` so you can store it if you need to select the font among multiple.
# - If the file cannot be loaded, the function will return C_NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
Expand All @@ -59,89 +38,45 @@ fonts = unsafe_load(CImGui.GetIO().Fonts)
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Roboto-Medium.ttf"), 16)
# @assert default_font != C_NULL

# setup Platform/Renderer bindings
lib.ImGui_ImplGlfw_InitForOpenGL(Ptr{lib.GLFWwindow}(window.handle), true)
lib.ImGui_ImplOpenGL3_Init("#version $(glsl_version)")

# for tests
if haskey(ENV, "AUTO_CLOSE_DEMO")
tsecs = parse(Int, ENV["AUTO_CLOSE_DEMO"])
Timer(tsecs) do t
GLFW.SetWindowShouldClose(window, true)
end
end

try
show_demo_window = true
show_another_window = false
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
while !GLFW.WindowShouldClose(window)
GLFW.PollEvents()
# start the Dear ImGui frame
lib.ImGui_ImplOpenGL3_NewFrame()
lib.ImGui_ImplGlfw_NewFrame()
CImGui.NewFrame()

# show the big demo window
show_demo_window && @c CImGui.ShowDemoWindow(&show_demo_window)

# show a simple window that we create ourselves.
# we use a Begin/End pair to created a named window.
@cstatic f=Cfloat(0.0) counter=Cint(0) begin
CImGui.Begin("Hello, world!") # create a window called "Hello, world!" and append into it.
CImGui.Text("This is some useful text.") # display some text
@c CImGui.Checkbox("Demo Window", &show_demo_window) # edit bools storing our window open/close state
@c CImGui.Checkbox("Another Window", &show_another_window)
show_demo_window = true
show_another_window = false
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]

@c CImGui.SliderFloat("float", &f, 0, 1) # edit 1 float using a slider from 0 to 1
CImGui.ColorEdit3("clear color", clear_color) # edit 3 floats representing a color
CImGui.Button("Button") && (counter += 1)
CImGui.render(ctx; window_title="Demo", clear_color=Ref(clear_color)) do
global show_demo_window, show_another_window, clear_color

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)))
# show the big demo window
show_demo_window && @c CImGui.ShowDemoWindow(&show_demo_window)

CImGui.End()
end
# show a simple window that we create ourselves.
# we use a Begin/End pair to created a named window.
@cstatic f=Cfloat(0.0) counter=Cint(0) begin
CImGui.Begin("Hello, world!") # create a window called "Hello, world!" and append into it.
CImGui.Text("This is some useful text.") # display some text
@c CImGui.Checkbox("Demo Window", &show_demo_window) # edit bools storing our window open/close state
@c CImGui.Checkbox("Another Window", &show_another_window)

# show another simple window.
if show_another_window
@c CImGui.Begin("Another Window", &show_another_window) # pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
CImGui.Text("Hello from another window!")
CImGui.Button("Close Me") && (show_another_window = false;)
CImGui.End()
end
@c CImGui.SliderFloat("float", &f, 0, 1) # edit 1 float using a slider from 0 to 1
CImGui.ColorEdit3("clear color", clear_color) # edit 3 floats representing a color
CImGui.Button("Button") && (counter += 1)

# rendering
CImGui.Render()
GLFW.MakeContextCurrent(window)
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)))

display_w, display_h = GLFW.GetFramebufferSize(window)

GL.glViewport(0, 0, display_w, display_h)
GL.glClearColor(clear_color...)
GL.glClear(GL.GL_COLOR_BUFFER_BIT)
lib.ImGui_ImplOpenGL3_RenderDrawData(Ptr{Cint}(CImGui.GetDrawData()))

GLFW.MakeContextCurrent(window)
GLFW.SwapBuffers(window)
CImGui.End()
end

if (unsafe_load(lib.igGetIO().ConfigFlags) & lib.ImGuiConfigFlags_ViewportsEnable) == lib.ImGuiConfigFlags_ViewportsEnable
backup_current_context = GLFW.GetCurrentContext()
lib.igUpdatePlatformWindows()
lib.igRenderPlatformWindowsDefault(C_NULL, C_NULL)
GLFW.MakeContextCurrent(backup_current_context)
end
# show another simple window.
if show_another_window
@c CImGui.Begin("Another Window", &show_another_window) # pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
CImGui.Text("Hello from another window!")
CImGui.Button("Close Me") && (show_another_window = false;)
CImGui.End()
end

yield() # to allow shutdown timer to run
# For tests
if haskey(ENV, "AUTO_CLOSE_DEMO")
return :imgui_exit_loop
end
catch e
@error "Error in renderloop!" exception=e
Base.show_backtrace(stderr, catch_backtrace())
finally
lib.ImGui_ImplOpenGL3_Shutdown()
lib.ImGui_ImplGlfw_Shutdown()
CImGui.DestroyContext(ctx)
GLFW.DestroyWindow(window)
GLFW.Terminate()
end
96 changes: 20 additions & 76 deletions examples/demo.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
using CImGui
using CImGui.ImGuiGLFWBackend
using CImGui.ImGuiGLFWBackend.LibCImGui
using CImGui.ImGuiGLFWBackend.LibGLFW
using CImGui.ImGuiOpenGLBackend
using CImGui.ImGuiOpenGLBackend.ModernGL
# using CImGui.ImGuiGLFWBackend.GLFW
using CImGui.LibCImGui
using CImGui.CSyntax

include(joinpath(@__DIR__, "demo_window.jl"))

glfwDefaultWindowHints()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2)
if Sys.isapple()
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
end
import GLFW
import ModernGL as GL

# create window
window = glfwCreateWindow(1280, 720, "Demo", C_NULL, C_NULL)
@assert window != C_NULL
glfwMakeContextCurrent(window)
glfwSwapInterval(1) # enable vsync

# create OpenGL and GLFW context
window_ctx = ImGuiGLFWBackend.create_context(window)
gl_ctx = ImGuiOpenGLBackend.create_context()
include(joinpath(@__DIR__, "demo_window.jl"))

# setup Dear ImGui context
CImGui.backend = :GlfwOpenGL
ctx = CImGui.CreateContext()

# enable docking and multi-viewport
Expand Down Expand Up @@ -70,61 +51,24 @@ CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Recursive Sans Linear-Regu

# 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)
ImGuiOpenGLBackend.init(gl_ctx)
image_id = nothing

try
demo_open = true
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
while glfwWindowShouldClose(window) == 0
glfwPollEvents()
# start the Dear ImGui frame
ImGuiOpenGLBackend.new_frame(gl_ctx)
ImGuiGLFWBackend.new_frame(window_ctx)
CImGui.NewFrame()
demo_open = true
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]

demo_open && @c ShowJuliaDemoWindow(&demo_open)

# 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()
glfwMakeContextCurrent(window)

width, height = Ref{Cint}(), Ref{Cint}() #! need helper fcn
glfwGetFramebufferSize(window, width, height)
display_w = width[]
display_h = height[]

glViewport(0, 0, display_w, display_h)
glClearColor(clear_color...)
glClear(GL_COLOR_BUFFER_BIT)
ImGuiOpenGLBackend.render(gl_ctx)
CImGui.render(ctx; clear_color=Ref(clear_color)) do
global image_id, demo_open
if isnothing(image_id)
image_id = CImGui.create_image_texture(img_width, img_height)
end

if unsafe_load(igGetIO().ConfigFlags) & ImGuiConfigFlags_ViewportsEnable == ImGuiConfigFlags_ViewportsEnable
backup_current_context = glfwGetCurrentContext()
igUpdatePlatformWindows()
GC.@preserve gl_ctx igRenderPlatformWindowsDefault(C_NULL, pointer_from_objref(gl_ctx))
glfwMakeContextCurrent(backup_current_context)
end
demo_open && @c ShowJuliaDemoWindow(&demo_open)

glfwSwapBuffers(window)
# show image example
if CImGui.Begin("Image Demo")
image = rand(GL.GLubyte, 4, img_width, img_height)
CImGui.update_image_texture(image_id, image, img_width, img_height)
CImGui.Image(Ptr{Cvoid}(image_id), CImGui.ImVec2(img_width, img_height))
CImGui.End()
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(ctx)
glfwDestroyWindow(window)
end
Loading

0 comments on commit 456fd8e

Please sign in to comment.