Skip to content

Commit 416695f

Browse files
committed
Update to new CImGui.jl
1 parent 9dba21b commit 416695f

File tree

4 files changed

+59
-53
lines changed

4 files changed

+59
-53
lines changed

Project.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ version = "0.2.1"
66
[deps]
77
CImGui = "5d785b6c-b76f-510e-a07c-3070796c7e87"
88
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
9+
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
910
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
1011
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
11-
LibCImGui = "9be01004-c4f5-478b-abeb-cb32b114cf5e"
1212
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1313
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
1414
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1515

1616
[compat]
17-
LibCImGui = "~1.82"
18-
CImGui = "~1.82"
17+
CImGui = "2"
1918
FileIO = "1.16"
2019
ImageCore = "0.10"
2120
ImageIO = "0.6"

examples/demo.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using CImGui
2-
using CImGui.ImGuiGLFWBackend.LibGLFW
2+
using GLFW
33
using LinearAlgebra
44
using StaticArrays
55
using NeuralGraphicsGL
@@ -31,7 +31,7 @@ function main()
3131
NGL.enable_blend()
3232

3333
NGL.render_loop(context; destroy_context=false) do
34-
NGL.imgui_begin(context)
34+
NGL.imgui_begin()
3535
NGL.clear()
3636
NGL.set_clear_color(0.2, 0.2, 0.2, 1.0)
3737

@@ -52,9 +52,9 @@ function main()
5252
CImGui.Text("HI!")
5353
CImGui.End()
5454

55-
NGL.imgui_end(context)
56-
glfwSwapBuffers(context.window)
57-
glfwPollEvents()
55+
NGL.imgui_end()
56+
GLFW.SwapBuffers(context.window)
57+
GLFW.PollEvents()
5858

5959
delta_time = time() - last_time
6060
last_time = time()

examples/framebuffer_demo.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using CImGui
2-
using CImGui.ImGuiGLFWBackend.LibGLFW
2+
using GLFW
33
using LinearAlgebra
44
using StaticArrays
55
using ModernGL
@@ -26,7 +26,7 @@ function main()
2626
elapsed_time = 0.0
2727

2828
NGL.render_loop(context; destroy_context=false) do
29-
NGL.imgui_begin(context)
29+
NGL.imgui_begin()
3030

3131
NGL.bind(fb)
3232

@@ -60,9 +60,9 @@ function main()
6060
CImGui.Text("HI!")
6161
CImGui.End()
6262

63-
NGL.imgui_end(context)
64-
glfwSwapBuffers(context.window)
65-
glfwPollEvents()
63+
NGL.imgui_end()
64+
GLFW.SwapBuffers(context.window)
65+
GLFW.PollEvents()
6666

6767
delta_time = time() - last_time
6868
last_time = time()

src/NeuralGraphicsGL.jl

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module NeuralGraphicsGL
22

33
using CImGui
4-
using CImGui.ImGuiGLFWBackend.LibGLFW
54
using FileIO
65
using ImageCore
76
using ImageIO
87
using LinearAlgebra
9-
using ModernGL
108
using StaticArrays
119

