Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Created February 28, 2019 02:14
Show Gist options
  • Save Gnimuc/d9b096e55ed28ee410627f9b3abcdee0 to your computer and use it in GitHub Desktop.
Save Gnimuc/d9b096e55ed28ee410627f9b3abcdee0 to your computer and use it in GitHub Desktop.
Nuklear works along side with CImGui
using Nuklear
using Nuklear.LibNuklear
using Nuklear.GLFWBackend
using CImGui
using CImGui.CSyntax
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
const WINDOW_WIDTH = 1200
const WINDOW_HEIGHT = 800
const MAX_VERTEX_BUFFER = 512 * 1024
const MAX_ELEMENT_BUFFER = 128 * 1024
@static if Sys.isapple()
const VERSION_MAJOR = 3
const VERSION_MINOR = 3
end
@static if Sys.isapple()
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, VERSION_MAJOR)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, VERSION_MINOR)
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)
GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE)
else
GLFW.DefaultWindowHints()
end
# set up GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)
# create window
window = GLFW.CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
# init context
nuklear_ctx = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER)
imgui_ctx = CImGui.CreateContext()
# setup Platform/Renderer bindings
CImGui.StyleColorsDark()
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(150)
# init font
nk_glfw3_font_stash_begin()
nk_glfw3_font_stash_end()
# states
show_imgui_window = true
while !GLFW.WindowShouldClose(window)
global show_imgui_window
GLFW.PollEvents()
ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplGlfw_NewFrame()
CImGui.NewFrame()
nk_glfw3_new_frame()
if Bool(nk_begin(nuklear_ctx, "Nuklear Window", nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
nk_layout_row_static(nuklear_ctx, 80, 80, 1)
nk_button_label(nuklear_ctx, "Open ImGui") == 1 && (show_imgui_window = true;)
nk_layout_row_static(nuklear_ctx, 80, 80, 1)
nk_button_label(nuklear_ctx, "Close ImGui") == 1 && (show_imgui_window = false;)
end
nk_end(nuklear_ctx)
if show_imgui_window
@c CImGui.Begin("ImGui Window", &show_imgui_window)
CImGui.Text("Hello from ImGui!")
CImGui.End()
end
# draw
CImGui.Render()
GLFW.MakeContextCurrent(window)
display_w, display_h = GLFW.GetFramebufferSize(window)
glViewport(0, 0, display_w, display_h)
glClear(GL_COLOR_BUFFER_BIT)
glClearColor(0.45, 0.55, 0.60, 1.00)
nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER)
ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())
GLFW.SwapBuffers(window)
end
nk_glfw3_shutdown()
ImGui_ImplOpenGL3_Shutdown()
ImGui_ImplGlfw_Shutdown()
CImGui.DestroyContext(imgui_ctx)
GLFW.DestroyWindow(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment