mirror of
https://github.com/Drezil/dear-imgui.hs.git
synced 2024-11-21 16:37:00 +00:00
Update README (#194)
This commit is contained in:
parent
4d1c66e9a1
commit
47402c1a93
66
README.md
66
README.md
@ -31,29 +31,30 @@ package dear-imgui
|
|||||||
With this done, the following module is the "Hello, World!" of ImGui:
|
With this done, the following module is the "Hello, World!" of ImGui:
|
||||||
|
|
||||||
``` haskell
|
``` haskell
|
||||||
{-# language BlockArguments #-}
|
|
||||||
{-# language LambdaCase #-}
|
|
||||||
{-# language OverloadedStrings #-}
|
{-# language OverloadedStrings #-}
|
||||||
|
|
||||||
module Main ( main ) where
|
module Main ( main ) where
|
||||||
|
|
||||||
import Control.Exception
|
|
||||||
import Control.Monad.IO.Class
|
|
||||||
import Control.Monad.Managed
|
|
||||||
import DearImGui
|
import DearImGui
|
||||||
import DearImGui.OpenGL2
|
import DearImGui.OpenGL3
|
||||||
import DearImGui.SDL
|
import DearImGui.SDL
|
||||||
import DearImGui.SDL.OpenGL
|
import DearImGui.SDL.OpenGL
|
||||||
|
|
||||||
import Graphics.GL
|
import Graphics.GL
|
||||||
import SDL
|
import SDL
|
||||||
|
|
||||||
|
import Control.Monad.Managed
|
||||||
|
import Control.Monad.IO.Class ()
|
||||||
|
import Control.Monad (when, unless)
|
||||||
|
import Control.Exception (bracket, bracket_)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
-- Initialize SDL
|
-- Initialize SDL
|
||||||
initializeAll
|
initializeAll
|
||||||
|
|
||||||
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 enable OpenGL too
|
||||||
window <- 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 }
|
||||||
@ -61,64 +62,59 @@ main = do
|
|||||||
|
|
||||||
-- Create an OpenGL context
|
-- Create an OpenGL context
|
||||||
glContext <- managed $ bracket (glCreateContext window) 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 window glContext) sdl2Shutdown
|
managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
|
||||||
|
|
||||||
-- Initialize ImGui's OpenGL backend
|
-- Initialize ImGui's OpenGL backend
|
||||||
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
|
managed_ $ bracket_ openGL3Init openGL3Shutdown
|
||||||
|
|
||||||
liftIO $ mainLoop window
|
liftIO $ mainLoop window
|
||||||
|
|
||||||
|
|
||||||
mainLoop :: Window -> IO ()
|
mainLoop :: Window -> IO ()
|
||||||
mainLoop window = unlessQuit do
|
mainLoop window = unlessQuit $ do
|
||||||
-- Tell ImGui we're starting a new frame
|
-- Tell ImGui we're starting a new frame
|
||||||
openGL2NewFrame
|
openGL3NewFrame
|
||||||
sdl2NewFrame
|
sdl2NewFrame
|
||||||
newFrame
|
newFrame
|
||||||
|
|
||||||
-- Build the GUI
|
-- Build the GUI
|
||||||
withWindowOpen "Hello, ImGui!" do
|
withWindowOpen "Hello, ImGui!" $ do
|
||||||
-- Add a text widget
|
-- Add a text widget
|
||||||
text "Hello, ImGui!"
|
text "Hello, ImGui!"
|
||||||
|
|
||||||
-- Add a button widget, and call 'putStrLn' when it's clicked
|
-- Add a button widget, and call 'putStrLn' when it's clicked
|
||||||
button "Clickety Click" >>= \case
|
button "Clickety Click" >>= \clicked ->
|
||||||
False -> return ()
|
when clicked $ putStrLn "Ow!"
|
||||||
True -> putStrLn "Ow!"
|
|
||||||
|
|
||||||
-- Show the ImGui demo window
|
-- Show the ImGui demo window
|
||||||
showDemoWindow
|
showDemoWindow
|
||||||
|
|
||||||
-- Render
|
-- Render
|
||||||
glClear GL_COLOR_BUFFER_BIT
|
glClear GL_COLOR_BUFFER_BIT
|
||||||
|
|
||||||
render
|
render
|
||||||
openGL2RenderDrawData =<< getDrawData
|
openGL3RenderDrawData =<< getDrawData
|
||||||
|
|
||||||
glSwapWindow window
|
glSwapWindow window
|
||||||
|
|
||||||
mainLoop window
|
mainLoop window
|
||||||
|
|
||||||
where
|
where
|
||||||
-- Process the event loop
|
-- Process the event loop
|
||||||
unlessQuit action = do
|
unlessQuit action = do
|
||||||
shouldQuit <- checkEvents
|
shouldQuit <- gotQuitEvent
|
||||||
if shouldQuit then pure () else action
|
unless shouldQuit action
|
||||||
|
|
||||||
checkEvents = do
|
gotQuitEvent = do
|
||||||
pollEventWithImGui >>= \case
|
ev <- pollEventWithImGui
|
||||||
Nothing ->
|
|
||||||
return False
|
|
||||||
Just event ->
|
|
||||||
(isQuit event ||) <$> checkEvents
|
|
||||||
|
|
||||||
isQuit event =
|
case ev of
|
||||||
SDL.eventPayload event == SDL.QuitEvent
|
Nothing ->
|
||||||
|
return False
|
||||||
|
Just event ->
|
||||||
|
(isQuit event ||) <$> gotQuitEvent
|
||||||
|
|
||||||
|
isQuit event =
|
||||||
|
eventPayload event == QuitEvent
|
||||||
```
|
```
|
||||||
|
|
||||||
# Hacking
|
# Hacking
|
||||||
|
@ -1,29 +1,30 @@
|
|||||||
-- NOTE: If this is file is edited, please also copy and paste it into
|
-- NOTE: If this is file is edited, please also copy and paste it into
|
||||||
-- README.md.
|
-- README.md.
|
||||||
|
|
||||||
{-# language BlockArguments #-}
|
|
||||||
{-# language LambdaCase #-}
|
|
||||||
{-# language OverloadedStrings #-}
|
{-# language OverloadedStrings #-}
|
||||||
|
|
||||||
module Main ( main ) where
|
module Main ( main ) where
|
||||||
|
|
||||||
import Control.Exception
|
|
||||||
import Control.Monad.IO.Class
|
|
||||||
import Control.Monad.Managed
|
|
||||||
import DearImGui
|
import DearImGui
|
||||||
import DearImGui.OpenGL2
|
import DearImGui.OpenGL3
|
||||||
import DearImGui.SDL
|
import DearImGui.SDL
|
||||||
import DearImGui.SDL.OpenGL
|
import DearImGui.SDL.OpenGL
|
||||||
|
|
||||||
import Graphics.GL
|
import Graphics.GL
|
||||||
import SDL
|
import SDL
|
||||||
|
|
||||||
|
import Control.Monad.Managed
|
||||||
|
import Control.Monad.IO.Class ()
|
||||||
|
import Control.Monad (when, unless)
|
||||||
|
import Control.Exception (bracket, bracket_)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
-- Initialize SDL
|
-- Initialize SDL
|
||||||
initializeAll
|
initializeAll
|
||||||
|
|
||||||
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 enable OpenGL too
|
||||||
window <- 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 }
|
||||||
@ -31,61 +32,56 @@ main = do
|
|||||||
|
|
||||||
-- Create an OpenGL context
|
-- Create an OpenGL context
|
||||||
glContext <- managed $ bracket (glCreateContext window) 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 window glContext) sdl2Shutdown
|
managed_ $ bracket_ (sdl2InitForOpenGL window glContext) sdl2Shutdown
|
||||||
|
|
||||||
-- Initialize ImGui's OpenGL backend
|
-- Initialize ImGui's OpenGL backend
|
||||||
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
|
managed_ $ bracket_ openGL3Init openGL3Shutdown
|
||||||
|
|
||||||
liftIO $ mainLoop window
|
liftIO $ mainLoop window
|
||||||
|
|
||||||
|
|
||||||
mainLoop :: Window -> IO ()
|
mainLoop :: Window -> IO ()
|
||||||
mainLoop window = unlessQuit do
|
mainLoop window = unlessQuit $ do
|
||||||
-- Tell ImGui we're starting a new frame
|
-- Tell ImGui we're starting a new frame
|
||||||
openGL2NewFrame
|
openGL3NewFrame
|
||||||
sdl2NewFrame
|
sdl2NewFrame
|
||||||
newFrame
|
newFrame
|
||||||
|
|
||||||
-- Build the GUI
|
-- Build the GUI
|
||||||
withWindowOpen "Hello, ImGui!" do
|
withWindowOpen "Hello, ImGui!" $ do
|
||||||
-- Add a text widget
|
-- Add a text widget
|
||||||
text "Hello, ImGui!"
|
text "Hello, ImGui!"
|
||||||
|
|
||||||
-- Add a button widget, and call 'putStrLn' when it's clicked
|
-- Add a button widget, and call 'putStrLn' when it's clicked
|
||||||
button "Clickety Click" >>= \case
|
button "Clickety Click" >>= \clicked ->
|
||||||
False -> return ()
|
when clicked $ putStrLn "Ow!"
|
||||||
True -> putStrLn "Ow!"
|
|
||||||
|
|
||||||
-- Show the ImGui demo window
|
-- Show the ImGui demo window
|
||||||
showDemoWindow
|
showDemoWindow
|
||||||
|
|
||||||
-- Render
|
-- Render
|
||||||
glClear GL_COLOR_BUFFER_BIT
|
glClear GL_COLOR_BUFFER_BIT
|
||||||
|
|
||||||
render
|
render
|
||||||
openGL2RenderDrawData =<< getDrawData
|
openGL3RenderDrawData =<< getDrawData
|
||||||
|
|
||||||
glSwapWindow window
|
glSwapWindow window
|
||||||
|
|
||||||
mainLoop window
|
mainLoop window
|
||||||
|
|
||||||
where
|
where
|
||||||
-- Process the event loop
|
-- Process the event loop
|
||||||
unlessQuit action = do
|
unlessQuit action = do
|
||||||
shouldQuit <- checkEvents
|
shouldQuit <- gotQuitEvent
|
||||||
if shouldQuit then pure () else action
|
unless shouldQuit action
|
||||||
|
|
||||||
checkEvents = do
|
gotQuitEvent = do
|
||||||
pollEventWithImGui >>= \case
|
ev <- pollEventWithImGui
|
||||||
Nothing ->
|
|
||||||
return False
|
|
||||||
Just event ->
|
|
||||||
(isQuit event ||) <$> checkEvents
|
|
||||||
|
|
||||||
isQuit event =
|
case ev of
|
||||||
SDL.eventPayload event == SDL.QuitEvent
|
Nothing ->
|
||||||
|
return False
|
||||||
|
Just event ->
|
||||||
|
(isQuit event ||) <$> gotQuitEvent
|
||||||
|
|
||||||
|
isQuit event =
|
||||||
|
eventPayload event == QuitEvent
|
||||||
|
Loading…
Reference in New Issue
Block a user