Skip to content

Commit 8aa1689

Browse files
committed
Add a README
1 parent bbef66b commit 8aa1689

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Dear ImGui.hs
2+
3+
> Dear ImGui is a **bloat-free graphical user interface library for C++**. It
4+
> outputs optimized vertex buffers that you can render anytime in your
5+
> 3D-pipeline enabled application. It is fast, portable, renderer agnostic and
6+
> self-contained (no external dependencies).
7+
8+
This project contains Haskell bindings to the
9+
[ImGui](https://github.com/ocornut/imgui) project. This allows you to rapidly
10+
put together graphical user interfaces in Haskell, with a particular focus to
11+
games and graphics intensive applications.
12+
13+
# Getting Started
14+
15+
`dear-imgui.hs` can be used like a normal Haskell library. If you use Cabal,
16+
simply add `dear-imgui` to your `build-depends`. ImGui supports a variety of
17+
backends, and you will need to choose your backend at configuration time.
18+
Backends can be enabled using Cabal flags, and these can be set through the
19+
`cabal.project` file. For example, if you want to use a combination of SDL and
20+
OpenGL:
21+
22+
```
23+
package dear-imgui
24+
flags: +sdl +opengl
25+
```
26+
27+
With this done, the following module is the "Hello, World!" of ImGui:
28+
29+
``` haskell
30+
{-# language BlockArguments #-}
31+
{-# language LambdaCase #-}
32+
{-# language OverloadedStrings #-}
33+
34+
module Main ( main ) where
35+
36+
import Control.Exception
37+
import Control.Monad.IO.Class
38+
import Control.Monad.Managed
39+
import DearImGui
40+
import DearImGui.OpenGL
41+
import DearImGui.SDL
42+
import DearImGui.SDL.OpenGL
43+
import Graphics.GL
44+
import SDL
45+
46+
main :: IO ()
47+
main = do
48+
-- Initialize SDL
49+
initializeAll
50+
51+
runManaged do
52+
-- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
53+
w <- do
54+
let title = "Hello, Dear ImGui!"
55+
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
56+
managed $ bracket (createWindow title config) destroyWindow
57+
58+
-- Create an OpenGL context
59+
glContext <- managed $ bracket (glCreateContext w) glDeleteContext
60+
61+
-- Create an ImGui context
62+
_ <- managed $ bracket createContext destroyContext
63+
64+
-- Initialize ImGui's SDL2 backend
65+
_ <- managed_ $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown
66+
67+
-- Initialize ImGui's OpenGL backend
68+
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
69+
70+
liftIO $ mainLoop w
71+
72+
73+
mainLoop :: Window -> IO ()
74+
mainLoop w = do
75+
-- Process the event loop
76+
untilNothingM pollEventWithImGui
77+
78+
-- Tell ImGui we're starting a new frame
79+
openGL2NewFrame
80+
sdl2NewFrame w
81+
newFrame
82+
83+
-- Build the GUI
84+
bracket_ (begin "Hello, ImGui!") end do
85+
-- Add a text widget
86+
text "Hello, ImGui!"
87+
88+
-- Add a button widget, and call 'putStrLn' when it's clicked
89+
button "Clickety Click" >>= \case
90+
False -> return ()
91+
True -> putStrLn "Ow!"
92+
93+
-- Render
94+
glClear GL_COLOR_BUFFER_BIT
95+
96+
render
97+
openGL2RenderDrawData =<< getDrawData
98+
99+
glSwapWindow w
100+
101+
mainLoop w
102+
103+
where
104+
untilNothingM m = m >>= maybe (return ()) (\_ -> untilNothingM m)
105+
```

examples/Readme.hs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
-- NOTE: If this is file is edited, please also copy and paste it into
2+
-- README.md.
3+
4+
{-# language BlockArguments #-}
5+
{-# language LambdaCase #-}
6+
{-# language OverloadedStrings #-}
7+
8+
module Main ( main ) where
9+
10+
import Control.Exception
11+
import Control.Monad.IO.Class
12+
import Control.Monad.Managed
13+
import DearImGui
14+
import DearImGui.OpenGL
15+
import DearImGui.SDL
16+
import DearImGui.SDL.OpenGL
17+
import Graphics.GL
18+
import SDL
19+
20+
main :: IO ()
21+
main = do
22+
-- Initialize SDL
23+
initializeAll
24+
25+
runManaged do
26+
-- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
27+
w <- do
28+
let title = "Hello, Dear ImGui!"
29+
let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }
30+
managed $ bracket (createWindow title config) destroyWindow
31+
32+
-- Create an OpenGL context
33+
glContext <- managed $ bracket (glCreateContext w) glDeleteContext
34+
35+
-- Create an ImGui context
36+
_ <- managed $ bracket createContext destroyContext
37+
38+
-- Initialize ImGui's SDL2 backend
39+
_ <- managed_ $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown
40+
41+
-- Initialize ImGui's OpenGL backend
42+
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
43+
44+
liftIO $ mainLoop w
45+
46+
47+
mainLoop :: Window -> IO ()
48+
mainLoop w = do
49+
-- Process the event loop
50+
untilNothingM pollEventWithImGui
51+
52+
-- Tell ImGui we're starting a new frame
53+
openGL2NewFrame
54+
sdl2NewFrame w
55+
newFrame
56+
57+
-- Build the GUI
58+
bracket_ (begin "Hello, ImGui!") end do
59+
-- Add a text widget
60+
text "Hello, ImGui!"
61+
62+
-- Add a button widget, and call 'putStrLn' when it's clicked
63+
button "Clickety Click" >>= \case
64+
False -> return ()
65+
True -> putStrLn "Ow!"
66+
67+
-- Render
68+
glClear GL_COLOR_BUFFER_BIT
69+
70+
render
71+
openGL2RenderDrawData =<< getDrawData
72+
73+
glSwapWindow w
74+
75+
mainLoop w
76+
77+
where
78+
untilNothingM m = m >>= maybe (return ()) (\_ -> untilNothingM m)

hs-dear-imgui.cabal

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ executable test
8383
default-language: Haskell2010
8484
build-depends: base, sdl2, gl, dear-imgui
8585
ghc-options: -Wall
86+
87+
88+
executable readme
89+
main-is: Readme.hs
90+
hs-source-dirs: examples
91+
default-language: Haskell2010
92+
build-depends: base, sdl2, gl, dear-imgui, managed
93+
ghc-options: -Wall

0 commit comments

Comments
 (0)