ibis/src/Util.hs

85 lines
1.8 KiB
Haskell

-- {-# LANGUAGE ForeignFunctionInterface #-}
module Util where
import qualified SDL hiding (V2)
import qualified Graphics.Rendering.OpenGL as GL
import Data.Int (Int32(..))
import Options.Applicative
import Control.Monad (when)
import System.IO
import Foreign.C.Types (CInt(..))
import Linear
-- internal imports
import Types
-- foreign import ccall unsafe "glewInit"
-- glewInit :: IO CInt
glWindowConfig :: SDL.WindowConfig
glWindowConfig = SDL.defaultWindow
{ SDL.windowResizable = True
, SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 3
}
}
putErrLn :: String -> IO ()
putErrLn s = hPutStrLn stderr s
options :: Parser Options
options = Options
<$> switch
( long "fullscreen"
<> short 'f'
<> help "Toggle fullscreen"
<> showDefault
)
<*> switch
( long "flip"
<> short 'p'
<> help "Flip screens"
<> showDefault
)
<*> switch
( long "verbose"
<> short 'v'
<> help "Show verbose console output"
<> showDefault
)
<*> argument str
( help "Input file"
<> metavar "FILE"
)
makeWindowConfig :: SDL.Display -> SDL.WindowConfig
makeWindowConfig d =
glWindowConfig
{ SDL.windowPosition = SDL.Absolute (SDL.displayBoundsPosition d)
}
verb :: Options -> String -> IO ()
verb opts str = do
when (optVerbose opts) $
putStrLn str
fitViewport :: Double -> V2 Int32 -> IO ()
fitViewport ratio (V2 w h) = do
if fromIntegral w / fromIntegral h > ratio
then do
let nw = floor (fromIntegral h * ratio)
dw = floor ((fromIntegral w - fromIntegral nw) / 2 :: Double)
GL.viewport SDL.$= (GL.Position dw 0, GL.Size nw h)
else do
let nh = floor (fromIntegral w / ratio)
dh = floor ((fromIntegral h - fromIntegral nh) / 2 :: Double)
GL.viewport SDL.$= (GL.Position 0 dh, GL.Size w nh)