Skip to content

Commit

Permalink
fix bevy Font loading (#33) (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
vav-dev authored Nov 21, 2023
1 parent 4962592 commit 28a7b91
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/2d_text.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bevy::prelude::*;
use bevy_text_mesh::TextMeshPlugin;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((DefaultPlugins, TextMeshPlugin))
.add_systems(Startup, setup)
.add_systems(Update, animate_rotation)
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}

fn setup_text_mesh(mut commands: Commands, asset_server: Res<AssetServer>) {
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraMono-Medium.ttf");
let font: Handle<TextMeshFont> = asset_server.load("fonts/FiraMono-Medium.ttf#mesh");

commands.spawn(TextMeshBundle {
text_mesh: TextMesh {
Expand Down
2 changes: 1 addition & 1 deletion examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn setup_text_mesh(
asset_server: Res<AssetServer>,
) {
let state = SceneState {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font: asset_server.load("fonts/FiraMono-Medium.ttf#mesh"),
text_count: 0,
text_update_count: 0,
material: materials.add(StandardMaterial {
Expand Down
13 changes: 9 additions & 4 deletions src/font_loader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::asset::AsyncReadExt;
use bevy::text::Font;
use std::error::Error;
use std::fmt::Display;

Expand All @@ -22,15 +23,15 @@ impl Display for FontLoaderError {
pub struct FontLoader;

impl AssetLoader for FontLoader {
type Asset = TextMeshFont;
type Asset = Font;
type Settings = ();
type Error = FontLoaderError;

fn load<'a>(
&'a self,
reader: &'a mut Reader,
_: &'a Self::Settings,
_: &'a mut LoadContext,
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
Expand All @@ -41,11 +42,15 @@ impl AssetLoader for FontLoader {

// ttf fontloading
let font = TextMeshFont {
ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes)
ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes.clone())
.expect("unable to decode asset"),
};

Ok(font)
load_context.add_labeled_asset("mesh".into(), font);

let original_font = Font::try_from_bytes(bytes.into()).expect("unable to read font");

Ok(original_font)
})
}

Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ pub struct TextMeshPlugin;

impl Plugin for TextMeshPlugin {
fn build(&self, app: &mut App) {
app.register_asset_loader(FontLoader)
.init_asset::<font_loader::TextMeshFont>()
app.init_asset::<TextMeshFont>()
.add_systems(Update, (mesh_system::text_mesh, mesh_system::font_loaded))
.insert_resource(MeshCache::default())
.init_asset_loader::<font_loader::FontLoader>();
.init_resource::<MeshCache>()
.init_asset_loader::<FontLoader>();
}
}

0 comments on commit 28a7b91

Please sign in to comment.