Skip to content

Commit 47402c1

Browse files
authored
Update README (#194)
1 parent 4d1c66e commit 47402c1

File tree

2 files changed

+66
-74
lines changed

2 files changed

+66
-74
lines changed

README.md

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,94 +31,90 @@ package dear-imgui
3131
With this done, the following module is the "Hello, World!" of ImGui:
3232

3333
``` haskell
34-
{-# language BlockArguments #-}
35-
{-# language LambdaCase #-}
3634
{-# language OverloadedStrings #-}
3735

3836
module Main ( main ) where
3937

40-
import Control.Exception
41-
import Control.Monad.IO.Class
42-
import Control.Monad.Managed
4338
import DearImGui
44-
import DearImGui.OpenGL2
39+
import DearImGui.OpenGL3
4540
import DearImGui.SDL
4641
import DearImGui.SDL.OpenGL
42+
4743
import Graphics.GL
4844
import SDL
4945

46+
import Control.Monad.Managed
47+
import Control.Monad.IO.Class ()
48+
import Control.Monad (when, unless)
49+
import Control.Exception (bracket, bracket_)
50+
5051
main :: IO ()
5152
main = do
5253
-- Initialize SDL
5354
initializeAll
5455

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
5758
window <- do
5859
let title = "Hello, Dear ImGui!"
5960
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
6061
managed $ bracket (createWindow title config) destroyWindow
6162

6263
-- Create an OpenGL context
6364
glContext <- managed $ bracket (glCreateContext window) glDeleteContext
64-
6565
-- Create an ImGui context
6666
_ <- managed $ bracket createContext destroyContext
6767

6868
-- Initialize ImGui's SDL2 backend
69-
_ <- managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
70-
69+
managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
7170
-- Initialize ImGui's OpenGL backend
72-
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
71+
managed_ $ bracket_ openGL3Init openGL3Shutdown
7372

7473
liftIO $ mainLoop window
7574

76-
7775
mainLoop :: Window -> IO ()
78-
mainLoop window = unlessQuit do
76+
mainLoop window = unlessQuit $ do
7977
-- Tell ImGui we're starting a new frame
80-
openGL2NewFrame
78+
openGL3NewFrame
8179
sdl2NewFrame
8280
newFrame
8381

8482
-- Build the GUI
85-
withWindowOpen "Hello, ImGui!" do
83+
withWindowOpen "Hello, ImGui!" $ do
8684
-- Add a text widget
8785
text "Hello, ImGui!"
8886

8987
-- 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!"
9390

9491
-- Show the ImGui demo window
9592
showDemoWindow
9693

9794
-- Render
9895
glClear GL_COLOR_BUFFER_BIT
99-
10096
render
101-
openGL2RenderDrawData =<< getDrawData
97+
openGL3RenderDrawData =<< getDrawData
10298

10399
glSwapWindow window
104-
105100
mainLoop window
106-
107101
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
122118
```
123119

124120
# Hacking

examples/Readme.hs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,87 @@
11
-- NOTE: If this is file is edited, please also copy and paste it into
22
-- README.md.
33

4-
{-# language BlockArguments #-}
5-
{-# language LambdaCase #-}
64
{-# language OverloadedStrings #-}
75

86
module Main ( main ) where
97

10-
import Control.Exception
11-
import Control.Monad.IO.Class
12-
import Control.Monad.Managed
138
import DearImGui
14-
import DearImGui.OpenGL2
9+
import DearImGui.OpenGL3
1510
import DearImGui.SDL
1611
import DearImGui.SDL.OpenGL
12+
1713
import Graphics.GL
1814
import SDL
1915

16+
import Control.Monad.Managed
17+
import Control.Monad.IO.Class ()
18+
import Control.Monad (when, unless)
19+
import Control.Exception (bracket, bracket_)
20+
2021
main :: IO ()
2122
main = do
2223
-- Initialize SDL
2324
initializeAll
2425

25-
runManaged do
26-
-- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
26+
runManaged $ do
27+
-- Create a window using SDL; as we're using OpenGL, we enable OpenGL too
2728
window <- do
2829
let title = "Hello, Dear ImGui!"
2930
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
3031
managed $ bracket (createWindow title config) destroyWindow
3132

3233
-- Create an OpenGL context
3334
glContext <- managed $ bracket (glCreateContext window) glDeleteContext
34-
3535
-- Create an ImGui context
3636
_ <- managed $ bracket createContext destroyContext
3737

3838
-- Initialize ImGui's SDL2 backend
39-
_ <- managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
40-
39+
managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
4140
-- Initialize ImGui's OpenGL backend
42-
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
41+
managed_ $ bracket_ openGL3Init openGL3Shutdown
4342

4443
liftIO $ mainLoop window
4544

46-
4745
mainLoop :: Window -> IO ()
48-
mainLoop window = unlessQuit do
46+
mainLoop window = unlessQuit $ do
4947
-- Tell ImGui we're starting a new frame
50-
openGL2NewFrame
48+
openGL3NewFrame
5149
sdl2NewFrame
5250
newFrame
5351

5452
-- Build the GUI
55-
withWindowOpen "Hello, ImGui!" do
53+
withWindowOpen "Hello, ImGui!" $ do
5654
-- Add a text widget
5755
text "Hello, ImGui!"
5856

5957
-- Add a button widget, and call 'putStrLn' when it's clicked
60-
button "Clickety Click" >>= \case
61-
False -> return ()
62-
True -> putStrLn "Ow!"
58+
button "Clickety Click" >>= \clicked ->
59+
when clicked $ putStrLn "Ow!"
6360

6461
-- Show the ImGui demo window
6562
showDemoWindow
6663

6764
-- Render
6865
glClear GL_COLOR_BUFFER_BIT
69-
7066
render
71-
openGL2RenderDrawData =<< getDrawData
67+
openGL3RenderDrawData =<< getDrawData
7268

7369
glSwapWindow window
74-
7570
mainLoop window
76-
7771
where
78-
-- Process the event loop
79-
unlessQuit action = do
80-
shouldQuit <- checkEvents
81-
if shouldQuit then pure () else action
82-
83-
checkEvents = do
84-
pollEventWithImGui >>= \case
85-
Nothing ->
86-
return False
87-
Just event ->
88-
(isQuit event ||) <$> checkEvents
89-
90-
isQuit event =
91-
SDL.eventPayload event == SDL.QuitEvent
72+
-- Process the event loop
73+
unlessQuit action = do
74+
shouldQuit <- gotQuitEvent
75+
unless shouldQuit action
76+
77+
gotQuitEvent = do
78+
ev <- pollEventWithImGui
79+
80+
case ev of
81+
Nothing ->
82+
return False
83+
Just event ->
84+
(isQuit event ||) <$> gotQuitEvent
85+
86+
isQuit event =
87+
eventPayload event == QuitEvent

0 commit comments

Comments
 (0)