|
| 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 | +``` |
0 commit comments