10+
using GLFW
11+
using ModernGL
12+
13+
import CImGui.lib as lib
14+
1215
"""
1316
Replaces:
1417
@@ -147,11 +150,11 @@ include("line.jl")
147150
include("frustum.jl")
148151
include("widget.jl")
149152

150-
const GLSL_VERSION = 410
153+
const GLSL_VERSION = 130
151154

152155
function init(version_major::Integer = 3, version_minor::Integer = 0)
153-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, version_major)
154-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, version_minor)
156+
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, version_major)
157+
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, version_minor)
155158
end
156159

157160
function get_gl_version()
@@ -161,10 +164,8 @@ function get_gl_version()
161164
end
162165

163166
struct Context
164-
window::Ptr{GLFWwindow}
167+
window::GLFW.Window
165168
imgui_ctx::Ptr{CImGui.ImGuiContext}
166-
glfw_ctx::CImGui.ImGuiGLFWBackend.Context
167-
gl_ctx::CImGui.ImGuiOpenGLBackend.Context
168169

169170
width::Int64
170171
height::Int64
@@ -181,45 +182,52 @@ function Context(
181182
error("You need to specify either `fullscreen` or `width` & `height` parameters.")
182183
end
183184

184-
glfwWindowHint(GLFW_VISIBLE, visible)
185-
if fullscreen
186-
glfwWindowHint(GLFW_RESIZABLE, false)
187-
monitor = glfwGetPrimaryMonitor()
188-
mode = unsafe_load(glfwGetVideoMode(monitor))
189-
window = glfwCreateWindow(mode.width, mode.height, title, monitor, C_NULL)
185+
imgui_ctx = CImGui.CreateContext()
186+
187+
GLFW.Init()
188+
GLFW.WindowHint(GLFW.VISIBLE, visible)
189+
190+
window = if fullscreen
191+
GLFW.WindowHint(GLFW.RESIZABLE, false)
192+
monitor = GLFW.GetPrimaryMonitor()
193+
mode = unsafe_load(GLFW.GetVideoMode(monitor))
194+
190195
width, height = mode.width, mode.height
196+
GLFW.CreateWindow(width, height, title, monitor)
191197
else
192-
glfwWindowHint(GLFW_RESIZABLE, resizable)
193-
window = glfwCreateWindow(width, height, title, C_NULL, C_NULL)
198+
GLFW.WindowHint(GLFW.RESIZABLE, resizable)
199+
GLFW.CreateWindow(width, height, title)
194200
end
195-
glfwMakeContextCurrent(window)
196-
glfwSwapInterval(vsync ? 1 : 0)
201+
@assert window != C_NULL
202+
203+
GLFW.MakeContextCurrent(window)
204+
GLFW.SwapInterval(vsync ? 1 : 0)
205+
206+
# Setup Platform/Renderer bindings.
207+
lib.ImGui_ImplGlfw_InitForOpenGL(Ptr{lib.GLFWwindow}(window.handle), true)
208+
lib.ImGui_ImplOpenGL3_Init("#version $GLSL_VERSION")
209+
197210
# You need this for RGB textures that their width is not a multiple of 4.
198211
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
199212

200-
#enable depth buffer
213+
# Enable depth buffer.
201214
glEnable(GL_DEPTH_TEST)
202215
glDepthMask(GL_TRUE)
203216
glClearDepth(1.0f0)
204217

205218
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
206219

207-
imgui_ctx = CImGui.CreateContext()
220+
# Set ImGui syle.
208221
CImGui.StyleColorsDark()
209222
style = CImGui.GetStyle()
210223
style.FrameRounding = 0f0
211224
style.WindowRounding = 0f0
212225
style.ScrollbarRounding = 0f0
213226

214-
glfw_ctx = CImGui.ImGuiGLFWBackend.create_context(window)
215-
gl_ctx = CImGui.ImGuiOpenGLBackend.create_context(GLSL_VERSION)
216-
217227
io = CImGui.GetIO()
218228
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_DockingEnable
219229

220-
CImGui.ImGuiGLFWBackend.init(glfw_ctx)
221-
CImGui.ImGuiOpenGLBackend.init(gl_ctx)
222-
Context(window, imgui_ctx, glfw_ctx, gl_ctx, width, height)
230+
Context(window, imgui_ctx, width, height)
223231
end
224232

225233
enable_blend() = glEnable(GL_BLEND)
@@ -236,32 +244,31 @@ disable_wireframe() = glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
236244

237245
function delete!(c::Context)
238246
imgui_shutdown!(c)
239-
glfwDestroyWindow(c.window)
247+
GLFW.DestroyWindow(c.window)
240248
end
241249

242250
function set_resizable_window!(c::Context, resizable::Bool)
243-
glfwSetWindowAttrib(c.window, GLFW_RESIZABLE, resizable)
251+
GLFW.SetWindowAttrib(c.window, GLFW.RESIZABLE, resizable)
244252
end
245253

246254
function set_resize_callback!(c::Context, callback)
247-
glfwSetWindowSizeCallback(
248-
c.window, @cfunction($callback, Cvoid, (Ptr{GLFWwindow}, Cint, Cint)))
255+
GLFW.SetWindowSizeCallback(c.window, callback)
249256
end
250257

251-
function imgui_begin(c::Context)
252-
CImGui.ImGuiOpenGLBackend.new_frame(c.gl_ctx)
253-
CImGui.ImGuiGLFWBackend.new_frame(c.glfw_ctx)
258+
function imgui_begin()
259+
lib.ImGui_ImplOpenGL3_NewFrame()
260+
lib.ImGui_ImplGlfw_NewFrame()
254261
CImGui.NewFrame()
255262
end
256263

257-
function imgui_end(c::Context)
264+
function imgui_end()
258265
CImGui.Render()
259-
CImGui.ImGuiOpenGLBackend.render(c.gl_ctx)
266+
lib.ImGui_ImplOpenGL3_RenderDrawData(Ptr{Cint}(CImGui.GetDrawData()))
260267
end
261268

262269
function imgui_shutdown!(c::Context)
263-
CImGui.ImGuiOpenGLBackend.shutdown(c.gl_ctx)
264-
CImGui.ImGuiGLFWBackend.shutdown(c.glfw_ctx)
270+
lib.ImGui_ImplOpenGL3_Shutdown()
271+
lib.ImGui_ImplGlfw_Shutdown()
265272
CImGui.DestroyContext(c.imgui_ctx)
266273
end
267274

@@ -271,13 +278,13 @@ set_clear_color(r, g, b, a) = glClearColor(r, g, b, a)
271278

272279
set_viewport(width, height) = glViewport(0, 0, width, height)
273280

274-
hide_cursor(w::GLFWwindow) = glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
281+
hide_cursor(w::GLFW.Window) = GLFW.SetInputMode(w, GLFW.CURSOR, GLFW.CURSOR_DISABLED)
275282

276-
show_cursor(w::GLFWwindow) = glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_NORMAL)
283+
show_cursor(w::GLFW.Window) = GLFW.SetInputMode(w, GLFW.CURSOR, GLFW.CURSOR_NORMAL)
277284

278285
function render_loop(draw_function, c::Context; destroy_context::Bool = true)
279286
try
280-
while glfwWindowShouldClose(c.window) == 0
287+
while GLFW.WindowShouldClose(c.window) == 0
281288
is_running = draw_function()
282289
is_running || break
283290
end

0 commit comments

Comments
 (0)