dear-imgui.hs icon indicating copy to clipboard operation
dear-imgui.hs copied to clipboard

strange error when trying to do Compatibility 4 1 profile vs Core 4 1 profile

Open cartazio opened this issue 1 year ago • 2 comments

Hey! So after some futzing, i got the readme example to work wiht >= opengl3 flavor stuff. as below.

i did hit a strange crash i'm not sure how to understand when i tried a compatiblity profile (not that i expect to need it, but was just trying things)

carterschonwald@CarterHydra ~/W/p/a/genicad (main) [1]> cabal run exe:trash_demo
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
 - genicad-0.1.0.0 (exe:trash_demo) (file app/Trash.hs changed)
Preprocessing executable 'trash_demo' for genicad-0.1.0.0..
Building executable 'trash_demo' for genicad-0.1.0.0..
trash_demo: SDLCallFailed {sdlExceptionCaller = "SDL.Video.glCreateContext", sdlFunction = "SDL_GL_CreateContext", sdlExceptionError = "Failed creating OpenGL context at version requested"}

the full code with working core profile is as below

{-# LANGUAGE OverloadedStrings,GHC2021 #-}

module Main ( main ) where

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

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.
    window <- do
      let title = "Hello, Dear ImGui!"
      let config = defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL{glProfile=Core Normal 4 1} , windowResizable       = True }
      managed $ bracket (createWindow title config) destroyWindow

    -- Create an OpenGL context
    glContext <- managed $ bracket (glCreateContext window) glDeleteContext

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

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

    -- Initialize ImGui's OpenGL backend
    _ <- managed_ $ bracket_ openGL3Init openGL3Shutdown

    liftIO $ mainLoop window


mainLoop :: Window -> IO ()
mainLoop window = unlessQuit $ do
  -- Tell ImGui we're starting a new frame
  openGL3NewFrame
  sdl2NewFrame
  newFrame

  -- Build the GUI
  withWindowOpen "Hello, ImGui!" $ do
    -- Add a text widget
    text "Hello, ImGui!"

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

  -- Show the ImGui demo window
  showDemoWindow

  -- Render
  glClear GL_COLOR_BUFFER_BIT

  render
  openGL3RenderDrawData =<< getDrawData

  glSwapWindow window

  mainLoop window

  where
    -- Process the event loop
    unlessQuit action = do
      shouldQuit <- checkEvents
      if shouldQuit then pure () else action

    checkEvents = do
      pollEventWithImGui >>= \x -> case x of 
        Nothing ->
          return False
        Just event ->
          (isQuit event ||) <$> checkEvents

    isQuit event =
      SDL.eventPayload event == SDL.QuitEvent


cartazio avatar Sep 03 '23 17:09 cartazio

So... what is the problem here?

Readme example uses OpenGL2 and its defaultOpengl requests a version 2 compat context.

You tried to request the 4.1 profile and it failed?.. Does it work without the dear-imgui stuff?

dpwiz avatar Sep 03 '23 17:09 dpwiz

it works when i set Core 4 1, but fails when I set the context to Compatibility 4 1, this might be an SDL2 binding/library issue

cartazio avatar Sep 03 '23 20:09 cartazio