Skip to content

Commit

Permalink
Add Music::from_bytes() function (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaise authored and Cobrand committed Sep 10, 2017
1 parent 6bea457 commit b52bd54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ breaking exisitng code too much.
* Adds the `unsafe_textures` feature to this crate, allowing to get rid of the lifetimes
in `Texture`s in the `render` module.

[PR #704](https://github.com/Rust-SDL2/rust-sdl2/pull/704)

* Adds the `Music::from_static_bytes` function, which creates a Music instance with the
static lifetime from a buffer that also has a static lifetime.

### v0.30

Re-exported sdl2\_sys as sdl2::sys
Expand Down
26 changes: 24 additions & 2 deletions src/sdl2/mixer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//!
//! A binding for the library `SDL2_mixer`
//!
//!
//!
//! Note that you need to build with the
//! feature `mixer` for this module to be enabled,
//! like so:
Expand All @@ -27,7 +27,7 @@ use std::ffi::{CString, CStr};
use std::str::from_utf8;
use std::borrow::ToOwned;
use std::path::Path;
use libc::{c_int, uint16_t, c_double, c_uint};
use libc::{c_int, uint16_t, c_double, c_uint, c_void};
use ::get_error;
use ::rwops::RWops;
use ::version::Version;
Expand Down Expand Up @@ -790,6 +790,28 @@ impl<'a> Music<'a> {
}
}

/// Load music from a static byte buffer.
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Music<'static>, String> {
let rw = unsafe {
::sys::rwops::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int)
};

if rw.is_null() {
return Err(get_error());
}

let raw = unsafe { ffi::Mix_LoadMUS_RW(rw, 0) };
if raw.is_null() {
Err(get_error())
} else {
Ok(Music {
raw: raw,
owned: true,
_marker: PhantomData,
})
}
}

/// The file format encoding of the music.
pub fn get_type(&self) -> MusicType {
let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as i32 } as c_uint;
Expand Down

0 comments on commit b52bd54

Please sign in to comment.