Haskell bindings to Dear ImGui, an immediate mode GUI toolkit
Go to file
2021-02-06 15:37:49 +00:00
.github/workflows Add Cachix (#14) 2021-01-27 09:08:33 +00:00
examples Merge branch 'main' of github.com:haskell-game/dear-imgui.hs into args 2021-02-06 15:28:41 +00:00
generator/DearImGui Parse enums from headers & generate code (#19) 2021-02-05 20:57:17 +00:00
imgui@58075c4414 Initial work 2021-01-24 15:27:03 +00:00
nix Add Haskell.nix and use it to provide a shell.nix (#3) 2021-01-25 16:47:56 +00:00
src Merge branch 'main' of github.com:haskell-game/dear-imgui.hs into args 2021-02-06 15:28:41 +00:00
.gitignore Ignore imgui.ini (#29) 2021-02-05 20:22:26 +00:00
.gitmodules Initial work 2021-01-24 15:27:03 +00:00
cabal.project Wrap the OpenGL 3 backend (#20) 2021-02-06 14:44:58 +00:00
dear-imgui.cabal Wrap the OpenGL 3 backend (#20) 2021-02-06 14:44:58 +00:00
default.nix Add support for GLFW (#26) 2021-02-05 21:44:52 +00:00
Example.png Update the README with the demo window and a screenshot 2021-01-25 09:18:13 +00:00
LICENSE Initial commit 2021-01-24 15:25:52 +00:00
Main.hs Merge branch 'main' of github.com:haskell-game/dear-imgui.hs into args 2021-02-06 15:28:41 +00:00
README.md Wrap the OpenGL 3 backend (#20) 2021-02-06 14:44:58 +00:00
shell.nix Add haskell-language-server to the dev shell 2021-02-06 15:36:52 +00:00

Dear ImGui.hs

Dear ImGui is a bloat-free graphical user interface library for C++. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline enabled application. It is fast, portable, renderer agnostic and self-contained (no external dependencies).

This project contains Haskell bindings to the ImGui project. This allows you to rapidly put together graphical user interfaces in Haskell, with a particular focus to games and graphics intensive applications.

Getting Started

To get started, we'll build the following:

dear-imgui.hs can be used like a normal Haskell library. If you use Cabal, simply add dear-imgui to your build-depends. ImGui supports a variety of backends, and you will need to choose your backend at configuration time. Backends can be enabled using Cabal flags, and these can be set through the cabal.project file. For example, if you want to use a combination of SDL and OpenGL:

package dear-imgui
  flags: +sdl +opengl

With this done, the following module is the "Hello, World!" of ImGui:

{-# language BlockArguments #-}
{-# language LambdaCase #-}
{-# language OverloadedStrings #-}

module Main ( main ) where

import Control.Exception
import Control.Monad.IO.Class
import Control.Monad.Managed
import DearImGui
import DearImGui.OpenGL2
import DearImGui.SDL
import DearImGui.SDL.OpenGL
import Graphics.GL
import SDL

main :: IO ()
main = do
  -- Initialize SDL
  initializeAll

  runManaged do
    -- Create a window using SDL. As we're using OpenGL, we need to enable OpenGL too.
    w <- 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

    -- Create an ImGui context
    _ <- managed $ bracket createContext destroyContext

    -- Initialize ImGui's SDL2 backend
    _ <- managed_ $ bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown

    -- Initialize ImGui's OpenGL backend
    _ <- managed_ $ bracket_ openGL2Init openGL2Shutdown

    liftIO $ mainLoop w


mainLoop :: Window -> IO ()
mainLoop w = do
  -- Process the event loop
  untilNothingM pollEventWithImGui

  -- Tell ImGui we're starting a new frame
  openGL2NewFrame
  sdl2NewFrame w
  newFrame

  -- Build the GUI
  bracket_ (begin "Hello, ImGui!") end do
    -- Add a text widget
    text "Hello, ImGui!"

    -- Add a button widget, and call 'putStrLn' when it's clicked
    button "Clickety Click" >>= \case
      False -> return ()
      True  -> putStrLn "Ow!"

  -- Show the ImGui demo window
  showDemoWindow

  -- Render
  glClear GL_COLOR_BUFFER_BIT

  render
  openGL2RenderDrawData =<< getDrawData

  glSwapWindow w

  mainLoop w

  where
    untilNothingM m = m >>= maybe (return ()) (\_ -> untilNothingM m)

Hacking

If you would like to help dear-imgui, here's how you can get started.

The best path to development is using Nix. Once you have Nix installed (either in your operating system, or by running NixOS), you can enter a development shell:

$ nix-shell

You should now be in a bash shell where you can run cabal build all, cabal run readme, etc.

If you experience any difficulties, please don't hesistate to raise an issue.

Getting Help

Feel free to raise bugs, questions and feature requests on the GitHub issue tracker.

We have a Matrix room at #dear-imgui.hs:ocharles.org.uk.