diff options
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/src/audio.rs | 28 | ||||
| -rw-r--r-- | graphics/src/lib.rs | 9 | ||||
| -rw-r--r-- | graphics/src/render.rs | 68 |
3 files changed, 93 insertions, 12 deletions
diff --git a/graphics/src/audio.rs b/graphics/src/audio.rs index 8cf155f..3a20f62 100644 --- a/graphics/src/audio.rs +++ b/graphics/src/audio.rs @@ -2,14 +2,27 @@ use raylib::audio::RaylibAudio; -#[expect(dead_code)] +macro_rules! load_audio { + ($handle:expr, $filepath:expr) => { + if cfg!(feature = "static") { + let bytes = include_bytes!(concat!("../../", $filepath)); + let wave = $handle.new_wave_from_memory(".ogg", bytes)?; + $handle.new_sound_from_wave(&wave)? + } else { + $handle.new_sound($filepath)? + } + }; +} + type Sound = raylib::audio::Sound<'static>; /// The `Audio` container initalizes the audio subsystem /// for raylib, leaks it (to gurentee audio is statically loaded), /// then loads all needed audio samples #[derive(Debug)] -pub struct Audio {} +pub struct Audio { + pub speak: Sound, +} impl Audio { pub(crate) fn load() -> crate::Result<Self> { // Phantom handle to the raylib audio subsystem @@ -21,12 +34,13 @@ impl Audio { // // NOTE: would this cause issues if `Audio::load` was // called multiple times? - let _handle = Box::leak(Box::new(RaylibAudio::init_audio_device()?)); - - // TODO: load audio samples + let handle = Box::leak(Box::new(RaylibAudio::init_audio_device()?)); - //let example = handle.new_sound("example.ogg")?; + // yes i know this is sans undertale, it was funny + // and i cannot think of anything better yet, haha + // - freya + let speak = load_audio!(handle, "assets/speak.ogg"); - Ok(Self {}) + Ok(Self { speak }) } } diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index d12eac0..0fdcedb 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -32,8 +32,11 @@ pub enum Key { Down, Left, Right, + // Interact + Return, // Debug keys F3, + F4, // Unknown/Unused key Unknown(KeyboardKey), } @@ -51,8 +54,11 @@ impl From<KeyboardKey> for Key { K::KEY_DOWN => Self::Down, K::KEY_LEFT => Self::Left, K::KEY_RIGHT => Self::Right, + // Interact + K::KEY_ENTER => Self::Return, // Debug keys K::KEY_F3 => Self::F3, + K::KEY_F4 => Self::F4, // Unknown/Unused key _ => Self::Unknown(key), } @@ -71,8 +77,11 @@ impl From<Key> for KeyboardKey { Key::Down => Self::KEY_DOWN, Key::Left => Self::KEY_LEFT, Key::Right => Self::KEY_RIGHT, + // Interact + Key::Return => Self::KEY_ENTER, // Debug keys Key::F3 => Self::KEY_F3, + Key::F4 => Self::KEY_F4, // Unknown/Unused key Key::Unknown(k) => k, } diff --git a/graphics/src/render.rs b/graphics/src/render.rs index 340189b..996aa37 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -496,13 +496,16 @@ impl Renderer { if self.debug { self.draw_debug_ui(r, dungeon); - return; + } else { + // Draw core ui components + self.draw_minimap(r, dungeon); + self.draw_inventory(r, &dungeon.player); + self.draw_stats(r, &dungeon.player); } - // Draw core ui components - self.draw_minimap(r, dungeon); - self.draw_inventory(r, &dungeon.player); - self.draw_stats(r, &dungeon.player); + if let Some(buf) = dungeon.msg.current() { + self.draw_msg(r, buf); + } } /// Draw in game minimap @@ -686,6 +689,61 @@ impl Renderer { draw_text!(self, r, UI_COL2, UI_ROW3, "FRAME {frame}"); } + /// Draw msg box + fn draw_msg<R>(&self, r: &mut R, msg: &[u8]) + where + R: RaylibDraw, + { + const MAX_LINES: u16 = 5; + const MSG_LEN: u16 = 20; + + const HEIGHT: u16 = MAX_LINES * FONT_SIZE + UI_PADDING * 2; + const WIDTH: u16 = MSG_LEN * FONT_SIZE + UI_PADDING * 2; + const X: u16 = RENDER_WIDTH / 2 - WIDTH / 2; + const Y: u16 = RENDER_HEIGHT - HEIGHT - UI_PADDING; + + // draw background + r.draw_rectangle( + X.into(), + Y.into(), + WIDTH.into(), + HEIGHT.into(), + Color::BLACK, + ); + r.draw_rectangle_lines( + X.into(), + Y.into(), + WIDTH.into(), + HEIGHT.into(), + Color::WHITE, + ); + + let mut x = 0; + let mut y = 0; + + let words = msg.split(u8::is_ascii_whitespace); + for word in words { + let left = MSG_LEN.saturating_sub(x); + if word.len() > left as usize { + y += 1; + x = 0; + } + + for char in word { + self.draw_char( + r, + *char, + X + UI_PADDING + x * FONT_SIZE, + Y + UI_PADDING + y * FONT_SIZE, + ); + + x += 1; + } + + x += 1; + } + } + /// Draws vertical text fn draw_text_vertical<R>(&self, r: &mut R, text: &[u8], x: u16, y_start: u16) where |