Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add custom_fonts example
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Sep 23, 2023
1 parent b47f591 commit 7b8d024
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# EXAMPLE=animated_shapes
# EXAMPLE=animated_text
EXAMPLE=custom_fonts
# EXAMPLE=sprite
# EXAMPLE=text
# EXAMPLE=particles
# EXAMPLE=post_processing
# EXAMPLE=shapes
# EXAMPLE=sound
EXAMPLE=music
# EXAMPLE=music
# EXAMPLE=ecs_sprite
# EXAMPLE=ecs_topdown_game
# EXAMPLE=particle_systems
Expand Down
12 changes: 9 additions & 3 deletions comfy-core/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub fn change_master_volume(change: f64) {
kira::tween::Tween::default(),
)
.unwrap();

info!("volume: {}", system.master_volume);
}
});
}
Expand All @@ -101,6 +103,8 @@ pub fn set_master_volume(value: f64) {
kira::tween::Tween::default(),
)
.unwrap();

info!("volume: {}", system.master_volume);
}
});
}
Expand Down Expand Up @@ -168,8 +172,8 @@ impl AudioSystemImpl {

if let Some(mut sound_data) = sounds.get(&sound).cloned() {
if let Some(settings) = settings {
sound_data.settings = settings
.volume(kira::Volume::Amplitude(self.master_volume));
sound_data.settings =
settings.output_destination(&self.master_track);

match track {
AudioTrack::None => {}
Expand All @@ -180,9 +184,11 @@ impl AudioSystemImpl {
.output_destination(&self.filter_track);
}
}
} else {
sound_data.settings =
sound_data.settings.output_destination(&self.master_track);
}


match self.manager.play(sound_data) {
Ok(handle) => {
match assets.sound_handles.entry(sound) {
Expand Down
5 changes: 2 additions & 3 deletions comfy-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ impl WgpuRenderer {
let screenshot_buffer = SizedBuffer::new(
"screenshot_buffer",
&context.device,
(size.width * size.height) as usize *
std::mem::size_of::<u32>() as usize,
(size.width * size.height) as usize * std::mem::size_of::<u32>(),
BufferType::Read,
);

Expand Down Expand Up @@ -1046,7 +1045,7 @@ impl WgpuRenderer {
pass_data.blend_mode, self.enable_z_buffer
);

self.pipelines.entry(name.clone().into()).or_insert_with(|| {
self.pipelines.entry(name.clone()).or_insert_with(|| {
create_render_pipeline_with_layout(
&name,
&self.context.device,
Expand Down
34 changes: 34 additions & 0 deletions comfy/examples/custom_fonts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use comfy::*;

simple_game!("Custom Fonts Example", setup, update);

fn setup(c: &mut EngineContext) {
c.load_fonts_from_bytes(&[(
"comfy-font",
include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../assets/fonts/Orbitron-Regular.ttf"
)),
)])
}

fn update(_c: &mut EngineContext) {
let text = "comfy has comfy text rendering with egui";

draw_text(text, vec2(0.0, 1.0), WHITE, TextAlign::Center);

draw_text_ex(
"with configurable fonts",
vec2(0.0, 0.0),
TextAlign::Center,
TextParams {
color: RED,
// Use egui fonts
font: egui::FontId::new(
32.0,
egui::FontFamily::Name("comfy-font".into()),
),
..Default::default()
},
);
}
20 changes: 20 additions & 0 deletions comfy/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ impl<'a> EngineContext<'a> {
ASSETS.borrow_mut().load_sound_from_bytes(name, bytes, settings);
}

pub fn load_fonts_from_bytes(&self, fonts: &[(&str, &[u8])]) {
let mut font_defs = egui::FontDefinitions::default();

for (name, bytes) in fonts {
let family_name = name.to_string();

font_defs.font_data.insert(
family_name.clone(),
egui::FontData::from_owned(bytes.to_vec()),
);

font_defs.families.insert(
egui::FontFamily::Name(family_name.clone().into()),
vec![family_name],
);
}

self.egui.set_fonts(font_defs);
}

pub fn commands(&self) -> core::cell::RefMut<CommandBuffer> {
self.commands.borrow_mut()
}
Expand Down

0 comments on commit 7b8d024

Please sign in to comment.