Reputation: 412
I am trying to build a rust game that requires text rendering. The way that I found is:
let font:&Font = &ttf_context.load_font(FONT_PATH, 128)?;
My issue is that this requires the built binary to need to have the font file. What I want is for the binary to contain the font file within itself.
I briefly tried using include_bytes!()
and include_dir!()
but I couldn't seem to get them to work with &ttf_context.load_font()
which expects a &str of the path which just brings me back to the original problem and I get the error "Couldn't open ./assets/Font.ttf"
Is there a way to include the font file in a way such I can still get its path or is there a different way I should render text?
Edit 1: Can I combine the binary and assets folder into a single file such as a .app
file for macOS or .exe
on windows?
Upvotes: 1
Views: 665
Reputation: 27186
There is no way to get a path to the included file, since that file doesn't necesarily exist at runtime and you can only use load_font
with files that actually exist in the file system.
Instead you can use Sdl2TtfContext::load_font_from_rwops
with the include
d bytes like this:
use sdl2::rwops::RWops;
let font: &[u8] = include_bytes!("./assets/Font.ttf");
let font: &Font = &ttf_context
.load_font_from_rwops(RWops::from_bytes(font)?, 128)?;
Upvotes: 2