packages feed

sdl2-image (empty) → 0.1.0.1

raw patch · 5 files changed

+236/−0 lines, 5 filesdep +basedep +sdl2setup-changed

Dependencies added: base, sdl2

Files

+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2014 Cai Lei++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sdl2-image.cabal view
@@ -0,0 +1,55 @@+name:                sdl2-image+version:             0.1.0.1+synopsis:            Haskell binding to sdl2-image.+description:         Haskell binding to sdl2-image.+license:             MIT+license-file:        LICENSE+author:              Cai Lei+maintainer:          cailei@live.com+category:            Graphics+build-type:          Simple+cabal-version:       >=1.10++source-repository head+  type:       git+  location:   https://github.com/ccll/hs-sdl2-image.git++source-repository this+  type:       git+  location:   https://github.com/ccll/hs-sdl2-image.git+  tag:        v0.1.0.1++library+  extra-libraries:+    sdl2++  pkgconfig-depends:+    sdl2 >= 2.0.1,+    SDL2_image >= 2.0.0++  build-depends:+    base ==4.6.*,+    sdl2 ==1.0.*++  hs-source-dirs:+    src++  ghc-options:+    -Wall++  default-extensions:+    CPP, EmptyDataDecls, ForeignFunctionInterface,+    MultiParamTypeClasses, FunctionalDependencies++  exposed-modules:+    Graphics.UI.SDL.Image++  other-modules:+    Graphics.UI.SDL.Utilities++  includes:+    SDL.h++  default-language:+    Haskell2010+
+ src/Graphics/UI/SDL/Image.hsc view
@@ -0,0 +1,96 @@+module Graphics.UI.SDL.Image+    ( InitFlag(..),+      imgInit,+      imgQuit,+      withImgInit,+      imgLoadTexture+    ) where++import Data.Word (Word32)+import Prelude hiding (init, Enum(..))+import Graphics.UI.SDL.Utilities (Enum(..), toBitmask)+import Control.Monad (when)+import Data.Maybe (fromMaybe)+import Foreign.C (withCString, peekCString, CString)+import Foreign.Ptr (nullPtr)+import Control.Exception (bracket_)+import Graphics.UI.SDL (Renderer, Texture)++#include "SDL_image.h"++data InitFlag = InitJPG+              | InitPNG+              | InitTIF+              | InitWEBP+    deriving(Eq, Ord, Show, Read)++instance Bounded InitFlag where+    minBound = InitJPG+    maxBound = InitWEBP++instance Enum InitFlag Word32 where+    fromEnum InitJPG = #{const IMG_INIT_JPG}+    fromEnum InitPNG = #{const IMG_INIT_PNG}+    fromEnum InitTIF = #{const IMG_INIT_TIF}+    fromEnum InitWEBP = #{const IMG_INIT_WEBP}+    toEnum #{const IMG_INIT_JPG} = InitJPG+    toEnum #{const IMG_INIT_PNG} = InitPNG+    toEnum #{const IMG_INIT_TIF} = InitTIF+    toEnum #{const IMG_INIT_WEBP} = InitWEBP+    toEnum _ = error "Graphics.UI.SDL.General.toEnum: bad argument"+    succ InitJPG = InitPNG+    succ InitPNG = InitTIF+    succ InitTIF = InitWEBP+    succ _ = error "Graphics.UI.SDL.General.succ: bad argument"+    pred InitPNG = InitJPG+    pred InitTIF = InitPNG+    pred InitWEBP = InitTIF+    pred _ = error "Graphics.UI.SDL.General.pred: bad argument"+    enumFromTo x y | x > y = []+                     | x == y = [y]+                     | True = x : enumFromTo (succ x) y+++-- | Initializes SDL2-image. This should be called before all other SDL functions.+imgInit :: [InitFlag] -> IO ()+imgInit flags+    = do ret <- _imgInit (fromIntegral (toBitmask flags))+         when (ret == (-1)) (failWithError "SDL_Init")+foreign import ccall unsafe "IMG_Init" _imgInit :: Word32 -> IO Int++withImgInit :: [InitFlag] -> IO a -> IO a+withImgInit flags action+    = bracket_ (imgInit flags) imgQuit action+++imgQuit :: IO ()+imgQuit = _imgQuit+foreign import ccall unsafe "IMG_Quit" _imgQuit :: IO ()+++-- | Load image file to a texture.+imgLoadTexture :: Renderer -> String -> IO (Either String Texture)+imgLoadTexture rend file+    = withCString file $ \cFile -> do+        tex <- _imgLoadTexture rend cFile+        err <- getError+        if tex == nullPtr+            then return (Left (fromMaybe "IMG_LoadTexture(): Unknown error!" err))+            else return (Right tex)+foreign import ccall unsafe "IMG_LoadTexture" _imgLoadTexture :: Renderer -> CString -> IO Texture+++-- | Returns a string containing the last error. Nothing if no error.+getError :: IO (Maybe String)+getError+    = do str <- peekCString =<< _sdlGetError+         if null str+            then return Nothing+            else return (Just str)+foreign import ccall unsafe "SDL_GetError" _sdlGetError :: IO CString+++failWithError :: String -> IO a+failWithError msg+    = do err <- fmap (fromMaybe "No SDL error") getError+         ioError $ userError $ msg ++ "\nSDL message: " ++ err
+ src/Graphics/UI/SDL/Utilities.hs view
@@ -0,0 +1,62 @@+-----------------------------------------------------------------------------+-- |+-- Module      :  Graphics.UI.SDL.General+-- Copyright   :  (c) David Himmelstrup 2005+-- License     :  BSD-like+--+-- Maintainer  :  lemmih@gmail.com+-- Stability   :  provisional+-- Portability :  portable+--+-- Various small functions which makes the binding process easier.+-----------------------------------------------------------------------------++module Graphics.UI.SDL.Utilities where++import Foreign (Bits((.|.), (.&.)))+import Foreign.C (CInt)++import Prelude hiding (Enum(..))++class Enum a b | a -> b where+  succ :: a -> a+  pred :: a -> a+  toEnum :: b -> a+  fromEnum :: a -> b+  enumFromTo :: a -> a -> [a]++++intToBool :: Int -> IO Int -> IO Bool+intToBool err action+    = fmap (err/=) action++toBitmask :: (Enum a b,Bits b,Num b) => [a] -> b+toBitmask = foldr (.|.) 0 . map fromEnum++fromBitmask :: (Bounded a,Enum a b,Bits b,Num b) => b -> [a]+fromBitmask mask = foldr worker [] lst+    where lst = enumFromTo minBound maxBound+          worker v+              = if (mask .&. fromEnum v) /= 0+                   then (:) v+                   else id+{-+toBitmaskW :: (UnsignedEnum a) => [a] -> Word32+toBitmaskW = foldr (.|.) 0 . map fromEnumW++fromBitmaskW :: (Bounded a,UnsignedEnum a) => Word32 -> [a]+fromBitmaskW mask = foldr worker [] lst+    where lst = enumFromToW minBound maxBound+          worker v+              = if (mask .&. fromEnumW v) /= 0+                   then (:) v+                   else id++-}++fromCInt :: Num a => CInt -> a+fromCInt = fromIntegral++toCInt :: Int -> CInt+toCInt = fromIntegral