GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.5 KiB

(import &comptime-only "Dependencies.cake" "CHelpers.cake")
;; (c-import &with-decls "imgui.h")
(c-import "imgui.h")
(add-c-search-directory-module "Dependencies/imgui")
(add-cpp-build-dependency "imgui.cpp"
(add-dependency-git-submodule clone-imgui "" "Dependencies/imgui")
(defmacro imgui-call (function symbol &rest &optional arguments any)
(if arguments
(tokenize-push output
(call (in ImGui (token-splice function))) (token-splice-rest arguments tokens))
(tokenize-push output
(call (in ImGui (token-splice function)))))
(return true))
(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"
(unless window
(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)
(imgui-call StyleColorsDark)
(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"
(* g-font-size-pixels g-dpi-scale)))
;; imgui-io.Fonts->GetTexDataAsRGBA32();
(call-on-ptr Build (field imgui-io Fonts)))
;; 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_ImplSDL2_NewFrame window)
(imgui-call NewFrame)
(imgui-call ShowDemoWindow)
(imgui-call Render)
(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))
(imgui-call DestroyContext)
(sdl-shutdown window)
(return 0))
(add-c-search-directory-module "Dependencies/imgui/backends"
(add-cpp-build-dependency "imgui_demo.cpp" "imgui_impl_sdl.cpp"
"ImGuiSDLOpenGL.cpp" "imgui_impl_opengl3.cpp"
(add-library-dependency "GL" "dl") ;; dl for gl3w
(add-build-options "-DIMGUI_IMPL_OPENGL_LOADER_GL3W")