@@ -31,94 +31,90 @@ package dear-imgui
31
31
With this done, the following module is the "Hello, World!" of ImGui:
32
32
33
33
``` haskell
34
- {-# language BlockArguments #-}
35
- {-# language LambdaCase #-}
36
34
{-# language OverloadedStrings #-}
37
35
38
36
module Main ( main ) where
39
37
40
- import Control.Exception
41
- import Control.Monad.IO.Class
42
- import Control.Monad.Managed
43
38
import DearImGui
44
- import DearImGui.OpenGL2
39
+ import DearImGui.OpenGL3
45
40
import DearImGui.SDL
46
41
import DearImGui.SDL.OpenGL
42
+
47
43
import Graphics.GL
48
44
import SDL
49
45
46
+ import Control.Monad.Managed
47
+ import Control.Monad.IO.Class ()
48
+ import Control.Monad (when , unless )
49
+ import Control.Exception (bracket , bracket_ )
50
+
50
51
main :: IO ()
51
52
main = do
52
53
-- Initialize SDL
53
54
initializeAll
54
55
55
- runManaged do
56
- -- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
56
+ runManaged $ do
57
+ -- Create a window using SDL; as we're using OpenGL, we enable OpenGL too
57
58
window <- do
58
59
let title = " Hello, Dear ImGui!"
59
60
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
60
61
managed $ bracket (createWindow title config) destroyWindow
61
62
62
63
-- Create an OpenGL context
63
64
glContext <- managed $ bracket (glCreateContext window) glDeleteContext
64
-
65
65
-- Create an ImGui context
66
66
_ <- managed $ bracket createContext destroyContext
67
67
68
68
-- Initialize ImGui's SDL2 backend
69
- _ <- managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
70
-
69
+ managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
71
70
-- Initialize ImGui's OpenGL backend
72
- _ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
71
+ managed_ $ bracket_ openGL3Init openGL3Shutdown
73
72
74
73
liftIO $ mainLoop window
75
74
76
-
77
75
mainLoop :: Window -> IO ()
78
- mainLoop window = unlessQuit do
76
+ mainLoop window = unlessQuit $ do
79
77
-- Tell ImGui we're starting a new frame
80
- openGL2NewFrame
78
+ openGL3NewFrame
81
79
sdl2NewFrame
82
80
newFrame
83
81
84
82
-- Build the GUI
85
- withWindowOpen " Hello, ImGui!" do
83
+ withWindowOpen " Hello, ImGui!" $ do
86
84
-- Add a text widget
87
85
text " Hello, ImGui!"
88
86
89
87
-- Add a button widget, and call 'putStrLn' when it's clicked
90
- button " Clickety Click" >>= \ case
91
- False -> return ()
92
- True -> putStrLn " Ow!"
88
+ button " Clickety Click" >>= \ clicked ->
89
+ when clicked $ putStrLn " Ow!"
93
90
94
91
-- Show the ImGui demo window
95
92
showDemoWindow
96
93
97
94
-- Render
98
95
glClear GL_COLOR_BUFFER_BIT
99
-
100
96
render
101
- openGL2RenderDrawData =<< getDrawData
97
+ openGL3RenderDrawData =<< getDrawData
102
98
103
99
glSwapWindow window
104
-
105
100
mainLoop window
106
-
107
101
where
108
- -- Process the event loop
109
- unlessQuit action = do
110
- shouldQuit <- checkEvents
111
- if shouldQuit then pure () else action
112
-
113
- checkEvents = do
114
- pollEventWithImGui >>= \ case
115
- Nothing ->
116
- return False
117
- Just event ->
118
- (isQuit event || ) <$> checkEvents
119
-
120
- isQuit event =
121
- SDL. eventPayload event == SDL. QuitEvent
102
+ -- Process the event loop
103
+ unlessQuit action = do
104
+ shouldQuit <- gotQuitEvent
105
+ unless shouldQuit action
106
+
107
+ gotQuitEvent = do
108
+ ev <- pollEventWithImGui
109
+
110
+ case ev of
111
+ Nothing ->
112
+ return False
113
+ Just event ->
114
+ (isQuit event || ) <$> gotQuitEvent
115
+
116
+ isQuit event =
117
+ eventPayload event == QuitEvent
122
118
```
123
119
124
120
# Hacking
0 commit comments