mirror of
https://github.com/Drezil/dear-imgui.hs.git
synced 2024-11-22 16:57:00 +00:00
parent
76ce7bb569
commit
b4bc36ca89
34
README.md
34
README.md
@ -54,38 +54,35 @@ main = do
|
|||||||
|
|
||||||
runManaged do
|
runManaged do
|
||||||
-- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
|
-- 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 title = "Hello, Dear ImGui!"
|
||||||
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
|
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
|
||||||
managed $ bracket (createWindow title config) destroyWindow
|
managed $ bracket (createWindow title config) destroyWindow
|
||||||
|
|
||||||
-- Create an OpenGL context
|
-- Create an OpenGL context
|
||||||
glContext <- managed $ bracket (glCreateContext w) glDeleteContext
|
glContext <- managed $ bracket (glCreateContext window) glDeleteContext
|
||||||
|
|
||||||
-- Create an ImGui context
|
-- Create an ImGui context
|
||||||
_ <- managed $ bracket createContext destroyContext
|
_ <- managed $ bracket createContext destroyContext
|
||||||
|
|
||||||
-- Initialize ImGui's SDL2 backend
|
-- Initialize ImGui's SDL2 backend
|
||||||
_ <- managed_ $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown
|
_ <- managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
|
||||||
|
|
||||||
-- Initialize ImGui's OpenGL backend
|
-- Initialize ImGui's OpenGL backend
|
||||||
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
|
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
|
||||||
|
|
||||||
liftIO $ mainLoop w
|
liftIO $ mainLoop window
|
||||||
|
|
||||||
|
|
||||||
mainLoop :: Window -> IO ()
|
mainLoop :: Window -> IO ()
|
||||||
mainLoop w = do
|
mainLoop window = unlessQuit do
|
||||||
-- Process the event loop
|
|
||||||
untilNothingM pollEventWithImGui
|
|
||||||
|
|
||||||
-- Tell ImGui we're starting a new frame
|
-- Tell ImGui we're starting a new frame
|
||||||
openGL2NewFrame
|
openGL2NewFrame
|
||||||
sdl2NewFrame
|
sdl2NewFrame
|
||||||
newFrame
|
newFrame
|
||||||
|
|
||||||
-- Build the GUI
|
-- Build the GUI
|
||||||
bracket_ (begin "Hello, ImGui!") end do
|
withWindowOpen "Hello, ImGui!" do
|
||||||
-- Add a text widget
|
-- Add a text widget
|
||||||
text "Hello, ImGui!"
|
text "Hello, ImGui!"
|
||||||
|
|
||||||
@ -103,12 +100,25 @@ mainLoop w = do
|
|||||||
render
|
render
|
||||||
openGL2RenderDrawData =<< getDrawData
|
openGL2RenderDrawData =<< getDrawData
|
||||||
|
|
||||||
glSwapWindow w
|
glSwapWindow window
|
||||||
|
|
||||||
mainLoop w
|
mainLoop window
|
||||||
|
|
||||||
where
|
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
|
# Hacking
|
||||||
|
@ -45,10 +45,7 @@ main = do
|
|||||||
|
|
||||||
|
|
||||||
mainLoop :: Window -> IO ()
|
mainLoop :: Window -> IO ()
|
||||||
mainLoop window = do
|
mainLoop window = unlessQuit do
|
||||||
-- Process the event loop
|
|
||||||
untilNothingM pollEventWithImGui
|
|
||||||
|
|
||||||
-- Tell ImGui we're starting a new frame
|
-- Tell ImGui we're starting a new frame
|
||||||
openGL2NewFrame
|
openGL2NewFrame
|
||||||
sdl2NewFrame
|
sdl2NewFrame
|
||||||
@ -78,4 +75,17 @@ mainLoop window = do
|
|||||||
mainLoop window
|
mainLoop window
|
||||||
|
|
||||||
where
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user