SFML-control: Higher level library on top of SFML

[ graphics, library, mit ] [ Propose Tags ]
Versions [RSS] 0.2.0.0, 0.2.0.1, 0.2.0.2
Dependencies base (>=4.6 && <5), mtl (>=2.0.0.0), SFML (>=0.2.0.0), template-haskell [details]
License MIT
Author Alfredo Di Napoli
Maintainer alfredo.dinapoli@gmail.com
Category Graphics
Home page https://github.com/SFML-haskell/SFML-control
Uploaded by AlfredoDiNapoli at 2013-12-15T16:51:29Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2927 total (8 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user [build log]
All reported builds failed [all 1 reports]

Readme for SFML-control-0.2.0.2

[back to package description]

SFML-control

This library expose a SFML monad which deliver a higher level of abstraction over the low level bindings.

What do you mean by higher level?

It exposes a SFML monad you must use and eventually run to go back into IO. In doing that, the SFML monad runs all the destructors for you. This means you don't have to worry about explicit deallocation of the underlying C resources.

How the bindings are achieved?

To scrap as much boilerplate as possible, TH has been used. In fact, thanks to the TH machinery, the whole SFML functions has been lifted appropriately here:

Conversions

Example

This is a 1:1 translation of this example:

module Main where

import Control.Monad.SFML
import qualified SFML.Graphics as G
import qualified SFML.Window as W
import SFML.Graphics.Color

import Paths_SFMLExamples

main :: IO ()
main = runSFML $ do
    let ctxSettings = Just $ W.ContextSettings 24 8 0 1 2
    wnd <- createRenderWindow (W.VideoMode 640 480 32)
           "SFML-Control Demo" [W.SFDefaultStyle] ctxSettings
    logoPath  <- liftIO $ getDataFileName "Haskell-Logo.png"
    fontPath  <- liftIO $ getDataFileName "Vera.ttf"
    musicPath <- liftIO $ getDataFileName "DST-BreakOut.ogg"
    tex <- textureFromFile logoPath Nothing
    spr <- createSprite
    fnt <- fontFromFile fontPath
    txt <- createText
    setTextString txt "Haskell-Control\nhandles memory\nfor you"
    setTextFont txt fnt
    setTextCharacterSize txt 20
    setTextColor txt blue
    msc <- musicFromFile musicPath
    play msc
    setTexture spr tex True
    loop wnd spr txt


loop :: G.RenderWindow -> G.Sprite -> G.Text -> SFML ()
loop wnd spr txt = do
    drawSprite wnd spr Nothing
    drawText   wnd txt $ Just (G.renderStates { G.transform = G.translation 460 40 })
    display wnd
    evt <- waitEvent wnd
    case evt of
        Nothing -> return ()
        Just W.SFEvtClosed -> return ()
        _ -> loop wnd spr txt

As you can see it's almost a 1:1 translation, you just need to run the monad and get rid of explicit destroy !

Why two libraries?

We decided that the user shouldn't pay the extra burder of a SFML monad if all he wants is a low level SFML binding.