module Gelatin.SDL2
(
SDL2Backends(..)
, initSDL2Window
, startupSDL2BackendsWithWindow
, startupSDL2BackendsWithConfig
, startupSDL2Backends
, module Gelatin.GL
) where
import Control.Monad.Except (MonadError)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.String (fromString)
import Gelatin.GL
import SDL hiding (Rectangle, Renderer,
glBindTexture, glUnbindTexture)
data SDL2Backends = SDL2Backends
{ backendV2V4 :: Backend GLuint Event V2V4 (V2 Float) Float Raster
, backendV2V2 :: Backend GLuint Event V2V2 (V2 Float) Float Raster
}
initSDL2Window
:: MonadIO m
=> WindowConfig
-> String
-> m Window
initSDL2Window cfg title = liftIO $ do
initializeAll
w <- createWindow (fromString title) cfg
_ <- glCreateContext w
return w
startupSDL2BackendsWithWindow
:: (MonadIO m, MonadError String m)
=> Window
-> m SDL2Backends
startupSDL2BackendsWithWindow window = do
sh <- loadSimple2DShader
liftIO $ do
glEnable GL_BLEND
glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
let wsize = do V2 x y <- get $ windowSize window
return (fromIntegral x, fromIntegral y)
fsize = do V2 x y <- glGetDrawableSize window
return (fromIntegral x, fromIntegral y)
ctx = Context { ctxFramebufferSize = fsize
, ctxWindowSize = wsize
}
rz = Rez sh ctx
ops = glOps rz (updateWindowSDL2 window) pollEvents
v2v4 = Backend ops $ glV2V4Compiler rz
v2v2 = Backend ops $ glV2V2Compiler rz
return $ SDL2Backends v2v4 v2v2
startupSDL2BackendsWithConfig
:: (MonadIO m, MonadError String m)
=> WindowConfig
-> String
-> m SDL2Backends
startupSDL2BackendsWithConfig cfg str = do
w <- initSDL2Window cfg str
startupSDL2BackendsWithWindow w
startupSDL2Backends
:: (MonadIO m, MonadError String m)
=> Int
-> Int
-> String
-> Bool
-> m SDL2Backends
startupSDL2Backends ww wh ws highDPI = do
let openGL = defaultOpenGL{ glProfile = Core Debug 3 3
}
window = defaultWindow{ windowInitialSize = V2 (fromIntegral ww)
(fromIntegral wh)
, windowOpenGL = Just openGL
, windowResizable = True
, windowHighDPI = highDPI
}
startupSDL2BackendsWithConfig window ws
updateWindowSDL2 :: Window -> IO ()
updateWindowSDL2 = glSwapWindow