Skip to content

Commit

Permalink
Include window icon and update audio
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Aug 17, 2022
1 parent 5daf375 commit 600124c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 45 deletions.
60 changes: 26 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dev = [

[dependencies]
bevy = { version = "0.8", default-features = false, features = ["bevy_asset", "bevy_winit", "render", "png", "x11"] }
bevy_kira_audio = { version = "0.11" }
bevy_kira_audio = { version = "0.12" }
bevy_asset_loader = { version = "0.12" }
rand = { version = "0.8.3" }

Expand Down
38 changes: 29 additions & 9 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::actions::Actions;
use crate::loading::AudioAssets;
use crate::GameState;
use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioPlugin};
use bevy_kira_audio::prelude::*;

pub struct InternalAudioPlugin;

Expand All @@ -17,16 +17,36 @@ impl Plugin for InternalAudioPlugin {
}
}

fn start_audio(audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
audio.set_volume(0.3);
audio.play_looped(audio_assets.flying.clone());
struct FlyingAudio(Handle<AudioInstance>);

fn start_audio(mut commands: Commands, audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
audio.pause();
let handle = audio
.play(audio_assets.flying.clone())
.looped()
.with_volume(0.3)
.handle();
commands.insert_resource(FlyingAudio(handle));
}

fn control_flying_sound(actions: Res<Actions>, audio: Res<Audio>) {
if actions.player_movement.is_some() {
audio.resume();
} else {
audio.pause()
fn control_flying_sound(
actions: Res<Actions>,
audio: Res<FlyingAudio>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
) {
if let Some(instance) = audio_instances.get_mut(&audio.0) {
match instance.state() {
PlaybackState::Paused { .. } => {
if actions.player_movement.is_some() {
instance.resume(AudioTween::default());
}
}
PlaybackState::Playing { .. } => {
if actions.player_movement.is_none() {
instance.pause(AudioTween::default());
}
}
_ => {}
}
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::window::WindowId;
use bevy::winit::WinitWindows;
use bevy::DefaultPlugins;
use bevy_game::GamePlugin;
use std::io::Cursor;
use winit::window::Icon;

fn main() {
Expand All @@ -28,7 +29,9 @@ fn main() {
// Sets the icon on windows and X11
fn set_window_icon(windows: NonSend<WinitWindows>) {
let primary = windows.get_window(WindowId::primary()).unwrap();
if let Ok(image) = image::open("assets/textures/bevy.png").map(|image| image.into_rgba8()) {
let icon_buf = Cursor::new(include_bytes!("../assets/textures/bevy.png"));
if let Ok(image) = image::load(icon_buf, image::ImageFormat::Png) {
let image = image.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
let icon = Icon::from_rgba(rgba, width, height).unwrap();
Expand Down

0 comments on commit 600124c

Please sign in to comment.