4 ms·
To embed binary assets into any language, write a program that emits the binary as a string containing hex codes. E.g. bytes [0, 12, 99] would become "\x00\x0C
by sillysaurus3 10y ago
To embed binary assets into any language, write a program that emits the binary as a string containing hex codes.
E.g. bytes [0, 12, 99] would become "\x00\x0C\x63".
Almost every language has facilities to treat a string as a set of bytes, so this works basically everywhere. It's nice because you don't need any special language-level support for it.
- jamescostian 10y agoWhile I do agree that it is possible to do something like that, I think we can all agree that the following (available in Rust) is soooooo much nicer: let stuff = include_bytes!("my.file");
- phkahler 10y agoNow I have to rewrite my arcade game emulator in rust just so I can use that to load rom images...
- FreeFull 10y agoinclude_bytes! is for compile-time inclusion of files though, not for runtime.
- phkahler 10y agoYeah but when it only runs 15 games with a max size of 32K... But yeah it makes it non-distributable because it would then include the images.
- jamescostian 10y agoNot only did you completely miss the point (a systems language has a very nice thing very well built-in - yay!), but you also built a strawman (as if praising a language means is the same as telling everyone to rewrite all code in that language) and then beat it up using a poor example (why package those images into the binary?). Congratulations! You just won the internet!
- dang 10y agoPlease don't comment uncivilly, regardless of what you think of someone else's comment. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newswelcome.html https://news.ycombinator.com/newswelcome.html
- zokier 10y agoWhile include_bytes is nice, xxd -i in a makefile is pretty workable solution in the scale of things.
- jamescostian 10y agoFair point, however I believe the OP made the game work for many different OSes, and I don't think xxd or Makefiles are the best when you have to deal with Windows (whereas Rust's include_bytes! provides a platform-independent solution). I had never heard of xxd until your comment though, so thank you very much!
- zokier 10y agoSomehow I'm reminded of the old adage "UNIX is IDE for C"
- pandaman 10y agoC++ string literals usually have fairly low maximum length (C++ standard suggests 64K in [implimits/2.16]). Initialized arrays can be longer in practice but they also have a limit (the same standard suggests 16K but even that will give you 128Kb if you initialize uint64). Objcopy, as suggested elsewhere in the comments has more comfortable limits.