0

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?

Henhen1227
  • 392
  • 1
  • 3
  • 12
  • It's extremely unusual to embed all of your game assets in the binary. Does `Font.ttf` actually exist and this is simply an issue with locating the `assets` folder at runtime? – trojanfoe Jan 10 '23 at 08:23
  • What I was hoping for is to be able to run the program without needing the assets folder in the same directory. Is there a different way I should do this such as a `.app` file on macOS? – Henhen1227 Jan 10 '23 at 15:40

1 Answers1

1

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 included 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)?;
cafce25
  • 15,907
  • 4
  • 25
  • 31