Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include sprites into binary -- Gloss library

Tags:

haskell

gloss

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

like image 535
Lorenzo Avatar asked Mar 06 '19 15:03

Lorenzo


1 Answers

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)
like image 145
Michael Snoyman Avatar answered Sep 22 '22 05:09

Michael Snoyman