File: Bitmap.hs

package info (click to toggle)
haskell-gloss-rendering 1.13.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 112 kB
  • sloc: haskell: 798; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 2,366 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_HADDOCK hide #-}

-- | Helper functions for rendering bitmaps
module Graphics.Gloss.Internals.Rendering.Bitmap
        ( Rectangle(..)
        , BitmapData(..)
        , BitmapFormat(..), PixelFormat(..), RowOrder(..)
        , bitmapPath
        , freeBitmapData)
where
import Data.Data
import Foreign


-- | Represents a rectangular section in a bitmap
data Rectangle
        = Rectangle
        { rectPos :: (Int, Int) -- ^ x- and y-pos in the bitmap in pixels
        , rectSize :: (Int, Int) -- ^ width/height of the area in pixelsi
        }
        deriving (Show, Read, Eq, Ord, Data, Typeable)

-- | Abstract 32-bit RGBA bitmap data.
data BitmapData
        = BitmapData
        { bitmapDataLength :: Int  -- length (in bytes)
        , bitmapFormat     :: BitmapFormat
        , bitmapSize       :: (Int, Int) -- ^ width, height in pixels
        , bitmapCacheMe    :: Bool
        , bitmapPointer    :: (ForeignPtr Word8) }
        deriving (Eq, Data, Typeable)


-- | Description of how the bitmap is layed out in memory.
--
--   * Prior version of Gloss assumed `BitmapFormat BottomToTop PxABGR`
--
data BitmapFormat
        = BitmapFormat
        { rowOrder    :: RowOrder
        , pixelFormat :: PixelFormat }
        deriving (Eq, Data, Typeable, Show, Ord)


-- | Order of rows in an image are either:
--
--   * `TopToBottom` - the top row, followed by the next-lower row and so on.
--   * `BottomToTop` - the bottom row followed by the next-higher row and so on.
--
data RowOrder
        = TopToBottom
        | BottomToTop
        deriving (Eq, Data, Typeable, Show, Ord, Enum, Bounded)


-- | Pixel formats describe the order of the color channels in memory.
data PixelFormat
        = PxRGBA | PxABGR
        deriving (Eq, Data, Typeable, Show, Ord, Enum, Bounded)

instance Show BitmapData where
 show _ = "BitmapData"


-- | Generates the point path to display the bitmap centred
bitmapPath :: Float -> Float -> [(Float, Float)]
bitmapPath width height
 = [(-width', -height'), (width', -height'), (width', height'), (-width', height')]
 where  width'  = width  / 2
        height' = height / 2


-- | Frees the allocated memory given to OpenGL to avoid a memory leak
freeBitmapData :: Ptr Word8 -> IO ()
freeBitmapData p = free p
{-# INLINE freeBitmapData #-}