4

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

Lorenzo
  • 2,160
  • 12
  • 29

2 Answers2

4

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)
adius
  • 13,685
  • 7
  • 45
  • 46
Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • I can't seem to understand how to use it, and the documentation is not the best. Could you please just give an example on how I'd need to chance my code to get the bmp – Lorenzo Mar 06 '19 at 23:16
  • Ok,looks like I need to convert the Q expr into a ByteString and then i can use `bitmapOfByteString` – Lorenzo Mar 06 '19 at 23:20
  • You use something like `$(embedFile "file.bmp")`. You'll need to enable the `TemplateHaskell` pragma. – Michael Snoyman Mar 07 '19 at 02:38
2

One approach is to use Data Files with cabal.

The idea is that you add all data files (text, images, sprites, other binaries) you want to bundle with your application and access at runtime under the Data-Files header in your .cabal file.

This will cause cabal to generate a Paths module for you, which you can access in whatever module needs it.

More info can be found here!

sara
  • 3,521
  • 14
  • 34
  • It seems like this only links the file with the binary, but it still needs to be installed with cabal. I am looking for a way to embed it in the binary so thats independent – Lorenzo Mar 06 '19 at 23:12