Skip to content

Commit bbef66b

Browse files
sheafocharles
authored andcommitted
add cabal flags for backend selection
1 parent 3c3646a commit bbef66b

File tree

6 files changed

+296
-100
lines changed

6 files changed

+296
-100
lines changed

Main.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module Main (main) where
77

88
import Data.IORef
99
import DearImGui
10+
import DearImGui.OpenGL
11+
import DearImGui.SDL
12+
import DearImGui.SDL.OpenGL
1013
import Control.Exception
1114
import Graphics.GL
1215
import SDL

hs-dear-imgui.cabal

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,79 @@ name: dear-imgui
33
version: 1.0.0
44
build-type: Simple
55

6+
flag opengl
7+
description:
8+
Enable OpenGL backend.
9+
default:
10+
True
11+
manual:
12+
False
13+
14+
flag sdl
15+
description:
16+
Enable SDL backend.
17+
default:
18+
True
19+
manual:
20+
False
21+
622
library
7-
exposed-modules: DearImGui
8-
hs-source-dirs: src
9-
default-language: Haskell2010
10-
ghc-options: -Wall
23+
exposed-modules:
24+
DearImGui
25+
hs-source-dirs:
26+
src
27+
default-language:
28+
Haskell2010
29+
ghc-options:
30+
-Wall
1131
cxx-sources:
1232
imgui/imgui.cpp
13-
imgui/backends/imgui_impl_opengl2.cpp
14-
imgui/backends/imgui_impl_sdl.cpp
33+
imgui/imgui_demo.cpp
34+
imgui/imgui_draw.cpp
1535
imgui/imgui_tables.cpp
1636
imgui/imgui_widgets.cpp
17-
imgui/imgui_draw.cpp
18-
imgui/imgui_demo.cpp
19-
cxx-options: -std=c++11
20-
extra-libraries: stdc++
21-
pkgconfig-depends: sdl2
22-
include-dirs: imgui
23-
build-depends: base, inline-c, inline-c-cpp, sdl2, StateVar
24-
extra-libraries: GL
37+
cxx-options:
38+
-std=c++11
39+
extra-libraries:
40+
stdc++
41+
include-dirs:
42+
imgui
43+
build-depends:
44+
base
45+
, inline-c
46+
, inline-c-cpp
47+
, StateVar
48+
49+
if flag(opengl)
50+
exposed-modules:
51+
DearImGui.OpenGL
52+
cxx-sources:
53+
imgui/backends/imgui_impl_opengl2.cpp
54+
if os(windows)
55+
extra-libraries:
56+
opengl32
57+
else
58+
extra-libraries:
59+
GL
60+
61+
if flag(sdl)
62+
exposed-modules:
63+
DearImGui.SDL
64+
build-depends:
65+
sdl2
66+
cxx-sources:
67+
imgui/backends/imgui_impl_sdl.cpp
68+
69+
if os(windows) || os(darwin)
70+
extra-libraries:
71+
sdl2
72+
else
73+
pkgconfig-depends:
74+
sdl2
75+
76+
if flag(opengl)
77+
exposed-modules:
78+
DearImGui.SDL.OpenGL
2579

2680

2781
executable test

src/DearImGui.hs

Lines changed: 20 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
{-# LANGUAGE QuasiQuotes #-}
77
{-# LANGUAGE TemplateHaskell #-}
88

9+
{-|
10+
Module: DearImGui
11+
12+
Main ImGui module, exporting the functions to create a GUI.
13+
-}
14+
915
module DearImGui
1016
( -- * Context Creation and Access
1117
Context(..)
@@ -20,18 +26,6 @@ module DearImGui
2026
, getDrawData
2127
, checkVersion
2228

23-
-- ** SDL2
24-
, sdl2InitForOpenGL
25-
, sdl2NewFrame
26-
, sdl2Shutdown
27-
, pollEventWithImGui
28-
29-
-- ** OpenGL 2
30-
, openGL2Init
31-
, openGL2Shutdown
32-
, openGL2NewFrame
33-
, openGL2RenderDrawData
34-
3529
-- * Demo, Debug, Information
3630
, showDemoWindow
3731
, showMetricsWindow
@@ -103,26 +97,28 @@ module DearImGui
10397
)
10498
where
10599

106-
import Control.Monad ( when )
107-
import Control.Monad.IO.Class ( MonadIO, liftIO )
100+
-- base
108101
import Data.Bool
109-
import Data.StateVar
110102
import Foreign
111103
import Foreign.C
104+
105+
-- inline-c
112106
import qualified Language.C.Inline as C
107+
108+
-- inline-c-cpp
113109
import qualified Language.C.Inline.Cpp as Cpp
114-
import SDL
115-
import SDL.Internal.Types
116-
import SDL.Raw.Enum as Raw
117-
import qualified SDL.Raw.Event as Raw
118-
import Unsafe.Coerce ( unsafeCoerce )
110+
111+
-- StateVar
112+
import Data.StateVar
113+
( HasGetter(get), HasSetter, ($=!) )
114+
115+
-- transformers
116+
import Control.Monad.IO.Class
117+
( MonadIO, liftIO )
118+
119119

120120
C.context (Cpp.cppCtx <> C.bsCtx)
121121
C.include "imgui.h"
122-
C.include "backends/imgui_impl_opengl2.h"
123-
C.include "backends/imgui_impl_sdl.h"
124-
C.include "SDL.h"
125-
C.include "SDL_opengl.h"
126122
Cpp.using "namespace ImGui"
127123

128124

@@ -184,68 +180,6 @@ checkVersion = liftIO do
184180
[C.exp| void { IMGUI_CHECKVERSION(); } |]
185181

186182

187-
-- | Wraps @ImGui_ImplSDL2_InitForOpenGL@.
188-
sdl2InitForOpenGL :: MonadIO m => Window -> GLContext -> m ()
189-
sdl2InitForOpenGL (Window windowPtr) glContext = liftIO do
190-
[C.exp| void { ImGui_ImplSDL2_InitForOpenGL((SDL_Window*)$(void* windowPtr), $(void* glContextPtr)); } |]
191-
where
192-
glContextPtr :: Ptr ()
193-
glContextPtr = unsafeCoerce glContext
194-
195-
196-
-- | Wraps @ImGui_ImplSDL2_NewFrame@.
197-
sdl2NewFrame :: MonadIO m => Window -> m ()
198-
sdl2NewFrame (Window windowPtr) = liftIO do
199-
[C.exp| void { ImGui_ImplSDL2_NewFrame((SDL_Window*)($(void* windowPtr))); } |]
200-
201-
202-
-- | Wraps @ImGui_ImplSDL2_Shutdown@.
203-
sdl2Shutdown :: MonadIO m => m ()
204-
sdl2Shutdown = liftIO do
205-
[C.exp| void { ImGui_ImplSDL2_Shutdown(); } |]
206-
207-
208-
-- | Call the SDL2 'pollEvent' function, while also dispatching the event to
209-
-- Dear ImGui. You should use this in your application instead of 'pollEvent'.
210-
pollEventWithImGui :: MonadIO m => m (Maybe Event)
211-
pollEventWithImGui = liftIO do
212-
alloca \evPtr -> do
213-
pumpEvents
214-
215-
-- We use NULL first to check if there's an event.
216-
nEvents <- Raw.peepEvents evPtr 1 Raw.SDL_PEEKEVENT Raw.SDL_FIRSTEVENT Raw.SDL_LASTEVENT
217-
218-
when (nEvents > 0) do
219-
let evPtr' = castPtr evPtr :: Ptr ()
220-
[C.exp| void { ImGui_ImplSDL2_ProcessEvent((SDL_Event*) $(void* evPtr')) } |]
221-
222-
pollEvent
223-
224-
225-
-- | Wraps @ImGui_ImplOpenGL2_Init@.
226-
openGL2Init :: MonadIO m => m ()
227-
openGL2Init = liftIO do
228-
[C.exp| void { ImGui_ImplOpenGL2_Init(); } |]
229-
230-
231-
-- | Wraps @ImGui_ImplOpenGL2_Shutdown@.
232-
openGL2Shutdown :: MonadIO m => m ()
233-
openGL2Shutdown = liftIO do
234-
[C.exp| void { ImGui_ImplOpenGL2_Shutdown(); } |]
235-
236-
237-
-- | Wraps @ImGui_ImplOpenGL2_NewFrame@.
238-
openGL2NewFrame :: MonadIO m => m ()
239-
openGL2NewFrame = liftIO do
240-
[C.exp| void { ImGui_ImplOpenGL2_NewFrame(); } |]
241-
242-
243-
-- | Wraps @ImGui_ImplOpenGL2_RenderDrawData@.
244-
openGL2RenderDrawData :: MonadIO m => DrawData -> m ()
245-
openGL2RenderDrawData (DrawData ptr) = liftIO do
246-
[C.exp| void { ImGui_ImplOpenGL2_RenderDrawData((ImDrawData*) $( void* ptr )) } |]
247-
248-
249183
-- | Create demo window. Demonstrate most ImGui features. Call this to learn
250184
-- about the library! Try to make it always available in your application!
251185
showDemoWindow :: MonadIO m => m ()

src/DearImGui/OpenGL.hs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{-# LANGUAGE BlockArguments #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
{-# LANGUAGE OverloadedStrings #-}
5+
{-# LANGUAGE PatternSynonyms #-}
6+
{-# LANGUAGE QuasiQuotes #-}
7+
{-# LANGUAGE TemplateHaskell #-}
8+
9+
{-|
10+
Module: DearImGui.OpenGL
11+
12+
OpenGL backend for Dear ImGui.
13+
-}
14+
15+
module DearImGui.OpenGL
16+
( openGL2Init
17+
, openGL2Shutdown
18+
, openGL2NewFrame
19+
, openGL2RenderDrawData
20+
)
21+
where
22+
23+
-- inline-c
24+
import qualified Language.C.Inline as C
25+
26+
-- inline-c-cpp
27+
import qualified Language.C.Inline.Cpp as Cpp
28+
29+
-- transformers
30+
import Control.Monad.IO.Class
31+
( MonadIO, liftIO )
32+
33+
-- DearImGui
34+
import DearImGui
35+
( DrawData(..) )
36+
37+
38+
C.context (Cpp.cppCtx <> C.bsCtx)
39+
C.include "imgui.h"
40+
C.include "backends/imgui_impl_opengl2.h"
41+
Cpp.using "namespace ImGui"
42+
43+
44+
-- | Wraps @ImGui_ImplOpenGL2_Init@.
45+
openGL2Init :: MonadIO m => m ()
46+
openGL2Init = liftIO do
47+
[C.exp| void { ImGui_ImplOpenGL2_Init(); } |]
48+
49+
50+
-- | Wraps @ImGui_ImplOpenGL2_Shutdown@.
51+
openGL2Shutdown :: MonadIO m => m ()
52+
openGL2Shutdown = liftIO do
53+
[C.exp| void { ImGui_ImplOpenGL2_Shutdown(); } |]
54+
55+
56+
-- | Wraps @ImGui_ImplOpenGL2_NewFrame@.
57+
openGL2NewFrame :: MonadIO m => m ()
58+
openGL2NewFrame = liftIO do
59+
[C.exp| void { ImGui_ImplOpenGL2_NewFrame(); } |]
60+
61+
62+
-- | Wraps @ImGui_ImplOpenGL2_RenderDrawData@.
63+
openGL2RenderDrawData :: MonadIO m => DrawData -> m ()
64+
openGL2RenderDrawData (DrawData ptr) = liftIO do
65+
[C.exp| void { ImGui_ImplOpenGL2_RenderDrawData((ImDrawData*) $( void* ptr )) } |]

src/DearImGui/SDL.hs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{-# LANGUAGE BlockArguments #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
{-# LANGUAGE OverloadedStrings #-}
5+
{-# LANGUAGE PatternSynonyms #-}
6+
{-# LANGUAGE QuasiQuotes #-}
7+
{-# LANGUAGE TemplateHaskell #-}
8+
9+
{-|
10+
Module: DearImGui.SDL
11+
12+
SDL2 specific functions backend for Dear ImGui.
13+
14+
Modules for initialising a backend with SDL2 can be found under the corresponding backend,
15+
e.g. "DearImGui.SDL.OpenGL".
16+
-}
17+
18+
module DearImGui.SDL (
19+
-- ** SDL2
20+
sdl2NewFrame
21+
, sdl2Shutdown
22+
, pollEventWithImGui
23+
)
24+
where
25+
26+
-- base
27+
import Control.Monad
28+
( when )
29+
import Foreign.Marshal.Alloc
30+
( alloca )
31+
import Foreign.Ptr
32+
( Ptr, castPtr )
33+
34+
-- inline-c
35+
import qualified Language.C.Inline as C
36+
37+
-- inline-c-cpp
38+
import qualified Language.C.Inline.Cpp as Cpp
39+
40+
-- sdl2
41+
import SDL
42+
import SDL.Internal.Types
43+
import SDL.Raw.Enum as Raw
44+
import qualified SDL.Raw.Event as Raw
45+
46+
-- transformers
47+
import Control.Monad.IO.Class
48+
( MonadIO, liftIO )
49+
50+
51+
C.context (Cpp.cppCtx <> C.bsCtx)
52+
C.include "imgui.h"
53+
C.include "backends/imgui_impl_sdl.h"
54+
C.include "SDL.h"
55+
Cpp.using "namespace ImGui"
56+
57+
58+
-- | Wraps @ImGui_ImplSDL2_NewFrame@.
59+
sdl2NewFrame :: MonadIO m => Window -> m ()
60+
sdl2NewFrame (Window windowPtr) = liftIO do
61+
[C.exp| void { ImGui_ImplSDL2_NewFrame((SDL_Window*)($(void* windowPtr))); } |]
62+
63+
64+
-- | Wraps @ImGui_ImplSDL2_Shutdown@.
65+
sdl2Shutdown :: MonadIO m => m ()
66+
sdl2Shutdown = liftIO do
67+
[C.exp| void { ImGui_ImplSDL2_Shutdown(); } |]
68+
69+
-- | Call the SDL2 'pollEvent' function, while also dispatching the event to
70+
-- Dear ImGui. You should use this in your application instead of 'pollEvent'.
71+
pollEventWithImGui :: MonadIO m => m (Maybe Event)
72+
pollEventWithImGui = liftIO do
73+
alloca \evPtr -> do
74+
pumpEvents
75+
76+
-- We use NULL first to check if there's an event.
77+
nEvents <- Raw.peepEvents evPtr 1 Raw.SDL_PEEKEVENT Raw.SDL_FIRSTEVENT Raw.SDL_LASTEVENT
78+
79+
when (nEvents > 0) do
80+
let evPtr' = castPtr evPtr :: Ptr ()
81+
[C.exp| void { ImGui_ImplSDL2_ProcessEvent((SDL_Event*) $(void* evPtr')) } |]
82+
83+
pollEvent

0 commit comments

Comments
 (0)