haskellgloss

Include sprites into binary -- Gloss library


I am trying to make a flappy bird game in Haskell and I'd like to know if there's a way to "compile" the .bmp files into the binary? So I can only share the executable and don't need a folder with the sprites.

I am using gloss-1.13.0.1 and loading the bmp as

bg0Pic = unsafePerformIO . loadBMP $ "bg0.bmp"

I know unsafePerformIO is not good practice but that's not of my concern yet. Should I use a different approach so that the compiler knows I need that image or is there just no way to do that?

Can find the whole code on GitHub


Solution

  • You can use the file-embed package, which uses Template Haskell to embed files.

    https://www.stackage.org/package/file-embed

    For example:

    sprites :: ByteString
    sprites = $(embedFile "images/sprites.png")
    
    wordsPic :: Picture
    wordsPic = fromMaybe mempty
      (either (\_ -> Nothing) Just (decodeImage sprites)
        >>= fromDynamicImage)