Skip to content

Load Font From Memory (make better documentation) #107

Open
@Klebestreifen

Description

@Klebestreifen

Hello,

I have problems loading a TTF font from the buffer. Since I don't want to load the font file as such from disk, I would like to use the LoadFontFromMemory function, but this doesn't seem to have a wrapper. I also tried to use the FFI binding but the raw FFI font struct is not readily compatible with the API and the constructor of the wrapped struct seems private.

So I need help loading a font from a [u8]. My sample code:

use raylib::prelude::*;
use raylib::ffi::LoadFontFromMemory;
use std::ffi::CString;
use std::convert::TryInto;
use structopt::StructOpt;

mod options;

fn main() {
    let opt = options::Opt::from_args();
    let (mut rl, thread) = opt.open_window("Logo");
    let (w, h) = (opt.width, opt.height);
    let rust_orange = Color::new(222, 165, 132, 255);
    let ray_white = Color::new(255, 255, 255, 255);

    let fontfile = include_bytes!("../rsc/font/PressStart2P.ttf");
    let fontfile_size = fontfile.len();
    let fontfile_type = CString::new("TTF").unwrap();
    let mut chars = 0i32;
    let font = rl.load_font_from_memory(); // What i want c(^-^c)
    // Not working:
    // let font = Font(unsafe{
    //     LoadFontFromMemory(
    //         fontfile_type.as_ptr(),
    //         fontfile.as_ptr(),
    //         fontfile_size.try_into().unwrap(),
    //         12,
    //         &mut chars,
    //         0
    //     )
    // });
    rl.set_target_fps(60);
    while !rl.window_should_close() {
        // Detect window close button or ESC key
        let mut d = rl.begin_drawing(&thread);
        d.clear_background(ray_white);
        d.draw_text_ex(&font, "Hello World!", Vector2::new(10f32,10f32), 12f32, 0f32, Color::new(0,0,0,255));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions