Browse Source

Moved ImGui test to helper macro

It's a bit gross, but it's nice to have a way to very quickly get a
GUI. I think that's worth the price.
windows-imgui
Macoy Madson 9 months ago
parent
commit
0946c4ea85
  1. 252
      src/ImGui.cake

252
src/ImGui.cake

@ -82,126 +82,152 @@
(comptime-cond
('auto-test
(import "SDL.cake")
(c-import "<cstdio>" "SDL.h"
"imgui_impl_sdl.h" ;; Platform (inputs etc.)
"imgui_impl_opengl3.h") ;; Rendering
;; From ImGuiSDLOpenGL.cpp
(declare-extern-function ImGuiSDLOpenGL_SetAttributes (glsl-version (* (* (const char)))))
(declare-extern-function ImGuiSDLOpenGL_InitializeGLLoader (&return bool))
(declare-extern-function ImGuiSDLOpenGL_ClearWindow (io (* ImGuiIO) clear-color ImVec4))
(defun test--imgui-main (&return int)
(printf "Hello ImGui\n")
(var glsl-version (* (const char)) null)
(ImGuiSDLOpenGL_SetAttributes (addr glsl-version))
(var window (* SDL_Window) null)
(set window
(SDL_CreateWindow "GameLib ImGui"
SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED 1920 1080
(bit-or SDL_WINDOW_RESIZABLE SDL_WINDOW_OPENGL)))
(unless window
(sdl-print-error)
(return 1))
(var gl-context SDL_GLContext (SDL_GL_CreateContext window))
(SDL_GL_MakeCurrent window gl-context)
(SDL_GL_SetSwapInterval 1) ;; Enable vsync
(unless (ImGuiSDLOpenGL_InitializeGLLoader)
(printf "Failed ImGuiSDLOpenGL_InitializeGLLoader\n")
(return 1))
(imgui-call CreateContext)
(scope ;; Enable docking
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(set (field imgui-io ConfigFlags) (bit-or (field imgui-io ConfigFlags)
ImGuiConfigFlags_DockingEnable)))
(scope ;; Enable keyboard navigation
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(set (field imgui-io ConfigFlags) (bit-or (field imgui-io ConfigFlags)
ImGuiConfigFlags_NavEnableKeyboard)))
(var g-dpi-scale float 1.7f)
(scope ;; DPI scaling
(var style (& ImGuiStyle) (imgui-call GetStyle))
(call-on ScaleAllSizes style g-dpi-scale))
(scope ;; Font
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(var g-font-size-pixels int 16)
(var ubuntu-mono-font (* ImFont)
(call-on-ptr AddFontFromFileTTF (field imgui-io Fonts)
;; "data/Fonts/UbuntuMono-R.ttf"
"data/Fonts/Ubuntu-R.ttf"
(* g-font-size-pixels g-dpi-scale)))
;; imgui-io.Fonts->GetTexDataAsRGBA32();
(call-on-ptr Build (field imgui-io Fonts)))
;; (imgui-call StyleColorsDark)
(imgui-set-style-red-dark)
;; Note: Passing in null for the context will break Viewports branch of ImGui
(unless (ImGui_ImplSDL2_InitForOpenGL window gl-context)
(printf "Failed ImGui_ImplSDL2_InitForOpenGL\n")
(return 1))
(ImGui_ImplOpenGL3_Init glsl-version)
(var exit-reason (* (const char)) null)
(while (not exit-reason)
(var event SDL_Event)
(while (SDL_PollEvent (addr event))
(ImGui_ImplSDL2_ProcessEvent (addr event))
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window event")))
(var currentKeyStates (* (const Uint8)) (SDL_GetKeyboardState null))
(when (at SDL_SCANCODE_ESCAPE currentKeyStates)
(set exit-reason "Escape pressed"))
(ImGui_ImplOpenGL3_NewFrame)
(ImGui_ImplSDL2_NewFrame window)
(imgui-call NewFrame)
(imgui-call DockSpaceOverViewport (imgui-call GetMainViewport))
(imgui-call ShowDemoWindow)
(imgui-call Render)
(scope ;; Probably will be handled by your game instead
(imgui-use-sdl-gl3w)
(c-import "<cstdio>")
(make-imgui-sdl-gl3w-application ;; If you want your own entry point, check this macro's body
test--imgui-main
"GameLib ImGui test"
(scope (printf "Initializing\n")) ;; Initialization
(scope ;; Once per frame
(imgui-call ShowDemoWindow))
(scope (printf "Shut down\n"))))) ;; Shutdown
;; Different loaders are available. Loader selection is done via macro to allow that flexibility
(defmacro imgui-use-sdl-gl3w ()
(tokenize-push output
(import "SDL.cake")
(c-import "SDL.h"
"imgui_impl_sdl.h" ;; Platform (inputs etc.)
"imgui_impl_opengl3.h") ;; Rendering
;; From ImGuiSDLOpenGL.cpp
(declare-extern-function ImGuiSDLOpenGL_SetAttributes (glsl-version (* (* (const char)))))
(declare-extern-function ImGuiSDLOpenGL_InitializeGLLoader (&return bool))
(declare-extern-function ImGuiSDLOpenGL_ClearWindow (io (* ImGuiIO) clear-color ImVec4))
(add-c-search-directory-module "Dependencies/imgui/backends"
"Dependencies/imgui/examples/libs/gl3w"
"Dependencies/imgui/examples/libs/gl3w/GL")
(add-cpp-build-dependency "imgui_demo.cpp" "imgui_impl_sdl.cpp"
"ImGuiSDLOpenGL.cpp" "imgui_impl_opengl3.cpp"
"gl3w.c")
(add-library-dependency "GL" "dl") ;; dl for gl3w
(add-build-options "-DIMGUI_IMPL_OPENGL_LOADER_GL3W")
(module-use-sdl-build-options))
(return true))
;; When you just don't care about the details and want a ImGui window
(defmacro make-imgui-sdl-gl3w-application (entry-point-name symbol
window-name string
initialization array
once-per-frame array
shutdown array)
(tokenize-push output
(defun (token-splice entry-point-name) (&return int)
(printf "Hello ImGui\n")
(var glsl-version (* (const char)) null)
(ImGuiSDLOpenGL_SetAttributes (addr glsl-version))
(var window (* SDL_Window) null)
(set window
(SDL_CreateWindow (token-splice window-name)
SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED 1920 1080
(bit-or SDL_WINDOW_RESIZABLE SDL_WINDOW_OPENGL)))
(unless window
(sdl-print-error)
(return 1))
(var gl-context SDL_GLContext (SDL_GL_CreateContext window))
(SDL_GL_MakeCurrent window gl-context)
(SDL_GL_SetSwapInterval 1) ;; Enable vsync
(unless (ImGuiSDLOpenGL_InitializeGLLoader)
(printf "Failed ImGuiSDLOpenGL_InitializeGLLoader\n")
(return 1))
(imgui-call CreateContext)
(scope ;; Enable docking
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(set (field imgui-io ConfigFlags) (bit-or (field imgui-io ConfigFlags)
ImGuiConfigFlags_DockingEnable)))
(scope ;; Enable keyboard navigation
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(var clear-color ImVec4 (array 0.2f 0.2f 0.2f 1.f))
(ImGuiSDLOpenGL_ClearWindow (addr imgui-io) clear-color))
(set (field imgui-io ConfigFlags) (bit-or (field imgui-io ConfigFlags)
ImGuiConfigFlags_NavEnableKeyboard)))
(var g-dpi-scale float 1.7f)
(scope ;; DPI scaling
(var style (& ImGuiStyle) (imgui-call GetStyle))
(call-on ScaleAllSizes style g-dpi-scale))
(scope ;; Font
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(var g-font-size-pixels int 16)
(var ubuntu-mono-font (* ImFont)
(call-on-ptr AddFontFromFileTTF (field imgui-io Fonts)
;; "data/Fonts/UbuntuMono-R.ttf"
"data/Fonts/Ubuntu-R.ttf"
(* g-font-size-pixels g-dpi-scale)))
;; imgui-io.Fonts->GetTexDataAsRGBA32();
(call-on-ptr Build (field imgui-io Fonts)))
;; (imgui-call StyleColorsDark)
(imgui-set-style-red-dark)
(ImGui_ImplOpenGL3_RenderDrawData (imgui-call GetDrawData))
(SDL_GL_SwapWindow window))
;; Note: Passing in null for the context will break Viewports branch of ImGui
(unless (ImGui_ImplSDL2_InitForOpenGL window gl-context)
(printf "Failed ImGui_ImplSDL2_InitForOpenGL\n")
(return 1))
(ImGui_ImplOpenGL3_Init glsl-version)
(when exit-reason
(printf "Exiting. Reason: %s\n" exit-reason))
(token-splice initialization)
(ImGui_ImplSDL2_Shutdown)
(var exit-reason (* (const char)) null)
(while (not exit-reason)
(var event SDL_Event)
(while (SDL_PollEvent (addr event))
(ImGui_ImplSDL2_ProcessEvent (addr event))
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window event")))
(imgui-call DestroyContext)
(var currentKeyStates (* (const Uint8)) (SDL_GetKeyboardState null))
(when (at SDL_SCANCODE_ESCAPE currentKeyStates)
(set exit-reason "Escape pressed"))
(sdl-shutdown window)
(return 0))
(ImGui_ImplOpenGL3_NewFrame)
(ImGui_ImplSDL2_NewFrame window)
(imgui-call NewFrame)
(add-c-search-directory-module "Dependencies/imgui/backends"
"Dependencies/imgui/examples/libs/gl3w"
"Dependencies/imgui/examples/libs/gl3w/GL")
(add-cpp-build-dependency "imgui_demo.cpp" "imgui_impl_sdl.cpp"
"ImGuiSDLOpenGL.cpp" "imgui_impl_opengl3.cpp"
"gl3w.c")
(imgui-call DockSpaceOverViewport (imgui-call GetMainViewport))
(add-library-dependency "GL" "dl") ;; dl for gl3w
(token-splice once-per-frame)
(add-build-options "-DIMGUI_IMPL_OPENGL_LOADER_GL3W")
(imgui-call Render)
(module-use-sdl-build-options)))
(scope ;; Probably will be handled by your game instead
(var imgui-io (& ImGuiIO) (imgui-call GetIO))
(var clear-color ImVec4 (array 0.2f 0.2f 0.2f 1.f))
(ImGuiSDLOpenGL_ClearWindow (addr imgui-io) clear-color))
(ImGui_ImplOpenGL3_RenderDrawData (imgui-call GetDrawData))
(SDL_GL_SwapWindow window))
(when exit-reason
(printf "Exiting. Reason: %s\n" exit-reason))
(token-splice shutdown)
(ImGui_ImplSDL2_Shutdown)
(imgui-call DestroyContext)
(sdl-shutdown window)
(return 0)))
(return true))

Loading…
Cancel
Save