From f9412effde27fec49b6447ce2e8c54e4be2d4426 Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Sat, 6 Feb 2021 14:44:58 +0000 Subject: [PATCH] Wrap the OpenGL 3 backend (#20) --- Main.hs | 10 ++-- README.md | 2 +- cabal.project | 2 +- dear-imgui.cabal | 40 +++++++++----- examples/Readme.hs | 2 +- examples/glfw/Main.hs | 4 +- src/DearImGui/{OpenGL.hs => OpenGL2.hs} | 4 +- src/DearImGui/OpenGL3.hs | 69 +++++++++++++++++++++++++ 8 files changed, 109 insertions(+), 24 deletions(-) rename src/DearImGui/{OpenGL.hs => OpenGL2.hs} (96%) create mode 100644 src/DearImGui/OpenGL3.hs diff --git a/Main.hs b/Main.hs index 841af6c..951ff99 100644 --- a/Main.hs +++ b/Main.hs @@ -8,7 +8,7 @@ module Main (main) where import Control.Monad import Data.IORef import DearImGui -import DearImGui.OpenGL +import DearImGui.OpenGL3 import DearImGui.SDL import DearImGui.SDL.OpenGL import Control.Exception @@ -23,7 +23,7 @@ main = do bracket (glCreateContext w) glDeleteContext \glContext -> bracket createContext destroyContext \_imguiContext -> bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $ - bracket_ openGL2Init openGL2Shutdown do + bracket_ openGL3Init openGL3Shutdown do checkVersion styleColorsLight @@ -38,7 +38,7 @@ main = do tab2 <- newIORef True loop w checked color slider r pos size' selected tab1 tab2 - openGL2Shutdown + openGL3Shutdown loop @@ -56,7 +56,7 @@ loop loop w checked color slider r pos size' selected tab1Ref tab2Ref = do quit <- pollEvents - openGL2NewFrame + openGL3NewFrame sdl2NewFrame w newFrame @@ -162,7 +162,7 @@ loop w checked color slider r pos size' selected tab1Ref tab2Ref = do render glClear GL_COLOR_BUFFER_BIT - openGL2RenderDrawData =<< getDrawData + openGL3RenderDrawData =<< getDrawData glSwapWindow w diff --git a/README.md b/README.md index c39f231..7ad3ea5 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ import Control.Exception import Control.Monad.IO.Class import Control.Monad.Managed import DearImGui -import DearImGui.OpenGL +import DearImGui.OpenGL2 import DearImGui.SDL import DearImGui.SDL.OpenGL import Graphics.GL diff --git a/cabal.project b/cabal.project index 74f9cfc..a5f90d8 100644 --- a/cabal.project +++ b/cabal.project @@ -1,3 +1,3 @@ packages: *.cabal package dear-imgui - flags: +sdl2 +glfw +opengl +vulkan + flags: +sdl2 +glfw +opengl2 +opengl3 +vulkan diff --git a/dear-imgui.cabal b/dear-imgui.cabal index cf1f506..ba67363 100644 --- a/dear-imgui.cabal +++ b/dear-imgui.cabal @@ -5,9 +5,17 @@ build-type: Simple data-files: imgui/imgui.h -flag opengl +flag opengl2 description: - Enable OpenGL backend. + Enable OpenGL 2 backend. + default: + False + manual: + False + +flag opengl3 + description: + Enable OpenGL 3 backend. default: True manual: @@ -76,21 +84,29 @@ library , inline-c-cpp , StateVar - if flag(opengl) + if flag(opengl2) exposed-modules: - DearImGui.OpenGL + DearImGui.OpenGL2 cxx-sources: imgui/backends/imgui_impl_opengl2.cpp + build-depends: + gl + + if flag(opengl3) + exposed-modules: + DearImGui.OpenGL3 + cxx-sources: + imgui/backends/imgui_impl_opengl3.cpp if os(windows) - extra-libraries: - opengl32 + buildable: + False else if os(darwin) - frameworks: - OpenGL + buildable: + False else - extra-libraries: - GL + pkgconfig-depends: + glew if flag(vulkan) exposed-modules: @@ -128,7 +144,7 @@ library pkgconfig-depends: sdl2 - if flag(opengl) + if flag(opengl2) || flag(opengl3) exposed-modules: DearImGui.SDL.OpenGL @@ -148,7 +164,7 @@ library pkgconfig-depends: glfw3 - if flag(opengl) + if flag(opengl2) || flag(opengl3) exposed-modules: DearImGui.GLFW.OpenGL diff --git a/examples/Readme.hs b/examples/Readme.hs index 85b17a6..598d908 100644 --- a/examples/Readme.hs +++ b/examples/Readme.hs @@ -11,7 +11,7 @@ import Control.Exception import Control.Monad.IO.Class import Control.Monad.Managed import DearImGui -import DearImGui.OpenGL +import DearImGui.OpenGL2 import DearImGui.SDL import DearImGui.SDL.OpenGL import Graphics.GL diff --git a/examples/glfw/Main.hs b/examples/glfw/Main.hs index 161488e..f85c891 100644 --- a/examples/glfw/Main.hs +++ b/examples/glfw/Main.hs @@ -9,7 +9,7 @@ import Control.Monad import Control.Monad.IO.Class import Control.Monad.Managed import DearImGui -import DearImGui.OpenGL +import DearImGui.OpenGL2 import DearImGui.GLFW import DearImGui.GLFW.OpenGL import Graphics.GL @@ -79,4 +79,4 @@ mainLoop win = do GLFW.swapBuffers win - mainLoop win \ No newline at end of file + mainLoop win diff --git a/src/DearImGui/OpenGL.hs b/src/DearImGui/OpenGL2.hs similarity index 96% rename from src/DearImGui/OpenGL.hs rename to src/DearImGui/OpenGL2.hs index bfbaf30..a0417ee 100644 --- a/src/DearImGui/OpenGL.hs +++ b/src/DearImGui/OpenGL2.hs @@ -9,10 +9,10 @@ {-| Module: DearImGui.OpenGL -OpenGL backend for Dear ImGui. +OpenGL 2 backend for Dear ImGui. -} -module DearImGui.OpenGL +module DearImGui.OpenGL2 ( openGL2Init , openGL2Shutdown , openGL2NewFrame diff --git a/src/DearImGui/OpenGL3.hs b/src/DearImGui/OpenGL3.hs new file mode 100644 index 0000000..b9039ba --- /dev/null +++ b/src/DearImGui/OpenGL3.hs @@ -0,0 +1,69 @@ +{-# LANGUAGE BlockArguments #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE TemplateHaskell #-} + +{-| +Module: DearImGui.OpenGL + +OpenGL 3 backend for Dear ImGui. +-} + +module DearImGui.OpenGL3 + ( openGL3Init + , openGL3Shutdown + , openGL3NewFrame + , openGL3RenderDrawData + ) + where + +-- inline-c +import qualified Language.C.Inline as C + +-- inline-c-cpp +import qualified Language.C.Inline.Cpp as Cpp + +-- transformers +import Control.Monad.IO.Class + ( MonadIO, liftIO ) + +-- DearImGui +import DearImGui + ( DrawData(..) ) + + +C.context (Cpp.cppCtx <> C.bsCtx) +C.include "imgui.h" +C.include "GL/glew.h" +C.include "backends/imgui_impl_opengl3.h" +Cpp.using "namespace ImGui" + + +-- | Wraps @ImGui_ImplOpenGL3_Init@. +openGL3Init :: MonadIO m => m Bool +openGL3Init = liftIO $ + ( 0 /= ) <$> [C.block| bool { + glewInit(); + return ImGui_ImplOpenGL3_Init(); + } |] + + +-- | Wraps @ImGui_ImplOpenGL3_Shutdown@. +openGL3Shutdown :: MonadIO m => m () +openGL3Shutdown = liftIO do + [C.exp| void { ImGui_ImplOpenGL3_Shutdown(); } |] + + +-- | Wraps @ImGui_ImplOpenGL3_NewFrame@. +openGL3NewFrame :: MonadIO m => m () +openGL3NewFrame = liftIO do + [C.exp| void { ImGui_ImplOpenGL3_NewFrame(); } |] + + +-- | Wraps @ImGui_ImplOpenGL3_RenderDrawData@. +openGL3RenderDrawData :: MonadIO m => DrawData -> m () +openGL3RenderDrawData (DrawData ptr) = liftIO do + [C.exp| void { ImGui_ImplOpenGL3_RenderDrawData((ImDrawData*) $( void* ptr )) } |]