free-game 0.3.1.0 → 0.3.1.1
raw patch · 3 files changed
+24/−27 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Graphics.FreeGame.Data.Font: text :: Font -> Float -> Color -> String -> Picture
+ Graphics.FreeGame.Data.Font: text :: Font -> Float -> String -> Picture
- Graphics.FreeGame.Data.Font: withRenderCharacters :: Font -> Float -> Color -> String -> ([Picture] -> Game a) -> Game a
+ Graphics.FreeGame.Data.Font: withRenderCharacters :: Font -> Float -> String -> ([Picture] -> Game a) -> Game a
- Graphics.FreeGame.Data.Font: withRenderString :: Font -> Float -> Color -> String -> (Picture -> Game a) -> Game a
+ Graphics.FreeGame.Data.Font: withRenderString :: Font -> Float -> String -> (Picture -> Game a) -> Game a
- Graphics.FreeGame.Simple: text :: Font -> Float -> Color -> String -> Picture
+ Graphics.FreeGame.Simple: text :: Font -> Float -> String -> Picture
Files
- Graphics/FreeGame/Data/Font.hs +20/−23
- examples/font.hs +3/−3
- free-game.cabal +1/−1
Graphics/FreeGame/Data/Font.hs view
@@ -22,7 +22,6 @@ import qualified Data.Map as M import Data.Word import Graphics.FreeGame.Base -import Graphics.FreeGame.Data.Color import Graphics.FreeGame.Data.Bitmap import Graphics.Rendering.FreeType.Internal import Graphics.Rendering.FreeType.Internal.GlyphSlot as GS @@ -40,7 +39,7 @@ import Paths_free_game -- | Font object -data Font = Font FT_Face (IORef (M.Map (Float, Color, Char) RenderedChar)) +data Font = Font FT_Face (IORef (M.Map (Float, Char) RenderedChar)) -- | Create a 'Font' from the given file. loadFont :: FilePath -> IO Font @@ -49,18 +48,18 @@ failFreeType e Font <$> peek p <*> newIORef M.empty --- | Render a text by the specified 'Font' and 'Color' and size. -text :: Font -> Float -> Color -> String -> Picture -text font size color str = IOPicture $ Pictures <$> renderCharacters font size color str +-- | Render a text by the specified 'Font'. +text :: Font -> Float -> String -> Picture +text font size str = IOPicture $ Pictures <$> renderCharacters font size str --- | Render the string by the given font and color, and pass it to the 'Game' action. -withRenderCharacters :: Font -> Float -> Color -> String -> ([Picture] -> Game a) -> Game a -withRenderCharacters font size color str action = bracket - $ embedIO (renderCharacters font size color str) >>= action +-- | Render the string by the given font, and pass it to the 'Game' action. +withRenderCharacters :: Font -> Float -> String -> ([Picture] -> Game a) -> Game a +withRenderCharacters font size str action = bracket + $ embedIO (renderCharacters font size str) >>= action --- | Render the string by the given font and color, and pass it to the 'Game' action. -withRenderString :: Font -> Float -> Color -> String -> (Picture -> Game a) -> Game a -withRenderString font size color str action = withRenderCharacters font size color str (action . Pictures) +-- | Render the string by the given font, and pass it to the 'Game' action. +withRenderString :: Font -> Float -> String -> (Picture -> Game a) -> Game a +withRenderString font size str action = withRenderCharacters font size str (action . Pictures) failFreeType 0 = return () failFreeType e = fail $ "FreeType Error:" Prelude.++ show e @@ -78,13 +77,13 @@ ,charAdvance :: Float } -charToBitmap :: Font -> Float -> Color -> Char -> IO RenderedChar -charToBitmap (Font face refCache) size col@(Color r g b a) ch = do +charToBitmap :: Font -> Float -> Char -> IO RenderedChar +charToBitmap (Font face refCache) size ch = do cache <- readIORef refCache - case M.lookup (size, col, ch) cache of + case M.lookup (size, ch) cache of Nothing -> do d <- render - writeIORef refCache $ M.insert (size, col, ch) d cache + writeIORef refCache $ M.insert (size, ch) d cache return d Just d -> return d @@ -114,21 +113,19 @@ ar :: R.Array U DIM2 Word8 <- unsafeFreezeMVec (Z:.h:.w) mv - let pixel (crd:.0) = floor $ fromIntegral (R.index ar crd) * a - pixel (crd:.1) = floor $ b * 255 - pixel (crd:.2) = floor $ g * 255 - pixel (crd:.3) = floor $ r * 255 + let pixel (crd:.0) = R.index ar crd + pixel (crd:._) = 255 result <- computeP (fromFunction (Z:.h:.w:.4) pixel) >>= makeStableBitmap return $ RenderedChar result (Vec2 left (-top)) (fromIntegral (V.x adv) / 64) -renderCharacters :: Font -> Float -> Color -> String -> IO [Picture] -renderCharacters font size color str = render str 0 +renderCharacters :: Font -> Float -> String -> IO [Picture] +renderCharacters font size str = render str 0 where render [] _ = return [] render (c:cs) pen = do - RenderedChar b (Vec2 x y) adv <- charToBitmap font size color c + RenderedChar b (Vec2 x y) adv <- charToBitmap font size c let (w,h) = bitmapSize b offset = Vec2 (pen + x + fromIntegral w / 2) (y + fromIntegral h / 2) (Translate offset (BitmapPicture b):)
examples/font.hs view
@@ -6,10 +6,10 @@ main = do font <- loadFont "VL-PGothic-Regular.ttf" runSimple defaultGameParam 0 $ \n -> do - drawPicture $ Translate (Vec2 30 120) $ text font 7 (halfD red) ("Counter: " ++ show n) + drawPicture $ Translate (Vec2 30 120) $ Colored (halfD red) $ text font 7 ("Counter: " ++ show n) - drawPicture $ Translate (Vec2 30 240) $ text font 9 (halfD blue) "日本語も、美しくレンダリング。" + drawPicture $ Translate (Vec2 30 240) $ Colored (halfD blue) $ text font 9 "日本語も、美しくレンダリング。" - withRenderString font 12 yellow "★☆★☆★☆★☆" $ drawPicture . Translate (Vec2 30 360) + withRenderString font 12 "★☆★☆★☆★☆" $ drawPicture . Translate (Vec2 30 360) . Colored yellow return $! n + 1
free-game.cabal view
@@ -1,5 +1,5 @@ name: free-game -version: 0.3.1.0 +version: 0.3.1.1 synopsis: Create graphical applications for free. description: Cross-platform GUI library based on free monads. homepage: https://github.com/fumieval/free-game