Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cartazio opened this issue Sep 3, 2023 · 2 comments

Comments

@cartazio
Copy link
Contributor

cartazio commented Sep 3, 2023

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

@dpwiz
Copy link
Member

dpwiz commented Sep 3, 2023

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?

@cartazio
Copy link
Contributor Author

cartazio commented Sep 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants