From b4bc36ca8979da0a3b5d7c35be78d87cfa12ae37 Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Tue, 14 Sep 2021 15:41:38 +0300 Subject: [PATCH] Update readme example (#103) Fixes #68 --- README.md | 34 ++++++++++++++++++++++------------ examples/Readme.hs | 20 +++++++++++++++----- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b32f884..3a57da4 100644 --- a/README.md +++ b/README.md @@ -54,38 +54,35 @@ main = do runManaged do -- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too. - w <- do + window <- do let title = "Hello, Dear ImGui!" let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL } managed $ bracket (createWindow title config) destroyWindow -- Create an OpenGL context - glContext <- managed $ bracket (glCreateContext w) glDeleteContext + glContext <- managed $ bracket (glCreateContext window) glDeleteContext -- Create an ImGui context _ <- managed $ bracket createContext destroyContext -- Initialize ImGui's SDL2 backend - _ <- managed_ $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown + _ <- managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown -- Initialize ImGui's OpenGL backend _ <- managed_ $ bracket_ openGL2Init openGL2Shutdown - liftIO $ mainLoop w + liftIO $ mainLoop window mainLoop :: Window -> IO () -mainLoop w = do - -- Process the event loop - untilNothingM pollEventWithImGui - +mainLoop window = unlessQuit do -- Tell ImGui we're starting a new frame openGL2NewFrame sdl2NewFrame newFrame -- Build the GUI - bracket_ (begin "Hello, ImGui!") end do + withWindowOpen "Hello, ImGui!" do -- Add a text widget text "Hello, ImGui!" @@ -103,12 +100,25 @@ mainLoop w = do render openGL2RenderDrawData =<< getDrawData - glSwapWindow w + glSwapWindow window - mainLoop w + mainLoop window where - untilNothingM m = m >>= maybe (return ()) (\_ -> untilNothingM m) + -- Process the event loop + unlessQuit action = do + shouldQuit <- checkEvents + if shouldQuit then pure () else action + + checkEvents = do + pollEventWithImGui >>= \case + Nothing -> + return False + Just event -> + (isQuit event ||) <$> checkEvents + + isQuit event = + SDL.eventPayload event == SDL.QuitEvent ``` # Hacking diff --git a/examples/Readme.hs b/examples/Readme.hs index 405aa65..918ed13 100644 --- a/examples/Readme.hs +++ b/examples/Readme.hs @@ -45,10 +45,7 @@ main = do mainLoop :: Window -> IO () -mainLoop window = do - -- Process the event loop - untilNothingM pollEventWithImGui - +mainLoop window = unlessQuit do -- Tell ImGui we're starting a new frame openGL2NewFrame sdl2NewFrame @@ -78,4 +75,17 @@ mainLoop window = do mainLoop window where - untilNothingM m = m >>= maybe (return ()) (\_ -> untilNothingM m) + -- Process the event loop + unlessQuit action = do + shouldQuit <- checkEvents + if shouldQuit then pure () else action + + checkEvents = do + pollEventWithImGui >>= \case + Nothing -> + return False + Just event -> + (isQuit event ||) <$> checkEvents + + isQuit event = + SDL.eventPayload event == SDL.QuitEvent