Wrap the OpenGL 3 backend (#20)

This commit is contained in:
Ollie Charles 2021-02-06 14:44:58 +00:00 committed by GitHub
parent ac74572121
commit f9412effde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 109 additions and 24 deletions

10
Main.hs
View File

@ -8,7 +8,7 @@ module Main (main) where
import Control.Monad import Control.Monad
import Data.IORef import Data.IORef
import DearImGui import DearImGui
import DearImGui.OpenGL import DearImGui.OpenGL3
import DearImGui.SDL import DearImGui.SDL
import DearImGui.SDL.OpenGL import DearImGui.SDL.OpenGL
import Control.Exception import Control.Exception
@ -23,7 +23,7 @@ main = do
bracket (glCreateContext w) glDeleteContext \glContext -> bracket (glCreateContext w) glDeleteContext \glContext ->
bracket createContext destroyContext \_imguiContext -> bracket createContext destroyContext \_imguiContext ->
bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $
bracket_ openGL2Init openGL2Shutdown do bracket_ openGL3Init openGL3Shutdown do
checkVersion checkVersion
styleColorsLight styleColorsLight
@ -38,7 +38,7 @@ main = do
tab2 <- newIORef True tab2 <- newIORef True
loop w checked color slider r pos size' selected tab1 tab2 loop w checked color slider r pos size' selected tab1 tab2
openGL2Shutdown openGL3Shutdown
loop loop
@ -56,7 +56,7 @@ loop
loop w checked color slider r pos size' selected tab1Ref tab2Ref = do loop w checked color slider r pos size' selected tab1Ref tab2Ref = do
quit <- pollEvents quit <- pollEvents
openGL2NewFrame openGL3NewFrame
sdl2NewFrame w sdl2NewFrame w
newFrame newFrame
@ -162,7 +162,7 @@ loop w checked color slider r pos size' selected tab1Ref tab2Ref = do
render render
glClear GL_COLOR_BUFFER_BIT glClear GL_COLOR_BUFFER_BIT
openGL2RenderDrawData =<< getDrawData openGL3RenderDrawData =<< getDrawData
glSwapWindow w glSwapWindow w

View File

@ -41,7 +41,7 @@ import Control.Exception
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Managed import Control.Monad.Managed
import DearImGui import DearImGui
import DearImGui.OpenGL import DearImGui.OpenGL2
import DearImGui.SDL import DearImGui.SDL
import DearImGui.SDL.OpenGL import DearImGui.SDL.OpenGL
import Graphics.GL import Graphics.GL

View File

@ -1,3 +1,3 @@
packages: *.cabal packages: *.cabal
package dear-imgui package dear-imgui
flags: +sdl2 +glfw +opengl +vulkan flags: +sdl2 +glfw +opengl2 +opengl3 +vulkan

View File

@ -5,9 +5,17 @@ build-type: Simple
data-files: data-files:
imgui/imgui.h imgui/imgui.h
flag opengl flag opengl2
description: description:
Enable OpenGL backend. Enable OpenGL 2 backend.
default:
False
manual:
False
flag opengl3
description:
Enable OpenGL 3 backend.
default: default:
True True
manual: manual:
@ -76,21 +84,29 @@ library
, inline-c-cpp , inline-c-cpp
, StateVar , StateVar
if flag(opengl) if flag(opengl2)
exposed-modules: exposed-modules:
DearImGui.OpenGL DearImGui.OpenGL2
cxx-sources: cxx-sources:
imgui/backends/imgui_impl_opengl2.cpp 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) if os(windows)
extra-libraries: buildable:
opengl32 False
else else
if os(darwin) if os(darwin)
frameworks: buildable:
OpenGL False
else else
extra-libraries: pkgconfig-depends:
GL glew
if flag(vulkan) if flag(vulkan)
exposed-modules: exposed-modules:
@ -128,7 +144,7 @@ library
pkgconfig-depends: pkgconfig-depends:
sdl2 sdl2
if flag(opengl) if flag(opengl2) || flag(opengl3)
exposed-modules: exposed-modules:
DearImGui.SDL.OpenGL DearImGui.SDL.OpenGL
@ -148,7 +164,7 @@ library
pkgconfig-depends: pkgconfig-depends:
glfw3 glfw3
if flag(opengl) if flag(opengl2) || flag(opengl3)
exposed-modules: exposed-modules:
DearImGui.GLFW.OpenGL DearImGui.GLFW.OpenGL

View File

@ -11,7 +11,7 @@ import Control.Exception
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Managed import Control.Monad.Managed
import DearImGui import DearImGui
import DearImGui.OpenGL import DearImGui.OpenGL2
import DearImGui.SDL import DearImGui.SDL
import DearImGui.SDL.OpenGL import DearImGui.SDL.OpenGL
import Graphics.GL import Graphics.GL

View File

@ -9,7 +9,7 @@ import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Managed import Control.Monad.Managed
import DearImGui import DearImGui
import DearImGui.OpenGL import DearImGui.OpenGL2
import DearImGui.GLFW import DearImGui.GLFW
import DearImGui.GLFW.OpenGL import DearImGui.GLFW.OpenGL
import Graphics.GL import Graphics.GL

View File

@ -9,10 +9,10 @@
{-| {-|
Module: DearImGui.OpenGL Module: DearImGui.OpenGL
OpenGL backend for Dear ImGui. OpenGL 2 backend for Dear ImGui.
-} -}
module DearImGui.OpenGL module DearImGui.OpenGL2
( openGL2Init ( openGL2Init
, openGL2Shutdown , openGL2Shutdown
, openGL2NewFrame , openGL2NewFrame

69
src/DearImGui/OpenGL3.hs Normal file
View File

@ -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 )) } |]