From 8e7268c661e8df25224c907ba68eeb5a9cc5ff11 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sat, 18 Oct 2025 13:21:40 -0400 Subject: graphics: add audio/texture subsytem --- graphics/src/assets.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 graphics/src/assets.rs (limited to 'graphics/src/assets.rs') diff --git a/graphics/src/assets.rs b/graphics/src/assets.rs new file mode 100644 index 0000000..3f2354a --- /dev/null +++ b/graphics/src/assets.rs @@ -0,0 +1,70 @@ +//! The `assets` crate stores all audio and image assets that need to be +//! loaded during runtime + +use std::error::Error; + +use raylib::{RaylibHandle, RaylibThread, audio::RaylibAudio}; + +#[expect(dead_code)] +type Sound = raylib::audio::Sound<'static>; + +/// The `AudioData` 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 AudioData {} +impl AudioData { + pub(crate) fn load() -> Result> { + // Phantom handle to the raylib audio subsystem + // Raylib doesnt use a handle, but the rust bindings + // have one to ensure memory safety. + // + // We must leak this handle after allocating it, + // if we dont then all audio will be unloaded :( + // + // NOTE: would this cause issues if `AudioData::load` was + // called multiple times? + let _handle = Box::leak(Box::new(RaylibAudio::init_audio_device()?)); + + // TODO: load audio samples + + //let example = handle.new_sound("example.ogg")?; + + Ok(Self {}) + } +} + +/// The `ImageData` container loads all game sprites, and other images into memory. +#[derive(Debug)] +pub(crate) struct ImageData {} +impl ImageData { + pub(crate) fn load( + _handle: &mut RaylibHandle, + _thread: &RaylibThread, + ) -> Result> { + // TODO: load image data + + //let example = handle.load_texture(&thread, "example.png"); + + Ok(Self {}) + } +} + +#[derive(Debug)] +pub(crate) struct Assets { + /// Audio needs to be accessible outside of the renderer + pub(crate) audio: AudioData, + /// Images are only needed by the renderer + pub(crate) image: ImageData, +} +impl Assets { + pub(crate) fn load( + handle: &mut RaylibHandle, + thread: &RaylibThread, + ) -> Result> { + let audio = AudioData::load()?; + let image = ImageData::load(handle, thread)?; + + Ok(Self { audio, image }) + } +} -- cgit v1.2.3-freya