File: Main.hs

package info (click to toggle)
haskell-sdl2-image 2.1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: haskell: 301; makefile: 7
file content (54 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

import Control.Concurrent (threadDelay)
import Control.Monad (forM_)
import Data.Text (Text)
import Data.Text.IO (putStrLn)
import qualified SDL
import qualified SDL.Image
import System.Environment (getArgs)
import System.Exit (exitFailure)
import Prelude hiding (putStrLn)

-- A sequence of example actions to be perfomed and displayed.
examples :: [(Text, SDL.Window -> FilePath -> IO ())]
examples =
  [ ( "Loading as surface, blitting",
      \window path -> do
        image <- SDL.Image.load path
        screen <- SDL.getWindowSurface window
        _ <- SDL.surfaceBlit image Nothing screen Nothing
        SDL.updateWindowSurface window
        SDL.freeSurface image
    ),
    ( "Loading as texture, rendering",
      \window path -> do
        r <- SDL.createRenderer window (-1) SDL.defaultRenderer
        texture <- SDL.Image.loadTexture r path
        SDL.clear r
        SDL.copy r texture Nothing Nothing
        SDL.present r
        SDL.destroyTexture texture
    )
  ]

main :: IO ()
main = do
  SDL.initialize [SDL.InitVideo]

  getArgs >>= \case
    [] -> do
      putStrLn "Usage: cabal run path/to/image.(png|jpg|...)"
      exitFailure
    (path : _) ->
      -- Run each of the examples within a newly-created window.
      forM_ examples $ \(name, action) -> do
        putStrLn name
        window <- SDL.createWindow name SDL.defaultWindow
        SDL.showWindow window
        action window path
        threadDelay 1000000
        SDL.destroyWindow window

  SDL.quit