From 554b8241519394f393baef11dd5e2cbadeaac688 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 11:17:35 +0800 Subject: [PATCH 001/114] initial commit --- LICENSE | 20 ++ README.md | 30 ++ src/sdl2_mixer/ffi.rs | 152 +++++++++ src/sdl2_mixer/lib.rs | 698 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 900 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/sdl2_mixer/ffi.rs create mode 100644 src/sdl2_mixer/lib.rs diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..5e611b20 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Mozilla Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..65eb8122 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Rust-SDL2_mixer +=============== + +Rust bindings for SDL2_mixer + +Requirements +------------ + +* [Rust-sdl2](https://github.com/AngryLawyer/rust-sdl2) +* sdl_mixer 2.0 development libraries +* Rust master + +Installation +------------ + +``` +git clone https://github.com/andelf/rust-sdl2_mixer +cd rust-sdl2_mixer +rustc src/sdl2_mixer/lib.rs +# OR if you are using the mac framework version +rustc --cfg mac_framework src/sdl2_mixer/lib.rs +``` + +Demo +---- + +``` +rustc -L. src/demo/main.rs -o demo +./demo mixer.[png|jpg] +``` diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs new file mode 100644 index 00000000..19ae4728 --- /dev/null +++ b/src/sdl2_mixer/ffi.rs @@ -0,0 +1,152 @@ +/* automatically generated by rust-bindgen */ + +use libc::*; +use sdl2::rwops::ll::SDL_RWops; + +pub struct SDL_version { + pub major: uint8_t, + pub minor: uint8_t, + pub patch: uint8_t, +} +pub type MIX_InitFlags = c_uint; +pub static MIX_INIT_FLAC: c_uint = 1; +pub static MIX_INIT_MOD: c_uint = 2; +pub static MIX_INIT_MODPLUG: c_uint = 4; +pub static MIX_INIT_MP3: c_uint = 8; +pub static MIX_INIT_OGG: c_uint = 16; +pub static MIX_INIT_FLUIDSYNTH: c_uint = 32; +pub struct Struct_Mix_Chunk { + pub allocated: c_int, + pub abuf: *uint8_t, + pub alen: uint32_t, + pub volume: uint8_t, +} +pub type Mix_Chunk = Struct_Mix_Chunk; +pub type Mix_Fading = c_uint; +pub static MIX_NO_FADING: c_uint = 0; +pub static MIX_FADING_OUT: c_uint = 1; +pub static MIX_FADING_IN: c_uint = 2; +pub type Mix_MusicType = c_uint; +pub static MUS_NONE: c_uint = 0; +pub static MUS_CMD: c_uint = 1; +pub static MUS_WAV: c_uint = 2; +pub static MUS_MOD: c_uint = 3; +pub static MUS_MID: c_uint = 4; +pub static MUS_OGG: c_uint = 5; +pub static MUS_MP3: c_uint = 6; +pub static MUS_MP3_MAD: c_uint = 7; +pub static MUS_FLAC: c_uint = 8; +pub static MUS_MODPLUG: c_uint = 9; +pub type Struct__Mix_Music = c_void; +pub type Mix_Music = Struct__Mix_Music; +pub type Mix_EffectFunc_t = + ::std::option::Option; +pub type Mix_EffectDone_t = + ::std::option::Option; +extern "C" { + pub fn Mix_Linked_Version() -> *SDL_version; + pub fn Mix_Init(flags: c_int) -> c_int; + pub fn Mix_Quit(); + pub fn Mix_OpenAudio(frequency: c_int, format: uint16_t, channels: c_int, + chunksize: c_int) -> c_int; + pub fn Mix_AllocateChannels(numchans: c_int) -> c_int; + pub fn Mix_QuerySpec(frequency: *c_int, format: *uint16_t, + channels: *c_int) -> c_int; + pub fn Mix_LoadWAV_RW(src: *SDL_RWops, freesrc: c_int) -> + *Mix_Chunk; + pub fn Mix_LoadMUS(file: *c_schar) -> *Mix_Music; + pub fn Mix_LoadMUS_RW(src: *SDL_RWops, freesrc: c_int) -> + *Mix_Music; + pub fn Mix_LoadMUSType_RW(src: *SDL_RWops, _type: Mix_MusicType, + freesrc: c_int) -> *Mix_Music; + pub fn Mix_QuickLoad_WAV(mem: *uint8_t) -> *Mix_Chunk; + pub fn Mix_QuickLoad_RAW(mem: *uint8_t, len: uint32_t) -> *Mix_Chunk; + pub fn Mix_FreeChunk(chunk: *Mix_Chunk); + pub fn Mix_FreeMusic(music: *Mix_Music); + pub fn Mix_GetNumChunkDecoders() -> c_int; + pub fn Mix_GetChunkDecoder(index: c_int) -> *c_schar; + pub fn Mix_GetNumMusicDecoders() -> c_int; + pub fn Mix_GetMusicDecoder(index: c_int) -> *c_schar; + pub fn Mix_GetMusicType(music: *Mix_Music) -> Mix_MusicType; + pub fn Mix_SetPostMix(mix_func: + ::std::option::Option, + arg: *c_void); + pub fn Mix_HookMusic(mix_func: + ::std::option::Option, + arg: *c_void); + pub fn Mix_HookMusicFinished(music_finished: + ::std::option::Option); + pub fn Mix_GetMusicHookData() -> *c_void; + pub fn Mix_ChannelFinished(channel_finished: + ::std::option::Option); + pub fn Mix_RegisterEffect(chan: c_int, f: Mix_EffectFunc_t, + d: Mix_EffectDone_t, arg: *c_void) -> c_int; + pub fn Mix_UnregisterEffect(channel: c_int, f: Mix_EffectFunc_t) -> c_int; + pub fn Mix_UnregisterAllEffects(channel: c_int) -> c_int; + pub fn Mix_SetPanning(channel: c_int, left: uint8_t, right: uint8_t) -> c_int; + pub fn Mix_SetPosition(channel: c_int, angle: int16_t, distance: uint8_t) -> + c_int; + pub fn Mix_SetDistance(channel: c_int, distance: uint8_t) -> c_int; + pub fn Mix_SetReverseStereo(channel: c_int, flip: c_int) -> c_int; + pub fn Mix_ReserveChannels(num: c_int) -> c_int; + pub fn Mix_GroupChannel(which: c_int, tag: c_int) -> c_int; + pub fn Mix_GroupChannels(from: c_int, to: c_int, tag: c_int) -> c_int; + pub fn Mix_GroupAvailable(tag: c_int) -> c_int; + pub fn Mix_GroupCount(tag: c_int) -> c_int; + pub fn Mix_GroupOldest(tag: c_int) -> c_int; + pub fn Mix_GroupNewer(tag: c_int) -> c_int; + pub fn Mix_PlayChannelTimed(channel: c_int, chunk: *Mix_Chunk, + loops: c_int, ticks: c_int) -> c_int; + pub fn Mix_PlayMusic(music: *Mix_Music, loops: c_int) -> c_int; + pub fn Mix_FadeInMusic(music: *Mix_Music, loops: c_int, ms: c_int) -> + c_int; + pub fn Mix_FadeInMusicPos(music: *Mix_Music, loops: c_int, ms: c_int, + position: c_double) -> c_int; + pub fn Mix_FadeInChannelTimed(channel: c_int, chunk: *Mix_Chunk, + loops: c_int, ms: c_int, ticks: c_int) -> + c_int; + pub fn Mix_Volume(channel: c_int, volume: c_int) -> c_int; + pub fn Mix_VolumeChunk(chunk: *Mix_Chunk, volume: c_int) -> c_int; + pub fn Mix_VolumeMusic(volume: c_int) -> c_int; + pub fn Mix_HaltChannel(channel: c_int) -> c_int; + pub fn Mix_HaltGroup(tag: c_int) -> c_int; + pub fn Mix_HaltMusic() -> c_int; + pub fn Mix_ExpireChannel(channel: c_int, ticks: c_int) -> c_int; + pub fn Mix_FadeOutChannel(which: c_int, ms: c_int) -> c_int; + pub fn Mix_FadeOutGroup(tag: c_int, ms: c_int) -> c_int; + pub fn Mix_FadeOutMusic(ms: c_int) -> c_int; + pub fn Mix_FadingMusic() -> Mix_Fading; + pub fn Mix_FadingChannel(which: c_int) -> Mix_Fading; + pub fn Mix_Pause(channel: c_int); + pub fn Mix_Resume(channel: c_int); + pub fn Mix_Paused(channel: c_int) -> c_int; + pub fn Mix_PauseMusic(); + pub fn Mix_ResumeMusic(); + pub fn Mix_RewindMusic(); + pub fn Mix_PausedMusic() -> c_int; + pub fn Mix_SetMusicPosition(position: c_double) -> c_int; + pub fn Mix_Playing(channel: c_int) -> c_int; + pub fn Mix_PlayingMusic() -> c_int; + pub fn Mix_SetMusicCMD(command: *c_schar) -> c_int; + pub fn Mix_SetSynchroValue(value: c_int) -> c_int; + pub fn Mix_GetSynchroValue() -> c_int; + pub fn Mix_SetSoundFonts(paths: *c_schar) -> c_int; + pub fn Mix_GetSoundFonts() -> *c_schar; + pub fn Mix_EachSoundFont(function: + ::std::option::Option c_int>, + data: *c_void) -> c_int; + pub fn Mix_GetChunk(channel: c_int) -> *Mix_Chunk; + pub fn Mix_CloseAudio(); +} diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs new file mode 100644 index 00000000..4ce6d474 --- /dev/null +++ b/src/sdl2_mixer/lib.rs @@ -0,0 +1,698 @@ +#![crate_id="sdl2_mixer#sdl2_mixer:0.1"] +#![crate_type = "lib"] +#![desc = "SDL2_mixer bindings and wrappers"] +#![comment = "SDL2_mixer bindings and wrappers"] +#![license = "MIT"] + +#![feature(globs)] + +extern crate libc; +extern crate sdl2; + +use libc::{c_int, uint16_t, c_double}; +use std::fmt; +use std::c_str::CString; +use std::default; +use std::ptr; +use sdl2::get_error; +use sdl2::rwops::RWops; + +// Setup linking for all targets. +#[cfg(target_os="macos")] +mod mac { + #[cfg(mac_framework)] + #[link(kind="framework", name="SDL2_mixer")] + extern {} + + #[cfg(not(mac_framework))] + #[link(name="SDL2_mixer")] + extern {} +} + +#[cfg(target_os="win32")] +#[cfg(target_os="linux")] +#[cfg(target_os="freebsd")] +mod others { + #[link(name="SDL2_mixer")] + extern {} +} + +#[allow(non_camel_case_types, dead_code)] +mod ffi; + +pub static DEFAULT_CHANNELS : int = 2; +pub static DEFAULT_FREQUENCY : int = 22050; +pub static MAX_VOLUME : int = 128; + + +/// The version of the libSDL.so that you are linked to +#[deriving(Eq, Clone)] +pub struct SDLVersion { + pub major: int, + pub minor: int, + pub patch: int, +} + +impl fmt::Show for SDLVersion { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{}.{}.{}", self.major, self.minor, self.patch) + } +} + +impl SDLVersion { + fn from_sdl_version(sv: *ffi::SDL_version) -> SDLVersion { + //! Converts a raw *SDL_version to SDLVersion + unsafe { + let v = *sv; + SDLVersion{ major: v.major as int, minor: v.minor as int, patch: v.patch as int } + } + } +} + +pub fn get_linked_version() -> SDLVersion { + //! Returns the version of the dynamically linked SDL_mixer library + unsafe { + SDLVersion::from_sdl_version(ffi::Mix_Linked_Version()) + } +} + +#[repr(C)] +#[deriving(Clone, Eq, Hash, Show)] +pub enum InitFlag { + InitFlac = ffi::MIX_INIT_FLAC as int, + InitMod = ffi::MIX_INIT_MOD as int, + InitModPlug = ffi::MIX_INIT_MODPLUG as int, + InitMp3 = ffi::MIX_INIT_MP3 as int, + InitOgg = ffi::MIX_INIT_OGG as int, + InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as int, +} + +pub fn init(flags: &[InitFlag]) -> ~[InitFlag] { + //! Loads dynamic libraries and prepares them for use. Flags should be + //! one or more flags from InitFlag. + //! It returns the flags successfully initialized, or [] on failure. + let mut used = ~[]; + unsafe { + let used_flags = ffi::Mix_Init( + flags.iter().fold(0, |flags, &flag| { + flags | flag as ffi::MIX_InitFlags + }) as c_int + ); + for flag in flags.iter() { + if used_flags & *flag as c_int != 0 { + used.push(*flag) + } + } + } + used +} + +pub fn quit() { + //! Cleans up all dynamically loaded library handles, freeing memory. + unsafe { ffi::Mix_Quit(); } +} + +pub fn open_audio(frequency: int, format: u16, channels: int, chunksize: int) -> Result<(), ~str> { + //! Open the mixer with a certain audio format. + let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; + if ret == 0 { Ok(()) } + else { Err(get_error()) } +} + +pub fn close_audio() { + //! Shutdown and cleanup the mixer API. + unsafe { ffi::Mix_CloseAudio() } +} + +pub fn query_spec() -> Result<(int, u16, int), ~str> { + //! Get the actual audio format in use by the opened audio device. + let frequency : c_int = 0; + let format : uint16_t = 0; + let channels : c_int = 0; + let ret = unsafe { ffi::Mix_QuerySpec(&frequency, &format, &channels) }; + if ret == 0 { + Ok((frequency as int, format as u16, channels as int)) + } else { + Err(get_error()) + } +} + +// 4.2 Samples + +pub fn get_chunk_decoders_number() -> int { + //! Get the number of sample chunk decoders available from the Mix_GetChunkDecoder function. + unsafe { ffi::Mix_GetNumChunkDecoders() as int } +} + +pub fn get_chunk_decoder(index: int) -> ~str { + //! Get the name of the indexed sample chunk decoder. + unsafe { + let name = ffi::Mix_GetChunkDecoder(index as c_int); + CString::new(name, false).as_str().unwrap().into_owned() + } +} + +#[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] +pub struct Chunk { + pub raw: *ffi::Mix_Chunk, + pub owned: bool +} + +impl Drop for Chunk { + fn drop(&mut self) { + if self.owned { + unsafe { ffi::Mix_FreeChunk(self.raw) } + } + } +} + +impl Chunk { + pub fn from_file(path: &Path) -> Result<~Chunk, ~str> { + //! Load file for use as a sample. + let raw = unsafe { + ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "wb")).raw, 0) + }; + if raw.is_null() { + Err(get_error()) + } else { + Ok(~Chunk{ raw: raw, owned: true }) + } + } + + pub fn set_volume(&mut self, volume: int) -> int { + //! Set chunk->volume to volume. + unsafe { + ffi::Mix_VolumeChunk(self.raw, volume as c_int) as int + } + } + + pub fn get_volume(&self) -> int { + //! current volume for the chunk. + unsafe { + ffi::Mix_VolumeChunk(self.raw, -1) as int + } + } +} + +pub trait LoaderRWops { + /// Load src for use as a sample. + fn load_wav(&self) -> Result<~Chunk, ~str>; +} + +impl LoaderRWops for RWops { + fn load_wav(&self) -> Result<~Chunk, ~str> { + //! Load src for use as a sample. + let raw = unsafe { + ffi::Mix_LoadWAV_RW(self.raw, 0) + }; + if raw == ptr::null() { + Err(get_error()) + } else { + Ok(~Chunk{ raw: raw, owned: true }) + } + } +} + + +// 4.3 Channels + +#[repr(C)] +#[deriving(Clone, Eq, Hash, Show, FromPrimitive)] +pub enum Fading { + FadingNo = ffi::MIX_NO_FADING as int, + FadingOut = ffi::MIX_FADING_OUT as int, + FadingIn = ffi::MIX_FADING_IN as int +} + +pub struct Channel(int); + +pub fn allocate_channels(numchans: int) -> int { + //! Set the number of channels being mixed. + unsafe { + ffi::Mix_AllocateChannels(numchans as c_int) as int + } +} + +impl Channel { + pub fn all() -> Channel { + //! all channel (-1) + Channel(-1) + } + + pub fn post() -> Channel { + //! MIX_CHANNEL_POST -2 + Channel(-2) + } + + pub fn set_volume(self, volume: int) -> int { + //! Set the volume for any allocated channel. + //! If channel is -1 then all channels at are set at once. + let Channel(ch) = self; + unsafe { + ffi::Mix_Volume(ch as c_int, volume as c_int) as int + } + } + + pub fn get_volume(self) -> int { + let Channel(ch) = self; + unsafe { + ffi::Mix_Volume(ch as c_int, -1) as int + } + } + + pub fn play(self, chunk: Chunk, loops: int) -> Result { + //! Play chunk on channel, or if channel is -1, pick the first free unreserved channel. + self.play_timed(chunk, loops, -1) + } + + pub fn play_timed(self, chunk: Chunk, loops: int, ticks: int) -> Result { + let Channel(ch) = self; + let ret = unsafe { + ffi::Mix_PlayChannelTimed(ch as c_int, chunk.raw, loops as c_int, ticks as c_int) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(Channel(ret as int)) + } + } + + pub fn fade_in(self, chunk: Chunk, loops: int, ms: int) -> Result { + //! Play chunk on channel, or if channel is -1, pick the first free unreserved channel. + self.fade_in_timed(chunk, loops, ms, -1) + } + + pub fn fade_in_timed(self, chunk: Chunk, loops: int, ms: int, ticks: int) -> Result { + let Channel(ch) = self; + let ret = unsafe { + ffi::Mix_FadeInChannelTimed(ch as c_int, chunk.raw, loops as c_int, ms as c_int, ticks as c_int) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(Channel(ret as int)) + } + } + + pub fn pause(self) { + //! Pause channel, or all playing channels if -1 is passed in. + let Channel(ch) = self; + unsafe { ffi::Mix_Pause(ch as c_int); } + } + + pub fn resume(self) { + //! Unpause channel, or all playing and paused channels if -1 is passed in. + let Channel(ch) = self; + unsafe { ffi::Mix_Resume(ch as c_int); } + } + + pub fn halt(self) { + //! Halt channel playback + let Channel(ch) = self; + unsafe { ffi::Mix_HaltChannel(ch as c_int); } + } + + pub fn expire(self, ticks: int) -> int { + //! Halt channel playback, after ticks milliseconds. + let Channel(ch) = self; + unsafe { ffi::Mix_ExpireChannel(ch as c_int, ticks as c_int) as int } + } + + pub fn fade_out(self, ms: int) -> int { + //! Gradually fade out which channel over ms milliseconds starting from now. + let Channel(ch) = self; + unsafe { ffi::Mix_FadeOutChannel(ch as c_int, ms as c_int) as int } + } + + // TODO: Mix_ChannelFinished + + pub fn is_playing(self) -> bool { + //! if channel is playing, or not. + let Channel(ch) = self; + unsafe { ffi::Mix_Playing(ch as c_int) != 0 } + } + + pub fn is_paused(self) -> bool { + //! if channel is paused, or not. + let Channel(ch) = self; + unsafe { ffi::Mix_Paused(ch as c_int) != 0 } + } + + pub fn get_fading(self) -> Fading { + //! if channel is fading in, out, or not + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as int }; + FromPrimitive::from_int(ret).unwrap() + } + + pub fn get_chunk(self) -> Option { + //! Get the most recent sample chunk pointer played on channel. + let Channel(ch) = self; + let raw = unsafe { ffi::Mix_GetChunk(ch as c_int) }; + if raw.is_null() { + None + } else { + Some( Chunk { raw: raw, owned: false } ) + } + } + + pub fn unregister_all_effects(self) -> Result<(), ~str> { + //! This removes all effects registered to channel. + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_UnregisterAllEffects(ch as c_int) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn set_panning(self, left: u8, right: u8) -> Result<(), ~str> { + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, left, right) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn set_distance(self, distance: u8) -> Result<(), ~str> { + //! This effect simulates a simple attenuation of volume due to distance. + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, distance) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn set_position(self, angle: i16, distance: u8) -> Result<(), ~str> { + //! This effect emulates a simple 3D audio effect. + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, angle, distance) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn set_reverse_stereo(self, flip: bool) -> Result<(), ~str> { + //! Simple reverse stereo, swaps left and right channel sound. + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetReverseStereo(ch as c_int, flip as c_int) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } +} + +pub fn get_playing_channels_number() -> int { + unsafe { ffi::Mix_Playing(-1) as int } +} + +pub fn get_paused_channels_number() -> int { + unsafe { ffi::Mix_Paused(-1) as int } +} + +// 4.4 Groups + +pub fn reserve_channels(num: int) -> int { + //! Reserve num channels from being used when playing samples when + //! passing in -1 as a channel number to playback functions. + unsafe { ffi::Mix_ReserveChannels(num as c_int) as int } +} + +pub struct Group(int); + +impl default::Default for Group { + fn default() -> Group { + Group(-1) + } +} + +impl Group { + pub fn add_channels_range(self, from: int, to: int) -> int { + //! Add channels starting at from up through to to group tag, + //! or reset it's group to the default group tag (-1). + let Group(g) = self; + unsafe { ffi::Mix_GroupChannels(from as c_int, to as c_int, g as c_int) as int } + } + + pub fn add_channel(self, Channel(ch): Channel) -> bool { + //! Add which channel to group tag, or reset it's group to the default group tag + let Group(g) = self; + unsafe { ffi::Mix_GroupChannel(ch as c_int, g as c_int) == 1 } + } + + pub fn count(self) -> int { + //! Count the number of channels in group + let Group(g) = self; + unsafe { ffi::Mix_GroupCount(g as c_int) as int } + } + + pub fn find_available(self) -> Option { + //! Find the first available (not playing) channel in group + let Group(g) = self; + let ret = unsafe { ffi::Mix_GroupAvailable(g as c_int) as int }; + if ret == -1 { + None + } else { + Some(Channel(ret)) + } + } + + pub fn find_oldest(self) -> Option { + //! Find the oldest actively playing channel in group + let Group(g) = self; + let ret = unsafe { ffi::Mix_GroupOldest(g as c_int) as int }; + if ret == -1 { + None + } else { + Some(Channel(ret)) + } + } + + pub fn find_newest(self) -> Option { + //! Find the newest, most recently started, actively playing channel in group. + let Group(g) = self; + let ret = unsafe { ffi::Mix_GroupNewer(g as c_int) as int }; + if ret == -1 { + None + } else { + Some(Channel(ret)) + } + } + + pub fn fade_out(self, ms: int) -> int { + //! Gradually fade out channels in group over some milliseconds starting from now. + //! Returns the number of channels set to fade out. + let Group(g) = self; + unsafe { ffi::Mix_FadeOutGroup(g as c_int, ms as c_int) as int } + } + + pub fn halt(self) { + //! Halt playback on all channels in group. + let Group(g) = self; + unsafe { ffi::Mix_HaltGroup(g as c_int); } + } +} + +// 4.5 Music + +pub fn get_music_decoders_number() -> int { + //! Get the number of music decoders available. + unsafe { ffi::Mix_GetNumMusicDecoders() as int } +} + +pub fn get_music_decoder(index: int) -> ~str { + //! Get the name of the indexed music decoder. + unsafe { + let name = ffi::Mix_GetMusicDecoder(index as c_int); + CString::new(name, false).as_str().unwrap().into_owned() + } +} + + +#[repr(C)] +#[deriving(Clone, Eq, Hash, Show, FromPrimitive)] +pub enum MusicType { + TypeNone = ffi::MUS_NONE as int, + TypeCmd = ffi::MUS_CMD as int, + TypeWav = ffi::MUS_WAV as int, + TypeMod = ffi::MUS_MOD as int, + TypeMid = ffi::MUS_MID as int, + TypeOgg = ffi::MUS_OGG as int, + TypeMp3 = ffi::MUS_MP3 as int, + TypeMp3Mad = ffi::MUS_MP3_MAD as int, + TypeFlac = ffi::MUS_FLAC as int, + TypeModPlug = ffi::MUS_MODPLUG as int +} + +/// This is an opaque data type used for Music data. +#[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] +pub struct Music { + pub raw: *ffi::Mix_Music, + pub owned: bool +} + +impl Drop for Music { + fn drop(&mut self) { + if self.owned { + unsafe { ffi::Mix_FreeMusic(self.raw) }; + } + } +} + +impl Music { + pub fn from_file(path: &Path) -> Result<~Music, ~str> { + //! Load music file to use. + let raw = unsafe { + ffi::Mix_LoadMUS(path.to_c_str().unwrap()) + }; + if raw.is_null() { + Err(get_error()) + } else { + Ok(~Music{ raw: raw, owned: true }) + } + } + + pub fn get_type(&self) -> MusicType { + //! The file format encoding of the music. + let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as int }; + FromPrimitive::from_int(ret).unwrap() + } + + pub fn play(&self, loops: int) -> Result<(), ~str> { + //! Play the loaded music loop times through from start to finish. + let ret = unsafe { + ffi::Mix_PlayMusic(self.raw, loops as c_int) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn fade_in(&self, loops: int, ms: int) -> Result<(), ~str> { + //! Fade in over ms milliseconds of time, the loaded music, + //! playing it loop times through from start to finish. + let ret = unsafe { + ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn fade_in_from_pos(&self, loops: int, ms: int, position: f64) -> Result<(), ~str> { + //! Fade in over ms milliseconds of time, from position. + let ret = unsafe { + ffi::Mix_FadeInMusicPos(self.raw, loops as c_int, ms as c_int, position as c_double) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + // FIXME: make these class method? + pub fn get__volume() -> int { + unsafe { ffi::Mix_VolumeMusic(-1) as int } + } + + pub fn set_volume(volume: int) -> int { + //! Set the volume. + unsafe { ffi::Mix_VolumeMusic(volume as c_int) as int } + } + + pub fn pause() { + //! Pause the music playback. + unsafe { ffi::Mix_PauseMusic(); } + } + + pub fn resume() { + //! Unpause the music. + unsafe { ffi::Mix_ResumeMusic(); } + } + + pub fn rewind() { + //! Rewind the music to the start. + unsafe { ffi::Mix_RewindMusic(); } + } + + pub fn set_pos(position: f64) -> Result<(), ~str> { + //! Set the position of the currently playing music. + let ret = unsafe { + ffi::Mix_SetMusicPosition(position as c_double) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn set_cmd(command: &str) -> Result<(), ~str> { + //! Setup a command line music player to use to play music. + let ret = unsafe { + ffi::Mix_SetMusicCMD(command.to_c_str().unwrap()) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + pub fn halt() { + //! Halt playback of music. + unsafe { ffi::Mix_HaltMusic(); } + } + + pub fn fade_out(ms: int) -> Result<(), ~str> { + //! Gradually fade out the music over ms milliseconds starting from now. + let ret = unsafe { + ffi::Mix_FadeOutMusic(ms as c_int) + }; + if ret == -1 { + Err(get_error()) + } else { + Ok(()) + } + } + + // TODO: Mix_HookMusic + // TODO: Mix_HookMusicFinished + // TODO: Mix_GetMusicHookData + + pub fn is_playing() -> bool { + //! If music is actively playing, or not. + unsafe { ffi::Mix_PlayingMusic() == 1 } + } + + pub fn is_paused() -> bool { + //! If music is paused, or not. + unsafe { ffi::Mix_PausedMusic() == 1 } + } + + pub fn get_fading() -> Fading { + //! If music is fading, or not. + let ret = unsafe { ffi::Mix_FadingMusic() as int }; + FromPrimitive::from_int(ret).unwrap() + } +} + +// 4.6 Effects + +// TODO: Mix_RegisterEffect +// TODO: Mix_UnregisterEffect +// TODO: Mix_SetPostMix From 8e7dfd5b2d006d58a5e17f84ae9ce9408964a9d3 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 11:19:17 +0800 Subject: [PATCH 002/114] README: fix demo command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 65eb8122..c2170b35 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,5 @@ Demo ``` rustc -L. src/demo/main.rs -o demo -./demo mixer.[png|jpg] +./demo mixer.[wav|mp3|ogg] ``` From 9754fc0e0f1339a8597cd47f8abc39cd01477333 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 12:08:50 +0800 Subject: [PATCH 003/114] fix typo in lib; add demo.rs --- src/demo/demo.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ src/sdl2_mixer/lib.rs | 4 +-- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/demo/demo.rs diff --git a/src/demo/demo.rs b/src/demo/demo.rs new file mode 100644 index 00000000..a8a64d6a --- /dev/null +++ b/src/demo/demo.rs @@ -0,0 +1,71 @@ +#![crate_type = "bin"] +#![crate_id="demo"] + +extern crate sdl2; +extern crate sdl2_mixer; + +use std::os; +use mix = sdl2_mixer; + + +fn main() { + let args = os::args(); + if args.len() < 2 { + println!("Usage: ./demo audio.[mp3|wav|ogg]") + } else { + match dump_info(&Path::new(args[1])) { + _ => () + } + } +} + +fn dump_info(filename: &Path) -> Result<(), ~str> { + println!("linked version: {}", mix::get_linked_version()); + sdl2::init([sdl2::InitAudio, sdl2::InitTimer]); + println!("inited => {}", mix::init([mix::InitMp3, mix::InitFlac])); + + // TODO: 0x8010 is SDL_audio flag + try!(mix::open_audio(mix::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); + mix::allocate_channels(0); + + { + let n = mix::get_chunk_decoders_number(); + println!("available chunk(sample) decoders: {}", n); + for i in range(0, n) { + println!("| decoder {} => {:?}", i, mix::get_chunk_decoder(i)); + } + } + + { + let n = mix::get_music_decoders_number(); + println!("available music decoders: {}", n); + for i in range(0, n) { + println!("| decoder {} => {:?}", i, mix::get_music_decoder(i)); + } + } + + println!("query spec => {}", mix::query_spec()); + + let music = mix::Music::from_file(filename).unwrap(); + println!("music => {:?}", music); + println!("music type => {}", music.get_type()); + + println!("music volume => {}", mix::Music::get_volume()); + + println!("play => {}", music.play(1)); + + sdl2::timer::delay(10000); + + println!("fading out ... {}", mix::Music::fade_out(4000)); + + sdl2::timer::delay(5000); + + println!("fading in from pos ... {}", music.fade_in_from_pos(1, 10000, 100.0)); + + sdl2::timer::delay(5000); + + mix::quit(); + sdl2::quit(); + + Ok(()) +} diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 4ce6d474..690632fa 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -605,7 +605,7 @@ impl Music { } // FIXME: make these class method? - pub fn get__volume() -> int { + pub fn get_volume() -> int { unsafe { ffi::Mix_VolumeMusic(-1) as int } } @@ -641,7 +641,7 @@ impl Music { } } - pub fn set_cmd(command: &str) -> Result<(), ~str> { + pub fn set_command(command: &str) -> Result<(), ~str> { //! Setup a command line music player to use to play music. let ret = unsafe { ffi::Mix_SetMusicCMD(command.to_c_str().unwrap()) From 521b2c9b72a0c7a54c62582cb717feed343643d4 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 12:10:15 +0800 Subject: [PATCH 004/114] README: fix demo command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c2170b35..cdb34eda 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,6 @@ Demo ---- ``` -rustc -L. src/demo/main.rs -o demo +rustc -L. src/demo/demo.rs ./demo mixer.[wav|mp3|ogg] ``` From 31df40c848fe672ef79b17756ab9f01ffa9d0ead Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 12:33:19 +0800 Subject: [PATCH 005/114] add .travis.yml for travis ci --- .travis.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..39053630 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,20 @@ +before_install: + - yes | sudo add-apt-repository ppa:hansjorg/rust + - sudo apt-get update +install: + - sudo apt-get install rust-nightly + - cd .. + - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz + - time wget -q http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz + - time tar xf SDL2-*.tar.gz + - time tar xf SDL2_mixer-*.tar.gz + - cd SDL2-* + - ./configure && make && sudo make install + - cd - + - cd SDL2_mixer-* + - ./configure && make && sudo make install + - cd - + - git clone https://github.com/andelf/rust-sdl2.git + - rustc rust-sdl2/src/sdl2/lib.rs +script: + - rustc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs From a207ac64150d5e93ce7142c08c0e702febd3f1c0 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 9 Apr 2014 13:58:42 +0800 Subject: [PATCH 006/114] add doc generating command to .travis.yml --- .travis.yml | 6 ++++++ src/sdl2_mixer/lib.rs | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 39053630..1e8a21b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,6 @@ +env: + global: + - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= before_install: - yes | sudo add-apt-repository ppa:hansjorg/rust - sudo apt-get update @@ -18,3 +21,6 @@ install: - rustc rust-sdl2/src/sdl2/lib.rs script: - rustc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs + - rustdoc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs +after_script: + - curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 690632fa..5dafa602 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -1,3 +1,7 @@ +/*! +A binding for SDL2_mixer. + */ + #![crate_id="sdl2_mixer#sdl2_mixer:0.1"] #![crate_type = "lib"] #![desc = "SDL2_mixer bindings and wrappers"] @@ -152,6 +156,7 @@ pub fn get_chunk_decoder(index: int) -> ~str { } } +/// The internal format for an audio chunk. #[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Chunk { pub raw: *ffi::Mix_Chunk, @@ -194,6 +199,7 @@ impl Chunk { } } +/// Loader trait for RWops pub trait LoaderRWops { /// Load src for use as a sample. fn load_wav(&self) -> Result<~Chunk, ~str>; @@ -224,6 +230,7 @@ pub enum Fading { FadingIn = ffi::MIX_FADING_IN as int } +/// Sound effect channel. pub struct Channel(int); pub fn allocate_channels(numchans: int) -> int { @@ -235,12 +242,12 @@ pub fn allocate_channels(numchans: int) -> int { impl Channel { pub fn all() -> Channel { - //! all channel (-1) + //! Represent for all channels (-1) Channel(-1) } pub fn post() -> Channel { - //! MIX_CHANNEL_POST -2 + //! This is the MIX_CHANNEL_POST (-2) Channel(-2) } @@ -427,6 +434,7 @@ pub fn reserve_channels(num: int) -> int { unsafe { ffi::Mix_ReserveChannels(num as c_int) as int } } +/// Sound effect channel grouping. pub struct Group(int); impl default::Default for Group { From 31545e0e60270322a4e29300b5add514a33935c4 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 14 Apr 2014 13:53:31 +0800 Subject: [PATCH 007/114] add AudioFormat definition --- src/sdl2_mixer/lib.rs | 79 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 14 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 5dafa602..5a98d056 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -44,8 +44,59 @@ mod others { #[allow(non_camel_case_types, dead_code)] mod ffi; +// This comes from SDL_audio.h +#[allow(non_camel_case_types)] +mod ll { + use libc::uint16_t; + + pub static AUDIO_U8 : uint16_t = 0x0008; + pub static AUDIO_S8 : uint16_t = 0x8008; + pub static AUDIO_U16LSB : uint16_t = 0x0010; + pub static AUDIO_S16LSB : uint16_t = 0x8010; + pub static AUDIO_U16MSB : uint16_t = 0x1010; + pub static AUDIO_S16MSB : uint16_t = 0x9010; + pub static AUDIO_U16 : uint16_t = AUDIO_U16LSB; + pub static AUDIO_S16 : uint16_t = AUDIO_S16LSB; + pub static AUDIO_S32LSB : uint16_t = 0x8020; + pub static AUDIO_S32MSB : uint16_t = 0x9020; + pub static AUDIO_S32 : uint16_t = AUDIO_S32LSB; + pub static AUDIO_F32LSB : uint16_t = 0x8120; + pub static AUDIO_F32MSB : uint16_t = 0x9120; + pub static AUDIO_F32 : uint16_t = AUDIO_F32LSB; + pub static AUDIO_U16SYS : uint16_t = AUDIO_U16LSB; + pub static AUDIO_S16SYS : uint16_t = AUDIO_S16LSB; + pub static AUDIO_S32SYS : uint16_t = AUDIO_S32LSB; + pub static AUDIO_F32SYS : uint16_t = AUDIO_F32LSB; +} + +pub type AudioFormat = uint16_t; + +pub static AudioU8 : AudioFormat = ll::AUDIO_U8; +pub static AudioS8 : AudioFormat = ll::AUDIO_S8; +pub static AudioU16LSB : AudioFormat = ll::AUDIO_U16LSB; +pub static AudioS16LSB : AudioFormat = ll::AUDIO_S16LSB; +pub static AudioU16MSB : AudioFormat = ll::AUDIO_U16MSB; +pub static AudioS16MSB : AudioFormat = ll::AUDIO_S16MSB; +pub static AudioU16 : AudioFormat = ll::AUDIO_U16; +pub static AudioS16 : AudioFormat = ll::AUDIO_S16; +pub static AudioS32LSB : AudioFormat = ll::AUDIO_S32LSB; +pub static AudioS32MSB : AudioFormat = ll::AUDIO_S32MSB; +pub static AudioS32 : AudioFormat = ll::AUDIO_S32; +pub static AudioF32LSB : AudioFormat = ll::AUDIO_F32LSB; +pub static AudioF32MSB : AudioFormat = ll::AUDIO_F32MSB; +pub static AudioF32 : AudioFormat = ll::AUDIO_F32; +pub static AudioU16SYS : AudioFormat = ll::AUDIO_U16SYS; +pub static AudioS16SYS : AudioFormat = ll::AUDIO_S16SYS; +pub static AudioS32SYS : AudioFormat = ll::AUDIO_S32SYS; +pub static AudioF32SYS : AudioFormat = ll::AUDIO_F32SYS; + +/// The suggested default is signed 16bit samples in host byte order. +pub static DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; +/// Defualt channels: Stereo. pub static DEFAULT_CHANNELS : int = 2; +/// Good default sample rate in Hz (samples per second) for PC sound cards. pub static DEFAULT_FREQUENCY : int = 22050; +/// Maximum value for any volume setting. pub static MAX_VOLUME : int = 128; @@ -116,7 +167,7 @@ pub fn quit() { unsafe { ffi::Mix_Quit(); } } -pub fn open_audio(frequency: int, format: u16, channels: int, chunksize: int) -> Result<(), ~str> { +pub fn open_audio(frequency: int, format: AudioFormat, channels: int, chunksize: int) -> Result<(), ~str> { //! Open the mixer with a certain audio format. let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; if ret == 0 { Ok(()) } @@ -128,14 +179,14 @@ pub fn close_audio() { unsafe { ffi::Mix_CloseAudio() } } -pub fn query_spec() -> Result<(int, u16, int), ~str> { +pub fn query_spec() -> Result<(int, AudioFormat, int), ~str> { //! Get the actual audio format in use by the opened audio device. let frequency : c_int = 0; let format : uint16_t = 0; let channels : c_int = 0; let ret = unsafe { ffi::Mix_QuerySpec(&frequency, &format, &channels) }; if ret == 0 { - Ok((frequency as int, format as u16, channels as int)) + Ok((frequency as int, format as AudioFormat, channels as int)) } else { Err(get_error()) } @@ -225,7 +276,7 @@ impl LoaderRWops for RWops { #[repr(C)] #[deriving(Clone, Eq, Hash, Show, FromPrimitive)] pub enum Fading { - FadingNo = ffi::MIX_NO_FADING as int, + NoFading = ffi::MIX_NO_FADING as int, FadingOut = ffi::MIX_FADING_OUT as int, FadingIn = ffi::MIX_FADING_IN as int } @@ -529,16 +580,16 @@ pub fn get_music_decoder(index: int) -> ~str { #[repr(C)] #[deriving(Clone, Eq, Hash, Show, FromPrimitive)] pub enum MusicType { - TypeNone = ffi::MUS_NONE as int, - TypeCmd = ffi::MUS_CMD as int, - TypeWav = ffi::MUS_WAV as int, - TypeMod = ffi::MUS_MOD as int, - TypeMid = ffi::MUS_MID as int, - TypeOgg = ffi::MUS_OGG as int, - TypeMp3 = ffi::MUS_MP3 as int, - TypeMp3Mad = ffi::MUS_MP3_MAD as int, - TypeFlac = ffi::MUS_FLAC as int, - TypeModPlug = ffi::MUS_MODPLUG as int + MusicNone = ffi::MUS_NONE as int, + MusicCmd = ffi::MUS_CMD as int, + MusicWav = ffi::MUS_WAV as int, + MusicMod = ffi::MUS_MOD as int, + MusicMid = ffi::MUS_MID as int, + MusicOgg = ffi::MUS_OGG as int, + MusicMp3 = ffi::MUS_MP3 as int, + MusicMp3Mad = ffi::MUS_MP3_MAD as int, + MusicFlac = ffi::MUS_FLAC as int, + MusicModPlug = ffi::MUS_MODPLUG as int } /// This is an opaque data type used for Music data. From c3beb759d0601bc64dde36942a77790325a24d05 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 14 Apr 2014 18:41:52 +0800 Subject: [PATCH 008/114] add finished hook/callback to lib --- src/demo/demo.rs | 8 ++++++ src/sdl2_mixer/lib.rs | 65 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/demo/demo.rs b/src/demo/demo.rs index a8a64d6a..5dbf93a8 100644 --- a/src/demo/demo.rs +++ b/src/demo/demo.rs @@ -47,6 +47,9 @@ fn dump_info(filename: &Path) -> Result<(), ~str> { println!("query spec => {}", mix::query_spec()); let music = mix::Music::from_file(filename).unwrap(); + + mix::Music::hook_finished(|| { println!("play ends! from rust cb") }); + println!("music => {:?}", music); println!("music type => {}", music.get_type()); @@ -64,6 +67,11 @@ fn dump_info(filename: &Path) -> Result<(), ~str> { sdl2::timer::delay(5000); + mix::Music::halt(); + + sdl2::timer::delay(1000); + // here will print hook_finished + mix::quit(); sdl2::quit(); diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 5a98d056..39f2ed33 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -18,6 +18,8 @@ use std::fmt; use std::c_str::CString; use std::default; use std::ptr; +use std::cast; +use std::raw; use sdl2::get_error; use sdl2::rwops::RWops; @@ -282,6 +284,7 @@ pub enum Fading { } /// Sound effect channel. +#[deriving(Eq, Show)] pub struct Channel(int); pub fn allocate_channels(numchans: int) -> int { @@ -291,6 +294,35 @@ pub fn allocate_channels(numchans: int) -> int { } } +static mut channel_finished_callback: Option = None; + +extern "C" fn c_channel_finished_callback(ch: c_int) { + unsafe { + match channel_finished_callback { + None => (), + Some(cb) => { + let cb = cast::transmute::<_, |Channel|>(cb); + cb(Channel(ch as int)) + } + } + } +} + +pub fn set_channel_finished(f: |Channel|:'static) { + //! When channel playback is halted, then the specified channel_finished function is called. + unsafe { + channel_finished_callback = Some(cast::transmute::<_, raw::Closure>(f)); + ffi::Mix_ChannelFinished(Some(c_channel_finished_callback)); + } +} + +pub fn unset_channel_finished() { + unsafe { + ffi::Mix_ChannelFinished(None); + channel_finished_callback = None; + } +} + impl Channel { pub fn all() -> Channel { //! Represent for all channels (-1) @@ -382,8 +414,6 @@ impl Channel { unsafe { ffi::Mix_FadeOutChannel(ch as c_int, ms as c_int) as int } } - // TODO: Mix_ChannelFinished - pub fn is_playing(self) -> bool { //! if channel is playing, or not. let Channel(ch) = self; @@ -592,11 +622,24 @@ pub enum MusicType { MusicModPlug = ffi::MUS_MODPLUG as int } +// hooks +static mut music_finished_hook: Option = None; + +extern "C" fn c_music_finished_hook() { + unsafe { match music_finished_hook { + None => (), + Some(f) => { + let f = cast::transmute::<_, ||>(f); + f() + } + } } +} + /// This is an opaque data type used for Music data. #[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Music { pub raw: *ffi::Mix_Music, - pub owned: bool + pub owned: bool, } impl Drop for Music { @@ -730,9 +773,23 @@ impl Music { } // TODO: Mix_HookMusic - // TODO: Mix_HookMusicFinished // TODO: Mix_GetMusicHookData + pub fn hook_finished(f: ||) { + unsafe { + music_finished_hook = Some(cast::transmute(f)); + ffi::Mix_HookMusicFinished(Some(c_music_finished_hook)); + } + } + + pub fn unhook_finished() { + unsafe { + ffi::Mix_HookMusicFinished(None); + // unset from c, then rust, to avoid race condiction + music_finished_hook = None; + } + } + pub fn is_playing() -> bool { //! If music is actively playing, or not. unsafe { ffi::Mix_PlayingMusic() == 1 } From 9a18d31b535bc78b6889c4ea4ed32dc891bf921c Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 14 Apr 2014 19:49:50 +0800 Subject: [PATCH 009/114] use new style InitFlags --- src/demo/demo.rs | 3 +- src/sdl2_mixer/lib.rs | 71 +++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/demo/demo.rs b/src/demo/demo.rs index 5dbf93a8..46aebb15 100644 --- a/src/demo/demo.rs +++ b/src/demo/demo.rs @@ -5,6 +5,7 @@ extern crate sdl2; extern crate sdl2_mixer; use std::os; +use std::default::Default; use mix = sdl2_mixer; @@ -22,7 +23,7 @@ fn main() { fn dump_info(filename: &Path) -> Result<(), ~str> { println!("linked version: {}", mix::get_linked_version()); sdl2::init([sdl2::InitAudio, sdl2::InitTimer]); - println!("inited => {}", mix::init([mix::InitMp3, mix::InitFlac])); + println!("inited => {}", mix::init(Default::default())); // TODO: 0x8010 is SDL_audio flag try!(mix::open_audio(mix::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 39f2ed33..4e138f43 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -20,6 +20,8 @@ use std::default; use std::ptr; use std::cast; use std::raw; +use std::ops::BitOr; +use std::default::Default; use sdl2::get_error; use sdl2::rwops::RWops; @@ -133,35 +135,58 @@ pub fn get_linked_version() -> SDLVersion { } } -#[repr(C)] #[deriving(Clone, Eq, Hash, Show)] pub enum InitFlag { - InitFlac = ffi::MIX_INIT_FLAC as int, - InitMod = ffi::MIX_INIT_MOD as int, - InitModPlug = ffi::MIX_INIT_MODPLUG as int, - InitMp3 = ffi::MIX_INIT_MP3 as int, - InitOgg = ffi::MIX_INIT_OGG as int, - InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as int, + InitFlac, + InitMod, + InitModPlug, + InitMp3, + InitOgg, + InitFluidSynth, + InitRaw(int), } -pub fn init(flags: &[InitFlag]) -> ~[InitFlag] { - //! Loads dynamic libraries and prepares them for use. Flags should be - //! one or more flags from InitFlag. - //! It returns the flags successfully initialized, or [] on failure. - let mut used = ~[]; - unsafe { - let used_flags = ffi::Mix_Init( - flags.iter().fold(0, |flags, &flag| { - flags | flag as ffi::MIX_InitFlags - }) as c_int - ); - for flag in flags.iter() { - if used_flags & *flag as c_int != 0 { - used.push(*flag) - } +impl InitFlag { + #[inline] + pub fn to_ll(self) -> c_int { + match self { + InitFlac => ffi::MIX_INIT_FLAC as c_int, + InitMod => ffi::MIX_INIT_MOD as c_int, + InitModPlug => ffi::MIX_INIT_MODPLUG as c_int, + InitMp3 => ffi::MIX_INIT_MP3 as c_int, + InitOgg => ffi::MIX_INIT_OGG as c_int, + InitFluidSynth => ffi::MIX_INIT_FLUIDSYNTH as c_int, + InitRaw(n) => n as c_int, } } - used + + pub fn from_ll(n: c_int) -> InitFlag { + InitRaw(n as int) + } + + pub fn mask(self, flag: InitFlag) -> bool { + self.to_ll() & flag.to_ll() == flag.to_ll() + } +} + +impl BitOr for InitFlag { + fn bitor(&self, rhs: &InitFlag) -> InitFlag { + InitRaw(self.to_ll() as int | rhs.to_ll() as int) + } +} + +impl Default for InitFlag { + fn default() -> InitFlag { + InitFlag::from_ll((ffi::MIX_INIT_FLAC | ffi::MIX_INIT_MOD | ffi::MIX_INIT_MODPLUG | + ffi::MIX_INIT_MP3 | ffi::MIX_INIT_OGG | ffi::MIX_INIT_FLUIDSYNTH) as c_int) + } +} + +pub fn init(flags: InitFlag) -> InitFlag { + //! Loads dynamic libraries and prepares them for use. Flags should be + //! one or more flags from InitFlag. + let ret = unsafe { ffi::Mix_Init(flags.to_ll()) }; + InitFlag::from_ll(ret) } pub fn quit() { From 6b630f86fa18fe20f67b444befc736e7010f0d59 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 14 Apr 2014 20:03:23 +0800 Subject: [PATCH 010/114] re-arrange import crates --- src/sdl2_mixer/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 4e138f43..fd923efc 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -13,15 +13,14 @@ A binding for SDL2_mixer. extern crate libc; extern crate sdl2; -use libc::{c_int, uint16_t, c_double}; use std::fmt; -use std::c_str::CString; use std::default; use std::ptr; use std::cast; use std::raw; +use std::c_str::CString; use std::ops::BitOr; -use std::default::Default; +use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; @@ -175,7 +174,7 @@ impl BitOr for InitFlag { } } -impl Default for InitFlag { +impl default::Default for InitFlag { fn default() -> InitFlag { InitFlag::from_ll((ffi::MIX_INIT_FLAC | ffi::MIX_INIT_MOD | ffi::MIX_INIT_MODPLUG | ffi::MIX_INIT_MP3 | ffi::MIX_INIT_OGG | ffi::MIX_INIT_FLUIDSYNTH) as c_int) From 568fbbee40ed8ca51d094de7e4a6cba073bd7814 Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 16 Apr 2014 19:43:35 +0800 Subject: [PATCH 011/114] switch to use sdl2::version::Version --- src/sdl2_mixer/ffi.rs | 6 +----- src/sdl2_mixer/lib.rs | 30 +++--------------------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 19ae4728..0be6931b 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -2,12 +2,8 @@ use libc::*; use sdl2::rwops::ll::SDL_RWops; +use sdl2::version::ll::SDL_version; -pub struct SDL_version { - pub major: uint8_t, - pub minor: uint8_t, - pub patch: uint8_t, -} pub type MIX_InitFlags = c_uint; pub static MIX_INIT_FLAC: c_uint = 1; pub static MIX_INIT_MOD: c_uint = 2; diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index fd923efc..fa024a30 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -13,7 +13,6 @@ A binding for SDL2_mixer. extern crate libc; extern crate sdl2; -use std::fmt; use std::default; use std::ptr; use std::cast; @@ -23,6 +22,7 @@ use std::ops::BitOr; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; +use sdl2::version::Version; // Setup linking for all targets. #[cfg(target_os="macos")] @@ -103,34 +103,10 @@ pub static DEFAULT_FREQUENCY : int = 22050; pub static MAX_VOLUME : int = 128; -/// The version of the libSDL.so that you are linked to -#[deriving(Eq, Clone)] -pub struct SDLVersion { - pub major: int, - pub minor: int, - pub patch: int, -} - -impl fmt::Show for SDLVersion { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f.buf, "{}.{}.{}", self.major, self.minor, self.patch) - } -} - -impl SDLVersion { - fn from_sdl_version(sv: *ffi::SDL_version) -> SDLVersion { - //! Converts a raw *SDL_version to SDLVersion - unsafe { - let v = *sv; - SDLVersion{ major: v.major as int, minor: v.minor as int, patch: v.patch as int } - } - } -} - -pub fn get_linked_version() -> SDLVersion { +pub fn get_linked_version() -> Version { //! Returns the version of the dynamically linked SDL_mixer library unsafe { - SDLVersion::from_sdl_version(ffi::Mix_Linked_Version()) + Version::from_ll(ffi::Mix_Linked_Version()) } } From 9f8a17d9a3fa586813e1bbfc95aebe28430e8bce Mon Sep 17 00:00:00 2001 From: Andelf Date: Thu, 17 Apr 2014 18:23:04 +0800 Subject: [PATCH 012/114] fix libc glob reexports change --- src/sdl2_mixer/ffi.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 0be6931b..c6d675fa 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -52,7 +52,7 @@ extern "C" { channels: *c_int) -> c_int; pub fn Mix_LoadWAV_RW(src: *SDL_RWops, freesrc: c_int) -> *Mix_Chunk; - pub fn Mix_LoadMUS(file: *c_schar) -> *Mix_Music; + pub fn Mix_LoadMUS(file: *c_char) -> *Mix_Music; pub fn Mix_LoadMUS_RW(src: *SDL_RWops, freesrc: c_int) -> *Mix_Music; pub fn Mix_LoadMUSType_RW(src: *SDL_RWops, _type: Mix_MusicType, @@ -62,9 +62,9 @@ extern "C" { pub fn Mix_FreeChunk(chunk: *Mix_Chunk); pub fn Mix_FreeMusic(music: *Mix_Music); pub fn Mix_GetNumChunkDecoders() -> c_int; - pub fn Mix_GetChunkDecoder(index: c_int) -> *c_schar; + pub fn Mix_GetChunkDecoder(index: c_int) -> *c_char; pub fn Mix_GetNumMusicDecoders() -> c_int; - pub fn Mix_GetMusicDecoder(index: c_int) -> *c_schar; + pub fn Mix_GetMusicDecoder(index: c_int) -> *c_char; pub fn Mix_GetMusicType(music: *Mix_Music) -> Mix_MusicType; pub fn Mix_SetPostMix(mix_func: ::std::option::Option c_int; pub fn Mix_Playing(channel: c_int) -> c_int; pub fn Mix_PlayingMusic() -> c_int; - pub fn Mix_SetMusicCMD(command: *c_schar) -> c_int; + pub fn Mix_SetMusicCMD(command: *c_char) -> c_int; pub fn Mix_SetSynchroValue(value: c_int) -> c_int; pub fn Mix_GetSynchroValue() -> c_int; - pub fn Mix_SetSoundFonts(paths: *c_schar) -> c_int; - pub fn Mix_GetSoundFonts() -> *c_schar; + pub fn Mix_SetSoundFonts(paths: *c_char) -> c_int; + pub fn Mix_GetSoundFonts() -> *c_char; pub fn Mix_EachSoundFont(function: ::std::option::Option c_int>, data: *c_void) -> c_int; From 6281272b5d52a2a43b04f46eec2e0c649585f507 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 21 Apr 2014 15:02:18 +0800 Subject: [PATCH 013/114] fix eq in InitFlag, use outer comment --- src/sdl2_mixer/lib.rs | 202 ++++++++++++++++++++++++------------------ 1 file changed, 117 insertions(+), 85 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index fa024a30..597254ca 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -18,7 +18,8 @@ use std::ptr; use std::cast; use std::raw; use std::c_str::CString; -use std::ops::BitOr; +use std::ops; +use std::num; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; @@ -102,14 +103,15 @@ pub static DEFAULT_FREQUENCY : int = 22050; /// Maximum value for any volume setting. pub static MAX_VOLUME : int = 128; - +/// Returns the version of the dynamically linked SDL_mixer library pub fn get_linked_version() -> Version { - //! Returns the version of the dynamically linked SDL_mixer library + unsafe { Version::from_ll(ffi::Mix_Linked_Version()) } } +#[allow(visible_private_types)] #[deriving(Clone, Eq, Hash, Show)] pub enum InitFlag { InitFlac, @@ -118,25 +120,33 @@ pub enum InitFlag { InitMp3, InitOgg, InitFluidSynth, - InitRaw(int), + InitRaw(ffi::MIX_InitFlags), } impl InitFlag { #[inline] - pub fn to_ll(self) -> c_int { + fn to_ll(self) -> ffi::MIX_InitFlags { match self { - InitFlac => ffi::MIX_INIT_FLAC as c_int, - InitMod => ffi::MIX_INIT_MOD as c_int, - InitModPlug => ffi::MIX_INIT_MODPLUG as c_int, - InitMp3 => ffi::MIX_INIT_MP3 as c_int, - InitOgg => ffi::MIX_INIT_OGG as c_int, - InitFluidSynth => ffi::MIX_INIT_FLUIDSYNTH as c_int, - InitRaw(n) => n as c_int, + InitFlac => ffi::MIX_INIT_FLAC, + InitMod => ffi::MIX_INIT_MOD, + InitModPlug => ffi::MIX_INIT_MODPLUG, + InitMp3 => ffi::MIX_INIT_MP3, + InitOgg => ffi::MIX_INIT_OGG, + InitFluidSynth => ffi::MIX_INIT_FLUIDSYNTH, + InitRaw(val) => val, } } - pub fn from_ll(n: c_int) -> InitFlag { - InitRaw(n as int) + fn from_ll(cflag: ffi::MIX_InitFlags) -> InitFlag { + match cflag { + ffi::MIX_INIT_FLAC => InitFlac, + ffi::MIX_INIT_MOD => InitMod, + ffi::MIX_INIT_MODPLUG => InitModPlug, + ffi::MIX_INIT_MP3 => InitMp3, + ffi::MIX_INIT_OGG => InitOgg, + ffi::MIX_INIT_FLUIDSYNTH => InitFluidSynth, + val => InitRaw(val), + } } pub fn mask(self, flag: InitFlag) -> bool { @@ -144,45 +154,67 @@ impl InitFlag { } } -impl BitOr for InitFlag { +impl ops::BitOr for InitFlag { fn bitor(&self, rhs: &InitFlag) -> InitFlag { - InitRaw(self.to_ll() as int | rhs.to_ll() as int) + InitFlag::from_ll(self.to_ll() | rhs.to_ll()) } } impl default::Default for InitFlag { fn default() -> InitFlag { InitFlag::from_ll((ffi::MIX_INIT_FLAC | ffi::MIX_INIT_MOD | ffi::MIX_INIT_MODPLUG | - ffi::MIX_INIT_MP3 | ffi::MIX_INIT_OGG | ffi::MIX_INIT_FLUIDSYNTH) as c_int) + ffi::MIX_INIT_MP3 | ffi::MIX_INIT_OGG | ffi::MIX_INIT_FLUIDSYNTH)) } } -pub fn init(flags: InitFlag) -> InitFlag { - //! Loads dynamic libraries and prepares them for use. Flags should be - //! one or more flags from InitFlag. - let ret = unsafe { ffi::Mix_Init(flags.to_ll()) }; - InitFlag::from_ll(ret) +impl num::FromPrimitive for InitFlag { + #[inline] + fn from_i64(n: i64) -> Option { + Some(InitFlag::from_ll(n as ffi::MIX_InitFlags)) + } + #[inline] + fn from_u64(n: u64) -> Option { + Some(InitFlag::from_ll(n as ffi::MIX_InitFlags)) + } } +impl num::ToPrimitive for InitFlag { + #[inline] + fn to_i64(&self) -> Option { + Some(self.to_ll() as i64) + } + #[inline] + fn to_u64(&self) -> Option { + Some(self.to_ll() as u64) + } +} + +/// Loads dynamic libraries and prepares them for use. Flags should be +/// one or more flags from InitFlag. +pub fn init(flags: InitFlag) -> InitFlag { + let ret = unsafe { ffi::Mix_Init(flags.to_ll() as c_int) }; + InitFlag::from_ll(ret as ffi::MIX_InitFlags) +} + +/// Cleans up all dynamically loaded library handles, freeing memory. pub fn quit() { - //! Cleans up all dynamically loaded library handles, freeing memory. unsafe { ffi::Mix_Quit(); } } +/// Open the mixer with a certain audio format. pub fn open_audio(frequency: int, format: AudioFormat, channels: int, chunksize: int) -> Result<(), ~str> { - //! Open the mixer with a certain audio format. let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; if ret == 0 { Ok(()) } else { Err(get_error()) } } +/// Shutdown and cleanup the mixer API. pub fn close_audio() { - //! Shutdown and cleanup the mixer API. unsafe { ffi::Mix_CloseAudio() } } +/// Get the actual audio format in use by the opened audio device. pub fn query_spec() -> Result<(int, AudioFormat, int), ~str> { - //! Get the actual audio format in use by the opened audio device. let frequency : c_int = 0; let format : uint16_t = 0; let channels : c_int = 0; @@ -196,13 +228,13 @@ pub fn query_spec() -> Result<(int, AudioFormat, int), ~str> { // 4.2 Samples +/// Get the number of sample chunk decoders available from the Mix_GetChunkDecoder function. pub fn get_chunk_decoders_number() -> int { - //! Get the number of sample chunk decoders available from the Mix_GetChunkDecoder function. unsafe { ffi::Mix_GetNumChunkDecoders() as int } } +/// Get the name of the indexed sample chunk decoder. pub fn get_chunk_decoder(index: int) -> ~str { - //! Get the name of the indexed sample chunk decoder. unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); CString::new(name, false).as_str().unwrap().into_owned() @@ -225,8 +257,8 @@ impl Drop for Chunk { } impl Chunk { + /// Load file for use as a sample. pub fn from_file(path: &Path) -> Result<~Chunk, ~str> { - //! Load file for use as a sample. let raw = unsafe { ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "wb")).raw, 0) }; @@ -237,15 +269,15 @@ impl Chunk { } } + /// Set chunk->volume to volume. pub fn set_volume(&mut self, volume: int) -> int { - //! Set chunk->volume to volume. unsafe { ffi::Mix_VolumeChunk(self.raw, volume as c_int) as int } } + /// current volume for the chunk. pub fn get_volume(&self) -> int { - //! current volume for the chunk. unsafe { ffi::Mix_VolumeChunk(self.raw, -1) as int } @@ -259,8 +291,8 @@ pub trait LoaderRWops { } impl LoaderRWops for RWops { + /// Load src for use as a sample. fn load_wav(&self) -> Result<~Chunk, ~str> { - //! Load src for use as a sample. let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw, 0) }; @@ -275,6 +307,7 @@ impl LoaderRWops for RWops { // 4.3 Channels +/// Fader effect type enumerations #[repr(C)] #[deriving(Clone, Eq, Hash, Show, FromPrimitive)] pub enum Fading { @@ -287,8 +320,8 @@ pub enum Fading { #[deriving(Eq, Show)] pub struct Channel(int); +/// Set the number of channels being mixed. pub fn allocate_channels(numchans: int) -> int { - //! Set the number of channels being mixed. unsafe { ffi::Mix_AllocateChannels(numchans as c_int) as int } @@ -308,8 +341,8 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { } } +/// When channel playback is halted, then the specified channel_finished function is called. pub fn set_channel_finished(f: |Channel|:'static) { - //! When channel playback is halted, then the specified channel_finished function is called. unsafe { channel_finished_callback = Some(cast::transmute::<_, raw::Closure>(f)); ffi::Mix_ChannelFinished(Some(c_channel_finished_callback)); @@ -324,19 +357,18 @@ pub fn unset_channel_finished() { } impl Channel { + /// Represent for all channels (-1) pub fn all() -> Channel { - //! Represent for all channels (-1) Channel(-1) } + /// This is the MIX_CHANNEL_POST (-2) pub fn post() -> Channel { - //! This is the MIX_CHANNEL_POST (-2) Channel(-2) } + /// Set the volume for any allocated channel. pub fn set_volume(self, volume: int) -> int { - //! Set the volume for any allocated channel. - //! If channel is -1 then all channels at are set at once. let Channel(ch) = self; unsafe { ffi::Mix_Volume(ch as c_int, volume as c_int) as int @@ -350,8 +382,8 @@ impl Channel { } } + /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. pub fn play(self, chunk: Chunk, loops: int) -> Result { - //! Play chunk on channel, or if channel is -1, pick the first free unreserved channel. self.play_timed(chunk, loops, -1) } @@ -367,8 +399,8 @@ impl Channel { } } + /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. pub fn fade_in(self, chunk: Chunk, loops: int, ms: int) -> Result { - //! Play chunk on channel, or if channel is -1, pick the first free unreserved channel. self.fade_in_timed(chunk, loops, ms, -1) } @@ -384,57 +416,57 @@ impl Channel { } } + /// Pause channel, or all playing channels if -1 is passed in. pub fn pause(self) { - //! Pause channel, or all playing channels if -1 is passed in. let Channel(ch) = self; unsafe { ffi::Mix_Pause(ch as c_int); } } + /// Unpause channel, or all playing and paused channels if -1 is passed in. pub fn resume(self) { - //! Unpause channel, or all playing and paused channels if -1 is passed in. let Channel(ch) = self; unsafe { ffi::Mix_Resume(ch as c_int); } } + /// Halt channel playback pub fn halt(self) { - //! Halt channel playback let Channel(ch) = self; unsafe { ffi::Mix_HaltChannel(ch as c_int); } } + /// Halt channel playback, after ticks milliseconds. pub fn expire(self, ticks: int) -> int { - //! Halt channel playback, after ticks milliseconds. let Channel(ch) = self; unsafe { ffi::Mix_ExpireChannel(ch as c_int, ticks as c_int) as int } } + /// Gradually fade out which channel over ms milliseconds starting from now. pub fn fade_out(self, ms: int) -> int { - //! Gradually fade out which channel over ms milliseconds starting from now. let Channel(ch) = self; unsafe { ffi::Mix_FadeOutChannel(ch as c_int, ms as c_int) as int } } + /// if channel is playing, or not. pub fn is_playing(self) -> bool { - //! if channel is playing, or not. let Channel(ch) = self; unsafe { ffi::Mix_Playing(ch as c_int) != 0 } } + /// if channel is paused, or not. pub fn is_paused(self) -> bool { - //! if channel is paused, or not. let Channel(ch) = self; unsafe { ffi::Mix_Paused(ch as c_int) != 0 } } + /// if channel is fading in, out, or not pub fn get_fading(self) -> Fading { - //! if channel is fading in, out, or not let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as int }; FromPrimitive::from_int(ret).unwrap() } + /// Get the most recent sample chunk pointer played on channel. pub fn get_chunk(self) -> Option { - //! Get the most recent sample chunk pointer played on channel. let Channel(ch) = self; let raw = unsafe { ffi::Mix_GetChunk(ch as c_int) }; if raw.is_null() { @@ -444,8 +476,8 @@ impl Channel { } } + /// This removes all effects registered to channel. pub fn unregister_all_effects(self) -> Result<(), ~str> { - //! This removes all effects registered to channel. let Channel(ch) = self; let ret = unsafe { ffi::Mix_UnregisterAllEffects(ch as c_int) }; if ret == 0 { @@ -465,8 +497,8 @@ impl Channel { } } + /// This effect simulates a simple attenuation of volume due to distance. pub fn set_distance(self, distance: u8) -> Result<(), ~str> { - //! This effect simulates a simple attenuation of volume due to distance. let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, distance) }; if ret == 0 { @@ -476,8 +508,8 @@ impl Channel { } } + /// This effect emulates a simple 3D audio effect. pub fn set_position(self, angle: i16, distance: u8) -> Result<(), ~str> { - //! This effect emulates a simple 3D audio effect. let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, angle, distance) }; if ret == 0 { @@ -487,8 +519,8 @@ impl Channel { } } + /// Simple reverse stereo, swaps left and right channel sound. pub fn set_reverse_stereo(self, flip: bool) -> Result<(), ~str> { - //! Simple reverse stereo, swaps left and right channel sound. let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetReverseStereo(ch as c_int, flip as c_int) }; if ret == 0 { @@ -509,9 +541,9 @@ pub fn get_paused_channels_number() -> int { // 4.4 Groups +/// Reserve num channels from being used when playing samples when +/// passing in -1 as a channel number to playback functions. pub fn reserve_channels(num: int) -> int { - //! Reserve num channels from being used when playing samples when - //! passing in -1 as a channel number to playback functions. unsafe { ffi::Mix_ReserveChannels(num as c_int) as int } } @@ -525,27 +557,27 @@ impl default::Default for Group { } impl Group { + /// Add channels starting at from up through to to group tag, + /// or reset it's group to the default group tag (-1). pub fn add_channels_range(self, from: int, to: int) -> int { - //! Add channels starting at from up through to to group tag, - //! or reset it's group to the default group tag (-1). let Group(g) = self; unsafe { ffi::Mix_GroupChannels(from as c_int, to as c_int, g as c_int) as int } } + /// Add which channel to group tag, or reset it's group to the default group tag pub fn add_channel(self, Channel(ch): Channel) -> bool { - //! Add which channel to group tag, or reset it's group to the default group tag let Group(g) = self; unsafe { ffi::Mix_GroupChannel(ch as c_int, g as c_int) == 1 } } + /// Count the number of channels in group pub fn count(self) -> int { - //! Count the number of channels in group let Group(g) = self; unsafe { ffi::Mix_GroupCount(g as c_int) as int } } + /// Find the first available (not playing) channel in group pub fn find_available(self) -> Option { - //! Find the first available (not playing) channel in group let Group(g) = self; let ret = unsafe { ffi::Mix_GroupAvailable(g as c_int) as int }; if ret == -1 { @@ -555,8 +587,8 @@ impl Group { } } + /// Find the oldest actively playing channel in group pub fn find_oldest(self) -> Option { - //! Find the oldest actively playing channel in group let Group(g) = self; let ret = unsafe { ffi::Mix_GroupOldest(g as c_int) as int }; if ret == -1 { @@ -566,8 +598,8 @@ impl Group { } } + /// Find the newest, most recently started, actively playing channel in group. pub fn find_newest(self) -> Option { - //! Find the newest, most recently started, actively playing channel in group. let Group(g) = self; let ret = unsafe { ffi::Mix_GroupNewer(g as c_int) as int }; if ret == -1 { @@ -577,15 +609,15 @@ impl Group { } } + /// Gradually fade out channels in group over some milliseconds starting from now. + /// Returns the number of channels set to fade out. pub fn fade_out(self, ms: int) -> int { - //! Gradually fade out channels in group over some milliseconds starting from now. - //! Returns the number of channels set to fade out. let Group(g) = self; unsafe { ffi::Mix_FadeOutGroup(g as c_int, ms as c_int) as int } } + /// Halt playback on all channels in group. pub fn halt(self) { - //! Halt playback on all channels in group. let Group(g) = self; unsafe { ffi::Mix_HaltGroup(g as c_int); } } @@ -593,20 +625,20 @@ impl Group { // 4.5 Music +/// Get the number of music decoders available. pub fn get_music_decoders_number() -> int { - //! Get the number of music decoders available. unsafe { ffi::Mix_GetNumMusicDecoders() as int } } +/// Get the name of the indexed music decoder. pub fn get_music_decoder(index: int) -> ~str { - //! Get the name of the indexed music decoder. unsafe { let name = ffi::Mix_GetMusicDecoder(index as c_int); CString::new(name, false).as_str().unwrap().into_owned() } } - +/// Music type enumerations #[repr(C)] #[deriving(Clone, Eq, Hash, Show, FromPrimitive)] pub enum MusicType { @@ -651,8 +683,8 @@ impl Drop for Music { } impl Music { + /// Load music file to use. pub fn from_file(path: &Path) -> Result<~Music, ~str> { - //! Load music file to use. let raw = unsafe { ffi::Mix_LoadMUS(path.to_c_str().unwrap()) }; @@ -663,14 +695,14 @@ impl Music { } } + /// The file format encoding of the music. pub fn get_type(&self) -> MusicType { - //! The file format encoding of the music. let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as int }; FromPrimitive::from_int(ret).unwrap() } + /// Play the loaded music loop times through from start to finish. pub fn play(&self, loops: int) -> Result<(), ~str> { - //! Play the loaded music loop times through from start to finish. let ret = unsafe { ffi::Mix_PlayMusic(self.raw, loops as c_int) }; @@ -681,9 +713,9 @@ impl Music { } } + /// Fade in over ms milliseconds of time, the loaded music, + /// playing it loop times through from start to finish. pub fn fade_in(&self, loops: int, ms: int) -> Result<(), ~str> { - //! Fade in over ms milliseconds of time, the loaded music, - //! playing it loop times through from start to finish. let ret = unsafe { ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) }; @@ -694,8 +726,8 @@ impl Music { } } + /// Fade in over ms milliseconds of time, from position. pub fn fade_in_from_pos(&self, loops: int, ms: int, position: f64) -> Result<(), ~str> { - //! Fade in over ms milliseconds of time, from position. let ret = unsafe { ffi::Mix_FadeInMusicPos(self.raw, loops as c_int, ms as c_int, position as c_double) }; @@ -711,28 +743,28 @@ impl Music { unsafe { ffi::Mix_VolumeMusic(-1) as int } } + /// Set the volume. pub fn set_volume(volume: int) -> int { - //! Set the volume. unsafe { ffi::Mix_VolumeMusic(volume as c_int) as int } } + /// Pause the music playback. pub fn pause() { - //! Pause the music playback. unsafe { ffi::Mix_PauseMusic(); } } + /// Unpause the music. pub fn resume() { - //! Unpause the music. unsafe { ffi::Mix_ResumeMusic(); } } + /// Rewind the music to the start. pub fn rewind() { - //! Rewind the music to the start. unsafe { ffi::Mix_RewindMusic(); } } + /// Set the position of the currently playing music. pub fn set_pos(position: f64) -> Result<(), ~str> { - //! Set the position of the currently playing music. let ret = unsafe { ffi::Mix_SetMusicPosition(position as c_double) }; @@ -743,8 +775,8 @@ impl Music { } } + /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> Result<(), ~str> { - //! Setup a command line music player to use to play music. let ret = unsafe { ffi::Mix_SetMusicCMD(command.to_c_str().unwrap()) }; @@ -755,13 +787,13 @@ impl Music { } } + /// Halt playback of music. pub fn halt() { - //! Halt playback of music. unsafe { ffi::Mix_HaltMusic(); } } + /// Gradually fade out the music over ms milliseconds starting from now. pub fn fade_out(ms: int) -> Result<(), ~str> { - //! Gradually fade out the music over ms milliseconds starting from now. let ret = unsafe { ffi::Mix_FadeOutMusic(ms as c_int) }; @@ -790,18 +822,18 @@ impl Music { } } + /// If music is actively playing, or not. pub fn is_playing() -> bool { - //! If music is actively playing, or not. unsafe { ffi::Mix_PlayingMusic() == 1 } } + /// If music is paused, or not. pub fn is_paused() -> bool { - //! If music is paused, or not. unsafe { ffi::Mix_PausedMusic() == 1 } } + /// If music is fading, or not. pub fn get_fading() -> Fading { - //! If music is fading, or not. let ret = unsafe { ffi::Mix_FadingMusic() as int }; FromPrimitive::from_int(ret).unwrap() } From a7ec06065741d78e95d6563b454ad2e4ebf12bb2 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 26 Apr 2014 22:41:52 +0800 Subject: [PATCH 014/114] use new style flag implementation --- src/demo/demo.rs | 8 +-- src/sdl2_mixer/flag.rs | 109 +++++++++++++++++++++++++++++++++++++++++ src/sdl2_mixer/lib.rs | 94 +++++------------------------------ 3 files changed, 125 insertions(+), 86 deletions(-) create mode 100644 src/sdl2_mixer/flag.rs diff --git a/src/demo/demo.rs b/src/demo/demo.rs index 46aebb15..3c226b51 100644 --- a/src/demo/demo.rs +++ b/src/demo/demo.rs @@ -5,7 +5,6 @@ extern crate sdl2; extern crate sdl2_mixer; use std::os; -use std::default::Default; use mix = sdl2_mixer; @@ -22,9 +21,10 @@ fn main() { fn dump_info(filename: &Path) -> Result<(), ~str> { println!("linked version: {}", mix::get_linked_version()); - sdl2::init([sdl2::InitAudio, sdl2::InitTimer]); - println!("inited => {}", mix::init(Default::default())); - + sdl2::init(sdl2::InitAudio | sdl2::InitTimer); + println!("inited => {}", mix::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | + sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | + sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).get()); // TODO: 0x8010 is SDL_audio flag try!(mix::open_audio(mix::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); mix::allocate_channels(0); diff --git a/src/sdl2_mixer/flag.rs b/src/sdl2_mixer/flag.rs new file mode 100644 index 00000000..4b573574 --- /dev/null +++ b/src/sdl2_mixer/flag.rs @@ -0,0 +1,109 @@ +// Loosely based on zlib-licensed code from RustAllegro +// https://github.com/SiegeLord/RustAllegro +// +// Implements efficient, type-safe flags with support for bitwise operators. +// +// Usage: +// +// flag_type!(FlagTypeName { +// FlagName1 = FlagValue1, +// FlagName2 = FlagValue2, +// ... +// FlagNameN = FlagValueN +// }) +// +// fn foo(flag: FlagTypeName) { +// let raw = (flag | FlagNameN).get(); +// bar(raw); +// } + +#![macro_escape] + +macro_rules! flag_type( + ($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => { + pub struct $typename { + bits: $supertype + } + + impl $typename { + #[inline] + pub fn new(bits: $supertype) -> $typename { + $typename { bits: bits } + } + + #[inline] + pub fn get(self) -> $supertype { + self.bits + } + } + + impl ::std::default::Default for $typename { + fn default() -> $typename { + $typename::new(0) + } + } + + impl ::std::cmp::Eq for $typename { + fn eq(&self, other: &$typename) -> bool { + self.bits == other.bits + } + } + + impl ::std::cmp::TotalEq for $typename {} + + impl ::std::cmp::Ord for $typename { + fn lt(&self, other: &$typename) -> bool { + self.bits < other.bits + } + } + + impl ::std::cmp::TotalOrd for $typename { + fn cmp(&self, other: &$typename) -> Ordering { + self.bits.cmp(&other.bits) + } + } + + impl ::std::ops::Not<$typename> for $typename { + fn not(&self) -> $typename { + $typename { bits: !self.bits } + } + } + + impl ::std::ops::BitAnd<$typename, $typename> for $typename { + fn bitand(&self, rhs: &$typename) -> $typename { + $typename { bits: self.bits & rhs.bits } + } + } + + impl ::std::ops::BitOr<$typename, $typename> for $typename { + fn bitor(&self, rhs: &$typename) -> $typename { + $typename { bits: self.bits | rhs.bits } + } + } + + impl ::std::ops::BitXor<$typename, $typename> for $typename { + fn bitxor(&self, rhs: &$typename) -> $typename { + $typename { bits: self.bits ^ rhs.bits } + } + } + + impl ::std::ops::Shl<$supertype, $typename> for $typename { + fn shl(&self, rhs: &$supertype) -> $typename { + $typename { bits: self.bits << *rhs } + } + } + + impl ::std::ops::Shr<$supertype, $typename> for $typename { + fn shr(&self, rhs: &$supertype) -> $typename { + $typename { bits: self.bits >> *rhs } + } + } + + $( + pub static $name: $typename = $typename { bits: $value as $supertype }; + )+ + }; + ($typename:ident { $($name:ident = $value:expr),* }) => { + flag_type!($typename : u32 { $($name = $value),* }) + } +) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 597254ca..a30872ae 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -8,7 +8,7 @@ A binding for SDL2_mixer. #![comment = "SDL2_mixer bindings and wrappers"] #![license = "MIT"] -#![feature(globs)] +#![feature(globs, macro_rules)] extern crate libc; extern crate sdl2; @@ -18,8 +18,6 @@ use std::ptr; use std::cast; use std::raw; use std::c_str::CString; -use std::ops; -use std::num; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; @@ -47,6 +45,7 @@ mod others { #[allow(non_camel_case_types, dead_code)] mod ffi; +mod flag; // This comes from SDL_audio.h #[allow(non_camel_case_types)] @@ -111,89 +110,20 @@ pub fn get_linked_version() -> Version { } } -#[allow(visible_private_types)] -#[deriving(Clone, Eq, Hash, Show)] -pub enum InitFlag { - InitFlac, - InitMod, - InitModPlug, - InitMp3, - InitOgg, - InitFluidSynth, - InitRaw(ffi::MIX_InitFlags), -} - -impl InitFlag { - #[inline] - fn to_ll(self) -> ffi::MIX_InitFlags { - match self { - InitFlac => ffi::MIX_INIT_FLAC, - InitMod => ffi::MIX_INIT_MOD, - InitModPlug => ffi::MIX_INIT_MODPLUG, - InitMp3 => ffi::MIX_INIT_MP3, - InitOgg => ffi::MIX_INIT_OGG, - InitFluidSynth => ffi::MIX_INIT_FLUIDSYNTH, - InitRaw(val) => val, - } - } - - fn from_ll(cflag: ffi::MIX_InitFlags) -> InitFlag { - match cflag { - ffi::MIX_INIT_FLAC => InitFlac, - ffi::MIX_INIT_MOD => InitMod, - ffi::MIX_INIT_MODPLUG => InitModPlug, - ffi::MIX_INIT_MP3 => InitMp3, - ffi::MIX_INIT_OGG => InitOgg, - ffi::MIX_INIT_FLUIDSYNTH => InitFluidSynth, - val => InitRaw(val), - } - } - - pub fn mask(self, flag: InitFlag) -> bool { - self.to_ll() & flag.to_ll() == flag.to_ll() - } -} - -impl ops::BitOr for InitFlag { - fn bitor(&self, rhs: &InitFlag) -> InitFlag { - InitFlag::from_ll(self.to_ll() | rhs.to_ll()) - } -} - -impl default::Default for InitFlag { - fn default() -> InitFlag { - InitFlag::from_ll((ffi::MIX_INIT_FLAC | ffi::MIX_INIT_MOD | ffi::MIX_INIT_MODPLUG | - ffi::MIX_INIT_MP3 | ffi::MIX_INIT_OGG | ffi::MIX_INIT_FLUIDSYNTH)) - } -} - -impl num::FromPrimitive for InitFlag { - #[inline] - fn from_i64(n: i64) -> Option { - Some(InitFlag::from_ll(n as ffi::MIX_InitFlags)) - } - #[inline] - fn from_u64(n: u64) -> Option { - Some(InitFlag::from_ll(n as ffi::MIX_InitFlags)) - } -} - -impl num::ToPrimitive for InitFlag { - #[inline] - fn to_i64(&self) -> Option { - Some(self.to_ll() as i64) - } - #[inline] - fn to_u64(&self) -> Option { - Some(self.to_ll() as u64) - } -} +flag_type!(InitFlag : c_int { + InitFlac = ffi::MIX_INIT_FLAC, + InitMod = ffi::MIX_INIT_MOD, + InitModPlug = ffi::MIX_INIT_MODPLUG, + InitMp3 = ffi::MIX_INIT_MP3, + InitOgg = ffi::MIX_INIT_OGG, + InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH +}) /// Loads dynamic libraries and prepares them for use. Flags should be /// one or more flags from InitFlag. pub fn init(flags: InitFlag) -> InitFlag { - let ret = unsafe { ffi::Mix_Init(flags.to_ll() as c_int) }; - InitFlag::from_ll(ret as ffi::MIX_InitFlags) + let ret = unsafe { ffi::Mix_Init(flags.get()) }; + InitFlag::new(ret) } /// Cleans up all dynamically loaded library handles, freeing memory. From 9d9cfa0c72ec50e51b124a4c698a34f18beece91 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 5 May 2014 21:33:44 +0800 Subject: [PATCH 015/114] use unboxed return type --- src/sdl2_mixer/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index a30872ae..8bbf727e 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -188,14 +188,14 @@ impl Drop for Chunk { impl Chunk { /// Load file for use as a sample. - pub fn from_file(path: &Path) -> Result<~Chunk, ~str> { + pub fn from_file(path: &Path) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "wb")).raw, 0) }; if raw.is_null() { Err(get_error()) } else { - Ok(~Chunk{ raw: raw, owned: true }) + Ok(Chunk{ raw: raw, owned: true }) } } @@ -217,19 +217,19 @@ impl Chunk { /// Loader trait for RWops pub trait LoaderRWops { /// Load src for use as a sample. - fn load_wav(&self) -> Result<~Chunk, ~str>; + fn load_wav(&self) -> Result; } impl LoaderRWops for RWops { /// Load src for use as a sample. - fn load_wav(&self) -> Result<~Chunk, ~str> { + fn load_wav(&self) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw, 0) }; if raw == ptr::null() { Err(get_error()) } else { - Ok(~Chunk{ raw: raw, owned: true }) + Ok(Chunk{ raw: raw, owned: true }) } } } @@ -614,14 +614,14 @@ impl Drop for Music { impl Music { /// Load music file to use. - pub fn from_file(path: &Path) -> Result<~Music, ~str> { + pub fn from_file(path: &Path) -> Result { let raw = unsafe { ffi::Mix_LoadMUS(path.to_c_str().unwrap()) }; if raw.is_null() { Err(get_error()) } else { - Ok(~Music{ raw: raw, owned: true }) + Ok(Music{ raw: raw, owned: true }) } } From 1844eaed0fa91953b307f1248b30d78cca61b30a Mon Sep 17 00:00:00 2001 From: Andelf Date: Sun, 11 May 2014 23:47:51 +0800 Subject: [PATCH 016/114] fix compile error --- src/sdl2_mixer/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 8bbf727e..c90e13bf 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -15,7 +15,7 @@ extern crate sdl2; use std::default; use std::ptr; -use std::cast; +use std::mem; use std::raw; use std::c_str::CString; use libc::{c_int, uint16_t, c_double}; @@ -264,7 +264,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { match channel_finished_callback { None => (), Some(cb) => { - let cb = cast::transmute::<_, |Channel|>(cb); + let cb = mem::transmute::<_, |Channel|>(cb); cb(Channel(ch as int)) } } @@ -274,7 +274,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { /// When channel playback is halted, then the specified channel_finished function is called. pub fn set_channel_finished(f: |Channel|:'static) { unsafe { - channel_finished_callback = Some(cast::transmute::<_, raw::Closure>(f)); + channel_finished_callback = Some(mem::transmute::<_, raw::Closure>(f)); ffi::Mix_ChannelFinished(Some(c_channel_finished_callback)); } } @@ -591,7 +591,7 @@ extern "C" fn c_music_finished_hook() { unsafe { match music_finished_hook { None => (), Some(f) => { - let f = cast::transmute::<_, ||>(f); + let f = mem::transmute::<_, ||>(f); f() } } } @@ -739,7 +739,7 @@ impl Music { pub fn hook_finished(f: ||) { unsafe { - music_finished_hook = Some(cast::transmute(f)); + music_finished_hook = Some(mem::transmute(f)); ffi::Mix_HookMusicFinished(Some(c_music_finished_hook)); } } From 7d769795c418519a99f71c1e368d86bd20723d76 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 12 May 2014 10:46:33 +0800 Subject: [PATCH 017/114] Chunk now pass borrowed; fix typo in Chunk. fix #1 --- src/sdl2_mixer/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index c90e13bf..501626ef 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -150,9 +150,9 @@ pub fn query_spec() -> Result<(int, AudioFormat, int), ~str> { let channels : c_int = 0; let ret = unsafe { ffi::Mix_QuerySpec(&frequency, &format, &channels) }; if ret == 0 { - Ok((frequency as int, format as AudioFormat, channels as int)) - } else { Err(get_error()) + } else { + Ok((frequency as int, format as AudioFormat, channels as int)) } } @@ -190,7 +190,7 @@ impl Chunk { /// Load file for use as a sample. pub fn from_file(path: &Path) -> Result { let raw = unsafe { - ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "wb")).raw, 0) + ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw, 0) }; if raw.is_null() { Err(get_error()) @@ -313,11 +313,11 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn play(self, chunk: Chunk, loops: int) -> Result { + pub fn play(self, chunk: &Chunk, loops: int) -> Result { self.play_timed(chunk, loops, -1) } - pub fn play_timed(self, chunk: Chunk, loops: int, ticks: int) -> Result { + pub fn play_timed(self, chunk: &Chunk, loops: int, ticks: int) -> Result { let Channel(ch) = self; let ret = unsafe { ffi::Mix_PlayChannelTimed(ch as c_int, chunk.raw, loops as c_int, ticks as c_int) @@ -330,11 +330,11 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn fade_in(self, chunk: Chunk, loops: int, ms: int) -> Result { + pub fn fade_in(self, chunk: &Chunk, loops: int, ms: int) -> Result { self.fade_in_timed(chunk, loops, ms, -1) } - pub fn fade_in_timed(self, chunk: Chunk, loops: int, ms: int, ticks: int) -> Result { + pub fn fade_in_timed(self, chunk: &Chunk, loops: int, ms: int, ticks: int) -> Result { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadeInChannelTimed(ch as c_int, chunk.raw, loops as c_int, ms as c_int, ticks as c_int) From f6655e88d769ea7cbe95d59eeef3c837924e3d43 Mon Sep 17 00:00:00 2001 From: Andelf Date: Thu, 15 May 2014 23:11:29 +0800 Subject: [PATCH 018/114] switch to use builtin bitflags! --- src/sdl2_mixer/flag.rs | 109 ----------------------------------------- src/sdl2_mixer/lib.rs | 21 ++++---- 2 files changed, 11 insertions(+), 119 deletions(-) delete mode 100644 src/sdl2_mixer/flag.rs diff --git a/src/sdl2_mixer/flag.rs b/src/sdl2_mixer/flag.rs deleted file mode 100644 index 4b573574..00000000 --- a/src/sdl2_mixer/flag.rs +++ /dev/null @@ -1,109 +0,0 @@ -// Loosely based on zlib-licensed code from RustAllegro -// https://github.com/SiegeLord/RustAllegro -// -// Implements efficient, type-safe flags with support for bitwise operators. -// -// Usage: -// -// flag_type!(FlagTypeName { -// FlagName1 = FlagValue1, -// FlagName2 = FlagValue2, -// ... -// FlagNameN = FlagValueN -// }) -// -// fn foo(flag: FlagTypeName) { -// let raw = (flag | FlagNameN).get(); -// bar(raw); -// } - -#![macro_escape] - -macro_rules! flag_type( - ($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => { - pub struct $typename { - bits: $supertype - } - - impl $typename { - #[inline] - pub fn new(bits: $supertype) -> $typename { - $typename { bits: bits } - } - - #[inline] - pub fn get(self) -> $supertype { - self.bits - } - } - - impl ::std::default::Default for $typename { - fn default() -> $typename { - $typename::new(0) - } - } - - impl ::std::cmp::Eq for $typename { - fn eq(&self, other: &$typename) -> bool { - self.bits == other.bits - } - } - - impl ::std::cmp::TotalEq for $typename {} - - impl ::std::cmp::Ord for $typename { - fn lt(&self, other: &$typename) -> bool { - self.bits < other.bits - } - } - - impl ::std::cmp::TotalOrd for $typename { - fn cmp(&self, other: &$typename) -> Ordering { - self.bits.cmp(&other.bits) - } - } - - impl ::std::ops::Not<$typename> for $typename { - fn not(&self) -> $typename { - $typename { bits: !self.bits } - } - } - - impl ::std::ops::BitAnd<$typename, $typename> for $typename { - fn bitand(&self, rhs: &$typename) -> $typename { - $typename { bits: self.bits & rhs.bits } - } - } - - impl ::std::ops::BitOr<$typename, $typename> for $typename { - fn bitor(&self, rhs: &$typename) -> $typename { - $typename { bits: self.bits | rhs.bits } - } - } - - impl ::std::ops::BitXor<$typename, $typename> for $typename { - fn bitxor(&self, rhs: &$typename) -> $typename { - $typename { bits: self.bits ^ rhs.bits } - } - } - - impl ::std::ops::Shl<$supertype, $typename> for $typename { - fn shl(&self, rhs: &$supertype) -> $typename { - $typename { bits: self.bits << *rhs } - } - } - - impl ::std::ops::Shr<$supertype, $typename> for $typename { - fn shr(&self, rhs: &$supertype) -> $typename { - $typename { bits: self.bits >> *rhs } - } - } - - $( - pub static $name: $typename = $typename { bits: $value as $supertype }; - )+ - }; - ($typename:ident { $($name:ident = $value:expr),* }) => { - flag_type!($typename : u32 { $($name = $value),* }) - } -) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 501626ef..9e173048 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -45,7 +45,6 @@ mod others { #[allow(non_camel_case_types, dead_code)] mod ffi; -mod flag; // This comes from SDL_audio.h #[allow(non_camel_case_types)] @@ -110,20 +109,22 @@ pub fn get_linked_version() -> Version { } } -flag_type!(InitFlag : c_int { - InitFlac = ffi::MIX_INIT_FLAC, - InitMod = ffi::MIX_INIT_MOD, - InitModPlug = ffi::MIX_INIT_MODPLUG, - InitMp3 = ffi::MIX_INIT_MP3, - InitOgg = ffi::MIX_INIT_OGG, - InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH +bitflags!(flags InitFlag : u32 { + static InitFlac = ffi::MIX_INIT_FLAC as u32, + static InitMod = ffi::MIX_INIT_MOD as u32, + static InitModPlug = ffi::MIX_INIT_MODPLUG as u32, + static InitMp3 = ffi::MIX_INIT_MP3 as u32, + static InitOgg = ffi::MIX_INIT_OGG as u32, + static InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as u32 }) /// Loads dynamic libraries and prepares them for use. Flags should be /// one or more flags from InitFlag. pub fn init(flags: InitFlag) -> InitFlag { - let ret = unsafe { ffi::Mix_Init(flags.get()) }; - InitFlag::new(ret) + unsafe { + let ret = ffi::Mix_Init(flags.bits() as c_int); + InitFlag::from_bits(ret as u32) + } } /// Cleans up all dynamically loaded library handles, freeing memory. From fc438fb1bddd0cb48d98b7335ef2959e59b7e9d2 Mon Sep 17 00:00:00 2001 From: Andelf Date: Thu, 15 May 2014 23:35:08 +0800 Subject: [PATCH 019/114] fix demo code compile error --- src/demo/demo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/demo/demo.rs b/src/demo/demo.rs index 3c226b51..5f2fa56e 100644 --- a/src/demo/demo.rs +++ b/src/demo/demo.rs @@ -13,7 +13,7 @@ fn main() { if args.len() < 2 { println!("Usage: ./demo audio.[mp3|wav|ogg]") } else { - match dump_info(&Path::new(args[1])) { + match dump_info(&Path::new(args.get(1).to_owned())) { _ => () } } @@ -24,7 +24,7 @@ fn dump_info(filename: &Path) -> Result<(), ~str> { sdl2::init(sdl2::InitAudio | sdl2::InitTimer); println!("inited => {}", mix::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | - sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).get()); + sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).bits()); // TODO: 0x8010 is SDL_audio flag try!(mix::open_audio(mix::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); mix::allocate_channels(0); From 65d77c26bb4dd7b9d3ff47deb8ad261beb4c7038 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 19 May 2014 22:40:26 +0800 Subject: [PATCH 020/114] fix travis-ci; fix build error --- .travis.yml | 6 +++--- src/sdl2_mixer/lib.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e8a21b8..ed7210e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,10 @@ env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= before_install: - - yes | sudo add-apt-repository ppa:hansjorg/rust - - sudo apt-get update + - wget -q http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz + - tar xf rust-nightly-x86_64-unknown-linux-gnu.tar.gz + - sudo ./rust-nightly-x86_64-unknown-linux-gnu/install.sh install: - - sudo apt-get install rust-nightly - cd .. - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz - time wget -q http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 9e173048..870512ea 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -123,7 +123,7 @@ bitflags!(flags InitFlag : u32 { pub fn init(flags: InitFlag) -> InitFlag { unsafe { let ret = ffi::Mix_Init(flags.bits() as c_int); - InitFlag::from_bits(ret as u32) + InitFlag::from_bits_truncate(ret as u32) } } From b14968ea69e6dca60bb220f6ba8160a978603246 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 19 May 2014 23:15:34 +0800 Subject: [PATCH 021/114] fix travis-ci --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ed7210e0..5848923f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ install: - ./configure && make && sudo make install - cd - - git clone https://github.com/andelf/rust-sdl2.git + - (cd rust-sdl2 && make) - rustc rust-sdl2/src/sdl2/lib.rs script: - rustc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs From faafb0d29f8a2e05a0b4f42d721d55750835a3a5 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 31 May 2014 17:21:08 +0800 Subject: [PATCH 022/114] update for String change and raw() change of rust-sdl2 --- src/sdl2_mixer/lib.rs | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 870512ea..e880ce41 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -23,6 +23,8 @@ use sdl2::get_error; use sdl2::rwops::RWops; use sdl2::version::Version; +pub type SdlResult = Result; + // Setup linking for all targets. #[cfg(target_os="macos")] mod mac { @@ -133,7 +135,7 @@ pub fn quit() { } /// Open the mixer with a certain audio format. -pub fn open_audio(frequency: int, format: AudioFormat, channels: int, chunksize: int) -> Result<(), ~str> { +pub fn open_audio(frequency: int, format: AudioFormat, channels: int, chunksize: int) -> SdlResult<()> { let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; if ret == 0 { Ok(()) } else { Err(get_error()) } @@ -145,7 +147,7 @@ pub fn close_audio() { } /// Get the actual audio format in use by the opened audio device. -pub fn query_spec() -> Result<(int, AudioFormat, int), ~str> { +pub fn query_spec() -> SdlResult<(int, AudioFormat, int)> { let frequency : c_int = 0; let format : uint16_t = 0; let channels : c_int = 0; @@ -165,10 +167,10 @@ pub fn get_chunk_decoders_number() -> int { } /// Get the name of the indexed sample chunk decoder. -pub fn get_chunk_decoder(index: int) -> ~str { +pub fn get_chunk_decoder(index: int) -> String { unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().into_owned() + CString::new(name, false).as_str().unwrap().into_string() } } @@ -189,9 +191,9 @@ impl Drop for Chunk { impl Chunk { /// Load file for use as a sample. - pub fn from_file(path: &Path) -> Result { + pub fn from_file(path: &Path) -> SdlResult { let raw = unsafe { - ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw, 0) + ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw(), 0) }; if raw.is_null() { Err(get_error()) @@ -218,14 +220,14 @@ impl Chunk { /// Loader trait for RWops pub trait LoaderRWops { /// Load src for use as a sample. - fn load_wav(&self) -> Result; + fn load_wav(&self) -> SdlResult; } impl LoaderRWops for RWops { /// Load src for use as a sample. - fn load_wav(&self) -> Result { + fn load_wav(&self) -> SdlResult { let raw = unsafe { - ffi::Mix_LoadWAV_RW(self.raw, 0) + ffi::Mix_LoadWAV_RW(self.raw(), 0) }; if raw == ptr::null() { Err(get_error()) @@ -314,11 +316,11 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn play(self, chunk: &Chunk, loops: int) -> Result { + pub fn play(self, chunk: &Chunk, loops: int) -> SdlResult { self.play_timed(chunk, loops, -1) } - pub fn play_timed(self, chunk: &Chunk, loops: int, ticks: int) -> Result { + pub fn play_timed(self, chunk: &Chunk, loops: int, ticks: int) -> SdlResult { let Channel(ch) = self; let ret = unsafe { ffi::Mix_PlayChannelTimed(ch as c_int, chunk.raw, loops as c_int, ticks as c_int) @@ -331,11 +333,11 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn fade_in(self, chunk: &Chunk, loops: int, ms: int) -> Result { + pub fn fade_in(self, chunk: &Chunk, loops: int, ms: int) -> SdlResult { self.fade_in_timed(chunk, loops, ms, -1) } - pub fn fade_in_timed(self, chunk: &Chunk, loops: int, ms: int, ticks: int) -> Result { + pub fn fade_in_timed(self, chunk: &Chunk, loops: int, ms: int, ticks: int) -> SdlResult { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadeInChannelTimed(ch as c_int, chunk.raw, loops as c_int, ms as c_int, ticks as c_int) @@ -408,7 +410,7 @@ impl Channel { } /// This removes all effects registered to channel. - pub fn unregister_all_effects(self) -> Result<(), ~str> { + pub fn unregister_all_effects(self) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_UnregisterAllEffects(ch as c_int) }; if ret == 0 { @@ -418,7 +420,7 @@ impl Channel { } } - pub fn set_panning(self, left: u8, right: u8) -> Result<(), ~str> { + pub fn set_panning(self, left: u8, right: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, left, right) }; if ret == 0 { @@ -429,7 +431,7 @@ impl Channel { } /// This effect simulates a simple attenuation of volume due to distance. - pub fn set_distance(self, distance: u8) -> Result<(), ~str> { + pub fn set_distance(self, distance: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, distance) }; if ret == 0 { @@ -440,7 +442,7 @@ impl Channel { } /// This effect emulates a simple 3D audio effect. - pub fn set_position(self, angle: i16, distance: u8) -> Result<(), ~str> { + pub fn set_position(self, angle: i16, distance: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, angle, distance) }; if ret == 0 { @@ -451,7 +453,7 @@ impl Channel { } /// Simple reverse stereo, swaps left and right channel sound. - pub fn set_reverse_stereo(self, flip: bool) -> Result<(), ~str> { + pub fn set_reverse_stereo(self, flip: bool) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetReverseStereo(ch as c_int, flip as c_int) }; if ret == 0 { @@ -562,10 +564,10 @@ pub fn get_music_decoders_number() -> int { } /// Get the name of the indexed music decoder. -pub fn get_music_decoder(index: int) -> ~str { +pub fn get_music_decoder(index: int) -> String { unsafe { let name = ffi::Mix_GetMusicDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().into_owned() + CString::new(name, false).as_str().unwrap().into_string() } } @@ -615,7 +617,7 @@ impl Drop for Music { impl Music { /// Load music file to use. - pub fn from_file(path: &Path) -> Result { + pub fn from_file(path: &Path) -> SdlResult { let raw = unsafe { ffi::Mix_LoadMUS(path.to_c_str().unwrap()) }; @@ -633,7 +635,7 @@ impl Music { } /// Play the loaded music loop times through from start to finish. - pub fn play(&self, loops: int) -> Result<(), ~str> { + pub fn play(&self, loops: int) -> SdlResult<()> { let ret = unsafe { ffi::Mix_PlayMusic(self.raw, loops as c_int) }; @@ -646,7 +648,7 @@ impl Music { /// Fade in over ms milliseconds of time, the loaded music, /// playing it loop times through from start to finish. - pub fn fade_in(&self, loops: int, ms: int) -> Result<(), ~str> { + pub fn fade_in(&self, loops: int, ms: int) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) }; @@ -658,7 +660,7 @@ impl Music { } /// Fade in over ms milliseconds of time, from position. - pub fn fade_in_from_pos(&self, loops: int, ms: int, position: f64) -> Result<(), ~str> { + pub fn fade_in_from_pos(&self, loops: int, ms: int, position: f64) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeInMusicPos(self.raw, loops as c_int, ms as c_int, position as c_double) }; @@ -695,7 +697,7 @@ impl Music { } /// Set the position of the currently playing music. - pub fn set_pos(position: f64) -> Result<(), ~str> { + pub fn set_pos(position: f64) -> SdlResult<()> { let ret = unsafe { ffi::Mix_SetMusicPosition(position as c_double) }; @@ -707,7 +709,7 @@ impl Music { } /// Setup a command line music player to use to play music. - pub fn set_command(command: &str) -> Result<(), ~str> { + pub fn set_command(command: &str) -> SdlResult<()> { let ret = unsafe { ffi::Mix_SetMusicCMD(command.to_c_str().unwrap()) }; @@ -724,7 +726,7 @@ impl Music { } /// Gradually fade out the music over ms milliseconds starting from now. - pub fn fade_out(ms: int) -> Result<(), ~str> { + pub fn fade_out(ms: int) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeOutMusic(ms as c_int) }; From 0add0de6297c75dace4c3710a9dfd2a10da8cc87 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 31 May 2014 22:47:29 +0800 Subject: [PATCH 023/114] fix compile error: rename Eq to PartialEq --- src/sdl2_mixer/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index e880ce41..2595847f 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -22,8 +22,7 @@ use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; use sdl2::version::Version; - -pub type SdlResult = Result; +use sdl2::SdlResult; // Setup linking for all targets. #[cfg(target_os="macos")] @@ -175,7 +174,7 @@ pub fn get_chunk_decoder(index: int) -> String { } /// The internal format for an audio chunk. -#[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Chunk { pub raw: *ffi::Mix_Chunk, pub owned: bool @@ -242,7 +241,7 @@ impl LoaderRWops for RWops { /// Fader effect type enumerations #[repr(C)] -#[deriving(Clone, Eq, Hash, Show, FromPrimitive)] +#[deriving(Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum Fading { NoFading = ffi::MIX_NO_FADING as int, FadingOut = ffi::MIX_FADING_OUT as int, @@ -250,7 +249,7 @@ pub enum Fading { } /// Sound effect channel. -#[deriving(Eq, Show)] +#[deriving(PartialEq, Show)] pub struct Channel(int); /// Set the number of channels being mixed. @@ -573,7 +572,7 @@ pub fn get_music_decoder(index: int) -> String { /// Music type enumerations #[repr(C)] -#[deriving(Clone, Eq, Hash, Show, FromPrimitive)] +#[deriving(Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum MusicType { MusicNone = ffi::MUS_NONE as int, MusicCmd = ffi::MUS_CMD as int, @@ -601,7 +600,7 @@ extern "C" fn c_music_finished_hook() { } /// This is an opaque data type used for Music data. -#[deriving(Eq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Music { pub raw: *ffi::Mix_Music, pub owned: bool, From 7d9f0532828cbf887ddbcaabfc4ad73d6aba4927 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 31 May 2014 23:00:50 +0800 Subject: [PATCH 024/114] fix demo compile error --- src/demo/demo.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/demo/demo.rs b/src/demo/demo.rs index 5f2fa56e..7bc63eba 100644 --- a/src/demo/demo.rs +++ b/src/demo/demo.rs @@ -1,11 +1,11 @@ #![crate_type = "bin"] #![crate_id="demo"] +extern crate debug; extern crate sdl2; extern crate sdl2_mixer; use std::os; -use mix = sdl2_mixer; fn main() { @@ -13,54 +13,54 @@ fn main() { if args.len() < 2 { println!("Usage: ./demo audio.[mp3|wav|ogg]") } else { - match dump_info(&Path::new(args.get(1).to_owned())) { + match dump_info(&Path::new(args.get(1).to_string())) { _ => () } } } -fn dump_info(filename: &Path) -> Result<(), ~str> { - println!("linked version: {}", mix::get_linked_version()); +fn dump_info(filename: &Path) -> Result<(), String> { + println!("linked version: {}", sdl2_mixer::get_linked_version()); sdl2::init(sdl2::InitAudio | sdl2::InitTimer); - println!("inited => {}", mix::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | + println!("inited => {}", sdl2_mixer::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).bits()); // TODO: 0x8010 is SDL_audio flag - try!(mix::open_audio(mix::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); - mix::allocate_channels(0); + try!(sdl2_mixer::open_audio(sdl2_mixer::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); + sdl2_mixer::allocate_channels(0); { - let n = mix::get_chunk_decoders_number(); + let n = sdl2_mixer::get_chunk_decoders_number(); println!("available chunk(sample) decoders: {}", n); for i in range(0, n) { - println!("| decoder {} => {:?}", i, mix::get_chunk_decoder(i)); + println!("| decoder {} => {:?}", i, sdl2_mixer::get_chunk_decoder(i)); } } { - let n = mix::get_music_decoders_number(); + let n = sdl2_mixer::get_music_decoders_number(); println!("available music decoders: {}", n); for i in range(0, n) { - println!("| decoder {} => {:?}", i, mix::get_music_decoder(i)); + println!("| decoder {} => {:?}", i, sdl2_mixer::get_music_decoder(i)); } } - println!("query spec => {}", mix::query_spec()); + println!("query spec => {}", sdl2_mixer::query_spec()); - let music = mix::Music::from_file(filename).unwrap(); + let music = sdl2_mixer::Music::from_file(filename).unwrap(); - mix::Music::hook_finished(|| { println!("play ends! from rust cb") }); + sdl2_mixer::Music::hook_finished(|| { println!("play ends! from rust cb") }); println!("music => {:?}", music); println!("music type => {}", music.get_type()); - println!("music volume => {}", mix::Music::get_volume()); + println!("music volume => {}", sdl2_mixer::Music::get_volume()); println!("play => {}", music.play(1)); sdl2::timer::delay(10000); - println!("fading out ... {}", mix::Music::fade_out(4000)); + println!("fading out ... {}", sdl2_mixer::Music::fade_out(4000)); sdl2::timer::delay(5000); @@ -68,12 +68,12 @@ fn dump_info(filename: &Path) -> Result<(), ~str> { sdl2::timer::delay(5000); - mix::Music::halt(); + sdl2_mixer::Music::halt(); sdl2::timer::delay(1000); // here will print hook_finished - mix::quit(); + sdl2_mixer::quit(); sdl2::quit(); Ok(()) From 67e9ffee22818451f31d5623aea584e5aab35628 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Sun, 8 Jun 2014 16:57:21 +0200 Subject: [PATCH 025/114] Added Rust-Empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * This does not affect the existing source structure. * `make exe` builds demo to the ‘bin/‘ folder * `make lib` builds the library to `target/cpu-vendor-os/lib` folder * added .gitignore --- .gitignore | 22 +++ Makefile | 474 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 496 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..da5ff62a --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +.DS_Store +*~ +*# +*.o +*.so +*.swp +*.dylib +*.dSYM +*.dll +*.rlib +*.dummy +*.exe +*-test +/bin/main +/bin/test-internal +/bin/test-external +/doc/ +/target/ +/build/ +/.rust/ +rusti.sh + diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..706c1b6f --- /dev/null +++ b/Makefile @@ -0,0 +1,474 @@ +# Rust-Empty: An Makefile to get started with Rust +# https://github.com/bvssvni/rust-empty +# +# The MIT License (MIT) +# +# Copyright (c) 2014 Sven Nilsen +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +# the Software, and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +SHELL := /bin/bash + +# The default make command. +# Change this to 'lib' if you are building a library. +DEFAULT = lib +# The entry file of library source. +# Change this to support multi-crate source structure. +# For advanced usage, you can rename the file 'rust-empty.mk' +# and call it with 'make -f rust-empty.mk ' from your Makefile. +LIB_ENTRY_FILE = src/sdl2_mixer/lib.rs +# The entry file of executable source. +EXE_ENTRY_FILE = src/demo/demo.rs + +EXAMPLE_FILES = examples/*.rs +SOURCE_FILES = $(shell test -e src/ && find src -type f) + +COMPILER = rustc + +# For release: + COMPILER_FLAGS = -O --cfg mac_framework +# For debugging: +# COMPILER_FLAGS = -g + +RUSTDOC = rustdoc + +# Extracts target from rustc. +TARGET = $(shell rustc --version | awk "/host:/ { print \$$2 }") +# TARGET = x86_64-unknown-linux-gnu +# TARGET = x86_64-apple-darwin + +TARGET_LIB_DIR = target/$(TARGET)/lib/ + +# Ask 'rustc' the file name of the library and use a dummy name if the source has not been created yet. +# The dummy file name is used to trigger the creation of the source first time. +# Next time 'rustc' will return the right file name. +RLIB_FILE = $(shell (rustc --crate-type=rlib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.rlib")) +# You can't have quotes around paths because 'make' doesn't see it exists. +RLIB = target/$(TARGET)/lib/$(RLIB_FILE) +DYLIB_FILE = $(shell (rustc --crate-type=dylib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.dylib")) +DYLIB = target/$(TARGET)/lib/$(DYLIB_FILE) + +# Use 'VERBOSE=1' to echo all commands, for example 'make help VERBOSE=1'. +ifdef VERBOSE + Q := +else + Q := @ +endif + +all: $(DEFAULT) + +help: + $(Q)echo "--- rust-empty (0.4 002)" \ + && echo "make run - Runs executable" \ + && echo "make exe - Builds main executable" \ + && echo "make lib - Both static and dynamic library" \ + && echo "make rlib - Static library" \ + && echo "make dylib - Dynamic library" \ + && echo "make test - Tests library internally and externally" \ + && echo "make test-internal - Tests library internally" \ + && echo "make test-external - Tests library externally" \ + && echo "make bench - Benchmarks library internally and externally" \ + && echo "make bench-internal - Benchmarks library internally" \ + && echo "make bench-external - Benchmarks library externally" \ + && echo "make doc - Builds documentation for library" \ + && echo "make git-ignore - Setup files to be ignored by Git" \ + && echo "make examples - Builds examples" \ + && echo "make cargo-lite-exe - Setup executable package" \ + && echo "make cargo-lite-lib - Setup library package" \ + && echo "make cargo-exe - EXPERIMENTAL: Setup executable package" \ + && echo "make cargo-lib - EXPERIMENTAL: Setup library package" \ + && echo "make rust-ci-lib - Setup Travis CI Rust library" \ + && echo "make rust-ci-exe - Setup Travis CI Rust executable" \ + && echo "make rusti - Setup 'rusti.sh' for interactive Rust" \ + && echo "make loc - Count lines of code in src folder" \ + && echo "make nightly-install - Installs Rust nightly built" \ + && echo "make nightly-uninstall - Uninstalls Rust nightly built" \ + && echo "make clean - Deletes binaries and documentation." \ + && echo "make clear-project - WARNING: Deletes project files except 'Makefile'" \ + && echo "make clear-git - WARNING: Deletes Git setup" \ + && echo "make symlink-info - Symlinked libraries dependency info" \ + && echo "make target-dir - Creates directory for current target" + +.PHONY: \ + bench \ + bench-internal \ + bench-external \ + cargo-lib \ + cargo-exe \ + cargo-lite-lib \ + cargo-lite-exe \ + clean \ + clear-git \ + clear-project \ + loc \ + nightly-install \ + nightly-uninstall \ + run \ + rusti \ + rust-ci-lib \ + rust-ci-exe \ + symlink-info \ + target-dir \ + test \ + test-internal \ + test-external + +nightly-install: + $(Q)cd ~ \ + && curl -s http://www.rust-lang.org/rustup.sh > rustup.sh \ + && ( \ + echo "Rust install-script stored as '~/rustup.sh'" ; \ + read -p "Do you want to install? [y/n]:" -n 1 -r ; \ + echo "" ; \ + if [[ $$REPLY =~ ^[Yy]$$ ]] ; \ + then \ + cat rustup.sh | sudo sh ; \ + fi \ + ) + +nightly-uninstall: + $(Q)cd ~ \ + && curl -s http://www.rust-lang.org/rustup.sh > rustup.sh \ + && ( \ + echo "Rust install-script stored as '~/rustup.sh'" ; \ + read -p "Do you want to uninstall? [y/n]:" -n 1 -r ; \ + echo "" ; \ + if [[ $$REPLY =~ ^[Yy]$$ ]] ; \ + then \ + cat rustup.sh | sudo sh -s -- --uninstall ; \ + fi \ + ) + +cargo-lite-exe: $(EXE_ENTRY_FILE) + $(Q)( \ + test -e cargo-lite.conf \ + && echo "--- The file 'cargo-lite.conf' already exists" \ + ) \ + || \ + ( \ + echo -e "deps = [\n]\n\n[build]\ncrate_root = \"$(EXE_ENTRY_FILE)\"\nrustc_args = []\n" > cargo-lite.conf \ + && echo "--- Created 'cargo-lite.conf' for executable" \ + && cat cargo-lite.conf \ + ) + +cargo-lite-lib: $(LIB_ENTRY_FILE) + $(Q)( \ + test -e cargo-lite.conf \ + && echo "--- The file 'cargo-lite.conf' already exists" \ + ) \ + || \ + ( \ + echo -e "deps = [\n]\n\n[build]\ncrate_root = \"$(LIB_ENTRY_FILE)\"\ncrate_type = \"library\"\nrustc_args = []\n" > cargo-lite.conf \ + && echo "--- Created 'cargo-lite.conf' for library" \ + && cat cargo-lite.conf \ + ) + +cargo-exe: $(EXE_ENTRY_FILE) + $(Q)( \ + test -e Cargo.toml \ + && echo "--- The file 'Cargo.toml' already exists" \ + ) \ + || \ + ( \ + name=$${PWD##/*/} ; \ + readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ + echo -e "[project]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ + && echo "--- Created 'Cargo.toml' for executable" \ + && cat Cargo.toml \ + ) + +cargo-lib: $(EXE_ENTRY_FILE) + $(Q)( \ + test -e Cargo.toml \ + && echo "--- The file 'Cargo.toml' already exists" \ + ) \ + || \ + ( \ + name=$${PWD##/*/} ; \ + readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ + echo -e "[project]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ + && echo "--- Created 'Cargo.toml' for executable" \ + && cat Cargo.toml \ + ) + +rust-ci-lib: $(LIB_ENTRY_FILE) + $(Q)( \ + test -e .travis.yml \ + && echo "--- The file '.travis.yml' already exists" \ + ) \ + || \ + ( \ + echo -e "before_install:\n\t- yes | sudo add-apt-repository ppa:hansjorg/rust\n\t- sudo apt-get update\ninstall:\n\t- sudo apt-get install rust-nightly\nscript:\n\t- make lib\n" > .travis.yml \ + && echo "--- Created '.travis.yml' for library" \ + && cat .travis.yml \ + ) + +rust-ci-exe: $(EXE_ENTRY_FILE) + $(Q)( \ + test -e .travis.yml \ + && echo "--- The file '.travis.yml' already exists" \ + ) \ + || \ + ( \ + echo -e "before_install:\n\t- yes | sudo add-apt-repository ppa:hansjorg/rust\n\t- sudo apt-get update\ninstall:\n\t- sudo apt-get install rust-nightly\nscript:\n\t- make exe\n" > .travis.yml \ + && echo "--- Created '.travis.yml' for executable" \ + && cat .travis.yml \ + ) + +doc: $(SOURCE_FILES) | src/ + $(Q)$(RUSTDOC) $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" \ + && echo "--- Built documentation" + +run: exe + $(Q)cd bin/ \ + && ./main + +target-dir: $(TARGET_LIB_DIR) + +exe: bin/main | $(TARGET_LIB_DIR) + +bin/main: $(SOURCE_FILES) | bin/ $(EXE_ENTRY_FILE) + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $(EXE_ENTRY_FILE) -o bin/main -L "target/$(TARGET)/lib" \ + && echo "--- Built executable" \ + && echo "--- Type 'make run' to run executable" + +test: test-internal test-external + $(Q)echo "--- Internal tests succeeded" \ + && echo "--- External tests succeeded" + +test-external: bin/test-external + $(Q)cd "bin/" \ + && ./test-external + +bin/test-external: $(SOURCE_FILES) | rlib bin/ src/test.rs + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test src/test.rs -o bin/test-external -L "target/$(TARGET)/lib" \ + && echo "--- Built external test runner" + +test-internal: bin/test-internal + $(Q)cd "bin/" \ + && ./test-internal + +bin/test-internal: $(SOURCE_FILES) | rlib src/ bin/ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test $(LIB_ENTRY_FILE) -o bin/test-internal -L "target/$(TARGET)/lib" \ + && echo "--- Built internal test runner" + +bench: bench-internal bench-external + +bench-external: test-external + $(Q)bin/test-external --bench + +bench-internal: test-internal + $(Q)bin/test-internal --bench + +lib: rlib dylib + $(Q)echo "--- Type 'make test' to test library" + +rlib: $(RLIB) + +$(RLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=rlib $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" --out-dir "target/$(TARGET)/lib/" \ + && echo "--- Built rlib" + +dylib: $(DYLIB) + +$(DYLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=dylib $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" --out-dir "target/$(TARGET)/lib/" \ + && echo "--- Built dylib" + +bin: + $(Q)mkdir -p bin + +$(TARGET_LIB_DIR): + $(Q)mkdir -p $(TARGET_LIB_DIR) + +src: + $(Q)mkdir -p src + +examples-dir: + $(Q)test -e examples \ + || \ + ( \ + mkdir examples \ + && echo -e "fn main() {\n\tprintln!(\"Hello!\");\n}\n" > examples/hello.rs \ + && echo "--- Created examples folder" \ + ) + +rust-dir: + $(Q)mkdir -p .rust + +git-ignore: + $(Q)( \ + test -e .gitignore \ + && echo "--- The file '.gitignore' already exists" \ + ) \ + || \ + ( \ + echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\n" > .gitignore \ + && echo "--- Created '.gitignore' for git" \ + && cat .gitignore \ + ) + +examples: $(EXAMPLE_FILES) + +$(EXAMPLE_FILES): lib examples-dir + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $@ -L "target/$(TARGET)/lib" --out-dir examples/ \ + && echo "--- Built examples" + +$(EXE_ENTRY_FILE): | src/ + $(Q)test -e $(EXE_ENTRY_FILE) \ + || \ + ( \ + echo -e "fn main() {\n\tprintln!(\"Hello world!\");\n}" > $(EXE_ENTRY_FILE) \ + ) + +src/test.rs: | src/ + $(Q)test -e src/test.rs \ + || \ + ( \ + touch src/test.rs \ + ) + +$(LIB_ENTRY_FILE): | src/ + $(Q)test -e $(LIB_ENTRY_FILE) \ + || \ + ( \ + echo -e "#![crate_id = \"\"]\n#![deny(missing_doc)]\n\n//! Documentation goes here.\n" > $(LIB_ENTRY_FILE) \ + ) + +clean: + $(Q)rm -f "$(RLIB)" + $(Q)rm -f "$(DYLIB)" + $(Q)rm -rf "doc/" + $(Q)rm -f "bin/main" + $(Q)rm -f "bin/test-internal" + $(Q)rm -f "bin/test-external" + $(Q)echo "--- Deleted binaries and documentation" + +clear-project: + $(Q)rm -f ".symlink-info" + $(Q)rm -f "cargo-lite.conf" + $(Q)rm -f ".travis.yml" + $(Q)rm -f "rusti.sh" + $(Q)rm -rf "target/" + $(Q)rm -rf "src/" + $(Q)rm -rf "bin/" + $(Q)rm -rf "examples/" + $(Q)rm -rf "doc/" + $(Q)echo "--- Removed all source files, binaries and documentation" \ + && echo "--- Content in project folder" \ + && ls -a + +clear-git: + $(Q)rm -f ".gitignore" + $(Q)rm -rf ".git" + $(Q)echo "--- Removed Git" \ + && echo "--- Content in project folder" \ + && ls -a + +# borrowed from http://stackoverflow.com/q/649246/1256624 +define RUSTI_SCRIPT +#!/bin/bash + +#written by mcpherrin + +while true; do + echo -n "> " + read line + TMP="`mktemp r.XXXXXX`" + $(COMPILER) - -o $$TMP -L "target/$(TARGET)/lib/" < rusti.sh \ + && chmod +x rusti.sh \ + && echo "--- Created 'rusti.sh'" \ + && echo "--- Type './rusti.sh' to start interactive Rust" \ + ) + +loc: + $(Q)echo "--- Counting lines of .rs files in 'src' (LOC):" \ + && find src/ -type f -name "*.rs" -exec cat {} \; | wc -l + +# Finds the original locations of symlinked libraries and +# prints the commit hash with remote branches containing that commit. +symlink-info: + $(Q) current=$$(pwd) ; \ + for symlib in $$(find target/*/lib -type l) ; do \ + cd $$current ; \ + echo $$symlib ; \ + original_file=$$(readlink $$symlib) ; \ + original_dir=$$(dirname $$original_file) ; \ + cd $$original_dir ; \ + commit=$$(git rev-parse HEAD) ; \ + echo $$commit ; \ + echo "origin:" ; \ + git config --get remote.origin.url ; \ + echo "upstream:" ; \ + git config --get remote.upstream.url ; \ + echo "available in remote branches:" ; \ + git branch -r --contains $$commit ; \ + echo "" ; \ + done \ + > .symlink-info \ + && cd $$current \ + && echo "--- Created '.symlink-info'" \ + && cat .symlink-info From 4d2b9dd875c7abe12daa0983dc54abd8de4eeb15 Mon Sep 17 00:00:00 2001 From: Coeuvre Date: Sat, 14 Jun 2014 09:56:49 +0800 Subject: [PATCH 026/114] Build with ENV 'SDL_MODE' --- Makefile | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 706c1b6f..eed43e4c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Rust-Empty: An Makefile to get started with Rust # https://github.com/bvssvni/rust-empty -# +# # The MIT License (MIT) # # Copyright (c) 2014 Sven Nilsen @@ -41,10 +41,28 @@ SOURCE_FILES = $(shell test -e src/ && find src -type f) COMPILER = rustc # For release: - COMPILER_FLAGS = -O --cfg mac_framework + COMPILER_FLAGS = -O # For debugging: # COMPILER_FLAGS = -g +ifeq ($(UNAME),Darwin) + # If the user wasn't explicit, see if SDL2 library exists + ifeq ("$(strip $(SDL_MODE))","") + SDL_CHECK=$(shell pkg-config --exists sdl2) + ifeq ($(SDL_CHECK),0) + SDL_MODE = dylib + else + SDL_MODE = framework + endif + endif + + ifeq ($(SDL_MODE),framework) + COMPILER_FLAGS+=--cfg mac_framework + else + COMPILER_FLAGS+=--cfg mac_dylib + endif +endif + RUSTDOC = rustdoc # Extracts target from rustc. From c244759929fe8b9a4ad52d6d8727d13da8db6785 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Sat, 14 Jun 2014 09:33:58 +0200 Subject: [PATCH 027/114] Added UNAME variable Closes https://github.com/andelf/rust-sdl2_mixer/issues/6 --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index eed43e4c..1b5fd786 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,8 @@ COMPILER = rustc # For debugging: # COMPILER_FLAGS = -g +UNAME=$(shell uname) + ifeq ($(UNAME),Darwin) # If the user wasn't explicit, see if SDL2 library exists ifeq ("$(strip $(SDL_MODE))","") From 7f506cff848f68eb0f26200f6cfdfe17b898606a Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Sun, 15 Jun 2014 23:29:18 +0200 Subject: [PATCH 028/114] Upgraded to latest Rust-Empty --- Makefile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 1b5fd786..bec7228c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Rust-Empty: An Makefile to get started with Rust # https://github.com/bvssvni/rust-empty -# +# # The MIT License (MIT) # # Copyright (c) 2014 Sven Nilsen @@ -68,7 +68,7 @@ endif RUSTDOC = rustdoc # Extracts target from rustc. -TARGET = $(shell rustc --version | awk "/host:/ { print \$$2 }") +TARGET = $(shell rustc --version 2> /dev/null | awk "/host:/ { print \$$2 }") # TARGET = x86_64-unknown-linux-gnu # TARGET = x86_64-apple-darwin @@ -93,7 +93,7 @@ endif all: $(DEFAULT) help: - $(Q)echo "--- rust-empty (0.4 002)" \ + $(Q)echo "--- rust-empty (0.4 004)" \ && echo "make run - Runs executable" \ && echo "make exe - Builds main executable" \ && echo "make lib - Both static and dynamic library" \ @@ -233,7 +233,7 @@ rust-ci-lib: $(LIB_ENTRY_FILE) ) \ || \ ( \ - echo -e "before_install:\n\t- yes | sudo add-apt-repository ppa:hansjorg/rust\n\t- sudo apt-get update\ninstall:\n\t- sudo apt-get install rust-nightly\nscript:\n\t- make lib\n" > .travis.yml \ + echo -e "install:\n - wget http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz -O - | sudo tar zxf - --strip-components 1 -C /usr/local\nscript:\n - make lib\n" > .travis.yml \ && echo "--- Created '.travis.yml' for library" \ && cat .travis.yml \ ) @@ -245,7 +245,7 @@ rust-ci-exe: $(EXE_ENTRY_FILE) ) \ || \ ( \ - echo -e "before_install:\n\t- yes | sudo add-apt-repository ppa:hansjorg/rust\n\t- sudo apt-get update\ninstall:\n\t- sudo apt-get install rust-nightly\nscript:\n\t- make exe\n" > .travis.yml \ + echo -e "install:\n - wget http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz -O - | sudo tar zxf - --strip-components 1 -C /usr/local\nscript:\n - make exe\n" > .travis.yml \ && echo "--- Created '.travis.yml' for executable" \ && cat .travis.yml \ ) @@ -310,13 +310,13 @@ $(DYLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=dylib $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" --out-dir "target/$(TARGET)/lib/" \ && echo "--- Built dylib" -bin: +bin/: $(Q)mkdir -p bin $(TARGET_LIB_DIR): $(Q)mkdir -p $(TARGET_LIB_DIR) -src: +src/: $(Q)mkdir -p src examples-dir: @@ -338,7 +338,7 @@ git-ignore: ) \ || \ ( \ - echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\n" > .gitignore \ + echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\n/examples/*\n!/examples/*.rs" > .gitignore \ && echo "--- Created '.gitignore' for git" \ && cat .gitignore \ ) From 922b528cb1ccbb29aeab353c36686c8b06ede989 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Wed, 25 Jun 2014 15:25:14 +0200 Subject: [PATCH 029/114] Upgraded to latest Rust-Empty --- Makefile | 273 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 238 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index bec7228c..384eca1c 100644 --- a/Makefile +++ b/Makefile @@ -93,36 +93,38 @@ endif all: $(DEFAULT) help: - $(Q)echo "--- rust-empty (0.4 004)" \ - && echo "make run - Runs executable" \ - && echo "make exe - Builds main executable" \ - && echo "make lib - Both static and dynamic library" \ - && echo "make rlib - Static library" \ - && echo "make dylib - Dynamic library" \ - && echo "make test - Tests library internally and externally" \ - && echo "make test-internal - Tests library internally" \ - && echo "make test-external - Tests library externally" \ - && echo "make bench - Benchmarks library internally and externally" \ - && echo "make bench-internal - Benchmarks library internally" \ - && echo "make bench-external - Benchmarks library externally" \ - && echo "make doc - Builds documentation for library" \ - && echo "make git-ignore - Setup files to be ignored by Git" \ - && echo "make examples - Builds examples" \ - && echo "make cargo-lite-exe - Setup executable package" \ - && echo "make cargo-lite-lib - Setup library package" \ - && echo "make cargo-exe - EXPERIMENTAL: Setup executable package" \ - && echo "make cargo-lib - EXPERIMENTAL: Setup library package" \ - && echo "make rust-ci-lib - Setup Travis CI Rust library" \ - && echo "make rust-ci-exe - Setup Travis CI Rust executable" \ - && echo "make rusti - Setup 'rusti.sh' for interactive Rust" \ - && echo "make loc - Count lines of code in src folder" \ - && echo "make nightly-install - Installs Rust nightly built" \ - && echo "make nightly-uninstall - Uninstalls Rust nightly built" \ - && echo "make clean - Deletes binaries and documentation." \ - && echo "make clear-project - WARNING: Deletes project files except 'Makefile'" \ - && echo "make clear-git - WARNING: Deletes Git setup" \ - && echo "make symlink-info - Symlinked libraries dependency info" \ - && echo "make target-dir - Creates directory for current target" + $(Q)echo "--- rust-empty (0.5 006)" + $(Q)echo "make run - Runs executable" + $(Q)echo "make exe - Builds main executable" + $(Q)echo "make lib - Both static and dynamic library" + $(Q)echo "make rlib - Static library" + $(Q)echo "make dylib - Dynamic library" + $(Q)echo "make test - Tests library internally and externally" + $(Q)echo "make test-internal - Tests library internally" + $(Q)echo "make test-external - Tests library externally" + $(Q)echo "make bench - Benchmarks library internally and externally" + $(Q)echo "make bench-internal - Benchmarks library internally" + $(Q)echo "make bench-external - Benchmarks library externally" + $(Q)echo "make doc - Builds documentation for library" + $(Q)echo "make git-ignore - Setup files to be ignored by Git" + $(Q)echo "make examples - Builds examples" + $(Q)echo "make cargo-lite-exe - Setup executable package" + $(Q)echo "make cargo-lite-lib - Setup library package" + $(Q)echo "make cargo-exe - EXPERIMENTAL: Setup executable package" + $(Q)echo "make cargo-lib - EXPERIMENTAL: Setup library package" + $(Q)echo "make rust-ci-lib - Setup Travis CI Rust library" + $(Q)echo "make rust-ci-exe - Setup Travis CI Rust executable" + $(Q)echo "make rusti - Setup 'rusti.sh' for interactive Rust" + $(Q)echo "make watch - Setup 'watch.sh' for compilation on save" + $(Q)echo "make loc - Count lines of code in src folder" + $(Q)echo "make nightly-install - Installs Rust nightly build" + $(Q)echo "make nightly-uninstall - Uninstalls Rust nightly build" + $(Q)echo "make clean - Deletes binaries and documentation." + $(Q)echo "make clear-project - WARNING: Deletes project files except 'Makefile'" + $(Q)echo "make clear-git - WARNING: Deletes Git setup" + $(Q)echo "make symlink-build - Creates a script for building dependencies + $(Q)echo "make symlink-info - Symlinked libraries dependency info" + $(Q)echo "make target-dir - Creates directory for current target" .PHONY: \ bench \ @@ -142,11 +144,13 @@ help: rusti \ rust-ci-lib \ rust-ci-exe \ + symlink-build \ symlink-info \ target-dir \ test \ test-internal \ - test-external + test-external \ + watch nightly-install: $(Q)cd ~ \ @@ -207,7 +211,7 @@ cargo-exe: $(EXE_ENTRY_FILE) ( \ name=$${PWD##/*/} ; \ readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[project]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ + echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ && echo "--- Created 'Cargo.toml' for executable" \ && cat Cargo.toml \ ) @@ -221,7 +225,7 @@ cargo-lib: $(EXE_ENTRY_FILE) ( \ name=$${PWD##/*/} ; \ readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[project]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ + echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ && echo "--- Created 'Cargo.toml' for executable" \ && cat Cargo.toml \ ) @@ -338,7 +342,7 @@ git-ignore: ) \ || \ ( \ - echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\n/examples/*\n!/examples/*.rs" > .gitignore \ + echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\nwatch.sh\n/examples/**\n!/examples/*.rs\n!/examples/assets/" > .gitignore \ && echo "--- Created '.gitignore' for git" \ && cat .gitignore \ ) @@ -347,7 +351,7 @@ examples: $(EXAMPLE_FILES) $(EXAMPLE_FILES): lib examples-dir $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $@ -L "target/$(TARGET)/lib" --out-dir examples/ \ - && echo "--- Built examples" + && echo "--- Built '$@' (make $@)" $(EXE_ENTRY_FILE): | src/ $(Q)test -e $(EXE_ENTRY_FILE) \ @@ -382,8 +386,10 @@ clean: clear-project: $(Q)rm -f ".symlink-info" $(Q)rm -f "cargo-lite.conf" + $(Q)rm -f "Cargo.toml" $(Q)rm -f ".travis.yml" $(Q)rm -f "rusti.sh" + $(Q)rm -f "watch.sh" $(Q)rm -rf "target/" $(Q)rm -rf "src/" $(Q)rm -rf "bin/" @@ -464,6 +470,203 @@ rusti: $(TARGET_LIB_DIR) && echo "--- Type './rusti.sh' to start interactive Rust" \ ) +# borrowed from http://stackoverflow.com/q/649246/1256624 +define WATCH_SCRIPT +#!/bin/bash + +#written by zzmp + +# This script will recompile a rust project using `make` +# every time something in the specified directory changes. + +# Watch files in infinite loop +watch () { + UNAME=$$(uname) + if [ -e "$$2" ]; then + echo "Watching files in $$2.." + CTIME=$$(date "+%s") + while :; do + sleep 1 + for f in `find $$2 -type f -name "*.rs"`; do + if [[ $$UNAME == "Darwin" ]]; then + st_mtime=$$(stat -f "%m" "$$f") + elif [[ $$UNAME == "FreeBSD" ]]; then + st_mtime=$$(stat -f "%m" "$$f") + else + st_mtime=$$(stat -c "%Y" "$$f") + fi + if [ $$st_mtime -gt $$CTIME ]; then + CTIME=$$(date "+%s") + echo "~~~ Rebuilding" + $$1 + if [ ! $$? -eq 0 ]; then + echo "" + fi + fi + done + done + else + echo "$$2 is not a valid directory" + fi +} + +# Capture user input with defaults +CMD=$${1:-make} +DIR=$${2:-src} + +if [ $${CMD:0:2} = '-h' ]; then +echo ' +This script will recompile a rust project using `make` +every time something in the specified directory changes. + +Use: ./watch.sh [CMD] [DIR] +Example: ./watch.sh "make run" src + +CMD: Command to execute + Complex commands may be passed as strings + `make` by default +DIR: Directory to watch + src by default + +If DIR is supplied, CMD must be as well.\n' +else + watch "$$CMD" "$$DIR" +fi + +endef +export WATCH_SCRIPT + +watch: $(TARGET_LIB_DIR) + $(Q)( \ + test -e watch.sh \ + && echo "--- The file 'watch.sh' already exists" \ + ) \ + || \ + ( \ + echo -e "$$WATCH_SCRIPT" > watch.sh \ + && chmod +x watch.sh \ + && echo "--- Created 'watch.sh'" \ + && echo "--- Type './watch.sh' to start compilation on save" \ + && echo "--- Type './watch.sh -h' for more options" \ + ) + +# borrowed from http://stackoverflow.com/q/649246/1256624 +define SYMLINK_BUILD_SCRIPT +#!/bin/bash +# written by bvssvni +# Modify the setting to do conditional compilation. +# For example "--cfg my_feature" +SETTINGS="" +# ================================================ + +MAKE=make +if [ "$$OS" == "Windows_NT" ]; then + MAKE=mingw32-make +fi + +# Checks if an item exists in an array. +# Copied from http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value +function contains() { + local n=$$# + local value=$${!n} + for ((i=1;i < $$#;i++)) { + if [ "$${!i}" == "$${value}" ]; then + echo "y" + return 0 + fi + } + echo "n" + return 1 +} + +# This is a counter used to insert dependencies. +# It is global because we need an array of all the +# visited dependencies. +i=0 +function build_deps { + local current=$$(pwd) + for symlib in $$(find target/*/lib -type l) ; do + cd $$current + echo $$symlib + local original_file=$$(readlink $$symlib) + local original_dir=$$(dirname $$original_file) + cd $$original_dir + + # Go to the git root directory. + local current_git_dir=$$(git rev-parse --show-toplevel) + echo "--- Parent $$current" + echo "--- Child $$current_git_dir" + cd $$current_git_dir + + # Skip building if it is already built. + if [ $$(contains "$${git_dir[@]}" $$current_git_dir) == "y" ]; then + echo "--- Visited $$current_git_dir" + continue + fi + + # Remember git directory to not build it twice + git_dir[i]=$$current_git_dir + + # Visit the symlinks and build the dependencies + build_deps + + # First check for a 'build.sh' script with default settings. + # Check for additional 'rust-empty.mk' file. \ + # Compile with the settings flags. \ + # If no other options, build with make. + ( \ + test -e build.sh \ + && ./build.sh \ + ) \ + || \ + ( \ + test -e rust-empty.mk \ + && $$MAKE -f rust-empty.mk clean \ + && $$MAKE -f rust-empty.mk \ + ) \ + || \ + ( \ + echo "--- Building $$current_git_dir" \ + && $$MAKE clean \ + && $$MAKE \ + ) + let i+=1 + done + cd $$current +} + +if [ "$$1" == "deps" ]; then + build_deps +fi + +echo "--- Building $$(pwd)" +( \ + test -e rust-empty.mk \ + && $$MAKE -f rust-empty.mk clean \ + && $$MAKE -f rust-empty.mk COMPILER_FLAGS+="$$SETTINGS" \ +) \ +|| \ +( \ + $$MAKE clean + $$MAKE COMPILER_FLAGS+="$$SETTINGS" +) + +endef +export SYMLINK_BUILD_SCRIPT + +symlink-build: + $(Q)( \ + test -e build.sh \ + && echo "--- The file 'build.sh' already exists" \ + ) \ + || \ + ( \ + echo -e "$$SYMLINK_BUILD_SCRIPT" > build.sh \ + && chmod +x build.sh \ + && echo "--- Created 'build.sh'" \ + && echo "--- Type './build.sh deps' to build everything" \ + ) + loc: $(Q)echo "--- Counting lines of .rs files in 'src' (LOC):" \ && find src/ -type f -name "*.rs" -exec cat {} \; | wc -l From 16a1953e0d10f9adb0e70e3b5839a522e3bda7f6 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Wed, 25 Jun 2014 20:52:18 +0200 Subject: [PATCH 030/114] Upgraded to latest Rust-Empty + Cargo --- Cargo.toml | 16 ++++++++++++++++ Makefile | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 Cargo.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..7bab181d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] + +name = "rust-sdl2_mixer" +version = "0.0.1" +readme = "README.md" +authors = ["ShuYu Wang"] + +[[lib]] + +name = "sdl2_mixer" +path = "src/sdl2_mixer/lib.rs" + +[dependencies.sdl2] + +git = "https://github.com/AngryLawyer/rust-sdl2" + diff --git a/Makefile b/Makefile index 384eca1c..32037764 100644 --- a/Makefile +++ b/Makefile @@ -93,7 +93,7 @@ endif all: $(DEFAULT) help: - $(Q)echo "--- rust-empty (0.5 006)" + $(Q)echo "--- rust-empty (0.5 007)" $(Q)echo "make run - Runs executable" $(Q)echo "make exe - Builds main executable" $(Q)echo "make lib - Both static and dynamic library" @@ -122,7 +122,7 @@ help: $(Q)echo "make clean - Deletes binaries and documentation." $(Q)echo "make clear-project - WARNING: Deletes project files except 'Makefile'" $(Q)echo "make clear-git - WARNING: Deletes Git setup" - $(Q)echo "make symlink-build - Creates a script for building dependencies + $(Q)echo "make symlink-build - Creates a script for building dependencies" $(Q)echo "make symlink-info - Symlinked libraries dependency info" $(Q)echo "make target-dir - Creates directory for current target" From 749f599e6ad63db6d032efbcb792341479106495 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Mon, 30 Jun 2014 12:51:26 +0200 Subject: [PATCH 031/114] Upgraded to latest Rust + Rust-Empty Added build script generated from Rust-Empty for building by symlinks. --- Makefile | 45 ++++++++++++----------- build.sh | 78 ++++++++++++++++++++++++++++++++++++++++ src/sdl2_mixer/ffi.rs | 84 +++++++++++++++++++++---------------------- src/sdl2_mixer/lib.rs | 4 +-- 4 files changed, 146 insertions(+), 65 deletions(-) create mode 100755 build.sh diff --git a/Makefile b/Makefile index 32037764..46285597 100644 --- a/Makefile +++ b/Makefile @@ -68,20 +68,20 @@ endif RUSTDOC = rustdoc # Extracts target from rustc. -TARGET = $(shell rustc --version 2> /dev/null | awk "/host:/ { print \$$2 }") +TARGET = $(shell rustc --version verbose 2> /dev/null | awk "/host:/ { print \$$2 }") # TARGET = x86_64-unknown-linux-gnu # TARGET = x86_64-apple-darwin -TARGET_LIB_DIR = target/$(TARGET)/lib/ +TARGET_LIB_DIR = target/deps/ # Ask 'rustc' the file name of the library and use a dummy name if the source has not been created yet. # The dummy file name is used to trigger the creation of the source first time. # Next time 'rustc' will return the right file name. RLIB_FILE = $(shell (rustc --crate-type=rlib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.rlib")) # You can't have quotes around paths because 'make' doesn't see it exists. -RLIB = target/$(TARGET)/lib/$(RLIB_FILE) +RLIB = target/$(RLIB_FILE) DYLIB_FILE = $(shell (rustc --crate-type=dylib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.dylib")) -DYLIB = target/$(TARGET)/lib/$(DYLIB_FILE) +DYLIB = target/$(DYLIB_FILE) # Use 'VERBOSE=1' to echo all commands, for example 'make help VERBOSE=1'. ifdef VERBOSE @@ -93,7 +93,7 @@ endif all: $(DEFAULT) help: - $(Q)echo "--- rust-empty (0.5 007)" + $(Q)echo "--- rust-empty (0.6 003)" $(Q)echo "make run - Runs executable" $(Q)echo "make exe - Builds main executable" $(Q)echo "make lib - Both static and dynamic library" @@ -110,8 +110,8 @@ help: $(Q)echo "make examples - Builds examples" $(Q)echo "make cargo-lite-exe - Setup executable package" $(Q)echo "make cargo-lite-lib - Setup library package" - $(Q)echo "make cargo-exe - EXPERIMENTAL: Setup executable package" - $(Q)echo "make cargo-lib - EXPERIMENTAL: Setup library package" + $(Q)echo "make cargo-exe - Setup executable package" + $(Q)echo "make cargo-lib - Setup library package" $(Q)echo "make rust-ci-lib - Setup Travis CI Rust library" $(Q)echo "make rust-ci-exe - Setup Travis CI Rust executable" $(Q)echo "make rusti - Setup 'rusti.sh' for interactive Rust" @@ -211,12 +211,12 @@ cargo-exe: $(EXE_ENTRY_FILE) ( \ name=$${PWD##/*/} ; \ readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ + echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ && echo "--- Created 'Cargo.toml' for executable" \ && cat Cargo.toml \ ) -cargo-lib: $(EXE_ENTRY_FILE) +cargo-lib: $(LIB_ENTRY_FILE) $(Q)( \ test -e Cargo.toml \ && echo "--- The file 'Cargo.toml' already exists" \ @@ -225,7 +225,7 @@ cargo-lib: $(EXE_ENTRY_FILE) ( \ name=$${PWD##/*/} ; \ readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ + echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ && echo "--- Created 'Cargo.toml' for executable" \ && cat Cargo.toml \ ) @@ -255,7 +255,7 @@ rust-ci-exe: $(EXE_ENTRY_FILE) ) doc: $(SOURCE_FILES) | src/ - $(Q)$(RUSTDOC) $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" \ + $(Q)$(RUSTDOC) $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" \ && echo "--- Built documentation" run: exe @@ -267,7 +267,7 @@ target-dir: $(TARGET_LIB_DIR) exe: bin/main | $(TARGET_LIB_DIR) bin/main: $(SOURCE_FILES) | bin/ $(EXE_ENTRY_FILE) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $(EXE_ENTRY_FILE) -o bin/main -L "target/$(TARGET)/lib" \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $(EXE_ENTRY_FILE) -o bin/main -L "$(TARGET_LIB_DIR)" -L "target" \ && echo "--- Built executable" \ && echo "--- Type 'make run' to run executable" @@ -280,7 +280,7 @@ test-external: bin/test-external && ./test-external bin/test-external: $(SOURCE_FILES) | rlib bin/ src/test.rs - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test src/test.rs -o bin/test-external -L "target/$(TARGET)/lib" \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test src/test.rs -o bin/test-external -L "$(TARGET_LIB_DIR)" -L "target" \ && echo "--- Built external test runner" test-internal: bin/test-internal @@ -288,7 +288,7 @@ test-internal: bin/test-internal && ./test-internal bin/test-internal: $(SOURCE_FILES) | rlib src/ bin/ - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test $(LIB_ENTRY_FILE) -o bin/test-internal -L "target/$(TARGET)/lib" \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test $(LIB_ENTRY_FILE) -o bin/test-internal -L "$(TARGET_LIB_DIR)" -L "target" \ && echo "--- Built internal test runner" bench: bench-internal bench-external @@ -305,13 +305,13 @@ lib: rlib dylib rlib: $(RLIB) $(RLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=rlib $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" --out-dir "target/$(TARGET)/lib/" \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=rlib $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" --out-dir "target" \ && echo "--- Built rlib" dylib: $(DYLIB) $(DYLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=dylib $(LIB_ENTRY_FILE) -L "target/$(TARGET)/lib" --out-dir "target/$(TARGET)/lib/" \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=dylib $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" --out-dir "target/" \ && echo "--- Built dylib" bin/: @@ -350,7 +350,7 @@ git-ignore: examples: $(EXAMPLE_FILES) $(EXAMPLE_FILES): lib examples-dir - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $@ -L "target/$(TARGET)/lib" --out-dir examples/ \ + $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $@ -L "$(TARGET_LIB_DIR)" -L "target" --out-dir examples/ \ && echo "--- Built '$@' (make $@)" $(EXE_ENTRY_FILE): | src/ @@ -416,7 +416,7 @@ while true; do echo -n "> " read line TMP="`mktemp r.XXXXXX`" - $(COMPILER) - -o $$TMP -L "target/$(TARGET)/lib/" <; + (arg1: c_int, arg2: *const c_void, arg3: c_int, + arg4: *const c_void)>; pub type Mix_EffectDone_t = - ::std::option::Option; + ::std::option::Option; extern "C" { - pub fn Mix_Linked_Version() -> *SDL_version; + pub fn Mix_Linked_Version() -> *const SDL_version; pub fn Mix_Init(flags: c_int) -> c_int; pub fn Mix_Quit(); pub fn Mix_OpenAudio(frequency: c_int, format: uint16_t, channels: c_int, chunksize: c_int) -> c_int; pub fn Mix_AllocateChannels(numchans: c_int) -> c_int; - pub fn Mix_QuerySpec(frequency: *c_int, format: *uint16_t, - channels: *c_int) -> c_int; - pub fn Mix_LoadWAV_RW(src: *SDL_RWops, freesrc: c_int) -> - *Mix_Chunk; - pub fn Mix_LoadMUS(file: *c_char) -> *Mix_Music; - pub fn Mix_LoadMUS_RW(src: *SDL_RWops, freesrc: c_int) -> - *Mix_Music; - pub fn Mix_LoadMUSType_RW(src: *SDL_RWops, _type: Mix_MusicType, - freesrc: c_int) -> *Mix_Music; - pub fn Mix_QuickLoad_WAV(mem: *uint8_t) -> *Mix_Chunk; - pub fn Mix_QuickLoad_RAW(mem: *uint8_t, len: uint32_t) -> *Mix_Chunk; - pub fn Mix_FreeChunk(chunk: *Mix_Chunk); - pub fn Mix_FreeMusic(music: *Mix_Music); + pub fn Mix_QuerySpec(frequency: *const c_int, format: *const uint16_t, + channels: *const c_int) -> c_int; + pub fn Mix_LoadWAV_RW(src: *const SDL_RWops, freesrc: c_int) -> + *const Mix_Chunk; + pub fn Mix_LoadMUS(file: *const c_char) -> *const Mix_Music; + pub fn Mix_LoadMUS_RW(src: *const SDL_RWops, freesrc: c_int) -> + *const Mix_Music; + pub fn Mix_LoadMUSType_RW(src: *const SDL_RWops, _type: Mix_MusicType, + freesrc: c_int) -> *const Mix_Music; + pub fn Mix_QuickLoad_WAV(mem: *const uint8_t) -> *const Mix_Chunk; + pub fn Mix_QuickLoad_RAW(mem: *const uint8_t, len: uint32_t) -> *const Mix_Chunk; + pub fn Mix_FreeChunk(chunk: *const Mix_Chunk); + pub fn Mix_FreeMusic(music: *const Mix_Music); pub fn Mix_GetNumChunkDecoders() -> c_int; - pub fn Mix_GetChunkDecoder(index: c_int) -> *c_char; + pub fn Mix_GetChunkDecoder(index: c_int) -> *const c_char; pub fn Mix_GetNumMusicDecoders() -> c_int; - pub fn Mix_GetMusicDecoder(index: c_int) -> *c_char; - pub fn Mix_GetMusicType(music: *Mix_Music) -> Mix_MusicType; + pub fn Mix_GetMusicDecoder(index: c_int) -> *const c_char; + pub fn Mix_GetMusicType(music: *const Mix_Music) -> Mix_MusicType; pub fn Mix_SetPostMix(mix_func: ::std::option::Option, - arg: *c_void); + arg: *const c_void); pub fn Mix_HookMusic(mix_func: ::std::option::Option, - arg: *c_void); + arg: *const c_void); pub fn Mix_HookMusicFinished(music_finished: ::std::option::Option); - pub fn Mix_GetMusicHookData() -> *c_void; + pub fn Mix_GetMusicHookData() -> *const c_void; pub fn Mix_ChannelFinished(channel_finished: ::std::option::Option); pub fn Mix_RegisterEffect(chan: c_int, f: Mix_EffectFunc_t, - d: Mix_EffectDone_t, arg: *c_void) -> c_int; + d: Mix_EffectDone_t, arg: *const c_void) -> c_int; pub fn Mix_UnregisterEffect(channel: c_int, f: Mix_EffectFunc_t) -> c_int; pub fn Mix_UnregisterAllEffects(channel: c_int) -> c_int; pub fn Mix_SetPanning(channel: c_int, left: uint8_t, right: uint8_t) -> c_int; @@ -100,18 +100,18 @@ extern "C" { pub fn Mix_GroupCount(tag: c_int) -> c_int; pub fn Mix_GroupOldest(tag: c_int) -> c_int; pub fn Mix_GroupNewer(tag: c_int) -> c_int; - pub fn Mix_PlayChannelTimed(channel: c_int, chunk: *Mix_Chunk, + pub fn Mix_PlayChannelTimed(channel: c_int, chunk: *const Mix_Chunk, loops: c_int, ticks: c_int) -> c_int; - pub fn Mix_PlayMusic(music: *Mix_Music, loops: c_int) -> c_int; - pub fn Mix_FadeInMusic(music: *Mix_Music, loops: c_int, ms: c_int) -> + pub fn Mix_PlayMusic(music: *const Mix_Music, loops: c_int) -> c_int; + pub fn Mix_FadeInMusic(music: *const Mix_Music, loops: c_int, ms: c_int) -> c_int; - pub fn Mix_FadeInMusicPos(music: *Mix_Music, loops: c_int, ms: c_int, + pub fn Mix_FadeInMusicPos(music: *const Mix_Music, loops: c_int, ms: c_int, position: c_double) -> c_int; - pub fn Mix_FadeInChannelTimed(channel: c_int, chunk: *Mix_Chunk, + pub fn Mix_FadeInChannelTimed(channel: c_int, chunk: *const Mix_Chunk, loops: c_int, ms: c_int, ticks: c_int) -> c_int; pub fn Mix_Volume(channel: c_int, volume: c_int) -> c_int; - pub fn Mix_VolumeChunk(chunk: *Mix_Chunk, volume: c_int) -> c_int; + pub fn Mix_VolumeChunk(chunk: *const Mix_Chunk, volume: c_int) -> c_int; pub fn Mix_VolumeMusic(volume: c_int) -> c_int; pub fn Mix_HaltChannel(channel: c_int) -> c_int; pub fn Mix_HaltGroup(tag: c_int) -> c_int; @@ -132,17 +132,17 @@ extern "C" { pub fn Mix_SetMusicPosition(position: c_double) -> c_int; pub fn Mix_Playing(channel: c_int) -> c_int; pub fn Mix_PlayingMusic() -> c_int; - pub fn Mix_SetMusicCMD(command: *c_char) -> c_int; + pub fn Mix_SetMusicCMD(command: *const c_char) -> c_int; pub fn Mix_SetSynchroValue(value: c_int) -> c_int; pub fn Mix_GetSynchroValue() -> c_int; - pub fn Mix_SetSoundFonts(paths: *c_char) -> c_int; - pub fn Mix_GetSoundFonts() -> *c_char; + pub fn Mix_SetSoundFonts(paths: *const c_char) -> c_int; + pub fn Mix_GetSoundFonts() -> *const c_char; pub fn Mix_EachSoundFont(function: ::std::option::Option c_int>, - data: *c_void) -> c_int; - pub fn Mix_GetChunk(channel: c_int) -> *Mix_Chunk; + data: *const c_void) -> c_int; + pub fn Mix_GetChunk(channel: c_int) -> *const Mix_Chunk; pub fn Mix_CloseAudio(); } diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 2595847f..aa92862c 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -176,7 +176,7 @@ pub fn get_chunk_decoder(index: int) -> String { /// The internal format for an audio chunk. #[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Chunk { - pub raw: *ffi::Mix_Chunk, + pub raw: *const ffi::Mix_Chunk, pub owned: bool } @@ -602,7 +602,7 @@ extern "C" fn c_music_finished_hook() { /// This is an opaque data type used for Music data. #[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] pub struct Music { - pub raw: *ffi::Mix_Music, + pub raw: *const ffi::Mix_Music, pub owned: bool, } From 62f17e9c2a7d1a1b153977e3e58f9ca83b53d255 Mon Sep 17 00:00:00 2001 From: EnvisionRed Date: Mon, 25 Aug 2014 18:22:14 -0400 Subject: [PATCH 032/114] Updated .gitignore to include Cargo.lock --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index da5ff62a..43e19a85 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ /.rust/ rusti.sh +Cargo.lock From 8d0c38972d3f0f821a6e7b6d8b527305619c2927 Mon Sep 17 00:00:00 2001 From: EnvisionRed Date: Mon, 25 Aug 2014 18:24:05 -0400 Subject: [PATCH 033/114] Updated to latest Cargo and changed package name to sdl2_mixer --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7bab181d..4da19496 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "rust-sdl2_mixer" +name = "sdl2_mixer" version = "0.0.1" readme = "README.md" authors = ["ShuYu Wang"] -[[lib]] +[lib] name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" From e0995d413e887485fe4426d98dfecef961f3e553 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 20 Oct 2014 21:31:10 +0800 Subject: [PATCH 034/114] fix build warnings and errors --- .travis.yml | 13 ++++- Cargo.toml | 8 ++- src/demo/{demo.rs => main.rs} | 17 +++--- src/sdl2_mixer/ffi.rs | 38 ++++++------- src/sdl2_mixer/lib.rs | 104 ++++++++++++++++++---------------- 5 files changed, 98 insertions(+), 82 deletions(-) rename src/demo/{demo.rs => main.rs} (76%) diff --git a/.travis.yml b/.travis.yml index 5848923f..96eb0944 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,8 @@ env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= before_install: - - wget -q http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz - - tar xf rust-nightly-x86_64-unknown-linux-gnu.tar.gz - - sudo ./rust-nightly-x86_64-unknown-linux-gnu/install.sh + - curl -sL https://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz | tar --strip-components=1 -C ~/rust -xzf - + - curl -sL https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-unknown-linux-gnu.tar.gz | tar --strip-components=1 -C ~/rust -xzf - install: - cd .. - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz @@ -25,3 +24,11 @@ script: - rustdoc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs after_script: - curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh + + + +8.03s$ +0.01s0.01s0.04s$ rustc --version +rustc 0.13.0-nightly (aff4f11cd 2014-10-19 23:32:07 +0000) +0.01s$ cargo --version +cargo 0.0.1-pre-nightly (664517e 2014-10-18 15:29:55 +0000) diff --git a/Cargo.toml b/Cargo.toml index 4da19496..394f33be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,7 @@ name = "sdl2_mixer" version = "0.0.1" -readme = "README.md" -authors = ["ShuYu Wang"] +authors = ["ShuYu Wang"] [lib] @@ -14,3 +13,8 @@ path = "src/sdl2_mixer/lib.rs" git = "https://github.com/AngryLawyer/rust-sdl2" + +[[bin]] + +name = "demo" +path = "src/demo/main.rs" diff --git a/src/demo/demo.rs b/src/demo/main.rs similarity index 76% rename from src/demo/demo.rs rename to src/demo/main.rs index 7bc63eba..0a3921ea 100644 --- a/src/demo/demo.rs +++ b/src/demo/main.rs @@ -1,7 +1,6 @@ #![crate_type = "bin"] -#![crate_id="demo"] +#![crate_name="demo"] -extern crate debug; extern crate sdl2; extern crate sdl2_mixer; @@ -13,7 +12,7 @@ fn main() { if args.len() < 2 { println!("Usage: ./demo audio.[mp3|wav|ogg]") } else { - match dump_info(&Path::new(args.get(1).to_string())) { + match dump_info(&Path::new(args[1].to_string())) { _ => () } } @@ -21,10 +20,10 @@ fn main() { fn dump_info(filename: &Path) -> Result<(), String> { println!("linked version: {}", sdl2_mixer::get_linked_version()); - sdl2::init(sdl2::InitAudio | sdl2::InitTimer); + sdl2::init(sdl2::INIT_AUDIO | sdl2::INIT_TIMER); println!("inited => {}", sdl2_mixer::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | - sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | - sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).bits()); + sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | + sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).bits()); // TODO: 0x8010 is SDL_audio flag try!(sdl2_mixer::open_audio(sdl2_mixer::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); sdl2_mixer::allocate_channels(0); @@ -33,7 +32,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { let n = sdl2_mixer::get_chunk_decoders_number(); println!("available chunk(sample) decoders: {}", n); for i in range(0, n) { - println!("| decoder {} => {:?}", i, sdl2_mixer::get_chunk_decoder(i)); + println!("| decoder {} => {:}", i, sdl2_mixer::get_chunk_decoder(i)); } } @@ -41,7 +40,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { let n = sdl2_mixer::get_music_decoders_number(); println!("available music decoders: {}", n); for i in range(0, n) { - println!("| decoder {} => {:?}", i, sdl2_mixer::get_music_decoder(i)); + println!("| decoder {} => {:}", i, sdl2_mixer::get_music_decoder(i)); } } @@ -51,7 +50,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { sdl2_mixer::Music::hook_finished(|| { println!("play ends! from rust cb") }); - println!("music => {:?}", music); + println!("music => {:}", music); println!("music type => {}", music.get_type()); println!("music volume => {}", sdl2_mixer::Music::get_volume()); diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index dc16a72b..7eb76729 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -5,12 +5,12 @@ use sdl2::rwops::ll::SDL_RWops; use sdl2::version::ll::SDL_version; pub type MIX_InitFlags = c_uint; -pub static MIX_INIT_FLAC: c_uint = 1; -pub static MIX_INIT_MOD: c_uint = 2; -pub static MIX_INIT_MODPLUG: c_uint = 4; -pub static MIX_INIT_MP3: c_uint = 8; -pub static MIX_INIT_OGG: c_uint = 16; -pub static MIX_INIT_FLUIDSYNTH: c_uint = 32; +pub const MIX_INIT_FLAC: c_uint = 1; +pub const MIX_INIT_MOD: c_uint = 2; +pub const MIX_INIT_MODPLUG: c_uint = 4; +pub const MIX_INIT_MP3: c_uint = 8; +pub const MIX_INIT_OGG: c_uint = 16; +pub const MIX_INIT_FLUIDSYNTH: c_uint = 32; pub struct Struct_Mix_Chunk { pub allocated: c_int, pub abuf: *const uint8_t, @@ -19,20 +19,20 @@ pub struct Struct_Mix_Chunk { } pub type Mix_Chunk = Struct_Mix_Chunk; pub type Mix_Fading = c_uint; -pub static MIX_NO_FADING: c_uint = 0; -pub static MIX_FADING_OUT: c_uint = 1; -pub static MIX_FADING_IN: c_uint = 2; +pub const MIX_NO_FADING: c_uint = 0; +pub const MIX_FADING_OUT: c_uint = 1; +pub const MIX_FADING_IN: c_uint = 2; pub type Mix_MusicType = c_uint; -pub static MUS_NONE: c_uint = 0; -pub static MUS_CMD: c_uint = 1; -pub static MUS_WAV: c_uint = 2; -pub static MUS_MOD: c_uint = 3; -pub static MUS_MID: c_uint = 4; -pub static MUS_OGG: c_uint = 5; -pub static MUS_MP3: c_uint = 6; -pub static MUS_MP3_MAD: c_uint = 7; -pub static MUS_FLAC: c_uint = 8; -pub static MUS_MODPLUG: c_uint = 9; +pub const MUS_NONE: c_uint = 0; +pub const MUS_CMD: c_uint = 1; +pub const MUS_WAV: c_uint = 2; +pub const MUS_MOD: c_uint = 3; +pub const MUS_MID: c_uint = 4; +pub const MUS_OGG: c_uint = 5; +pub const MUS_MP3: c_uint = 6; +pub const MUS_MP3_MAD: c_uint = 7; +pub const MUS_FLAC: c_uint = 8; +pub const MUS_MODPLUG: c_uint = 9; pub type Struct__Mix_Music = c_void; pub type Mix_Music = Struct__Mix_Music; pub type Mix_EffectFunc_t = diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index aa92862c..3c3acdf6 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -17,6 +17,7 @@ use std::default; use std::ptr; use std::mem; use std::raw; +use std::fmt; use std::c_str::CString; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; @@ -36,9 +37,7 @@ mod mac { extern {} } -#[cfg(target_os="win32")] -#[cfg(target_os="linux")] -#[cfg(target_os="freebsd")] +#[cfg(any(target_os="windows", target_os="linux", target_os="freebsd"))] mod others { #[link(name="SDL2_mixer")] extern {} @@ -52,55 +51,55 @@ mod ffi; mod ll { use libc::uint16_t; - pub static AUDIO_U8 : uint16_t = 0x0008; - pub static AUDIO_S8 : uint16_t = 0x8008; - pub static AUDIO_U16LSB : uint16_t = 0x0010; - pub static AUDIO_S16LSB : uint16_t = 0x8010; - pub static AUDIO_U16MSB : uint16_t = 0x1010; - pub static AUDIO_S16MSB : uint16_t = 0x9010; - pub static AUDIO_U16 : uint16_t = AUDIO_U16LSB; - pub static AUDIO_S16 : uint16_t = AUDIO_S16LSB; - pub static AUDIO_S32LSB : uint16_t = 0x8020; - pub static AUDIO_S32MSB : uint16_t = 0x9020; - pub static AUDIO_S32 : uint16_t = AUDIO_S32LSB; - pub static AUDIO_F32LSB : uint16_t = 0x8120; - pub static AUDIO_F32MSB : uint16_t = 0x9120; - pub static AUDIO_F32 : uint16_t = AUDIO_F32LSB; - pub static AUDIO_U16SYS : uint16_t = AUDIO_U16LSB; - pub static AUDIO_S16SYS : uint16_t = AUDIO_S16LSB; - pub static AUDIO_S32SYS : uint16_t = AUDIO_S32LSB; - pub static AUDIO_F32SYS : uint16_t = AUDIO_F32LSB; + pub const AUDIO_U8 : uint16_t = 0x0008; + pub const AUDIO_S8 : uint16_t = 0x8008; + pub const AUDIO_U16LSB : uint16_t = 0x0010; + pub const AUDIO_S16LSB : uint16_t = 0x8010; + pub const AUDIO_U16MSB : uint16_t = 0x1010; + pub const AUDIO_S16MSB : uint16_t = 0x9010; + pub const AUDIO_U16 : uint16_t = AUDIO_U16LSB; + pub const AUDIO_S16 : uint16_t = AUDIO_S16LSB; + pub const AUDIO_S32LSB : uint16_t = 0x8020; + pub const AUDIO_S32MSB : uint16_t = 0x9020; + pub const AUDIO_S32 : uint16_t = AUDIO_S32LSB; + pub const AUDIO_F32LSB : uint16_t = 0x8120; + pub const AUDIO_F32MSB : uint16_t = 0x9120; + pub const AUDIO_F32 : uint16_t = AUDIO_F32LSB; + pub const AUDIO_U16SYS : uint16_t = AUDIO_U16LSB; + pub const AUDIO_S16SYS : uint16_t = AUDIO_S16LSB; + pub const AUDIO_S32SYS : uint16_t = AUDIO_S32LSB; + pub const AUDIO_F32SYS : uint16_t = AUDIO_F32LSB; } pub type AudioFormat = uint16_t; -pub static AudioU8 : AudioFormat = ll::AUDIO_U8; -pub static AudioS8 : AudioFormat = ll::AUDIO_S8; -pub static AudioU16LSB : AudioFormat = ll::AUDIO_U16LSB; -pub static AudioS16LSB : AudioFormat = ll::AUDIO_S16LSB; -pub static AudioU16MSB : AudioFormat = ll::AUDIO_U16MSB; -pub static AudioS16MSB : AudioFormat = ll::AUDIO_S16MSB; -pub static AudioU16 : AudioFormat = ll::AUDIO_U16; -pub static AudioS16 : AudioFormat = ll::AUDIO_S16; -pub static AudioS32LSB : AudioFormat = ll::AUDIO_S32LSB; -pub static AudioS32MSB : AudioFormat = ll::AUDIO_S32MSB; -pub static AudioS32 : AudioFormat = ll::AUDIO_S32; -pub static AudioF32LSB : AudioFormat = ll::AUDIO_F32LSB; -pub static AudioF32MSB : AudioFormat = ll::AUDIO_F32MSB; -pub static AudioF32 : AudioFormat = ll::AUDIO_F32; -pub static AudioU16SYS : AudioFormat = ll::AUDIO_U16SYS; -pub static AudioS16SYS : AudioFormat = ll::AUDIO_S16SYS; -pub static AudioS32SYS : AudioFormat = ll::AUDIO_S32SYS; -pub static AudioF32SYS : AudioFormat = ll::AUDIO_F32SYS; +pub const AudioU8 : AudioFormat = ll::AUDIO_U8; +pub const AudioS8 : AudioFormat = ll::AUDIO_S8; +pub const AudioU16LSB : AudioFormat = ll::AUDIO_U16LSB; +pub const AudioS16LSB : AudioFormat = ll::AUDIO_S16LSB; +pub const AudioU16MSB : AudioFormat = ll::AUDIO_U16MSB; +pub const AudioS16MSB : AudioFormat = ll::AUDIO_S16MSB; +pub const AudioU16 : AudioFormat = ll::AUDIO_U16; +pub const AudioS16 : AudioFormat = ll::AUDIO_S16; +pub const AudioS32LSB : AudioFormat = ll::AUDIO_S32LSB; +pub const AudioS32MSB : AudioFormat = ll::AUDIO_S32MSB; +pub const AudioS32 : AudioFormat = ll::AUDIO_S32; +pub const AudioF32LSB : AudioFormat = ll::AUDIO_F32LSB; +pub const AudioF32MSB : AudioFormat = ll::AUDIO_F32MSB; +pub const AudioF32 : AudioFormat = ll::AUDIO_F32; +pub const AudioU16SYS : AudioFormat = ll::AUDIO_U16SYS; +pub const AudioS16SYS : AudioFormat = ll::AUDIO_S16SYS; +pub const AudioS32SYS : AudioFormat = ll::AUDIO_S32SYS; +pub const AudioF32SYS : AudioFormat = ll::AUDIO_F32SYS; /// The suggested default is signed 16bit samples in host byte order. -pub static DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; +pub const DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; /// Defualt channels: Stereo. -pub static DEFAULT_CHANNELS : int = 2; +pub const DEFAULT_CHANNELS : int = 2; /// Good default sample rate in Hz (samples per second) for PC sound cards. -pub static DEFAULT_FREQUENCY : int = 22050; +pub const DEFAULT_FREQUENCY : int = 22050; /// Maximum value for any volume setting. -pub static MAX_VOLUME : int = 128; +pub const MAX_VOLUME : int = 128; /// Returns the version of the dynamically linked SDL_mixer library pub fn get_linked_version() -> Version { @@ -111,12 +110,12 @@ pub fn get_linked_version() -> Version { } bitflags!(flags InitFlag : u32 { - static InitFlac = ffi::MIX_INIT_FLAC as u32, - static InitMod = ffi::MIX_INIT_MOD as u32, - static InitModPlug = ffi::MIX_INIT_MODPLUG as u32, - static InitMp3 = ffi::MIX_INIT_MP3 as u32, - static InitOgg = ffi::MIX_INIT_OGG as u32, - static InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as u32 + const InitFlac = ffi::MIX_INIT_FLAC as u32, + const InitMod = ffi::MIX_INIT_MOD as u32, + const InitModPlug = ffi::MIX_INIT_MODPLUG as u32, + const InitMp3 = ffi::MIX_INIT_MP3 as u32, + const InitOgg = ffi::MIX_INIT_OGG as u32, + const InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as u32 }) /// Loads dynamic libraries and prepares them for use. Flags should be @@ -614,6 +613,13 @@ impl Drop for Music { } } +impl fmt::Show for Music { + /// Shows the original regular expression. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "") + } +} + impl Music { /// Load music file to use. pub fn from_file(path: &Path) -> SdlResult { From 67cdfcc4743e44678515936548b845102db5b201 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 20 Oct 2014 21:34:26 +0800 Subject: [PATCH 035/114] try fix travis-ci --- .travis.yml | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 96eb0944..6f8abf7f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ +language: rust +os: + - linux env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= -before_install: - - curl -sL https://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz | tar --strip-components=1 -C ~/rust -xzf - - - curl -sL https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-unknown-linux-gnu.tar.gz | tar --strip-components=1 -C ~/rust -xzf - install: - cd .. - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz @@ -16,19 +16,10 @@ install: - cd SDL2_mixer-* - ./configure && make && sudo make install - cd - - - git clone https://github.com/andelf/rust-sdl2.git - - (cd rust-sdl2 && make) - - rustc rust-sdl2/src/sdl2/lib.rs script: - - rustc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs - - rustdoc -L. rust-sdl2_mixer/src/sdl2_mixer/lib.rs + - cd rust-sdl2_mixer + - cargo build -v + - cargo test -v + - cargo doc -v after_script: - curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh - - - -8.03s$ -0.01s0.01s0.04s$ rustc --version -rustc 0.13.0-nightly (aff4f11cd 2014-10-19 23:32:07 +0000) -0.01s$ cargo --version -cargo 0.0.1-pre-nightly (664517e 2014-10-18 15:29:55 +0000) From 7ec534afbb671f55eeeb5d4f78219d9946be7767 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 20 Oct 2014 21:44:58 +0800 Subject: [PATCH 036/114] try fix travis-ci --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 6f8abf7f..a2f94a11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ os: env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= + - LD_LIBRARY_PATH: /usr/local/lib install: - cd .. - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz From 90933660e00c079304b71fa3918992482a94a862 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 20 Oct 2014 22:31:16 +0800 Subject: [PATCH 037/114] now use rust-sdl2 style init flags --- Cargo.toml | 9 ++++----- src/demo/main.rs | 6 +++--- src/sdl2_mixer/lib.rs | 14 +++++++------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 394f33be..f1d55fee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,11 @@ authors = ["ShuYu Wang"] name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" -[dependencies.sdl2] - -git = "https://github.com/AngryLawyer/rust-sdl2" - - [[bin]] name = "demo" path = "src/demo/main.rs" + +[dependencies.sdl2] + +git = "https://github.com/AngryLawyer/rust-sdl2" diff --git a/src/demo/main.rs b/src/demo/main.rs index 0a3921ea..5ef46e73 100644 --- a/src/demo/main.rs +++ b/src/demo/main.rs @@ -21,9 +21,9 @@ fn main() { fn dump_info(filename: &Path) -> Result<(), String> { println!("linked version: {}", sdl2_mixer::get_linked_version()); sdl2::init(sdl2::INIT_AUDIO | sdl2::INIT_TIMER); - println!("inited => {}", sdl2_mixer::init(sdl2_mixer::InitMp3 | sdl2_mixer::InitFlac | - sdl2_mixer::InitMod | sdl2_mixer::InitFluidSynth | - sdl2_mixer::InitModPlug | sdl2_mixer::InitOgg).bits()); + println!("inited => {}", sdl2_mixer::init(sdl2_mixer::INIT_MP3 | sdl2_mixer::INIT_FLAC | + sdl2_mixer::INIT_MOD | sdl2_mixer::INIT_FLUIDSYNTH | + sdl2_mixer::INIT_MODPLUG | sdl2_mixer::INIT_OGG).bits()); // TODO: 0x8010 is SDL_audio flag try!(sdl2_mixer::open_audio(sdl2_mixer::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); sdl2_mixer::allocate_channels(0); diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 3c3acdf6..f0c72add 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -2,7 +2,7 @@ A binding for SDL2_mixer. */ -#![crate_id="sdl2_mixer#sdl2_mixer:0.1"] +#![crate_name = "sdl2_mixer"] #![crate_type = "lib"] #![desc = "SDL2_mixer bindings and wrappers"] #![comment = "SDL2_mixer bindings and wrappers"] @@ -110,12 +110,12 @@ pub fn get_linked_version() -> Version { } bitflags!(flags InitFlag : u32 { - const InitFlac = ffi::MIX_INIT_FLAC as u32, - const InitMod = ffi::MIX_INIT_MOD as u32, - const InitModPlug = ffi::MIX_INIT_MODPLUG as u32, - const InitMp3 = ffi::MIX_INIT_MP3 as u32, - const InitOgg = ffi::MIX_INIT_OGG as u32, - const InitFluidSynth = ffi::MIX_INIT_FLUIDSYNTH as u32 + const INIT_FLAC = ffi::MIX_INIT_FLAC as u32, + const INIT_MOD = ffi::MIX_INIT_MOD as u32, + const INIT_MODPLUG = ffi::MIX_INIT_MODPLUG as u32, + const INIT_MP3 = ffi::MIX_INIT_MP3 as u32, + const INIT_OGG = ffi::MIX_INIT_OGG as u32, + const INIT_FLUIDSYNTH = ffi::MIX_INIT_FLUIDSYNTH as u32 }) /// Loads dynamic libraries and prepares them for use. Flags should be From 47fc5fc0c56a362ca582a49b6ff7e3cd3b1d65af Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Mon, 20 Oct 2014 18:17:07 +0200 Subject: [PATCH 038/114] Upgrade to latest Rust (breaking changes) * Use `#[repr(C)]` on Struct_Mix_Chunk * Use ELEPHANT_CASE on Audio constants --- src/sdl2_mixer/ffi.rs | 1 + src/sdl2_mixer/lib.rs | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 7eb76729..5f023826 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -11,6 +11,7 @@ pub const MIX_INIT_MODPLUG: c_uint = 4; pub const MIX_INIT_MP3: c_uint = 8; pub const MIX_INIT_OGG: c_uint = 16; pub const MIX_INIT_FLUIDSYNTH: c_uint = 32; +#[repr(C)] pub struct Struct_Mix_Chunk { pub allocated: c_int, pub abuf: *const uint8_t, diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index f0c72add..f5e11328 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -73,24 +73,24 @@ mod ll { pub type AudioFormat = uint16_t; -pub const AudioU8 : AudioFormat = ll::AUDIO_U8; -pub const AudioS8 : AudioFormat = ll::AUDIO_S8; -pub const AudioU16LSB : AudioFormat = ll::AUDIO_U16LSB; -pub const AudioS16LSB : AudioFormat = ll::AUDIO_S16LSB; -pub const AudioU16MSB : AudioFormat = ll::AUDIO_U16MSB; -pub const AudioS16MSB : AudioFormat = ll::AUDIO_S16MSB; -pub const AudioU16 : AudioFormat = ll::AUDIO_U16; -pub const AudioS16 : AudioFormat = ll::AUDIO_S16; -pub const AudioS32LSB : AudioFormat = ll::AUDIO_S32LSB; -pub const AudioS32MSB : AudioFormat = ll::AUDIO_S32MSB; -pub const AudioS32 : AudioFormat = ll::AUDIO_S32; -pub const AudioF32LSB : AudioFormat = ll::AUDIO_F32LSB; -pub const AudioF32MSB : AudioFormat = ll::AUDIO_F32MSB; -pub const AudioF32 : AudioFormat = ll::AUDIO_F32; -pub const AudioU16SYS : AudioFormat = ll::AUDIO_U16SYS; -pub const AudioS16SYS : AudioFormat = ll::AUDIO_S16SYS; -pub const AudioS32SYS : AudioFormat = ll::AUDIO_S32SYS; -pub const AudioF32SYS : AudioFormat = ll::AUDIO_F32SYS; +pub const AUDIO_U8 : AudioFormat = ll::AUDIO_U8; +pub const AUDIO_S8 : AudioFormat = ll::AUDIO_S8; +pub const AUDIO_U16LSB : AudioFormat = ll::AUDIO_U16LSB; +pub const AUDIO_S16LSB : AudioFormat = ll::AUDIO_S16LSB; +pub const AUDIO_U16MSB : AudioFormat = ll::AUDIO_U16MSB; +pub const AUDIO_S16MSB : AudioFormat = ll::AUDIO_S16MSB; +pub const AUDIO_U16 : AudioFormat = ll::AUDIO_U16; +pub const AUDIO_S16 : AudioFormat = ll::AUDIO_S16; +pub const AUDIO_S32LSB : AudioFormat = ll::AUDIO_S32LSB; +pub const AUDIO_S32MSB : AudioFormat = ll::AUDIO_S32MSB; +pub const AUDIO_S32 : AudioFormat = ll::AUDIO_S32; +pub const AUDIO_F32LSB : AudioFormat = ll::AUDIO_F32LSB; +pub const AUDIO_F32MSB : AudioFormat = ll::AUDIO_F32MSB; +pub const AUDIO_F32 : AudioFormat = ll::AUDIO_F32; +pub const AUDIO_U16SYS : AudioFormat = ll::AUDIO_U16SYS; +pub const AUDIO_S16SYS : AudioFormat = ll::AUDIO_S16SYS; +pub const AUDIO_S32SYS : AudioFormat = ll::AUDIO_S32SYS; +pub const AUDIO_F32SYS : AudioFormat = ll::AUDIO_F32SYS; /// The suggested default is signed 16bit samples in host byte order. pub const DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; @@ -173,7 +173,7 @@ pub fn get_chunk_decoder(index: int) -> String { } /// The internal format for an audio chunk. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] pub struct Chunk { pub raw: *const ffi::Mix_Chunk, pub owned: bool @@ -599,7 +599,7 @@ extern "C" fn c_music_finished_hook() { } /// This is an opaque data type used for Music data. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] pub struct Music { pub raw: *const ffi::Mix_Music, pub owned: bool, From 2b75a7e6262a91eb9db5eff659bcf39eda4ea118 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 1 Nov 2014 23:26:05 +0800 Subject: [PATCH 039/114] fix compile warnings --- src/sdl2_mixer/ffi.rs | 1 + src/sdl2_mixer/lib.rs | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 7eb76729..5f023826 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -11,6 +11,7 @@ pub const MIX_INIT_MODPLUG: c_uint = 4; pub const MIX_INIT_MP3: c_uint = 8; pub const MIX_INIT_OGG: c_uint = 16; pub const MIX_INIT_FLUIDSYNTH: c_uint = 32; +#[repr(C)] pub struct Struct_Mix_Chunk { pub allocated: c_int, pub abuf: *const uint8_t, diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index f0c72add..f5e11328 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -73,24 +73,24 @@ mod ll { pub type AudioFormat = uint16_t; -pub const AudioU8 : AudioFormat = ll::AUDIO_U8; -pub const AudioS8 : AudioFormat = ll::AUDIO_S8; -pub const AudioU16LSB : AudioFormat = ll::AUDIO_U16LSB; -pub const AudioS16LSB : AudioFormat = ll::AUDIO_S16LSB; -pub const AudioU16MSB : AudioFormat = ll::AUDIO_U16MSB; -pub const AudioS16MSB : AudioFormat = ll::AUDIO_S16MSB; -pub const AudioU16 : AudioFormat = ll::AUDIO_U16; -pub const AudioS16 : AudioFormat = ll::AUDIO_S16; -pub const AudioS32LSB : AudioFormat = ll::AUDIO_S32LSB; -pub const AudioS32MSB : AudioFormat = ll::AUDIO_S32MSB; -pub const AudioS32 : AudioFormat = ll::AUDIO_S32; -pub const AudioF32LSB : AudioFormat = ll::AUDIO_F32LSB; -pub const AudioF32MSB : AudioFormat = ll::AUDIO_F32MSB; -pub const AudioF32 : AudioFormat = ll::AUDIO_F32; -pub const AudioU16SYS : AudioFormat = ll::AUDIO_U16SYS; -pub const AudioS16SYS : AudioFormat = ll::AUDIO_S16SYS; -pub const AudioS32SYS : AudioFormat = ll::AUDIO_S32SYS; -pub const AudioF32SYS : AudioFormat = ll::AUDIO_F32SYS; +pub const AUDIO_U8 : AudioFormat = ll::AUDIO_U8; +pub const AUDIO_S8 : AudioFormat = ll::AUDIO_S8; +pub const AUDIO_U16LSB : AudioFormat = ll::AUDIO_U16LSB; +pub const AUDIO_S16LSB : AudioFormat = ll::AUDIO_S16LSB; +pub const AUDIO_U16MSB : AudioFormat = ll::AUDIO_U16MSB; +pub const AUDIO_S16MSB : AudioFormat = ll::AUDIO_S16MSB; +pub const AUDIO_U16 : AudioFormat = ll::AUDIO_U16; +pub const AUDIO_S16 : AudioFormat = ll::AUDIO_S16; +pub const AUDIO_S32LSB : AudioFormat = ll::AUDIO_S32LSB; +pub const AUDIO_S32MSB : AudioFormat = ll::AUDIO_S32MSB; +pub const AUDIO_S32 : AudioFormat = ll::AUDIO_S32; +pub const AUDIO_F32LSB : AudioFormat = ll::AUDIO_F32LSB; +pub const AUDIO_F32MSB : AudioFormat = ll::AUDIO_F32MSB; +pub const AUDIO_F32 : AudioFormat = ll::AUDIO_F32; +pub const AUDIO_U16SYS : AudioFormat = ll::AUDIO_U16SYS; +pub const AUDIO_S16SYS : AudioFormat = ll::AUDIO_S16SYS; +pub const AUDIO_S32SYS : AudioFormat = ll::AUDIO_S32SYS; +pub const AUDIO_F32SYS : AudioFormat = ll::AUDIO_F32SYS; /// The suggested default is signed 16bit samples in host byte order. pub const DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; @@ -173,7 +173,7 @@ pub fn get_chunk_decoder(index: int) -> String { } /// The internal format for an audio chunk. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] pub struct Chunk { pub raw: *const ffi::Mix_Chunk, pub owned: bool @@ -599,7 +599,7 @@ extern "C" fn c_music_finished_hook() { } /// This is an opaque data type used for Music data. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving, visible_private_types)] +#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] pub struct Music { pub raw: *const ffi::Mix_Music, pub owned: bool, From 30328d8aaa6d3e19f43c4699b7f0c5bc9b341c02 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sat, 1 Nov 2014 23:48:15 +0800 Subject: [PATCH 040/114] fix README --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cdb34eda..09526a46 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ Rust-SDL2_mixer =============== -Rust bindings for SDL2_mixer +[![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) + +Rust bindings for SDL2_mixer. Requirements ------------ @@ -15,9 +17,8 @@ Installation ``` git clone https://github.com/andelf/rust-sdl2_mixer -cd rust-sdl2_mixer -rustc src/sdl2_mixer/lib.rs -# OR if you are using the mac framework version +cargo build +# TODO: OR if you are using the mac framework version rustc --cfg mac_framework src/sdl2_mixer/lib.rs ``` @@ -25,6 +26,5 @@ Demo ---- ``` -rustc -L. src/demo/demo.rs -./demo mixer.[wav|mp3|ogg] +cargo run /path/to/wav_file.wav ``` From 256bf8a45b474b1abb7522f65f8e85c89a87ca53 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 26 Nov 2014 18:04:07 +0800 Subject: [PATCH 041/114] update for crates.io --- Cargo.toml | 12 +- Makefile | 700 ----------------------------------------------------- README.md | 31 ++- build.sh | 78 ------ 4 files changed, 31 insertions(+), 790 deletions(-) delete mode 100644 Makefile delete mode 100755 build.sh diff --git a/Cargo.toml b/Cargo.toml index f1d55fee..d719e9ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,12 @@ [package] name = "sdl2_mixer" -version = "0.0.1" -authors = ["ShuYu Wang"] +description = "SDL2_mixer bindings for Rust" +repository = "https://github.com/andelf/rust-sdl2_mixer" +version = "0.0.3" +license = "MIT" +authors = [ "ShuYu Wang " ] +keywords = ["SDL", "windowing", "graphics", "music", "sound"] [lib] @@ -14,6 +18,6 @@ path = "src/sdl2_mixer/lib.rs" name = "demo" path = "src/demo/main.rs" -[dependencies.sdl2] +[dependencies] -git = "https://github.com/AngryLawyer/rust-sdl2" +sdl2 = "~0.0.3" diff --git a/Makefile b/Makefile deleted file mode 100644 index 46285597..00000000 --- a/Makefile +++ /dev/null @@ -1,700 +0,0 @@ -# Rust-Empty: An Makefile to get started with Rust -# https://github.com/bvssvni/rust-empty -# -# The MIT License (MIT) -# -# Copyright (c) 2014 Sven Nilsen -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of -# this software and associated documentation files (the "Software"), to deal in -# the Software without restriction, including without limitation the rights to -# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -# the Software, and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -SHELL := /bin/bash - -# The default make command. -# Change this to 'lib' if you are building a library. -DEFAULT = lib -# The entry file of library source. -# Change this to support multi-crate source structure. -# For advanced usage, you can rename the file 'rust-empty.mk' -# and call it with 'make -f rust-empty.mk ' from your Makefile. -LIB_ENTRY_FILE = src/sdl2_mixer/lib.rs -# The entry file of executable source. -EXE_ENTRY_FILE = src/demo/demo.rs - -EXAMPLE_FILES = examples/*.rs -SOURCE_FILES = $(shell test -e src/ && find src -type f) - -COMPILER = rustc - -# For release: - COMPILER_FLAGS = -O -# For debugging: -# COMPILER_FLAGS = -g - -UNAME=$(shell uname) - -ifeq ($(UNAME),Darwin) - # If the user wasn't explicit, see if SDL2 library exists - ifeq ("$(strip $(SDL_MODE))","") - SDL_CHECK=$(shell pkg-config --exists sdl2) - ifeq ($(SDL_CHECK),0) - SDL_MODE = dylib - else - SDL_MODE = framework - endif - endif - - ifeq ($(SDL_MODE),framework) - COMPILER_FLAGS+=--cfg mac_framework - else - COMPILER_FLAGS+=--cfg mac_dylib - endif -endif - -RUSTDOC = rustdoc - -# Extracts target from rustc. -TARGET = $(shell rustc --version verbose 2> /dev/null | awk "/host:/ { print \$$2 }") -# TARGET = x86_64-unknown-linux-gnu -# TARGET = x86_64-apple-darwin - -TARGET_LIB_DIR = target/deps/ - -# Ask 'rustc' the file name of the library and use a dummy name if the source has not been created yet. -# The dummy file name is used to trigger the creation of the source first time. -# Next time 'rustc' will return the right file name. -RLIB_FILE = $(shell (rustc --crate-type=rlib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.rlib")) -# You can't have quotes around paths because 'make' doesn't see it exists. -RLIB = target/$(RLIB_FILE) -DYLIB_FILE = $(shell (rustc --crate-type=dylib --crate-file-name "$(LIB_ENTRY_FILE)" 2> /dev/null) || (echo "dummy.dylib")) -DYLIB = target/$(DYLIB_FILE) - -# Use 'VERBOSE=1' to echo all commands, for example 'make help VERBOSE=1'. -ifdef VERBOSE - Q := -else - Q := @ -endif - -all: $(DEFAULT) - -help: - $(Q)echo "--- rust-empty (0.6 003)" - $(Q)echo "make run - Runs executable" - $(Q)echo "make exe - Builds main executable" - $(Q)echo "make lib - Both static and dynamic library" - $(Q)echo "make rlib - Static library" - $(Q)echo "make dylib - Dynamic library" - $(Q)echo "make test - Tests library internally and externally" - $(Q)echo "make test-internal - Tests library internally" - $(Q)echo "make test-external - Tests library externally" - $(Q)echo "make bench - Benchmarks library internally and externally" - $(Q)echo "make bench-internal - Benchmarks library internally" - $(Q)echo "make bench-external - Benchmarks library externally" - $(Q)echo "make doc - Builds documentation for library" - $(Q)echo "make git-ignore - Setup files to be ignored by Git" - $(Q)echo "make examples - Builds examples" - $(Q)echo "make cargo-lite-exe - Setup executable package" - $(Q)echo "make cargo-lite-lib - Setup library package" - $(Q)echo "make cargo-exe - Setup executable package" - $(Q)echo "make cargo-lib - Setup library package" - $(Q)echo "make rust-ci-lib - Setup Travis CI Rust library" - $(Q)echo "make rust-ci-exe - Setup Travis CI Rust executable" - $(Q)echo "make rusti - Setup 'rusti.sh' for interactive Rust" - $(Q)echo "make watch - Setup 'watch.sh' for compilation on save" - $(Q)echo "make loc - Count lines of code in src folder" - $(Q)echo "make nightly-install - Installs Rust nightly build" - $(Q)echo "make nightly-uninstall - Uninstalls Rust nightly build" - $(Q)echo "make clean - Deletes binaries and documentation." - $(Q)echo "make clear-project - WARNING: Deletes project files except 'Makefile'" - $(Q)echo "make clear-git - WARNING: Deletes Git setup" - $(Q)echo "make symlink-build - Creates a script for building dependencies" - $(Q)echo "make symlink-info - Symlinked libraries dependency info" - $(Q)echo "make target-dir - Creates directory for current target" - -.PHONY: \ - bench \ - bench-internal \ - bench-external \ - cargo-lib \ - cargo-exe \ - cargo-lite-lib \ - cargo-lite-exe \ - clean \ - clear-git \ - clear-project \ - loc \ - nightly-install \ - nightly-uninstall \ - run \ - rusti \ - rust-ci-lib \ - rust-ci-exe \ - symlink-build \ - symlink-info \ - target-dir \ - test \ - test-internal \ - test-external \ - watch - -nightly-install: - $(Q)cd ~ \ - && curl -s http://www.rust-lang.org/rustup.sh > rustup.sh \ - && ( \ - echo "Rust install-script stored as '~/rustup.sh'" ; \ - read -p "Do you want to install? [y/n]:" -n 1 -r ; \ - echo "" ; \ - if [[ $$REPLY =~ ^[Yy]$$ ]] ; \ - then \ - cat rustup.sh | sudo sh ; \ - fi \ - ) - -nightly-uninstall: - $(Q)cd ~ \ - && curl -s http://www.rust-lang.org/rustup.sh > rustup.sh \ - && ( \ - echo "Rust install-script stored as '~/rustup.sh'" ; \ - read -p "Do you want to uninstall? [y/n]:" -n 1 -r ; \ - echo "" ; \ - if [[ $$REPLY =~ ^[Yy]$$ ]] ; \ - then \ - cat rustup.sh | sudo sh -s -- --uninstall ; \ - fi \ - ) - -cargo-lite-exe: $(EXE_ENTRY_FILE) - $(Q)( \ - test -e cargo-lite.conf \ - && echo "--- The file 'cargo-lite.conf' already exists" \ - ) \ - || \ - ( \ - echo -e "deps = [\n]\n\n[build]\ncrate_root = \"$(EXE_ENTRY_FILE)\"\nrustc_args = []\n" > cargo-lite.conf \ - && echo "--- Created 'cargo-lite.conf' for executable" \ - && cat cargo-lite.conf \ - ) - -cargo-lite-lib: $(LIB_ENTRY_FILE) - $(Q)( \ - test -e cargo-lite.conf \ - && echo "--- The file 'cargo-lite.conf' already exists" \ - ) \ - || \ - ( \ - echo -e "deps = [\n]\n\n[build]\ncrate_root = \"$(LIB_ENTRY_FILE)\"\ncrate_type = \"library\"\nrustc_args = []\n" > cargo-lite.conf \ - && echo "--- Created 'cargo-lite.conf' for library" \ - && cat cargo-lite.conf \ - ) - -cargo-exe: $(EXE_ENTRY_FILE) - $(Q)( \ - test -e Cargo.toml \ - && echo "--- The file 'Cargo.toml' already exists" \ - ) \ - || \ - ( \ - name=$${PWD##/*/} ; \ - readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[bin]]\n\nname = \"$$name\"\npath = \"$(EXE_ENTRY_FILE)\"\n" > Cargo.toml \ - && echo "--- Created 'Cargo.toml' for executable" \ - && cat Cargo.toml \ - ) - -cargo-lib: $(LIB_ENTRY_FILE) - $(Q)( \ - test -e Cargo.toml \ - && echo "--- The file 'Cargo.toml' already exists" \ - ) \ - || \ - ( \ - name=$${PWD##/*/} ; \ - readme=$$((test -e README.md && echo -e "readme = \"README.md\"") || ("")) ; \ - echo -e "[package]\n\nname = \"$$name\"\nversion = \"0.0.0\"\n$$readme\nauthors = [\"Your Name \"]\ntags = []\n\n[[lib]]\n\nname = \"$$name\"\npath = \"$(LIB_ENTRY_FILE)\"\n" > Cargo.toml \ - && echo "--- Created 'Cargo.toml' for executable" \ - && cat Cargo.toml \ - ) - -rust-ci-lib: $(LIB_ENTRY_FILE) - $(Q)( \ - test -e .travis.yml \ - && echo "--- The file '.travis.yml' already exists" \ - ) \ - || \ - ( \ - echo -e "install:\n - wget http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz -O - | sudo tar zxf - --strip-components 1 -C /usr/local\nscript:\n - make lib\n" > .travis.yml \ - && echo "--- Created '.travis.yml' for library" \ - && cat .travis.yml \ - ) - -rust-ci-exe: $(EXE_ENTRY_FILE) - $(Q)( \ - test -e .travis.yml \ - && echo "--- The file '.travis.yml' already exists" \ - ) \ - || \ - ( \ - echo -e "install:\n - wget http://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz -O - | sudo tar zxf - --strip-components 1 -C /usr/local\nscript:\n - make exe\n" > .travis.yml \ - && echo "--- Created '.travis.yml' for executable" \ - && cat .travis.yml \ - ) - -doc: $(SOURCE_FILES) | src/ - $(Q)$(RUSTDOC) $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" \ - && echo "--- Built documentation" - -run: exe - $(Q)cd bin/ \ - && ./main - -target-dir: $(TARGET_LIB_DIR) - -exe: bin/main | $(TARGET_LIB_DIR) - -bin/main: $(SOURCE_FILES) | bin/ $(EXE_ENTRY_FILE) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $(EXE_ENTRY_FILE) -o bin/main -L "$(TARGET_LIB_DIR)" -L "target" \ - && echo "--- Built executable" \ - && echo "--- Type 'make run' to run executable" - -test: test-internal test-external - $(Q)echo "--- Internal tests succeeded" \ - && echo "--- External tests succeeded" - -test-external: bin/test-external - $(Q)cd "bin/" \ - && ./test-external - -bin/test-external: $(SOURCE_FILES) | rlib bin/ src/test.rs - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test src/test.rs -o bin/test-external -L "$(TARGET_LIB_DIR)" -L "target" \ - && echo "--- Built external test runner" - -test-internal: bin/test-internal - $(Q)cd "bin/" \ - && ./test-internal - -bin/test-internal: $(SOURCE_FILES) | rlib src/ bin/ - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --test $(LIB_ENTRY_FILE) -o bin/test-internal -L "$(TARGET_LIB_DIR)" -L "target" \ - && echo "--- Built internal test runner" - -bench: bench-internal bench-external - -bench-external: test-external - $(Q)bin/test-external --bench - -bench-internal: test-internal - $(Q)bin/test-internal --bench - -lib: rlib dylib - $(Q)echo "--- Type 'make test' to test library" - -rlib: $(RLIB) - -$(RLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=rlib $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" --out-dir "target" \ - && echo "--- Built rlib" - -dylib: $(DYLIB) - -$(DYLIB): $(SOURCE_FILES) | $(LIB_ENTRY_FILE) $(TARGET_LIB_DIR) - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) --crate-type=dylib $(LIB_ENTRY_FILE) -L "$(TARGET_LIB_DIR)" --out-dir "target/" \ - && echo "--- Built dylib" - -bin/: - $(Q)mkdir -p bin - -$(TARGET_LIB_DIR): - $(Q)mkdir -p $(TARGET_LIB_DIR) - -src/: - $(Q)mkdir -p src - -examples-dir: - $(Q)test -e examples \ - || \ - ( \ - mkdir examples \ - && echo -e "fn main() {\n\tprintln!(\"Hello!\");\n}\n" > examples/hello.rs \ - && echo "--- Created examples folder" \ - ) - -rust-dir: - $(Q)mkdir -p .rust - -git-ignore: - $(Q)( \ - test -e .gitignore \ - && echo "--- The file '.gitignore' already exists" \ - ) \ - || \ - ( \ - echo -e ".DS_Store\n*~\n*#\n*.o\n*.so\n*.swp\n*.dylib\n*.dSYM\n*.dll\n*.rlib\n*.dummy\n*.exe\n*-test\n/bin/main\n/bin/test-internal\n/bin/test-external\n/doc/\n/target/\n/build/\n/.rust/\nrusti.sh\nwatch.sh\n/examples/**\n!/examples/*.rs\n!/examples/assets/" > .gitignore \ - && echo "--- Created '.gitignore' for git" \ - && cat .gitignore \ - ) - -examples: $(EXAMPLE_FILES) - -$(EXAMPLE_FILES): lib examples-dir - $(Q)$(COMPILER) --target "$(TARGET)" $(COMPILER_FLAGS) $@ -L "$(TARGET_LIB_DIR)" -L "target" --out-dir examples/ \ - && echo "--- Built '$@' (make $@)" - -$(EXE_ENTRY_FILE): | src/ - $(Q)test -e $(EXE_ENTRY_FILE) \ - || \ - ( \ - echo -e "fn main() {\n\tprintln!(\"Hello world!\");\n}" > $(EXE_ENTRY_FILE) \ - ) - -src/test.rs: | src/ - $(Q)test -e src/test.rs \ - || \ - ( \ - touch src/test.rs \ - ) - -$(LIB_ENTRY_FILE): | src/ - $(Q)test -e $(LIB_ENTRY_FILE) \ - || \ - ( \ - echo -e "#![crate_id = \"\"]\n#![deny(missing_doc)]\n\n//! Documentation goes here.\n" > $(LIB_ENTRY_FILE) \ - ) - -clean: - $(Q)rm -f "$(RLIB)" - $(Q)rm -f "$(DYLIB)" - $(Q)rm -rf "doc/" - $(Q)rm -f "bin/main" - $(Q)rm -f "bin/test-internal" - $(Q)rm -f "bin/test-external" - $(Q)echo "--- Deleted binaries and documentation" - -clear-project: - $(Q)rm -f ".symlink-info" - $(Q)rm -f "cargo-lite.conf" - $(Q)rm -f "Cargo.toml" - $(Q)rm -f ".travis.yml" - $(Q)rm -f "rusti.sh" - $(Q)rm -f "watch.sh" - $(Q)rm -rf "target/" - $(Q)rm -rf "src/" - $(Q)rm -rf "bin/" - $(Q)rm -rf "examples/" - $(Q)rm -rf "doc/" - $(Q)echo "--- Removed all source files, binaries and documentation" \ - && echo "--- Content in project folder" \ - && ls -a - -clear-git: - $(Q)rm -f ".gitignore" - $(Q)rm -rf ".git" - $(Q)echo "--- Removed Git" \ - && echo "--- Content in project folder" \ - && ls -a - -# borrowed from http://stackoverflow.com/q/649246/1256624 -define RUSTI_SCRIPT -#!/bin/bash - -#written by mcpherrin - -while true; do - echo -n "> " - read line - TMP="`mktemp r.XXXXXX`" - $(COMPILER) - -o $$TMP -L "$(TARGET_LIB_DIR)" < rusti.sh \ - && chmod +x rusti.sh \ - && echo "--- Created 'rusti.sh'" \ - && echo "--- Type './rusti.sh' to start interactive Rust" \ - ) - -# borrowed from http://stackoverflow.com/q/649246/1256624 -define WATCH_SCRIPT -#!/bin/bash - -#written by zzmp - -# This script will recompile a rust project using `make` -# every time something in the specified directory changes. - -# Watch files in infinite loop -watch () { - UNAME=$$(uname) - if [ -e "$$2" ]; then - echo "Watching files in $$2.." - CTIME=$$(date "+%s") - while :; do - sleep 1 - for f in `find $$2 -type f -name "*.rs"`; do - if [[ $$UNAME == "Darwin" ]]; then - st_mtime=$$(stat -f "%m" "$$f") - elif [[ $$UNAME == "FreeBSD" ]]; then - st_mtime=$$(stat -f "%m" "$$f") - else - st_mtime=$$(stat -c "%Y" "$$f") - fi - if [ $$st_mtime -gt $$CTIME ]; then - CTIME=$$(date "+%s") - echo "~~~ Rebuilding" - $$1 - if [ ! $$? -eq 0 ]; then - echo "" - fi - fi - done - done - else - echo "$$2 is not a valid directory" - fi -} - -# Capture user input with defaults -CMD=$${1:-make} -DIR=$${2:-src} - -if [ $${CMD:0:2} = '-h' ]; then -echo ' -This script will recompile a rust project using `make` -every time something in the specified directory changes. - -Use: ./watch.sh [CMD] [DIR] -Example: ./watch.sh "make run" src - -CMD: Command to execute - Complex commands may be passed as strings - `make` by default -DIR: Directory to watch - src by default - -If DIR is supplied, CMD must be as well.\n' -else - watch "$$CMD" "$$DIR" -fi - -endef -export WATCH_SCRIPT - -watch: $(TARGET_LIB_DIR) - $(Q)( \ - test -e watch.sh \ - && echo "--- The file 'watch.sh' already exists" \ - ) \ - || \ - ( \ - echo -e "$$WATCH_SCRIPT" > watch.sh \ - && chmod +x watch.sh \ - && echo "--- Created 'watch.sh'" \ - && echo "--- Type './watch.sh' to start compilation on save" \ - && echo "--- Type './watch.sh -h' for more options" \ - ) - -# borrowed from http://stackoverflow.com/q/649246/1256624 -define SYMLINK_BUILD_SCRIPT -#!/bin/bash -# written by bvssvni -# Modify the setting to do conditional compilation. -# For example "--cfg my_feature" -SETTINGS="" -# ================================================ - -MAKE=make -if [ "$$OS" == "Windows_NT" ]; then - MAKE=mingw32-make -fi - -# Checks if an item exists in an array. -# Copied from http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value -function contains() { - local n=$$# - local value=$${!n} - for ((i=1;i < $$#;i++)) { - if [ "$${!i}" == "$${value}" ]; then - echo "y" - return 0 - fi - } - echo "n" - return 1 -} - -# This is a counter used to insert dependencies. -# It is global because we need an array of all the -# visited dependencies. -i=0 -function build_deps { - local current=$$(pwd) - for symlib in $$(find $(TARGET_LIB_DIR) -type l) ; do - cd $$current - echo $$symlib - local original_file=$$(readlink $$symlib) - local original_dir=$$(dirname $$original_file) - cd $$original_dir - - # Go to the git root directory. - local current_git_dir=$$(git rev-parse --show-toplevel) - echo "--- Parent $$current" - echo "--- Child $$current_git_dir" - cd $$current_git_dir - - # Skip building if it is already built. - if [ $$(contains "$${git_dir[@]}" $$current_git_dir) == "y" ]; then - echo "--- Visited $$current_git_dir" - continue - fi - - # Remember git directory to not build it twice - git_dir[i]=$$current_git_dir - let i+=1 - - # Visit the symlinks and build the dependencies - build_deps - - # First check for a 'build.sh' script with default settings. - # Check for additional 'rust-empty.mk' file. \ - # Compile with the settings flags. \ - # If no other options, build with make. - ( \ - test -e build.sh \ - && ./build.sh \ - ) \ - || \ - ( \ - test -e rust-empty.mk \ - && $$MAKE -f rust-empty.mk clean \ - && $$MAKE -f rust-empty.mk \ - ) \ - || \ - ( \ - echo "--- Building $$current_git_dir" \ - && $$MAKE clean \ - && $$MAKE \ - ) - done - cd $$current -} - -# Mark main project as visited to avoid infinite loop. -git_dir[i]=$$(pwd) -let i+=1 -if [ "$$1" == "deps" ]; then - build_deps -fi - -echo "--- Building $$(pwd)" -( \ - test -e rust-empty.mk \ - && $$MAKE -f rust-empty.mk clean \ - && $$MAKE -f rust-empty.mk COMPILER_FLAGS+="$$SETTINGS" \ -) \ -|| \ -( \ - $$MAKE clean - $$MAKE COMPILER_FLAGS+="$$SETTINGS" -) - -endef -export SYMLINK_BUILD_SCRIPT - -symlink-build: - $(Q)( \ - test -e build.sh \ - && echo "--- The file 'build.sh' already exists" \ - ) \ - || \ - ( \ - echo -e "$$SYMLINK_BUILD_SCRIPT" > build.sh \ - && chmod +x build.sh \ - && echo "--- Created 'build.sh'" \ - && echo "--- Type './build.sh deps' to build everything" \ - ) - -loc: - $(Q)echo "--- Counting lines of .rs files in 'src' (LOC):" \ - && find src/ -type f -name "*.rs" -exec cat {} \; | wc -l - -# Finds the original locations of symlinked libraries and -# prints the commit hash with remote branches containing that commit. -symlink-info: - $(Q) current=$$(pwd) ; \ - for symlib in $$(find $(TARGET_LIB_DIR) -type l) ; do \ - cd $$current ; \ - echo $$symlib ; \ - original_file=$$(readlink $$symlib) ; \ - original_dir=$$(dirname $$original_file) ; \ - cd $$original_dir ; \ - commit=$$(git rev-parse HEAD) ; \ - echo $$commit ; \ - echo "origin:" ; \ - git config --get remote.origin.url ; \ - echo "upstream:" ; \ - git config --get remote.upstream.url ; \ - echo "available in remote branches:" ; \ - git branch -r --contains $$commit ; \ - echo "" ; \ - done \ - > .symlink-info \ - && cd $$current \ - && echo "--- Created '.symlink-info'" \ - && cat .symlink-info diff --git a/README.md b/README.md index 09526a46..8a812ae3 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,21 @@ -Rust-SDL2_mixer -=============== +# Rust-SDL2_mixer + [![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) Rust bindings for SDL2_mixer. +## Installation + Requirements ------------- * [Rust-sdl2](https://github.com/AngryLawyer/rust-sdl2) * sdl_mixer 2.0 development libraries -* Rust master +* Rust nightly +* Cargo nightly -Installation ------------- + +### normal ``` git clone https://github.com/andelf/rust-sdl2_mixer @@ -22,8 +24,21 @@ cargo build rustc --cfg mac_framework src/sdl2_mixer/lib.rs ``` -Demo ----- +### cargo + +``` +[dependencies] +sdl2_mixer = "$version-here$" +``` + +or + +``` +[dependencies.sdl2_mixer] +git = "https://github.com/andelf/rust-sdl2_mixer" +``` + +## Demo ``` cargo run /path/to/wav_file.wav diff --git a/build.sh b/build.sh deleted file mode 100755 index da9b733c..00000000 --- a/build.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash -# written by bvssvni -# Modify the setting to do conditional compilation. -# For example "--cfg my_feature" -SETTINGS="" -# ================================================ - -MAKE=make -if [ "$OS" == "Windows_NT" ]; then - MAKE=mingw32-make -fi - -# Checks if an item exists in an array. -# Copied from http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value -function contains() { - local n=$# - local value=${!n} - for ((i=1;i < $#;i++)) { - if [ "${!i}" == "${value}" ]; then - echo "y" - return 0 - fi - } - echo "n" - return 1 -} - -# This is a counter used to insert dependencies. -# It is global because we need an array of all the -# visited dependencies. -i=0 -function build_deps { - local current=$(pwd) - for symlib in $(find target/deps/ -type l) ; do - cd $current - echo $symlib - local original_file=$(readlink $symlib) - local original_dir=$(dirname $original_file) - cd $original_dir - - # Go to the git root directory. - local current_git_dir=$(git rev-parse --show-toplevel) - echo "--- Parent $current" - echo "--- Child $current_git_dir" - cd $current_git_dir - - # Skip building if it is already built. - if [ $(contains "${git_dir[@]}" $current_git_dir) == "y" ]; then - echo "--- Visited $current_git_dir" - continue - fi - - # Remember git directory to not build it twice - git_dir[i]=$current_git_dir - let i+=1 - - # Visit the symlinks and build the dependencies - build_deps - - # First check for a 'build.sh' script with default settings. - # Check for additional 'rust-empty.mk' file. # Compile with the settings flags. # If no other options, build with make. - ( test -e build.sh && ./build.sh ) || ( test -e rust-empty.mk && $MAKE -f rust-empty.mk clean && $MAKE -f rust-empty.mk ) || ( echo "--- Building $current_git_dir" && $MAKE clean && $MAKE ) - done - cd $current -} - -# Mark main project as visited to avoid infinite loop. -git_dir[i]=$(pwd) -let i+=1 -if [ "$1" == "deps" ]; then - build_deps -fi - -echo "--- Building $(pwd)" -( test -e rust-empty.mk && $MAKE -f rust-empty.mk clean && $MAKE -f rust-empty.mk COMPILER_FLAGS+="$SETTINGS" ) || ( $MAKE clean - $MAKE COMPILER_FLAGS+="$SETTINGS" -) - From 2348da92035e4b18c07dc4a0d97f1032242f9f35 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Tue, 23 Dec 2014 19:05:26 +0100 Subject: [PATCH 042/114] Upgraded to latest Rust * Removed unused crate attributes (license is in Cargo.toml) * Added missing `Copy` impls * Explicit coercion to fn * Replaced `into_string` with `to_string` --- src/sdl2_mixer/lib.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index f5e11328..65713712 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -4,9 +4,6 @@ A binding for SDL2_mixer. #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] -#![desc = "SDL2_mixer bindings and wrappers"] -#![comment = "SDL2_mixer bindings and wrappers"] -#![license = "MIT"] #![feature(globs, macro_rules)] @@ -116,7 +113,7 @@ bitflags!(flags InitFlag : u32 { const INIT_MP3 = ffi::MIX_INIT_MP3 as u32, const INIT_OGG = ffi::MIX_INIT_OGG as u32, const INIT_FLUIDSYNTH = ffi::MIX_INIT_FLUIDSYNTH as u32 -}) +}); /// Loads dynamic libraries and prepares them for use. Flags should be /// one or more flags from InitFlag. @@ -168,7 +165,7 @@ pub fn get_chunk_decoders_number() -> int { pub fn get_chunk_decoder(index: int) -> String { unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().into_string() + CString::new(name, false).as_str().unwrap().to_string() } } @@ -240,7 +237,7 @@ impl LoaderRWops for RWops { /// Fader effect type enumerations #[repr(C)] -#[deriving(Clone, PartialEq, Hash, Show, FromPrimitive)] +#[deriving(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum Fading { NoFading = ffi::MIX_NO_FADING as int, FadingOut = ffi::MIX_FADING_OUT as int, @@ -248,7 +245,7 @@ pub enum Fading { } /// Sound effect channel. -#[deriving(PartialEq, Show)] +#[deriving(Copy, PartialEq, Show)] pub struct Channel(int); /// Set the number of channels being mixed. @@ -276,7 +273,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { pub fn set_channel_finished(f: |Channel|:'static) { unsafe { channel_finished_callback = Some(mem::transmute::<_, raw::Closure>(f)); - ffi::Mix_ChannelFinished(Some(c_channel_finished_callback)); + ffi::Mix_ChannelFinished(Some(c_channel_finished_callback as extern "C" fn (ch: c_int))); } } @@ -479,6 +476,7 @@ pub fn reserve_channels(num: int) -> int { } /// Sound effect channel grouping. +#[deriving(Copy)] pub struct Group(int); impl default::Default for Group { @@ -565,13 +563,13 @@ pub fn get_music_decoders_number() -> int { pub fn get_music_decoder(index: int) -> String { unsafe { let name = ffi::Mix_GetMusicDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().into_string() + CString::new(name, false).as_str().unwrap().to_string() } } /// Music type enumerations #[repr(C)] -#[deriving(Clone, PartialEq, Hash, Show, FromPrimitive)] +#[deriving(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum MusicType { MusicNone = ffi::MUS_NONE as int, MusicCmd = ffi::MUS_CMD as int, @@ -624,7 +622,7 @@ impl Music { /// Load music file to use. pub fn from_file(path: &Path) -> SdlResult { let raw = unsafe { - ffi::Mix_LoadMUS(path.to_c_str().unwrap()) + ffi::Mix_LoadMUS(path.to_c_str().into_inner()) }; if raw.is_null() { Err(get_error()) @@ -716,7 +714,7 @@ impl Music { /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> SdlResult<()> { let ret = unsafe { - ffi::Mix_SetMusicCMD(command.to_c_str().unwrap()) + ffi::Mix_SetMusicCMD(command.to_c_str().into_inner()) }; if ret == -1 { Err(get_error()) @@ -748,7 +746,7 @@ impl Music { pub fn hook_finished(f: ||) { unsafe { music_finished_hook = Some(mem::transmute(f)); - ffi::Mix_HookMusicFinished(Some(c_music_finished_hook)); + ffi::Mix_HookMusicFinished(Some(c_music_finished_hook as extern "C" fn ())); } } From 94af9a42a8bd47a438fa0abba3ca0ce51cb60b19 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Tue, 30 Dec 2014 22:09:05 +0100 Subject: [PATCH 043/114] Point sdl2 to git Closes https://github.com/andelf/rust-sdl2_mixer/issues/15 --- Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d719e9ac..f1deb1fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ path = "src/sdl2_mixer/lib.rs" name = "demo" path = "src/demo/main.rs" -[dependencies] +[dependencies.sdl2] + +git = "https://github.com/AngryLawyer/rust-sdl2" +#version = "~0.0.3" -sdl2 = "~0.0.3" From 048e4d55a91400caa97b2c3e17b7aa53f026bb2e Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Sat, 17 Jan 2015 23:14:39 +0100 Subject: [PATCH 044/114] Upgrade to latest Rust * Replaced closure callback with normal function (See https://github.com/andelf/rust-sdl2_mixer/issues/19) --- src/demo/main.rs | 30 ++++---- src/sdl2_mixer/lib.rs | 163 +++++++++++++++++++++--------------------- 2 files changed, 98 insertions(+), 95 deletions(-) diff --git a/src/demo/main.rs b/src/demo/main.rs index 5ef46e73..ecc91c89 100644 --- a/src/demo/main.rs +++ b/src/demo/main.rs @@ -5,21 +5,21 @@ extern crate sdl2; extern crate sdl2_mixer; use std::os; - +use std::borrow::ToOwned; fn main() { let args = os::args(); if args.len() < 2 { println!("Usage: ./demo audio.[mp3|wav|ogg]") } else { - match dump_info(&Path::new(args[1].to_string())) { + match dump_info(&Path::new(args[1].to_owned())) { _ => () } } } fn dump_info(filename: &Path) -> Result<(), String> { - println!("linked version: {}", sdl2_mixer::get_linked_version()); + println!("linked version: {:?}", sdl2_mixer::get_linked_version()); sdl2::init(sdl2::INIT_AUDIO | sdl2::INIT_TIMER); println!("inited => {}", sdl2_mixer::init(sdl2_mixer::INIT_MP3 | sdl2_mixer::INIT_FLAC | sdl2_mixer::INIT_MOD | sdl2_mixer::INIT_FLUIDSYNTH | @@ -31,7 +31,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { { let n = sdl2_mixer::get_chunk_decoders_number(); println!("available chunk(sample) decoders: {}", n); - for i in range(0, n) { + for i in (0..n) { println!("| decoder {} => {:}", i, sdl2_mixer::get_chunk_decoder(i)); } } @@ -39,31 +39,35 @@ fn dump_info(filename: &Path) -> Result<(), String> { { let n = sdl2_mixer::get_music_decoders_number(); println!("available music decoders: {}", n); - for i in range(0, n) { + for i in (0..n) { println!("| decoder {} => {:}", i, sdl2_mixer::get_music_decoder(i)); } } - println!("query spec => {}", sdl2_mixer::query_spec()); + println!("query spec => {:?}", sdl2_mixer::query_spec()); let music = sdl2_mixer::Music::from_file(filename).unwrap(); - sdl2_mixer::Music::hook_finished(|| { println!("play ends! from rust cb") }); + fn hook_finished() { + println!("play ends! from rust cb"); + } - println!("music => {:}", music); - println!("music type => {}", music.get_type()); + sdl2_mixer::Music::hook_finished(hook_finished); - println!("music volume => {}", sdl2_mixer::Music::get_volume()); + println!("music => {:?}", music); + println!("music type => {:?}", music.get_type()); - println!("play => {}", music.play(1)); + println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); + + println!("play => {:?}", music.play(1)); sdl2::timer::delay(10000); - println!("fading out ... {}", sdl2_mixer::Music::fade_out(4000)); + println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); sdl2::timer::delay(5000); - println!("fading in from pos ... {}", music.fade_in_from_pos(1, 10000, 100.0)); + println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); sdl2::timer::delay(5000); diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 65713712..a03b0452 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -5,8 +5,6 @@ A binding for SDL2_mixer. #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] -#![feature(globs, macro_rules)] - extern crate libc; extern crate sdl2; @@ -15,7 +13,10 @@ use std::ptr; use std::mem; use std::raw; use std::fmt; -use std::c_str::CString; +use std::ffi::{ c_str_to_bytes, CString }; +use std::str::from_utf8; +use std::borrow::ToOwned; +use std::num::FromPrimitive; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; @@ -92,11 +93,11 @@ pub const AUDIO_F32SYS : AudioFormat = ll::AUDIO_F32SYS; /// The suggested default is signed 16bit samples in host byte order. pub const DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; /// Defualt channels: Stereo. -pub const DEFAULT_CHANNELS : int = 2; +pub const DEFAULT_CHANNELS : isize = 2; /// Good default sample rate in Hz (samples per second) for PC sound cards. -pub const DEFAULT_FREQUENCY : int = 22050; +pub const DEFAULT_FREQUENCY : isize = 22050; /// Maximum value for any volume setting. -pub const MAX_VOLUME : int = 128; +pub const MAX_VOLUME : isize = 128; /// Returns the version of the dynamically linked SDL_mixer library pub fn get_linked_version() -> Version { @@ -130,7 +131,7 @@ pub fn quit() { } /// Open the mixer with a certain audio format. -pub fn open_audio(frequency: int, format: AudioFormat, channels: int, chunksize: int) -> SdlResult<()> { +pub fn open_audio(frequency: isize, format: AudioFormat, channels: isize, chunksize: isize) -> SdlResult<()> { let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; if ret == 0 { Ok(()) } else { Err(get_error()) } @@ -142,7 +143,7 @@ pub fn close_audio() { } /// Get the actual audio format in use by the opened audio device. -pub fn query_spec() -> SdlResult<(int, AudioFormat, int)> { +pub fn query_spec() -> SdlResult<(isize, AudioFormat, isize)> { let frequency : c_int = 0; let format : uint16_t = 0; let channels : c_int = 0; @@ -150,27 +151,27 @@ pub fn query_spec() -> SdlResult<(int, AudioFormat, int)> { if ret == 0 { Err(get_error()) } else { - Ok((frequency as int, format as AudioFormat, channels as int)) + Ok((frequency as isize, format as AudioFormat, channels as isize)) } } // 4.2 Samples /// Get the number of sample chunk decoders available from the Mix_GetChunkDecoder function. -pub fn get_chunk_decoders_number() -> int { - unsafe { ffi::Mix_GetNumChunkDecoders() as int } +pub fn get_chunk_decoders_number() -> isize { + unsafe { ffi::Mix_GetNumChunkDecoders() as isize } } /// Get the name of the indexed sample chunk decoder. -pub fn get_chunk_decoder(index: int) -> String { +pub fn get_chunk_decoder(index: isize) -> String { unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().to_string() + from_utf8(c_str_to_bytes(&name)).unwrap().to_owned() } } /// The internal format for an audio chunk. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] +#[derive(PartialEq)] #[allow(raw_pointer_derive)] pub struct Chunk { pub raw: *const ffi::Mix_Chunk, pub owned: bool @@ -198,16 +199,16 @@ impl Chunk { } /// Set chunk->volume to volume. - pub fn set_volume(&mut self, volume: int) -> int { + pub fn set_volume(&mut self, volume: isize) -> isize { unsafe { - ffi::Mix_VolumeChunk(self.raw, volume as c_int) as int + ffi::Mix_VolumeChunk(self.raw, volume as c_int) as isize } } /// current volume for the chunk. - pub fn get_volume(&self) -> int { + pub fn get_volume(&self) -> isize { unsafe { - ffi::Mix_VolumeChunk(self.raw, -1) as int + ffi::Mix_VolumeChunk(self.raw, -1) as isize } } } @@ -237,16 +238,16 @@ impl LoaderRWops for RWops { /// Fader effect type enumerations #[repr(C)] -#[deriving(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] +#[derive(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum Fading { - NoFading = ffi::MIX_NO_FADING as int, - FadingOut = ffi::MIX_FADING_OUT as int, - FadingIn = ffi::MIX_FADING_IN as int + NoFading = ffi::MIX_NO_FADING as isize, + FadingOut = ffi::MIX_FADING_OUT as isize, + FadingIn = ffi::MIX_FADING_IN as isize } /// Sound effect channel. -#[deriving(Copy, PartialEq, Show)] -pub struct Channel(int); +#[derive(Copy, PartialEq, Show)] +pub struct Channel(isize); /// Set the number of channels being mixed. pub fn allocate_channels(numchans: int) -> int { @@ -255,14 +256,13 @@ pub fn allocate_channels(numchans: int) -> int { } } -static mut channel_finished_callback: Option = None; +static mut channel_finished_callback: Option = None; extern "C" fn c_channel_finished_callback(ch: c_int) { unsafe { match channel_finished_callback { None => (), Some(cb) => { - let cb = mem::transmute::<_, |Channel|>(cb); cb(Channel(ch as int)) } } @@ -270,9 +270,9 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { } /// When channel playback is halted, then the specified channel_finished function is called. -pub fn set_channel_finished(f: |Channel|:'static) { +pub fn set_channel_finished(f: fn(Channel)) { unsafe { - channel_finished_callback = Some(mem::transmute::<_, raw::Closure>(f)); + channel_finished_callback = Some(f); ffi::Mix_ChannelFinished(Some(c_channel_finished_callback as extern "C" fn (ch: c_int))); } } @@ -296,26 +296,26 @@ impl Channel { } /// Set the volume for any allocated channel. - pub fn set_volume(self, volume: int) -> int { + pub fn set_volume(self, volume: isize) -> isize { let Channel(ch) = self; unsafe { - ffi::Mix_Volume(ch as c_int, volume as c_int) as int + ffi::Mix_Volume(ch as c_int, volume as c_int) as isize } } - pub fn get_volume(self) -> int { + pub fn get_volume(self) -> isize { let Channel(ch) = self; unsafe { - ffi::Mix_Volume(ch as c_int, -1) as int + ffi::Mix_Volume(ch as c_int, -1) as isize } } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn play(self, chunk: &Chunk, loops: int) -> SdlResult { + pub fn play(self, chunk: &Chunk, loops: isize) -> SdlResult { self.play_timed(chunk, loops, -1) } - pub fn play_timed(self, chunk: &Chunk, loops: int, ticks: int) -> SdlResult { + pub fn play_timed(self, chunk: &Chunk, loops: isize, ticks: isize) -> SdlResult { let Channel(ch) = self; let ret = unsafe { ffi::Mix_PlayChannelTimed(ch as c_int, chunk.raw, loops as c_int, ticks as c_int) @@ -323,16 +323,16 @@ impl Channel { if ret == -1 { Err(get_error()) } else { - Ok(Channel(ret as int)) + Ok(Channel(ret as isize)) } } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn fade_in(self, chunk: &Chunk, loops: int, ms: int) -> SdlResult { + pub fn fade_in(self, chunk: &Chunk, loops: isize, ms: isize) -> SdlResult { self.fade_in_timed(chunk, loops, ms, -1) } - pub fn fade_in_timed(self, chunk: &Chunk, loops: int, ms: int, ticks: int) -> SdlResult { + pub fn fade_in_timed(self, chunk: &Chunk, loops: isize, ms: isize, ticks: isize) -> SdlResult { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadeInChannelTimed(ch as c_int, chunk.raw, loops as c_int, ms as c_int, ticks as c_int) @@ -340,7 +340,7 @@ impl Channel { if ret == -1 { Err(get_error()) } else { - Ok(Channel(ret as int)) + Ok(Channel(ret as isize)) } } @@ -363,15 +363,15 @@ impl Channel { } /// Halt channel playback, after ticks milliseconds. - pub fn expire(self, ticks: int) -> int { + pub fn expire(self, ticks: isize) -> isize { let Channel(ch) = self; - unsafe { ffi::Mix_ExpireChannel(ch as c_int, ticks as c_int) as int } + unsafe { ffi::Mix_ExpireChannel(ch as c_int, ticks as c_int) as isize } } /// Gradually fade out which channel over ms milliseconds starting from now. - pub fn fade_out(self, ms: int) -> int { + pub fn fade_out(self, ms: isize) -> isize { let Channel(ch) = self; - unsafe { ffi::Mix_FadeOutChannel(ch as c_int, ms as c_int) as int } + unsafe { ffi::Mix_FadeOutChannel(ch as c_int, ms as c_int) as isize } } /// if channel is playing, or not. @@ -389,7 +389,7 @@ impl Channel { /// if channel is fading in, out, or not pub fn get_fading(self) -> Fading { let Channel(ch) = self; - let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as int }; + let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as isize }; FromPrimitive::from_int(ret).unwrap() } @@ -459,25 +459,25 @@ impl Channel { } } -pub fn get_playing_channels_number() -> int { - unsafe { ffi::Mix_Playing(-1) as int } +pub fn get_playing_channels_number() -> isize { + unsafe { ffi::Mix_Playing(-1) as isize } } -pub fn get_paused_channels_number() -> int { - unsafe { ffi::Mix_Paused(-1) as int } +pub fn get_paused_channels_number() -> isize { + unsafe { ffi::Mix_Paused(-1) as isize } } // 4.4 Groups /// Reserve num channels from being used when playing samples when /// passing in -1 as a channel number to playback functions. -pub fn reserve_channels(num: int) -> int { - unsafe { ffi::Mix_ReserveChannels(num as c_int) as int } +pub fn reserve_channels(num: isize) -> isize { + unsafe { ffi::Mix_ReserveChannels(num as c_int) as isize } } /// Sound effect channel grouping. -#[deriving(Copy)] -pub struct Group(int); +#[derive(Copy)] +pub struct Group(isize); impl default::Default for Group { fn default() -> Group { @@ -488,9 +488,9 @@ impl default::Default for Group { impl Group { /// Add channels starting at from up through to to group tag, /// or reset it's group to the default group tag (-1). - pub fn add_channels_range(self, from: int, to: int) -> int { + pub fn add_channels_range(self, from: isize, to: isize) -> isize { let Group(g) = self; - unsafe { ffi::Mix_GroupChannels(from as c_int, to as c_int, g as c_int) as int } + unsafe { ffi::Mix_GroupChannels(from as c_int, to as c_int, g as c_int) as isize } } /// Add which channel to group tag, or reset it's group to the default group tag @@ -500,15 +500,15 @@ impl Group { } /// Count the number of channels in group - pub fn count(self) -> int { + pub fn count(self) -> isize { let Group(g) = self; - unsafe { ffi::Mix_GroupCount(g as c_int) as int } + unsafe { ffi::Mix_GroupCount(g as c_int) as isize } } /// Find the first available (not playing) channel in group pub fn find_available(self) -> Option { let Group(g) = self; - let ret = unsafe { ffi::Mix_GroupAvailable(g as c_int) as int }; + let ret = unsafe { ffi::Mix_GroupAvailable(g as c_int) as isize }; if ret == -1 { None } else { @@ -519,7 +519,7 @@ impl Group { /// Find the oldest actively playing channel in group pub fn find_oldest(self) -> Option { let Group(g) = self; - let ret = unsafe { ffi::Mix_GroupOldest(g as c_int) as int }; + let ret = unsafe { ffi::Mix_GroupOldest(g as c_int) as isize }; if ret == -1 { None } else { @@ -530,7 +530,7 @@ impl Group { /// Find the newest, most recently started, actively playing channel in group. pub fn find_newest(self) -> Option { let Group(g) = self; - let ret = unsafe { ffi::Mix_GroupNewer(g as c_int) as int }; + let ret = unsafe { ffi::Mix_GroupNewer(g as c_int) as isize }; if ret == -1 { None } else { @@ -540,9 +540,9 @@ impl Group { /// Gradually fade out channels in group over some milliseconds starting from now. /// Returns the number of channels set to fade out. - pub fn fade_out(self, ms: int) -> int { + pub fn fade_out(self, ms: isize) -> isize { let Group(g) = self; - unsafe { ffi::Mix_FadeOutGroup(g as c_int, ms as c_int) as int } + unsafe { ffi::Mix_FadeOutGroup(g as c_int, ms as c_int) as isize } } /// Halt playback on all channels in group. @@ -555,21 +555,21 @@ impl Group { // 4.5 Music /// Get the number of music decoders available. -pub fn get_music_decoders_number() -> int { - unsafe { ffi::Mix_GetNumMusicDecoders() as int } +pub fn get_music_decoders_number() -> isize { + unsafe { ffi::Mix_GetNumMusicDecoders() as isize } } /// Get the name of the indexed music decoder. -pub fn get_music_decoder(index: int) -> String { +pub fn get_music_decoder(index: isize) -> String { unsafe { let name = ffi::Mix_GetMusicDecoder(index as c_int); - CString::new(name, false).as_str().unwrap().to_string() + from_utf8(c_str_to_bytes(&name)).unwrap().to_owned() } } /// Music type enumerations #[repr(C)] -#[deriving(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] +#[derive(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] pub enum MusicType { MusicNone = ffi::MUS_NONE as int, MusicCmd = ffi::MUS_CMD as int, @@ -584,20 +584,19 @@ pub enum MusicType { } // hooks -static mut music_finished_hook: Option = None; +static mut music_finished_hook: Option = None; extern "C" fn c_music_finished_hook() { unsafe { match music_finished_hook { None => (), Some(f) => { - let f = mem::transmute::<_, ||>(f); f() } } } } /// This is an opaque data type used for Music data. -#[deriving(PartialEq)] #[allow(raw_pointer_deriving)] +#[derive(PartialEq)] #[allow(raw_pointer_derive)] pub struct Music { pub raw: *const ffi::Mix_Music, pub owned: bool, @@ -622,7 +621,7 @@ impl Music { /// Load music file to use. pub fn from_file(path: &Path) -> SdlResult { let raw = unsafe { - ffi::Mix_LoadMUS(path.to_c_str().into_inner()) + ffi::Mix_LoadMUS(CString::from_slice(path.filename().unwrap()).as_ptr()) }; if raw.is_null() { Err(get_error()) @@ -633,12 +632,12 @@ impl Music { /// The file format encoding of the music. pub fn get_type(&self) -> MusicType { - let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as int }; + let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize }; FromPrimitive::from_int(ret).unwrap() } /// Play the loaded music loop times through from start to finish. - pub fn play(&self, loops: int) -> SdlResult<()> { + pub fn play(&self, loops: isize) -> SdlResult<()> { let ret = unsafe { ffi::Mix_PlayMusic(self.raw, loops as c_int) }; @@ -651,7 +650,7 @@ impl Music { /// Fade in over ms milliseconds of time, the loaded music, /// playing it loop times through from start to finish. - pub fn fade_in(&self, loops: int, ms: int) -> SdlResult<()> { + pub fn fade_in(&self, loops: isize, ms: isize) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) }; @@ -663,7 +662,7 @@ impl Music { } /// Fade in over ms milliseconds of time, from position. - pub fn fade_in_from_pos(&self, loops: int, ms: int, position: f64) -> SdlResult<()> { + pub fn fade_in_from_pos(&self, loops: isize, ms: isize, position: f64) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeInMusicPos(self.raw, loops as c_int, ms as c_int, position as c_double) }; @@ -675,13 +674,13 @@ impl Music { } // FIXME: make these class method? - pub fn get_volume() -> int { - unsafe { ffi::Mix_VolumeMusic(-1) as int } + pub fn get_volume() -> isize { + unsafe { ffi::Mix_VolumeMusic(-1) as isize } } /// Set the volume. - pub fn set_volume(volume: int) -> int { - unsafe { ffi::Mix_VolumeMusic(volume as c_int) as int } + pub fn set_volume(volume: isize) -> isize { + unsafe { ffi::Mix_VolumeMusic(volume as c_int) as isize } } /// Pause the music playback. @@ -714,7 +713,7 @@ impl Music { /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> SdlResult<()> { let ret = unsafe { - ffi::Mix_SetMusicCMD(command.to_c_str().into_inner()) + ffi::Mix_SetMusicCMD(CString::from_slice(command.as_bytes()).as_ptr()) }; if ret == -1 { Err(get_error()) @@ -729,7 +728,7 @@ impl Music { } /// Gradually fade out the music over ms milliseconds starting from now. - pub fn fade_out(ms: int) -> SdlResult<()> { + pub fn fade_out(ms: isize) -> SdlResult<()> { let ret = unsafe { ffi::Mix_FadeOutMusic(ms as c_int) }; @@ -743,9 +742,9 @@ impl Music { // TODO: Mix_HookMusic // TODO: Mix_GetMusicHookData - pub fn hook_finished(f: ||) { + pub fn hook_finished(f: fn()) { unsafe { - music_finished_hook = Some(mem::transmute(f)); + music_finished_hook = Some(f); ffi::Mix_HookMusicFinished(Some(c_music_finished_hook as extern "C" fn ())); } } @@ -770,7 +769,7 @@ impl Music { /// If music is fading, or not. pub fn get_fading() -> Fading { - let ret = unsafe { ffi::Mix_FadingMusic() as int }; + let ret = unsafe { ffi::Mix_FadingMusic() as isize }; FromPrimitive::from_int(ret).unwrap() } } From 9e156660d7abde686ef004ad84d428faef1e775b Mon Sep 17 00:00:00 2001 From: Evgeny Khudoba Date: Fri, 23 Jan 2015 00:55:42 +0600 Subject: [PATCH 045/114] fix bitflags dependency --- Cargo.toml | 3 +++ src/sdl2_mixer/lib.rs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f1deb1fe..3fdffd16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,6 @@ path = "src/demo/main.rs" git = "https://github.com/AngryLawyer/rust-sdl2" #version = "~0.0.3" +[dependencies] + +bitflags = "*" diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index a03b0452..8543fe52 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -5,6 +5,8 @@ A binding for SDL2_mixer. #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] +#[macro_use] +extern crate bitflags; extern crate libc; extern crate sdl2; From f838fd1342444dd6ae6357cd7cc84dbe5514e39f Mon Sep 17 00:00:00 2001 From: sbidin Date: Sun, 22 Mar 2015 17:01:01 +0100 Subject: [PATCH 046/114] Make library compilable by newest Rust nightly. * Add sdl2-sys as explicit dependency. * Use CStr instead of deprecated c_str_to_bytes. * Use CString::new instead of deprecated CString::from_slice. * Removed unused imports such as std::mem and std::raw. * Silenced some warnings by adding libc and core features. * Use new Path instead of deprecated old one. * Use Debug instead of deprecated Show. * Use isize instead of deprecated int. * Use FromPrimitive::from_size instead of FromPrimitive::from_int. --- Cargo.toml | 19 +++++++-------- src/sdl2_mixer/ffi.rs | 6 ++--- src/sdl2_mixer/lib.rs | 56 ++++++++++++++++++++++--------------------- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fdffd16..52382f73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,28 +1,25 @@ [package] - name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" version = "0.0.3" license = "MIT" -authors = [ "ShuYu Wang " ] +authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] [lib] - name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" -[[bin]] - -name = "demo" -path = "src/demo/main.rs" +[dependencies] +bitflags = "0.1" [dependencies.sdl2] - git = "https://github.com/AngryLawyer/rust-sdl2" -#version = "~0.0.3" -[dependencies] +[dependencies.sdl2-sys] +git = "https://github.com/AngryLawyer/rust-sdl2" -bitflags = "*" +[[bin]] +name = "demo" +path = "src/demo/main.rs" diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 5f023826..6e934ac6 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -1,8 +1,8 @@ -/* automatically generated by rust-bindgen */ +extern crate "sdl2-sys" as sys; use libc::*; -use sdl2::rwops::ll::SDL_RWops; -use sdl2::version::ll::SDL_version; +use self::sys::rwops::SDL_RWops; +use self::sys::version::SDL_version; pub type MIX_InitFlags = c_uint; pub const MIX_INIT_FLAC: c_uint = 1; diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 8543fe52..4b57d1f6 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -2,6 +2,9 @@ A binding for SDL2_mixer. */ +#![feature(libc)] +#![feature(core)] + #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] @@ -12,13 +15,12 @@ extern crate sdl2; use std::default; use std::ptr; -use std::mem; -use std::raw; use std::fmt; -use std::ffi::{ c_str_to_bytes, CString }; +use std::ffi::{CString, CStr}; use std::str::from_utf8; use std::borrow::ToOwned; use std::num::FromPrimitive; +use std::path::Path; use libc::{c_int, uint16_t, c_double}; use sdl2::get_error; use sdl2::rwops::RWops; @@ -168,7 +170,7 @@ pub fn get_chunk_decoders_number() -> isize { pub fn get_chunk_decoder(index: isize) -> String { unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); - from_utf8(c_str_to_bytes(&name)).unwrap().to_owned() + from_utf8(CStr::from_ptr(name).to_bytes()).unwrap().to_owned() } } @@ -240,7 +242,7 @@ impl LoaderRWops for RWops { /// Fader effect type enumerations #[repr(C)] -#[derive(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] +#[derive(Debug, Copy, Clone, PartialEq, Hash, FromPrimitive)] pub enum Fading { NoFading = ffi::MIX_NO_FADING as isize, FadingOut = ffi::MIX_FADING_OUT as isize, @@ -248,13 +250,13 @@ pub enum Fading { } /// Sound effect channel. -#[derive(Copy, PartialEq, Show)] +#[derive(Debug, Copy, PartialEq)] pub struct Channel(isize); /// Set the number of channels being mixed. -pub fn allocate_channels(numchans: int) -> int { +pub fn allocate_channels(numchans: isize) -> isize { unsafe { - ffi::Mix_AllocateChannels(numchans as c_int) as int + ffi::Mix_AllocateChannels(numchans as c_int) as isize } } @@ -265,7 +267,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { match channel_finished_callback { None => (), Some(cb) => { - cb(Channel(ch as int)) + cb(Channel(ch as isize)) } } } @@ -392,7 +394,7 @@ impl Channel { pub fn get_fading(self) -> Fading { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as isize }; - FromPrimitive::from_int(ret).unwrap() + FromPrimitive::from_isize(ret).unwrap() } /// Get the most recent sample chunk pointer played on channel. @@ -565,24 +567,24 @@ pub fn get_music_decoders_number() -> isize { pub fn get_music_decoder(index: isize) -> String { unsafe { let name = ffi::Mix_GetMusicDecoder(index as c_int); - from_utf8(c_str_to_bytes(&name)).unwrap().to_owned() + from_utf8(CStr::from_ptr(name).to_bytes()).unwrap().to_owned() } } /// Music type enumerations #[repr(C)] -#[derive(Copy, Clone, PartialEq, Hash, Show, FromPrimitive)] +#[derive(Copy, Clone, PartialEq, Hash, Debug, FromPrimitive)] pub enum MusicType { - MusicNone = ffi::MUS_NONE as int, - MusicCmd = ffi::MUS_CMD as int, - MusicWav = ffi::MUS_WAV as int, - MusicMod = ffi::MUS_MOD as int, - MusicMid = ffi::MUS_MID as int, - MusicOgg = ffi::MUS_OGG as int, - MusicMp3 = ffi::MUS_MP3 as int, - MusicMp3Mad = ffi::MUS_MP3_MAD as int, - MusicFlac = ffi::MUS_FLAC as int, - MusicModPlug = ffi::MUS_MODPLUG as int + MusicNone = ffi::MUS_NONE as isize, + MusicCmd = ffi::MUS_CMD as isize, + MusicWav = ffi::MUS_WAV as isize, + MusicMod = ffi::MUS_MOD as isize, + MusicMid = ffi::MUS_MID as isize, + MusicOgg = ffi::MUS_OGG as isize, + MusicMp3 = ffi::MUS_MP3 as isize, + MusicMp3Mad = ffi::MUS_MP3_MAD as isize, + MusicFlac = ffi::MUS_FLAC as isize, + MusicModPlug = ffi::MUS_MODPLUG as isize } // hooks @@ -612,7 +614,7 @@ impl Drop for Music { } } -impl fmt::Show for Music { +impl fmt::Debug for Music { /// Shows the original regular expression. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "") @@ -623,7 +625,7 @@ impl Music { /// Load music file to use. pub fn from_file(path: &Path) -> SdlResult { let raw = unsafe { - ffi::Mix_LoadMUS(CString::from_slice(path.filename().unwrap()).as_ptr()) + ffi::Mix_LoadMUS(CString::new(path.to_str().unwrap()).unwrap().as_ptr()) }; if raw.is_null() { Err(get_error()) @@ -635,7 +637,7 @@ impl Music { /// The file format encoding of the music. pub fn get_type(&self) -> MusicType { let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize }; - FromPrimitive::from_int(ret).unwrap() + FromPrimitive::from_isize(ret).unwrap() } /// Play the loaded music loop times through from start to finish. @@ -715,7 +717,7 @@ impl Music { /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> SdlResult<()> { let ret = unsafe { - ffi::Mix_SetMusicCMD(CString::from_slice(command.as_bytes()).as_ptr()) + ffi::Mix_SetMusicCMD(CString::new(command).unwrap().as_ptr()) }; if ret == -1 { Err(get_error()) @@ -772,7 +774,7 @@ impl Music { /// If music is fading, or not. pub fn get_fading() -> Fading { let ret = unsafe { ffi::Mix_FadingMusic() as isize }; - FromPrimitive::from_int(ret).unwrap() + FromPrimitive::from_isize(ret).unwrap() } } From f1176a8eb87a10a32932de3f3e6604860dfc7033 Mon Sep 17 00:00:00 2001 From: sbidin Date: Sun, 22 Mar 2015 17:07:11 +0100 Subject: [PATCH 047/114] Make demo work with newest Rust and sdl2_mixer. Use new Path, new std::env and similar things. --- src/demo/main.rs | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) mode change 100644 => 100755 src/demo/main.rs diff --git a/src/demo/main.rs b/src/demo/main.rs old mode 100644 new mode 100755 index ecc91c89..d72b5115 --- a/src/demo/main.rs +++ b/src/demo/main.rs @@ -1,38 +1,44 @@ #![crate_type = "bin"] -#![crate_name="demo"] extern crate sdl2; extern crate sdl2_mixer; -use std::os; -use std::borrow::ToOwned; +use std::env; +use std::path::Path; +use sdl2::{INIT_AUDIO, INIT_TIMER}; +use sdl2_mixer::{INIT_MP3, INIT_FLAC, INIT_MOD, INIT_FLUIDSYNTH, INIT_MODPLUG, + INIT_OGG, DEFAULT_FREQUENCY}; fn main() { - let args = os::args(); + + let args: Vec<_> = env::args().collect(); + if args.len() < 2 { println!("Usage: ./demo audio.[mp3|wav|ogg]") } else { - match dump_info(&Path::new(args[1].to_owned())) { - _ => () - } + demo(Path::new(&args[1])); } } -fn dump_info(filename: &Path) -> Result<(), String> { - println!("linked version: {:?}", sdl2_mixer::get_linked_version()); - sdl2::init(sdl2::INIT_AUDIO | sdl2::INIT_TIMER); - println!("inited => {}", sdl2_mixer::init(sdl2_mixer::INIT_MP3 | sdl2_mixer::INIT_FLAC | - sdl2_mixer::INIT_MOD | sdl2_mixer::INIT_FLUIDSYNTH | - sdl2_mixer::INIT_MODPLUG | sdl2_mixer::INIT_OGG).bits()); +fn demo(filename: &Path) { + + println!("linked version: {}", sdl2_mixer::get_linked_version()); + + let _ = sdl2::init(INIT_AUDIO | INIT_TIMER); + + println!("mixer initialized: {}", sdl2_mixer::init( + INIT_MP3 | INIT_FLAC | INIT_MOD | INIT_FLUIDSYNTH | + INIT_MODPLUG | INIT_OGG).bits()); + // TODO: 0x8010 is SDL_audio flag - try!(sdl2_mixer::open_audio(sdl2_mixer::DEFAULT_FREQUENCY, 0x8010u16, 2, 1024)); + let _ = sdl2_mixer::open_audio(DEFAULT_FREQUENCY, 0x8010u16, 2, 1024); sdl2_mixer::allocate_channels(0); { let n = sdl2_mixer::get_chunk_decoders_number(); println!("available chunk(sample) decoders: {}", n); for i in (0..n) { - println!("| decoder {} => {:}", i, sdl2_mixer::get_chunk_decoder(i)); + println!(" decoder {} => {}", i, sdl2_mixer::get_chunk_decoder(i)); } } @@ -40,7 +46,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { let n = sdl2_mixer::get_music_decoders_number(); println!("available music decoders: {}", n); for i in (0..n) { - println!("| decoder {} => {:}", i, sdl2_mixer::get_music_decoder(i)); + println!(" decoder {} => {}", i, sdl2_mixer::get_music_decoder(i)); } } @@ -56,9 +62,7 @@ fn dump_info(filename: &Path) -> Result<(), String> { println!("music => {:?}", music); println!("music type => {:?}", music.get_type()); - println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); - println!("play => {:?}", music.play(1)); sdl2::timer::delay(10000); @@ -70,14 +74,12 @@ fn dump_info(filename: &Path) -> Result<(), String> { println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); sdl2::timer::delay(5000); - sdl2_mixer::Music::halt(); - sdl2::timer::delay(1000); + // here will print hook_finished + println!("qutting mixer"); sdl2_mixer::quit(); - sdl2::quit(); - - Ok(()) + println!("quitting sdl"); } From e51959910f52c4c890821eba08fb32f4ff3038f0 Mon Sep 17 00:00:00 2001 From: sbidin Date: Mon, 23 Mar 2015 00:28:14 +0100 Subject: [PATCH 048/114] Fix demo where music was freed after mixer quit. --- src/demo/main.rs | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/demo/main.rs b/src/demo/main.rs index d72b5115..32ab232a 100755 --- a/src/demo/main.rs +++ b/src/demo/main.rs @@ -52,31 +52,35 @@ fn demo(filename: &Path) { println!("query spec => {:?}", sdl2_mixer::query_spec()); - let music = sdl2_mixer::Music::from_file(filename).unwrap(); + // We want music to be freed before we quit sdl2_mixer (otherwise an error + // happens), so we do all of the following within its own block. + { + let music = sdl2_mixer::Music::from_file(filename).unwrap(); - fn hook_finished() { - println!("play ends! from rust cb"); + fn hook_finished() { + println!("play ends! from rust cb"); + } + + sdl2_mixer::Music::hook_finished(hook_finished); + + println!("music => {:?}", music); + println!("music type => {:?}", music.get_type()); + println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); + println!("play => {:?}", music.play(1)); + + sdl2::timer::delay(10000); + + println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); + + sdl2::timer::delay(5000); + + println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); + + sdl2::timer::delay(5000); + sdl2_mixer::Music::halt(); + sdl2::timer::delay(1000); } - sdl2_mixer::Music::hook_finished(hook_finished); - - println!("music => {:?}", music); - println!("music type => {:?}", music.get_type()); - println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); - println!("play => {:?}", music.play(1)); - - sdl2::timer::delay(10000); - - println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); - - sdl2::timer::delay(5000); - - println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); - - sdl2::timer::delay(5000); - sdl2_mixer::Music::halt(); - sdl2::timer::delay(1000); - // here will print hook_finished println!("qutting mixer"); From 02d9a02a195c8636c2070ddf7e81bd2cf5c20269 Mon Sep 17 00:00:00 2001 From: sbidin Date: Tue, 24 Mar 2015 18:56:05 +0100 Subject: [PATCH 049/114] Switch to Cargo dependencies, bump version. --- Cargo.toml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 52382f73..7bfc8247 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.0.3" +version = "0.0.31" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,12 +13,14 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.1" +sdl2 = "0.0.31" +sdl2-sys = "0.0.31" -[dependencies.sdl2] -git = "https://github.com/AngryLawyer/rust-sdl2" +# [dependencies.sdl2] +# git = "https://github.com/AngryLawyer/rust-sdl2" -[dependencies.sdl2-sys] -git = "https://github.com/AngryLawyer/rust-sdl2" +# [dependencies.sdl2-sys] +# git = "https://github.com/AngryLawyer/rust-sdl2" [[bin]] name = "demo" From 41b3f1db13f78c2cdcc9618591e91c05d4698bbe Mon Sep 17 00:00:00 2001 From: sbidin Date: Tue, 24 Mar 2015 19:41:01 +0100 Subject: [PATCH 050/114] Add more information to README. --- README.md | 68 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8a812ae3..37d378c4 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,67 @@ -# Rust-SDL2_mixer - +Rust-SDL2_mixer +============= [![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) Rust bindings for SDL2_mixer. +## Overview + +Rust-SDL2_mixer is a library for talking to the new SDL2_mixer library from Rust. + +Rust-SDL2_mixer uses the MIT licence. + +## Requirements + +* [Rust-SDL2](https://github.com/AngryLawyer/rust-sdl2) +* SDL2_mixer development libraries +* Rust master or nightly + ## Installation -Requirements - -* [Rust-sdl2](https://github.com/AngryLawyer/rust-sdl2) -* sdl_mixer 2.0 development libraries -* Rust nightly -* Cargo nightly - - -### normal +Place the following into your project's Cargo.toml file: +```toml +[dependencies] +sdl2_mixer = "0.0.31" # Doesn't work yet, needs to be published. ``` + +Or, to depend on the newest rust-sdl2_mixer, reference the repository: + +```toml +[dependencies.sdl2_mixer] +git = "https://github.com/andelf/rust-sdl2-mixer" +``` + +You can also just clone and build the library yourself: + +```bash git clone https://github.com/andelf/rust-sdl2_mixer +cd rust-sdl2_mixer cargo build # TODO: OR if you are using the mac framework version rustc --cfg mac_framework src/sdl2_mixer/lib.rs ``` -### cargo +If you're not using Cargo, you can compile the library manually: -``` -[dependencies] -sdl2_mixer = "$version-here$" -``` - -or - -``` -[dependencies.sdl2_mixer] -git = "https://github.com/andelf/rust-sdl2_mixer" +```bash +git clone https://github.com/andelf/rust-sdl2_mixer +cd rust-sdl2_mixer +rustc src/sdl2_mixer/lib.rs ``` ## Demo +A simple demo that plays out a portion of a given music file is included: + +```bash +cargo run path/to/music.(mp3|flac|ogg|wav) ``` -cargo run /path/to/wav_file.wav + +Or: + +```bash +rustc -L. src/demo/main.rs -o demo +./demo path/to/music.(mp3|flac|ogg|wav) ``` From 15046ff025fd59bf399455e8c3bb676cffffa157 Mon Sep 17 00:00:00 2001 From: sbidin Date: Tue, 24 Mar 2015 19:44:04 +0100 Subject: [PATCH 051/114] Fix bad repository URL. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37d378c4..4ddd8649 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Or, to depend on the newest rust-sdl2_mixer, reference the repository: ```toml [dependencies.sdl2_mixer] -git = "https://github.com/andelf/rust-sdl2-mixer" +git = "https://github.com/andelf/rust-sdl2_mixer" ``` You can also just clone and build the library yourself: From 718e7584171236f00ab4fe84e52cad81e928c997 Mon Sep 17 00:00:00 2001 From: Outfl3sh Date: Fri, 27 Mar 2015 20:37:35 +0100 Subject: [PATCH 052/114] updated dependencies, bumped up version number --- Cargo.toml | 8 ++++---- README.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7bfc8247..3ddf0b16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.0.31" +version = "0.0.33" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -12,9 +12,9 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.1" -sdl2 = "0.0.31" -sdl2-sys = "0.0.31" +bitflags = "0.1.1" +sdl2 = "0.0.33" +sdl2-sys = "0.0.33" # [dependencies.sdl2] # git = "https://github.com/AngryLawyer/rust-sdl2" diff --git a/README.md b/README.md index 4ddd8649..5e5d345e 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.0.31" # Doesn't work yet, needs to be published. +sdl2_mixer = "0.0.33" # Doesn't work yet, needs to be published. ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From 13f605f6fded93f6f513e9aca5dbeb306af470a8 Mon Sep 17 00:00:00 2001 From: Outfl3sh Date: Fri, 27 Mar 2015 20:51:15 +0100 Subject: [PATCH 053/114] ... --- src/sdl2_mixer/ffi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 6e934ac6..1fe48088 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -1,4 +1,4 @@ -extern crate "sdl2-sys" as sys; +extern crate sdl2_sys as sys; use libc::*; use self::sys::rwops::SDL_RWops; From 00bfbec75756b186ff8be71402796a147eb8b95b Mon Sep 17 00:00:00 2001 From: LQKI Date: Mon, 13 Apr 2015 16:06:33 +0300 Subject: [PATCH 054/114] update versions of sdl2 dependencies --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ddf0b16..c743b8c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.1.1" -sdl2 = "0.0.33" -sdl2-sys = "0.0.33" +sdl2 = "0.2.0" +sdl2-sys = "0.2.0" # [dependencies.sdl2] # git = "https://github.com/AngryLawyer/rust-sdl2" From 4570fc271199053d0933f9cbacd29a64b48c5ad5 Mon Sep 17 00:00:00 2001 From: LQKI Date: Mon, 13 Apr 2015 16:08:11 +0300 Subject: [PATCH 055/114] update 'Group' and 'Channel' to have derive(Clone) --- src/sdl2_mixer/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 4b57d1f6..8855d62b 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -250,7 +250,7 @@ pub enum Fading { } /// Sound effect channel. -#[derive(Debug, Copy, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct Channel(isize); /// Set the number of channels being mixed. @@ -480,7 +480,7 @@ pub fn reserve_channels(num: isize) -> isize { } /// Sound effect channel grouping. -#[derive(Copy)] +#[derive(Copy, Clone)] pub struct Group(isize); impl default::Default for Group { From bcc9f9b3af29ba5ec2949e6f261d3f1e9379cabe Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 13 Apr 2015 20:46:27 +0300 Subject: [PATCH 056/114] update version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c743b8c4..990ffd18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.0.33" +version = "0.2.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From dc7b84ae5106b6f3ce088d45db85348aada7d6c5 Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 16:31:23 +0800 Subject: [PATCH 057/114] fix rust version in .travis.yml --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a2f94a11..59da32ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: rust -os: - - linux +rust: + - 1.0.0-betaos: env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= From 28492e164b551a3e44f798a5fd31f5a0f909fd8c Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 18:32:53 +0800 Subject: [PATCH 058/114] fix rust 1.0.0-beta compatiblity --- .travis.yml | 19 ++++++------------ Cargo.toml | 7 ++++--- src/sdl2_mixer/lib.rs | 46 ++++++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index 59da32ef..514c24ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,26 +1,19 @@ language: rust rust: - - 1.0.0-betaos: + - 1.0.0-beta + env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= - LD_LIBRARY_PATH: /usr/local/lib install: - - cd .. - - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz - - time wget -q http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz - - time tar xf SDL2-*.tar.gz - - time tar xf SDL2_mixer-*.tar.gz - - cd SDL2-* - - ./configure && make && sudo make install - - cd - - - cd SDL2_mixer-* - - ./configure && make && sudo make install - - cd - + - sudo add-apt-repository ppa:team-xbmc/ppa -y + - sudo apt-get update -q + - sudo apt-get install libsdl2-dev - cd .. script: - - cd rust-sdl2_mixer - cargo build -v - cargo test -v - cargo doc -v after_script: + - cp -R target/doc doc - curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh diff --git a/Cargo.toml b/Cargo.toml index 990ffd18..767b6e8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,10 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.1.1" -sdl2 = "0.2.0" -sdl2-sys = "0.2.0" +bitflags = "0.1" +sdl2 = "0.2.1" +sdl2-sys = "0.2.1" +libc = "0.1.6" # [dependencies.sdl2] # git = "https://github.com/AngryLawyer/rust-sdl2" diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 8855d62b..b7b3ddb2 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -2,9 +2,6 @@ A binding for SDL2_mixer. */ -#![feature(libc)] -#![feature(core)] - #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] @@ -19,9 +16,8 @@ use std::fmt; use std::ffi::{CString, CStr}; use std::str::from_utf8; use std::borrow::ToOwned; -use std::num::FromPrimitive; use std::path::Path; -use libc::{c_int, uint16_t, c_double}; +use libc::{c_int, uint16_t, c_double, c_uint}; use sdl2::get_error; use sdl2::rwops::RWops; use sdl2::version::Version; @@ -107,7 +103,7 @@ pub const MAX_VOLUME : isize = 128; pub fn get_linked_version() -> Version { unsafe { - Version::from_ll(ffi::Mix_Linked_Version()) + Version::from_ll(*ffi::Mix_Linked_Version()) } } @@ -242,7 +238,7 @@ impl LoaderRWops for RWops { /// Fader effect type enumerations #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Hash, FromPrimitive)] +#[derive(Debug, Copy, Clone, PartialEq, Hash)] pub enum Fading { NoFading = ffi::MIX_NO_FADING as isize, FadingOut = ffi::MIX_FADING_OUT as isize, @@ -393,8 +389,13 @@ impl Channel { /// if channel is fading in, out, or not pub fn get_fading(self) -> Fading { let Channel(ch) = self; - let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as isize }; - FromPrimitive::from_isize(ret).unwrap() + let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as c_uint }; + match ret { + ffi::MIX_NO_FADING => Fading::NoFading, + ffi::MIX_FADING_OUT => Fading::FadingOut, + ffi::MIX_FADING_IN => Fading::FadingIn, + _ => Fading::NoFading + } } /// Get the most recent sample chunk pointer played on channel. @@ -573,7 +574,7 @@ pub fn get_music_decoder(index: isize) -> String { /// Music type enumerations #[repr(C)] -#[derive(Copy, Clone, PartialEq, Hash, Debug, FromPrimitive)] +#[derive(Copy, Clone, PartialEq, Hash, Debug)] pub enum MusicType { MusicNone = ffi::MUS_NONE as isize, MusicCmd = ffi::MUS_CMD as isize, @@ -636,8 +637,20 @@ impl Music { /// The file format encoding of the music. pub fn get_type(&self) -> MusicType { - let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize }; - FromPrimitive::from_isize(ret).unwrap() + let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize } as c_uint; + match ret { + ffi::MUS_NONE => MusicType::MusicNone, + ffi::MUS_CMD => MusicType::MusicCmd, + ffi::MUS_WAV => MusicType::MusicWav, + ffi::MUS_MOD => MusicType::MusicMod, + ffi::MUS_MID => MusicType::MusicMid, + ffi::MUS_OGG => MusicType::MusicOgg, + ffi::MUS_MP3 => MusicType::MusicMp3, + ffi::MUS_MP3_MAD => MusicType::MusicMp3Mad, + ffi::MUS_FLAC => MusicType::MusicFlac, + ffi::MUS_MODPLUG => MusicType::MusicModPlug, + _ => MusicType::MusicNone + } } /// Play the loaded music loop times through from start to finish. @@ -773,8 +786,13 @@ impl Music { /// If music is fading, or not. pub fn get_fading() -> Fading { - let ret = unsafe { ffi::Mix_FadingMusic() as isize }; - FromPrimitive::from_isize(ret).unwrap() + let ret = unsafe { ffi::Mix_FadingMusic() as isize } as c_uint; + match ret { + ffi::MIX_NO_FADING => Fading::NoFading, + ffi::MIX_FADING_OUT => Fading::FadingOut, + ffi::MIX_FADING_IN => Fading::FadingIn, + _ => Fading::NoFading + } } } From b69de4707f4ceaaeb72587e69a8d2d2f891bd7dc Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 18:36:51 +0800 Subject: [PATCH 059/114] fix typo in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 514c24ba..5c2cbded 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ env: install: - sudo add-apt-repository ppa:team-xbmc/ppa -y - sudo apt-get update -q - - sudo apt-get install libsdl2-dev - cd .. + - sudo apt-get install libsdl2-dev script: - cargo build -v - cargo test -v From 2291572c1bd7791ee714d64a47fa98c1142fdc8a Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 18:42:53 +0800 Subject: [PATCH 060/114] fix typo in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5c2cbded..b8a4a0f3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ env: install: - sudo add-apt-repository ppa:team-xbmc/ppa -y - sudo apt-get update -q - - sudo apt-get install libsdl2-dev + - sudo apt-get install libsdl2-dev libsdl2-mixer-dev script: - cargo build -v - cargo test -v From 4bb97af1ecaa687b33a533edb817d459e98bb69d Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 18:48:47 +0800 Subject: [PATCH 061/114] fix typo in .travis.yml --- .travis.yml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b8a4a0f3..2290c426 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,18 @@ env: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= - LD_LIBRARY_PATH: /usr/local/lib install: - - sudo add-apt-repository ppa:team-xbmc/ppa -y - - sudo apt-get update -q - - sudo apt-get install libsdl2-dev libsdl2-mixer-dev + - cd .. + - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz + - time wget -q http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz + - time tar xf SDL2-*.tar.gz + - time tar xf SDL2_mixer-*.tar.gz + - cd SDL2-* + - ./configure && make && sudo make install + - cd - + - cd SDL2_mixer-* + - ./configure && make && sudo make install + - cd - + - cd rust-sdl2_mixer script: - cargo build -v - cargo test -v From 33f8e30518720826ac025091bc0b43573a83494b Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 21:57:53 +0800 Subject: [PATCH 062/114] change version to 0.2.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 767b6e8d..99911d0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.2.0" +version = "0.2.1" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From 31445bba24960e75614ac2ddbb6d97a3ff9b58d7 Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Apr 2015 22:47:20 +0800 Subject: [PATCH 063/114] README: update version str --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e5d345e..f610d947 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.0.33" # Doesn't work yet, needs to be published. +sdl2_mixer = "0.2.1" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From 8a594a11cd989007c1031fcf7b10d276cff0b825 Mon Sep 17 00:00:00 2001 From: jeron Date: Fri, 22 May 2015 12:17:49 -0500 Subject: [PATCH 064/114] changed Cargo.toml & updated main.rs to work with a newer version of SDL. --- Cargo.toml | 6 +++--- src/demo/main.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 99911d0a..3a6639fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.2.1" +version = "0.3.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.1" -sdl2 = "0.2.1" -sdl2-sys = "0.2.1" +sdl2 = "0.3.0" +sdl2-sys = "0.3.0" libc = "0.1.6" # [dependencies.sdl2] diff --git a/src/demo/main.rs b/src/demo/main.rs index 32ab232a..0641c779 100755 --- a/src/demo/main.rs +++ b/src/demo/main.rs @@ -5,7 +5,7 @@ extern crate sdl2_mixer; use std::env; use std::path::Path; -use sdl2::{INIT_AUDIO, INIT_TIMER}; +use sdl2::*; use sdl2_mixer::{INIT_MP3, INIT_FLAC, INIT_MOD, INIT_FLUIDSYNTH, INIT_MODPLUG, INIT_OGG, DEFAULT_FREQUENCY}; @@ -24,7 +24,7 @@ fn demo(filename: &Path) { println!("linked version: {}", sdl2_mixer::get_linked_version()); - let _ = sdl2::init(INIT_AUDIO | INIT_TIMER); + let _ = sdl2::init().audio().timer(); println!("mixer initialized: {}", sdl2_mixer::init( INIT_MP3 | INIT_FLAC | INIT_MOD | INIT_FLUIDSYNTH | From ff9eabb14fa42f1bcbb012c9b4bc2a6ad5817f3c Mon Sep 17 00:00:00 2001 From: Jakob Lautrup Nysom Date: Thu, 4 Jun 2015 13:10:29 +0200 Subject: [PATCH 065/114] add: upgrade to SDL2 version 0.4 --- Cargo.toml | 6 +++--- src/sdl2_mixer/lib.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a6639fc..2b78b208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.3.0" +version = "0.4.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.1" -sdl2 = "0.3.0" -sdl2-sys = "0.3.0" +sdl2 = "0.4" +sdl2-sys = "0.4" libc = "0.1.6" # [dependencies.sdl2] diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index b7b3ddb2..76e6872a 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -219,7 +219,7 @@ pub trait LoaderRWops { fn load_wav(&self) -> SdlResult; } -impl LoaderRWops for RWops { +impl<'a> LoaderRWops for RWops<'a> { /// Load src for use as a sample. fn load_wav(&self) -> SdlResult { let raw = unsafe { From 84d68400aa3ce8826b36b3c963b8e03edb8fc257 Mon Sep 17 00:00:00 2001 From: andelf Date: Sun, 7 Jun 2015 11:26:07 +0800 Subject: [PATCH 066/114] fix libc version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2b78b208..f0ffe664 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ path = "src/sdl2_mixer/lib.rs" bitflags = "0.1" sdl2 = "0.4" sdl2-sys = "0.4" -libc = "0.1.6" +libc = "0.1" # [dependencies.sdl2] # git = "https://github.com/AngryLawyer/rust-sdl2" From ec770f2702e025673f60a459b28fbe887bf62ce5 Mon Sep 17 00:00:00 2001 From: andelf Date: Mon, 29 Jun 2015 15:46:20 +0800 Subject: [PATCH 067/114] bump version: 0.5.0 --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f0ffe664..e2ff30fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.4.0" +version = "0.5.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -12,9 +12,9 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.1" -sdl2 = "0.4" -sdl2-sys = "0.4" +bitflags = "0.2" +sdl2 = "0.5" +sdl2-sys = "0.5" libc = "0.1" # [dependencies.sdl2] From f6d458a9d77b28843e36b2c6a4877d7e80962abe Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 16 Jul 2015 19:09:09 +0800 Subject: [PATCH 068/114] fix travis-ci --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2290c426..6b823e06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: rust rust: - - 1.0.0-beta + - nightly + - beta env: global: From aafc54a99ea10ce137fb42361c2489d8c113146a Mon Sep 17 00:00:00 2001 From: andelf Date: Thu, 23 Jul 2015 17:43:48 +0800 Subject: [PATCH 069/114] fix demo path; bump version 0.6.0 --- Cargo.toml | 10 +++------- README.md | 11 ++--------- src/demo/main.rs => examples/demo.rs | 2 -- 3 files changed, 5 insertions(+), 18 deletions(-) rename src/demo/main.rs => examples/demo.rs (99%) diff --git a/Cargo.toml b/Cargo.toml index e2ff30fe..31695be6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.5.0" +version = "0.6.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.2" -sdl2 = "0.5" -sdl2-sys = "0.5" +sdl2 = "0.6" +sdl2-sys = "0.6" libc = "0.1" # [dependencies.sdl2] @@ -22,7 +22,3 @@ libc = "0.1" # [dependencies.sdl2-sys] # git = "https://github.com/AngryLawyer/rust-sdl2" - -[[bin]] -name = "demo" -path = "src/demo/main.rs" diff --git a/README.md b/README.md index f610d947..1a266260 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.2.1" +sdl2_mixer = "0.6.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: @@ -56,12 +56,5 @@ rustc src/sdl2_mixer/lib.rs A simple demo that plays out a portion of a given music file is included: ```bash -cargo run path/to/music.(mp3|flac|ogg|wav) -``` - -Or: - -```bash -rustc -L. src/demo/main.rs -o demo -./demo path/to/music.(mp3|flac|ogg|wav) +cargo run --example demo path/to/music.(mp3|flac|ogg|wav) ``` diff --git a/src/demo/main.rs b/examples/demo.rs similarity index 99% rename from src/demo/main.rs rename to examples/demo.rs index 0641c779..5d102d68 100755 --- a/src/demo/main.rs +++ b/examples/demo.rs @@ -1,5 +1,3 @@ -#![crate_type = "bin"] - extern crate sdl2; extern crate sdl2_mixer; From 1d2663826cbcb11fc6b1d0817a0dd33a45f491e9 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Thu, 20 Aug 2015 22:45:09 +0200 Subject: [PATCH 070/114] Update sdl2 dependency to 0.8 --- Cargo.toml | 2 +- examples/demo.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31695be6..058aa1f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.2" -sdl2 = "0.6" +sdl2 = "0.8" sdl2-sys = "0.6" libc = "0.1" diff --git a/examples/demo.rs b/examples/demo.rs index 5d102d68..1f334e9d 100755 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -22,7 +22,9 @@ fn demo(filename: &Path) { println!("linked version: {}", sdl2_mixer::get_linked_version()); - let _ = sdl2::init().audio().timer(); + let sdl = sdl2::init().unwrap(); + let _audio = sdl.audio().unwrap(); + let mut timer = sdl.timer().unwrap(); println!("mixer initialized: {}", sdl2_mixer::init( INIT_MP3 | INIT_FLAC | INIT_MOD | INIT_FLUIDSYNTH | @@ -66,17 +68,17 @@ fn demo(filename: &Path) { println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); println!("play => {:?}", music.play(1)); - sdl2::timer::delay(10000); + timer.delay(10000); println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); - sdl2::timer::delay(5000); + timer.delay(5000); println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); - sdl2::timer::delay(5000); + timer.delay(5000); sdl2_mixer::Music::halt(); - sdl2::timer::delay(1000); + timer.delay(1000); } // here will print hook_finished From 01f309baee92eb20b7c720cd7ba42c0b15ff05b9 Mon Sep 17 00:00:00 2001 From: andelf Date: Fri, 21 Aug 2015 13:22:37 +0800 Subject: [PATCH 071/114] bump version and publish --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 058aa1f2..1fb7118c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.6.0" +version = "0.7.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From bd8b5c79aee15725b21ad07d4b8e783db4f88d62 Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 22 Sep 2015 23:50:20 +0800 Subject: [PATCH 072/114] bump version, update to newest sdl2 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1fb7118c..5ea37ecd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.7.0" +version = "0.7.1" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.2" -sdl2 = "0.8" +sdl2 = "0.9" sdl2-sys = "0.6" libc = "0.1" From 953f5d70a2465d40297f0c8d8478c2a57c8c63bd Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Sun, 11 Oct 2015 11:43:20 -0700 Subject: [PATCH 073/114] Add simple OSX SDL2 mixer library install instructions. Would have saved me some googling. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1a266260..c0b91ca1 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,10 @@ Rust-SDL2_mixer uses the MIT licence. * SDL2_mixer development libraries * Rust master or nightly +#### OSX + + brew install sdl2_mixer + ## Installation Place the following into your project's Cargo.toml file: From bf68c1572bafd81db7b1d0da8fa58d171def471a Mon Sep 17 00:00:00 2001 From: Xavier Shay Date: Sun, 11 Oct 2015 12:58:42 -0700 Subject: [PATCH 074/114] Add a constructor for Channel. Other considered options: * Channel::new(1) (too long) * Channel(1) (unnecessary dependency on data structure) --- src/sdl2_mixer/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 76e6872a..ee460176 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -249,6 +249,11 @@ pub enum Fading { #[derive(Debug, Copy, Clone, PartialEq)] pub struct Channel(isize); +/// Return a channel object. +pub fn channel(chan: isize) -> Channel { + Channel(chan) +} + /// Set the number of channels being mixed. pub fn allocate_channels(numchans: isize) -> isize { unsafe { From b80c38f640fab8a8df9c111c1b2a4e23ce85c0c4 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Wed, 11 Nov 2015 23:10:36 +0100 Subject: [PATCH 075/114] Updated dependencies - Bumped to 0.8.0 --- Cargo.toml | 10 +++++----- examples/demo.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ea37ecd..7e2e6153 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.7.1" +version = "0.8.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -12,10 +12,10 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.2" -sdl2 = "0.9" -sdl2-sys = "0.6" -libc = "0.1" +bitflags = "0.3.2" +sdl2 = "0.10" +sdl2-sys = "0.7" +libc = "0.2" # [dependencies.sdl2] # git = "https://github.com/AngryLawyer/rust-sdl2" diff --git a/examples/demo.rs b/examples/demo.rs index 1f334e9d..08d0a3c2 100755 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -37,7 +37,7 @@ fn demo(filename: &Path) { { let n = sdl2_mixer::get_chunk_decoders_number(); println!("available chunk(sample) decoders: {}", n); - for i in (0..n) { + for i in 0..n { println!(" decoder {} => {}", i, sdl2_mixer::get_chunk_decoder(i)); } } @@ -45,7 +45,7 @@ fn demo(filename: &Path) { { let n = sdl2_mixer::get_music_decoders_number(); println!("available music decoders: {}", n); - for i in (0..n) { + for i in 0..n { println!(" decoder {} => {}", i, sdl2_mixer::get_music_decoder(i)); } } From 513c294b5899963e7577f22e3ef77d02612b10d1 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Fri, 20 Nov 2015 15:24:40 +0100 Subject: [PATCH 076/114] Updated to latest rust-sdl2 - Bumped to 0.9.0 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7e2e6153..a6779875 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.8.0" +version = "0.9.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.10" +sdl2 = "0.11" sdl2-sys = "0.7" libc = "0.2" From 2bdf12832a3e379d7f523aa296be69b9dca6be7f Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Tue, 1 Dec 2015 23:28:40 +0100 Subject: [PATCH 077/114] Updated dependencies - Bumped to 0.10.0 - Removed `allow(raw_pointer_derive)`, see discussion https://github.com/rust-lang/rust/issues/14615 --- Cargo.toml | 4 ++-- src/sdl2_mixer/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6779875..940be7e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.9.0" +version = "0.10.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.11" +sdl2 = "0.12" sdl2-sys = "0.7" libc = "0.2" diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index ee460176..db83d4bb 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -171,7 +171,7 @@ pub fn get_chunk_decoder(index: isize) -> String { } /// The internal format for an audio chunk. -#[derive(PartialEq)] #[allow(raw_pointer_derive)] +#[derive(PartialEq)] pub struct Chunk { pub raw: *const ffi::Mix_Chunk, pub owned: bool @@ -606,7 +606,7 @@ extern "C" fn c_music_finished_hook() { } /// This is an opaque data type used for Music data. -#[derive(PartialEq)] #[allow(raw_pointer_derive)] +#[derive(PartialEq)] pub struct Music { pub raw: *const ffi::Mix_Music, pub owned: bool, From 923066b37b3c19997c58402d6a9bbc7a8f10279e Mon Sep 17 00:00:00 2001 From: werecat Date: Fri, 1 Jan 2016 22:06:26 -0500 Subject: [PATCH 078/114] Added Sdl2MixerContext and tweaked various other things. Sdl2MixerContext is created with init, and has drop implemented. This means that it will automatically drop at the end, after Music would drop, solving Issue #32. Additionally, I also changed init to check returned flags and produce an error if every single flag is not initialized. This is what InitFlags.to_string() is for. I also updated examples/demo.rs to work with these new changes. Finally, I added some documentation and tweaked some functions slightly. I also ran rustfmt on the project. --- examples/demo.rs | 70 ++++--- src/sdl2_mixer/ffi.rs | 107 ++++++----- src/sdl2_mixer/lib.rs | 438 +++++++++++++++++++++++++++--------------- 3 files changed, 377 insertions(+), 238 deletions(-) mode change 100755 => 100644 examples/demo.rs diff --git a/examples/demo.rs b/examples/demo.rs old mode 100755 new mode 100644 index 08d0a3c2..054b5b44 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -4,8 +4,8 @@ extern crate sdl2_mixer; use std::env; use std::path::Path; use sdl2::*; -use sdl2_mixer::{INIT_MP3, INIT_FLAC, INIT_MOD, INIT_FLUIDSYNTH, INIT_MODPLUG, - INIT_OGG, DEFAULT_FREQUENCY}; +use sdl2_mixer::{INIT_MP3, INIT_FLAC, INIT_MOD, INIT_FLUIDSYNTH, INIT_MODPLUG, INIT_OGG, + AUDIO_S16LSB}; fn main() { @@ -25,13 +25,16 @@ fn demo(filename: &Path) { let sdl = sdl2::init().unwrap(); let _audio = sdl.audio().unwrap(); let mut timer = sdl.timer().unwrap(); + let _mixer_context = sdl2_mixer::init(INIT_MP3 | INIT_FLAC | INIT_MOD | INIT_FLUIDSYNTH | + INIT_MODPLUG | + INIT_OGG) + .unwrap(); - println!("mixer initialized: {}", sdl2_mixer::init( - INIT_MP3 | INIT_FLAC | INIT_MOD | INIT_FLUIDSYNTH | - INIT_MODPLUG | INIT_OGG).bits()); - - // TODO: 0x8010 is SDL_audio flag - let _ = sdl2_mixer::open_audio(DEFAULT_FREQUENCY, 0x8010u16, 2, 1024); + let frequency = 44100; + let format = AUDIO_S16LSB; // signed 16 bit samples, in little-endian byte order + let channels = 2; // Stereo + let chunk_size = 1024; + let _ = sdl2_mixer::open_audio(frequency, format, channels, chunk_size).unwrap(); sdl2_mixer::allocate_channels(0); { @@ -52,38 +55,33 @@ fn demo(filename: &Path) { println!("query spec => {:?}", sdl2_mixer::query_spec()); - // We want music to be freed before we quit sdl2_mixer (otherwise an error - // happens), so we do all of the following within its own block. - { - let music = sdl2_mixer::Music::from_file(filename).unwrap(); - fn hook_finished() { - println!("play ends! from rust cb"); - } + let music = sdl2_mixer::Music::from_file(filename).unwrap(); - sdl2_mixer::Music::hook_finished(hook_finished); - - println!("music => {:?}", music); - println!("music type => {:?}", music.get_type()); - println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); - println!("play => {:?}", music.play(1)); - - timer.delay(10000); - - println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); - - timer.delay(5000); - - println!("fading in from pos ... {:?}", music.fade_in_from_pos(1, 10000, 100.0)); - - timer.delay(5000); - sdl2_mixer::Music::halt(); - timer.delay(1000); + fn hook_finished() { + println!("play ends! from rust cb"); } - // here will print hook_finished + sdl2_mixer::Music::hook_finished(hook_finished); + + println!("music => {:?}", music); + println!("music type => {:?}", music.get_type()); + println!("music volume => {:?}", sdl2_mixer::Music::get_volume()); + println!("play => {:?}", music.play(1)); + + timer.delay(10000); + + println!("fading out ... {:?}", sdl2_mixer::Music::fade_out(4000)); + + timer.delay(5000); + + println!("fading in from pos ... {:?}", + music.fade_in_from_pos(1, 10000, 100.0)); + + timer.delay(5000); + sdl2_mixer::Music::halt(); + timer.delay(1000); + - println!("qutting mixer"); - sdl2_mixer::quit(); println!("quitting sdl"); } diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 1fe48088..78be5ed7 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -36,28 +36,35 @@ pub const MUS_FLAC: c_uint = 8; pub const MUS_MODPLUG: c_uint = 9; pub type Struct__Mix_Music = c_void; pub type Mix_Music = Struct__Mix_Music; -pub type Mix_EffectFunc_t = - ::std::option::Option; -pub type Mix_EffectDone_t = - ::std::option::Option; +pub type Mix_EffectFunc_t = ::std::option::Option; +pub type Mix_EffectDone_t = ::std::option::Option; extern "C" { pub fn Mix_Linked_Version() -> *const SDL_version; pub fn Mix_Init(flags: c_int) -> c_int; pub fn Mix_Quit(); - pub fn Mix_OpenAudio(frequency: c_int, format: uint16_t, channels: c_int, - chunksize: c_int) -> c_int; + pub fn Mix_OpenAudio(frequency: c_int, + format: uint16_t, + channels: c_int, + chunksize: c_int) + -> c_int; pub fn Mix_AllocateChannels(numchans: c_int) -> c_int; - pub fn Mix_QuerySpec(frequency: *const c_int, format: *const uint16_t, - channels: *const c_int) -> c_int; - pub fn Mix_LoadWAV_RW(src: *const SDL_RWops, freesrc: c_int) -> - *const Mix_Chunk; + pub fn Mix_QuerySpec(frequency: *const c_int, + format: *const uint16_t, + channels: *const c_int) + -> c_int; + pub fn Mix_LoadWAV_RW(src: *const SDL_RWops, freesrc: c_int) -> *const Mix_Chunk; pub fn Mix_LoadMUS(file: *const c_char) -> *const Mix_Music; - pub fn Mix_LoadMUS_RW(src: *const SDL_RWops, freesrc: c_int) -> - *const Mix_Music; - pub fn Mix_LoadMUSType_RW(src: *const SDL_RWops, _type: Mix_MusicType, - freesrc: c_int) -> *const Mix_Music; + pub fn Mix_LoadMUS_RW(src: *const SDL_RWops, freesrc: c_int) -> *const Mix_Music; + pub fn Mix_LoadMUSType_RW(src: *const SDL_RWops, + _type: Mix_MusicType, + freesrc: c_int) + -> *const Mix_Music; pub fn Mix_QuickLoad_WAV(mem: *const uint8_t) -> *const Mix_Chunk; pub fn Mix_QuickLoad_RAW(mem: *const uint8_t, len: uint32_t) -> *const Mix_Chunk; pub fn Mix_FreeChunk(chunk: *const Mix_Chunk); @@ -67,31 +74,30 @@ extern "C" { pub fn Mix_GetNumMusicDecoders() -> c_int; pub fn Mix_GetMusicDecoder(index: c_int) -> *const c_char; pub fn Mix_GetMusicType(music: *const Mix_Music) -> Mix_MusicType; - pub fn Mix_SetPostMix(mix_func: - ::std::option::Option, + pub fn Mix_SetPostMix(mix_func: ::std::option::Option, arg: *const c_void); - pub fn Mix_HookMusic(mix_func: - ::std::option::Option, + pub fn Mix_HookMusic(mix_func: ::std::option::Option, arg: *const c_void); - pub fn Mix_HookMusicFinished(music_finished: - ::std::option::Option); + pub fn Mix_HookMusicFinished(music_finished: ::std::option::Option); pub fn Mix_GetMusicHookData() -> *const c_void; pub fn Mix_ChannelFinished(channel_finished: ::std::option::Option); - pub fn Mix_RegisterEffect(chan: c_int, f: Mix_EffectFunc_t, - d: Mix_EffectDone_t, arg: *const c_void) -> c_int; + pub fn Mix_RegisterEffect(chan: c_int, + f: Mix_EffectFunc_t, + d: Mix_EffectDone_t, + arg: *const c_void) + -> c_int; pub fn Mix_UnregisterEffect(channel: c_int, f: Mix_EffectFunc_t) -> c_int; pub fn Mix_UnregisterAllEffects(channel: c_int) -> c_int; pub fn Mix_SetPanning(channel: c_int, left: uint8_t, right: uint8_t) -> c_int; - pub fn Mix_SetPosition(channel: c_int, angle: int16_t, distance: uint8_t) -> - c_int; + pub fn Mix_SetPosition(channel: c_int, angle: int16_t, distance: uint8_t) -> c_int; pub fn Mix_SetDistance(channel: c_int, distance: uint8_t) -> c_int; pub fn Mix_SetReverseStereo(channel: c_int, flip: c_int) -> c_int; pub fn Mix_ReserveChannels(num: c_int) -> c_int; @@ -101,16 +107,24 @@ extern "C" { pub fn Mix_GroupCount(tag: c_int) -> c_int; pub fn Mix_GroupOldest(tag: c_int) -> c_int; pub fn Mix_GroupNewer(tag: c_int) -> c_int; - pub fn Mix_PlayChannelTimed(channel: c_int, chunk: *const Mix_Chunk, - loops: c_int, ticks: c_int) -> c_int; + pub fn Mix_PlayChannelTimed(channel: c_int, + chunk: *const Mix_Chunk, + loops: c_int, + ticks: c_int) + -> c_int; pub fn Mix_PlayMusic(music: *const Mix_Music, loops: c_int) -> c_int; - pub fn Mix_FadeInMusic(music: *const Mix_Music, loops: c_int, ms: c_int) -> - c_int; - pub fn Mix_FadeInMusicPos(music: *const Mix_Music, loops: c_int, ms: c_int, - position: c_double) -> c_int; - pub fn Mix_FadeInChannelTimed(channel: c_int, chunk: *const Mix_Chunk, - loops: c_int, ms: c_int, ticks: c_int) -> - c_int; + pub fn Mix_FadeInMusic(music: *const Mix_Music, loops: c_int, ms: c_int) -> c_int; + pub fn Mix_FadeInMusicPos(music: *const Mix_Music, + loops: c_int, + ms: c_int, + position: c_double) + -> c_int; + pub fn Mix_FadeInChannelTimed(channel: c_int, + chunk: *const Mix_Chunk, + loops: c_int, + ms: c_int, + ticks: c_int) + -> c_int; pub fn Mix_Volume(channel: c_int, volume: c_int) -> c_int; pub fn Mix_VolumeChunk(chunk: *const Mix_Chunk, volume: c_int) -> c_int; pub fn Mix_VolumeMusic(volume: c_int) -> c_int; @@ -138,12 +152,11 @@ extern "C" { pub fn Mix_GetSynchroValue() -> c_int; pub fn Mix_SetSoundFonts(paths: *const c_char) -> c_int; pub fn Mix_GetSoundFonts() -> *const c_char; - pub fn Mix_EachSoundFont(function: - ::std::option::Option c_int>, - data: *const c_void) -> c_int; + pub fn Mix_EachSoundFont(function: ::std::option::Option c_int>, + data: *const c_void) + -> c_int; pub fn Mix_GetChunk(channel: c_int) -> *const Mix_Chunk; pub fn Mix_CloseAudio(); } diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index db83d4bb..4011d834 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -1,6 +1,5 @@ -/*! -A binding for SDL2_mixer. - */ +//! A binding for SDL2_mixer. +//! #![crate_name = "sdl2_mixer"] #![crate_type = "lib"] @@ -28,17 +27,20 @@ use sdl2::SdlResult; mod mac { #[cfg(mac_framework)] #[link(kind="framework", name="SDL2_mixer")] - extern {} + extern "C" { + } #[cfg(not(mac_framework))] #[link(name="SDL2_mixer")] - extern {} + extern "C" { + } } #[cfg(any(target_os="windows", target_os="linux", target_os="freebsd"))] mod others { #[link(name="SDL2_mixer")] - extern {} + extern "C" { + } } #[allow(non_camel_case_types, dead_code)] @@ -49,62 +51,60 @@ mod ffi; mod ll { use libc::uint16_t; - pub const AUDIO_U8 : uint16_t = 0x0008; - pub const AUDIO_S8 : uint16_t = 0x8008; - pub const AUDIO_U16LSB : uint16_t = 0x0010; - pub const AUDIO_S16LSB : uint16_t = 0x8010; - pub const AUDIO_U16MSB : uint16_t = 0x1010; - pub const AUDIO_S16MSB : uint16_t = 0x9010; - pub const AUDIO_U16 : uint16_t = AUDIO_U16LSB; - pub const AUDIO_S16 : uint16_t = AUDIO_S16LSB; - pub const AUDIO_S32LSB : uint16_t = 0x8020; - pub const AUDIO_S32MSB : uint16_t = 0x9020; - pub const AUDIO_S32 : uint16_t = AUDIO_S32LSB; - pub const AUDIO_F32LSB : uint16_t = 0x8120; - pub const AUDIO_F32MSB : uint16_t = 0x9120; - pub const AUDIO_F32 : uint16_t = AUDIO_F32LSB; - pub const AUDIO_U16SYS : uint16_t = AUDIO_U16LSB; - pub const AUDIO_S16SYS : uint16_t = AUDIO_S16LSB; - pub const AUDIO_S32SYS : uint16_t = AUDIO_S32LSB; - pub const AUDIO_F32SYS : uint16_t = AUDIO_F32LSB; + pub const AUDIO_U8: uint16_t = 0x0008; + pub const AUDIO_S8: uint16_t = 0x8008; + pub const AUDIO_U16LSB: uint16_t = 0x0010; + pub const AUDIO_S16LSB: uint16_t = 0x8010; + pub const AUDIO_U16MSB: uint16_t = 0x1010; + pub const AUDIO_S16MSB: uint16_t = 0x9010; + pub const AUDIO_U16: uint16_t = AUDIO_U16LSB; + pub const AUDIO_S16: uint16_t = AUDIO_S16LSB; + pub const AUDIO_S32LSB: uint16_t = 0x8020; + pub const AUDIO_S32MSB: uint16_t = 0x9020; + pub const AUDIO_S32: uint16_t = AUDIO_S32LSB; + pub const AUDIO_F32LSB: uint16_t = 0x8120; + pub const AUDIO_F32MSB: uint16_t = 0x9120; + pub const AUDIO_F32: uint16_t = AUDIO_F32LSB; + pub const AUDIO_U16SYS: uint16_t = AUDIO_U16LSB; + pub const AUDIO_S16SYS: uint16_t = AUDIO_S16LSB; + pub const AUDIO_S32SYS: uint16_t = AUDIO_S32LSB; + pub const AUDIO_F32SYS: uint16_t = AUDIO_F32LSB; } pub type AudioFormat = uint16_t; -pub const AUDIO_U8 : AudioFormat = ll::AUDIO_U8; -pub const AUDIO_S8 : AudioFormat = ll::AUDIO_S8; -pub const AUDIO_U16LSB : AudioFormat = ll::AUDIO_U16LSB; -pub const AUDIO_S16LSB : AudioFormat = ll::AUDIO_S16LSB; -pub const AUDIO_U16MSB : AudioFormat = ll::AUDIO_U16MSB; -pub const AUDIO_S16MSB : AudioFormat = ll::AUDIO_S16MSB; -pub const AUDIO_U16 : AudioFormat = ll::AUDIO_U16; -pub const AUDIO_S16 : AudioFormat = ll::AUDIO_S16; -pub const AUDIO_S32LSB : AudioFormat = ll::AUDIO_S32LSB; -pub const AUDIO_S32MSB : AudioFormat = ll::AUDIO_S32MSB; -pub const AUDIO_S32 : AudioFormat = ll::AUDIO_S32; -pub const AUDIO_F32LSB : AudioFormat = ll::AUDIO_F32LSB; -pub const AUDIO_F32MSB : AudioFormat = ll::AUDIO_F32MSB; -pub const AUDIO_F32 : AudioFormat = ll::AUDIO_F32; -pub const AUDIO_U16SYS : AudioFormat = ll::AUDIO_U16SYS; -pub const AUDIO_S16SYS : AudioFormat = ll::AUDIO_S16SYS; -pub const AUDIO_S32SYS : AudioFormat = ll::AUDIO_S32SYS; -pub const AUDIO_F32SYS : AudioFormat = ll::AUDIO_F32SYS; +pub const AUDIO_U8: AudioFormat = ll::AUDIO_U8; +pub const AUDIO_S8: AudioFormat = ll::AUDIO_S8; +pub const AUDIO_U16LSB: AudioFormat = ll::AUDIO_U16LSB; +pub const AUDIO_S16LSB: AudioFormat = ll::AUDIO_S16LSB; +pub const AUDIO_U16MSB: AudioFormat = ll::AUDIO_U16MSB; +pub const AUDIO_S16MSB: AudioFormat = ll::AUDIO_S16MSB; +pub const AUDIO_U16: AudioFormat = ll::AUDIO_U16; +pub const AUDIO_S16: AudioFormat = ll::AUDIO_S16; +pub const AUDIO_S32LSB: AudioFormat = ll::AUDIO_S32LSB; +pub const AUDIO_S32MSB: AudioFormat = ll::AUDIO_S32MSB; +pub const AUDIO_S32: AudioFormat = ll::AUDIO_S32; +pub const AUDIO_F32LSB: AudioFormat = ll::AUDIO_F32LSB; +pub const AUDIO_F32MSB: AudioFormat = ll::AUDIO_F32MSB; +pub const AUDIO_F32: AudioFormat = ll::AUDIO_F32; +pub const AUDIO_U16SYS: AudioFormat = ll::AUDIO_U16SYS; +pub const AUDIO_S16SYS: AudioFormat = ll::AUDIO_S16SYS; +pub const AUDIO_S32SYS: AudioFormat = ll::AUDIO_S32SYS; +pub const AUDIO_F32SYS: AudioFormat = ll::AUDIO_F32SYS; /// The suggested default is signed 16bit samples in host byte order. pub const DEFAULT_FORMAT: AudioFormat = ll::AUDIO_S16SYS; /// Defualt channels: Stereo. -pub const DEFAULT_CHANNELS : isize = 2; +pub const DEFAULT_CHANNELS: isize = 2; /// Good default sample rate in Hz (samples per second) for PC sound cards. -pub const DEFAULT_FREQUENCY : isize = 22050; +pub const DEFAULT_FREQUENCY: isize = 22050; /// Maximum value for any volume setting. -pub const MAX_VOLUME : isize = 128; +pub const MAX_VOLUME: isize = 128; /// Returns the version of the dynamically linked SDL_mixer library pub fn get_linked_version() -> Version { - unsafe { - Version::from_ll(*ffi::Mix_Linked_Version()) - } + unsafe { Version::from_ll(*ffi::Mix_Linked_Version()) } } bitflags!(flags InitFlag : u32 { @@ -116,25 +116,83 @@ bitflags!(flags InitFlag : u32 { const INIT_FLUIDSYNTH = ffi::MIX_INIT_FLUIDSYNTH as u32 }); -/// Loads dynamic libraries and prepares them for use. Flags should be -/// one or more flags from InitFlag. -pub fn init(flags: InitFlag) -> InitFlag { - unsafe { - let ret = ffi::Mix_Init(flags.bits() as c_int); - InitFlag::from_bits_truncate(ret as u32) +impl ToString for InitFlag { + fn to_string(&self) -> String { + let mut string = "".to_string(); + if self.contains(INIT_FLAC) { + string = string + &"INIT_FLAC ".to_string(); + } + if self.contains(INIT_MOD) { + string = string + &"INIT_MOD ".to_string(); + } + if self.contains(INIT_MODPLUG) { + string = string + &"INIT_MODPLUG ".to_string(); + } + if self.contains(INIT_MP3) { + string = string + &"INIT_MP3 ".to_string(); + } + if self.contains(INIT_OGG) { + string = string + &"INIT_OGG ".to_string(); + } + if self.contains(INIT_FLUIDSYNTH) { + string = string + &"INIT_FLUIDSYNTH ".to_string(); + } + string } } +/// Context manager for sdl2_mixer to manage init and quit +pub struct Sdl2MixerContext; + /// Cleans up all dynamically loaded library handles, freeing memory. -pub fn quit() { - unsafe { ffi::Mix_Quit(); } +impl Drop for Sdl2MixerContext { + fn drop(&mut self) { + unsafe { + ffi::Mix_Quit(); + } + } } +/// Loads dynamic libraries and prepares them for use. Flags should be +/// one or more flags from InitFlag. +pub fn init(flags: InitFlag) -> SdlResult { + let return_flags = unsafe { + let ret = ffi::Mix_Init(flags.bits() as c_int); + InitFlag::from_bits_truncate(ret as u32) + }; + // Check if all init flags were set + if !flags.intersects(return_flags) { + // Flags not matching won't always set the error message text + // according to sdl docs + if get_error() == sdl2::ErrorMessage("".to_string()) { + let un_init_flags = return_flags ^ flags; + let error_str = &("Could not init: ".to_string() + &un_init_flags.to_string()); + sdl2::set_error(error_str); + } + Err(get_error()) + } else { + Ok(Sdl2MixerContext) + } +} + + /// Open the mixer with a certain audio format. -pub fn open_audio(frequency: isize, format: AudioFormat, channels: isize, chunksize: isize) -> SdlResult<()> { - let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, channels as c_int, chunksize as c_int) }; - if ret == 0 { Ok(()) } - else { Err(get_error()) } +pub fn open_audio(frequency: isize, + format: AudioFormat, + channels: isize, + chunksize: isize) + -> SdlResult<()> { + let ret = unsafe { + ffi::Mix_OpenAudio(frequency as c_int, + format, + channels as c_int, + chunksize as c_int) + }; + if ret == 0 { + Ok(()) + } else { + Err(get_error()) + } } /// Shutdown and cleanup the mixer API. @@ -144,9 +202,9 @@ pub fn close_audio() { /// Get the actual audio format in use by the opened audio device. pub fn query_spec() -> SdlResult<(isize, AudioFormat, isize)> { - let frequency : c_int = 0; - let format : uint16_t = 0; - let channels : c_int = 0; + let frequency: c_int = 0; + let format: uint16_t = 0; + let channels: c_int = 0; let ret = unsafe { ffi::Mix_QuerySpec(&frequency, &format, &channels) }; if ret == 0 { Err(get_error()) @@ -164,7 +222,7 @@ pub fn get_chunk_decoders_number() -> isize { /// Get the name of the indexed sample chunk decoder. pub fn get_chunk_decoder(index: isize) -> String { - unsafe { + unsafe { let name = ffi::Mix_GetChunkDecoder(index as c_int); from_utf8(CStr::from_ptr(name).to_bytes()).unwrap().to_owned() } @@ -174,7 +232,7 @@ pub fn get_chunk_decoder(index: isize) -> String { #[derive(PartialEq)] pub struct Chunk { pub raw: *const ffi::Mix_Chunk, - pub owned: bool + pub owned: bool, } impl Drop for Chunk { @@ -188,28 +246,25 @@ impl Drop for Chunk { impl Chunk { /// Load file for use as a sample. pub fn from_file(path: &Path) -> SdlResult { - let raw = unsafe { - ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw(), 0) - }; + let raw = unsafe { ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw(), 0) }; if raw.is_null() { Err(get_error()) } else { - Ok(Chunk{ raw: raw, owned: true }) + Ok(Chunk { + raw: raw, + owned: true, + }) } } /// Set chunk->volume to volume. pub fn set_volume(&mut self, volume: isize) -> isize { - unsafe { - ffi::Mix_VolumeChunk(self.raw, volume as c_int) as isize - } + unsafe { ffi::Mix_VolumeChunk(self.raw, volume as c_int) as isize } } /// current volume for the chunk. pub fn get_volume(&self) -> isize { - unsafe { - ffi::Mix_VolumeChunk(self.raw, -1) as isize - } + unsafe { ffi::Mix_VolumeChunk(self.raw, -1) as isize } } } @@ -222,13 +277,14 @@ pub trait LoaderRWops { impl<'a> LoaderRWops for RWops<'a> { /// Load src for use as a sample. fn load_wav(&self) -> SdlResult { - let raw = unsafe { - ffi::Mix_LoadWAV_RW(self.raw(), 0) - }; + let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw(), 0) }; if raw == ptr::null() { Err(get_error()) } else { - Ok(Chunk{ raw: raw, owned: true }) + Ok(Chunk { + raw: raw, + owned: true, + }) } } } @@ -240,9 +296,9 @@ impl<'a> LoaderRWops for RWops<'a> { #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Hash)] pub enum Fading { - NoFading = ffi::MIX_NO_FADING as isize, + NoFading = ffi::MIX_NO_FADING as isize, FadingOut = ffi::MIX_FADING_OUT as isize, - FadingIn = ffi::MIX_FADING_IN as isize + FadingIn = ffi::MIX_FADING_IN as isize, } /// Sound effect channel. @@ -256,9 +312,7 @@ pub fn channel(chan: isize) -> Channel { /// Set the number of channels being mixed. pub fn allocate_channels(numchans: isize) -> isize { - unsafe { - ffi::Mix_AllocateChannels(numchans as c_int) as isize - } + unsafe { ffi::Mix_AllocateChannels(numchans as c_int) as isize } } static mut channel_finished_callback: Option = None; @@ -267,9 +321,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { unsafe { match channel_finished_callback { None => (), - Some(cb) => { - cb(Channel(ch as isize)) - } + Some(cb) => cb(Channel(ch as isize)), } } } @@ -278,10 +330,12 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { pub fn set_channel_finished(f: fn(Channel)) { unsafe { channel_finished_callback = Some(f); - ffi::Mix_ChannelFinished(Some(c_channel_finished_callback as extern "C" fn (ch: c_int))); + ffi::Mix_ChannelFinished(Some(c_channel_finished_callback as extern "C" fn(ch: c_int))); } } +/// Unhooks the specified function set before, so no function is called when channel playback is +/// halted. pub fn unset_channel_finished() { unsafe { ffi::Mix_ChannelFinished(None); @@ -303,16 +357,13 @@ impl Channel { /// Set the volume for any allocated channel. pub fn set_volume(self, volume: isize) -> isize { let Channel(ch) = self; - unsafe { - ffi::Mix_Volume(ch as c_int, volume as c_int) as isize - } + unsafe { ffi::Mix_Volume(ch as c_int, volume as c_int) as isize } } + /// Returns the channels volume on scale of 0 to 128. pub fn get_volume(self) -> isize { let Channel(ch) = self; - unsafe { - ffi::Mix_Volume(ch as c_int, -1) as isize - } + unsafe { ffi::Mix_Volume(ch as c_int, -1) as isize } } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. @@ -337,10 +388,19 @@ impl Channel { self.fade_in_timed(chunk, loops, ms, -1) } - pub fn fade_in_timed(self, chunk: &Chunk, loops: isize, ms: isize, ticks: isize) -> SdlResult { + pub fn fade_in_timed(self, + chunk: &Chunk, + loops: isize, + ms: isize, + ticks: isize) + -> SdlResult { let Channel(ch) = self; let ret = unsafe { - ffi::Mix_FadeInChannelTimed(ch as c_int, chunk.raw, loops as c_int, ms as c_int, ticks as c_int) + ffi::Mix_FadeInChannelTimed(ch as c_int, + chunk.raw, + loops as c_int, + ms as c_int, + ticks as c_int) }; if ret == -1 { Err(get_error()) @@ -352,19 +412,25 @@ impl Channel { /// Pause channel, or all playing channels if -1 is passed in. pub fn pause(self) { let Channel(ch) = self; - unsafe { ffi::Mix_Pause(ch as c_int); } + unsafe { + ffi::Mix_Pause(ch as c_int); + } } /// Unpause channel, or all playing and paused channels if -1 is passed in. pub fn resume(self) { let Channel(ch) = self; - unsafe { ffi::Mix_Resume(ch as c_int); } + unsafe { + ffi::Mix_Resume(ch as c_int); + } } /// Halt channel playback pub fn halt(self) { let Channel(ch) = self; - unsafe { ffi::Mix_HaltChannel(ch as c_int); } + unsafe { + ffi::Mix_HaltChannel(ch as c_int); + } } /// Halt channel playback, after ticks milliseconds. @@ -396,10 +462,10 @@ impl Channel { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as c_uint }; match ret { - ffi::MIX_NO_FADING => Fading::NoFading, + ffi::MIX_NO_FADING => Fading::NoFading, ffi::MIX_FADING_OUT => Fading::FadingOut, - ffi::MIX_FADING_IN => Fading::FadingIn, - _ => Fading::NoFading + ffi::MIX_FADING_IN => Fading::FadingIn, + _ => Fading::NoFading, } } @@ -410,7 +476,10 @@ impl Channel { if raw.is_null() { None } else { - Some( Chunk { raw: raw, owned: false } ) + Some(Chunk { + raw: raw, + owned: false, + }) } } @@ -425,6 +494,8 @@ impl Channel { } } + /// Sets a panning effect, where left and right is the volume of the left and right channels. + /// They range from 0 (silence) to 255 (loud). pub fn set_panning(self, left: u8, right: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, left, right) }; @@ -435,7 +506,19 @@ impl Channel { } } + /// Unregisters panning effect. + pub fn unset_panning(self) -> SdlResult<()> { + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, 255, 255) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + /// This effect simulates a simple attenuation of volume due to distance. + /// distance ranges from 0 (close/loud) to 255 (far/quiet). pub fn set_distance(self, distance: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, distance) }; @@ -446,7 +529,20 @@ impl Channel { } } + /// Unregisters distance effect. + pub fn unset_distance(self) -> SdlResult<()> { + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, 0) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + /// This effect emulates a simple 3D audio effect. + /// angle ranges from 0 to 360 degrees going clockwise, where 0 is directly in front. + /// distance ranges from 0 (close/loud) to 255 (far/quiet). pub fn set_position(self, angle: i16, distance: u8) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, angle, distance) }; @@ -457,7 +553,19 @@ impl Channel { } } + /// Unregisters position effect. + pub fn unset_position(self) -> SdlResult<()> { + let Channel(ch) = self; + let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, 0, 0) }; + if ret == 0 { + Err(get_error()) + } else { + Ok(()) + } + } + /// Simple reverse stereo, swaps left and right channel sound. + /// true for reverse, false to unregister effect. pub fn set_reverse_stereo(self, flip: bool) -> SdlResult<()> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetReverseStereo(ch as c_int, flip as c_int) }; @@ -469,10 +577,12 @@ impl Channel { } } +/// Returns how many channels are currently playing. pub fn get_playing_channels_number() -> isize { unsafe { ffi::Mix_Playing(-1) as isize } } +/// Returns how many channels are currently paused. pub fn get_paused_channels_number() -> isize { unsafe { ffi::Mix_Paused(-1) as isize } } @@ -558,7 +668,9 @@ impl Group { /// Halt playback on all channels in group. pub fn halt(self) { let Group(g) = self; - unsafe { ffi::Mix_HaltGroup(g as c_int); } + unsafe { + ffi::Mix_HaltGroup(g as c_int); + } } } @@ -581,28 +693,28 @@ pub fn get_music_decoder(index: isize) -> String { #[repr(C)] #[derive(Copy, Clone, PartialEq, Hash, Debug)] pub enum MusicType { - MusicNone = ffi::MUS_NONE as isize, - MusicCmd = ffi::MUS_CMD as isize, - MusicWav = ffi::MUS_WAV as isize, - MusicMod = ffi::MUS_MOD as isize, - MusicMid = ffi::MUS_MID as isize, - MusicOgg = ffi::MUS_OGG as isize, - MusicMp3 = ffi::MUS_MP3 as isize, - MusicMp3Mad = ffi::MUS_MP3_MAD as isize, - MusicFlac = ffi::MUS_FLAC as isize, - MusicModPlug = ffi::MUS_MODPLUG as isize + MusicNone = ffi::MUS_NONE as isize, + MusicCmd = ffi::MUS_CMD as isize, + MusicWav = ffi::MUS_WAV as isize, + MusicMod = ffi::MUS_MOD as isize, + MusicMid = ffi::MUS_MID as isize, + MusicOgg = ffi::MUS_OGG as isize, + MusicMp3 = ffi::MUS_MP3 as isize, + MusicMp3Mad = ffi::MUS_MP3_MAD as isize, + MusicFlac = ffi::MUS_FLAC as isize, + MusicModPlug = ffi::MUS_MODPLUG as isize, } // hooks static mut music_finished_hook: Option = None; extern "C" fn c_music_finished_hook() { - unsafe { match music_finished_hook { - None => (), - Some(f) => { - f() + unsafe { + match music_finished_hook { + None => (), + Some(f) => f(), } - } } + } } /// This is an opaque data type used for Music data. @@ -636,7 +748,10 @@ impl Music { if raw.is_null() { Err(get_error()) } else { - Ok(Music{ raw: raw, owned: true }) + Ok(Music { + raw: raw, + owned: true, + }) } } @@ -644,25 +759,23 @@ impl Music { pub fn get_type(&self) -> MusicType { let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize } as c_uint; match ret { - ffi::MUS_NONE => MusicType::MusicNone, - ffi::MUS_CMD => MusicType::MusicCmd, - ffi::MUS_WAV => MusicType::MusicWav, - ffi::MUS_MOD => MusicType::MusicMod, - ffi::MUS_MID => MusicType::MusicMid, - ffi::MUS_OGG => MusicType::MusicOgg, - ffi::MUS_MP3 => MusicType::MusicMp3, + ffi::MUS_NONE => MusicType::MusicNone, + ffi::MUS_CMD => MusicType::MusicCmd, + ffi::MUS_WAV => MusicType::MusicWav, + ffi::MUS_MOD => MusicType::MusicMod, + ffi::MUS_MID => MusicType::MusicMid, + ffi::MUS_OGG => MusicType::MusicOgg, + ffi::MUS_MP3 => MusicType::MusicMp3, ffi::MUS_MP3_MAD => MusicType::MusicMp3Mad, - ffi::MUS_FLAC => MusicType::MusicFlac, + ffi::MUS_FLAC => MusicType::MusicFlac, ffi::MUS_MODPLUG => MusicType::MusicModPlug, - _ => MusicType::MusicNone + _ => MusicType::MusicNone, } } /// Play the loaded music loop times through from start to finish. pub fn play(&self, loops: isize) -> SdlResult<()> { - let ret = unsafe { - ffi::Mix_PlayMusic(self.raw, loops as c_int) - }; + let ret = unsafe { ffi::Mix_PlayMusic(self.raw, loops as c_int) }; if ret == -1 { Err(get_error()) } else { @@ -673,9 +786,7 @@ impl Music { /// Fade in over ms milliseconds of time, the loaded music, /// playing it loop times through from start to finish. pub fn fade_in(&self, loops: isize, ms: isize) -> SdlResult<()> { - let ret = unsafe { - ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) - }; + let ret = unsafe { ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) }; if ret == -1 { Err(get_error()) } else { @@ -696,35 +807,42 @@ impl Music { } // FIXME: make these class method? + /// Returns current volume pub fn get_volume() -> isize { unsafe { ffi::Mix_VolumeMusic(-1) as isize } } - /// Set the volume. - pub fn set_volume(volume: isize) -> isize { - unsafe { ffi::Mix_VolumeMusic(volume as c_int) as isize } + /// Set the volume on a scale of 0 to 128. + /// Values greater than 128 will use 128. + pub fn set_volume(volume: isize) { + // This shouldn't return anything. Use get_volume instead + let _ = unsafe { ffi::Mix_VolumeMusic(volume as c_int) as isize }; } /// Pause the music playback. pub fn pause() { - unsafe { ffi::Mix_PauseMusic(); } + unsafe { + ffi::Mix_PauseMusic(); + } } /// Unpause the music. pub fn resume() { - unsafe { ffi::Mix_ResumeMusic(); } + unsafe { + ffi::Mix_ResumeMusic(); + } } /// Rewind the music to the start. pub fn rewind() { - unsafe { ffi::Mix_RewindMusic(); } + unsafe { + ffi::Mix_RewindMusic(); + } } /// Set the position of the currently playing music. pub fn set_pos(position: f64) -> SdlResult<()> { - let ret = unsafe { - ffi::Mix_SetMusicPosition(position as c_double) - }; + let ret = unsafe { ffi::Mix_SetMusicPosition(position as c_double) }; if ret == -1 { Err(get_error()) } else { @@ -734,9 +852,7 @@ impl Music { /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> SdlResult<()> { - let ret = unsafe { - ffi::Mix_SetMusicCMD(CString::new(command).unwrap().as_ptr()) - }; + let ret = unsafe { ffi::Mix_SetMusicCMD(CString::new(command).unwrap().as_ptr()) }; if ret == -1 { Err(get_error()) } else { @@ -746,14 +862,14 @@ impl Music { /// Halt playback of music. pub fn halt() { - unsafe { ffi::Mix_HaltMusic(); } + unsafe { + ffi::Mix_HaltMusic(); + } } /// Gradually fade out the music over ms milliseconds starting from now. pub fn fade_out(ms: isize) -> SdlResult<()> { - let ret = unsafe { - ffi::Mix_FadeOutMusic(ms as c_int) - }; + let ret = unsafe { ffi::Mix_FadeOutMusic(ms as c_int) }; if ret == -1 { Err(get_error()) } else { @@ -764,13 +880,25 @@ impl Music { // TODO: Mix_HookMusic // TODO: Mix_GetMusicHookData + /// Sets up a function to be called when music playback is halted. + /// + /// # Examples + /// + /// ``` + /// fn after_music() { + /// println!("Music has ended"); + /// } + /// + /// sdl2_mixer::Music::hook_finished(after_music); + /// ``` pub fn hook_finished(f: fn()) { unsafe { music_finished_hook = Some(f); - ffi::Mix_HookMusicFinished(Some(c_music_finished_hook as extern "C" fn ())); + ffi::Mix_HookMusicFinished(Some(c_music_finished_hook as extern "C" fn())); } } + /// A previously set up function would no longer be called when music playback is halted. pub fn unhook_finished() { unsafe { ffi::Mix_HookMusicFinished(None); @@ -793,10 +921,10 @@ impl Music { pub fn get_fading() -> Fading { let ret = unsafe { ffi::Mix_FadingMusic() as isize } as c_uint; match ret { - ffi::MIX_NO_FADING => Fading::NoFading, + ffi::MIX_NO_FADING => Fading::NoFading, ffi::MIX_FADING_OUT => Fading::FadingOut, - ffi::MIX_FADING_IN => Fading::FadingIn, - _ => Fading::NoFading + ffi::MIX_FADING_IN => Fading::FadingIn, + _ => Fading::NoFading, } } } From 1af508c496a0914724df6740ff0545f0ff594c49 Mon Sep 17 00:00:00 2001 From: werecat Date: Fri, 1 Jan 2016 22:18:33 -0500 Subject: [PATCH 079/114] Bumped up version --- Cargo.toml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 940be7e7..3c205d40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.10.0" +version = "1.0.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] diff --git a/README.md b/README.md index c0b91ca1..9b3820d6 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.6.0" +sdl2_mixer = "1.0.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From 398aac7422d30fd2fca213de6ab47b63ed692255 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 6 Jan 2016 00:02:48 +0800 Subject: [PATCH 080/114] fix version num --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3c205d40..a6693dc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "1.0.0" +version = "0.11.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From bf3ce8befa0003cb36f2dffae12181d97964ea0b Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 6 Jan 2016 00:19:41 +0800 Subject: [PATCH 081/114] fix sdl2-sys version conflicts --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a6693dc8..0819b428 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" sdl2 = "0.12" -sdl2-sys = "0.7" +sdl2-sys = "0.8" libc = "0.2" # [dependencies.sdl2] From 51805c144bb07a70619727c1205ce1266b452dae Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 6 Jan 2016 00:45:44 +0800 Subject: [PATCH 082/114] add stable to travis-ci rust version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6b823e06..5deeccdb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: rust rust: - nightly - beta - + - stable env: global: - secure: JLix99cbDVr9+s9QOux8FmnyEHGmpNHavKoO7lo1dG4Em8XyeByIXeBNggYX3d/vpdQ+FrqNO+3HrRJpZvXl1OO3uPGFtIQLH6bXLTaggC3z/67maEQSQQS8rcSHImkg1CDKoPWKvZfnB6IqWRYLTBJWILwBLoocure7/+/8III= From 832f6daa37ba57d988d3d2b6c2209eda8aece732 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 6 Jan 2016 11:49:16 +0800 Subject: [PATCH 083/114] drop nightly support --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5deeccdb..bf6540cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: rust rust: - - nightly - beta - stable env: From d17cce98a149e1e97a3060337df3550a068c953d Mon Sep 17 00:00:00 2001 From: werecat Date: Wed, 6 Jan 2016 00:44:15 -0500 Subject: [PATCH 084/114] Updated README version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b3820d6..648e8bbc 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "1.0.0" +sdl2_mixer = "0.11.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From cf18e30b6bd913acce09b1a22af0308e9728beb4 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Thu, 7 Jan 2016 19:17:47 +0100 Subject: [PATCH 085/114] Updated dependencies - Bumped to 0.12.0 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0819b428..d0f9d5f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.11.0" +version = "0.12.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.12" +sdl2 = "0.13" sdl2-sys = "0.8" libc = "0.2" From f38b7f71fcfffdd7353e76c5991d50d2c51639ee Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 12 Jan 2016 20:54:52 +0800 Subject: [PATCH 086/114] README: update version description --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 648e8bbc..d4647e46 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,14 @@ Rust-SDL2_mixer uses the MIT licence. #### OSX brew install sdl2_mixer - + ## Installation Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.11.0" +sdl2_mixer = "0.12.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From b1b63344315e2d1e0d25844a8274f0b12a843d06 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Wed, 10 Feb 2016 19:30:25 +0100 Subject: [PATCH 087/114] Update dependencies - Bumped to 0.13.0 - Removed unused `use` in demo example --- Cargo.toml | 4 ++-- examples/demo.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d0f9d5f9..e8eb5ea5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.12.0" +version = "0.13.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.13" +sdl2 = "0.14" sdl2-sys = "0.8" libc = "0.2" diff --git a/examples/demo.rs b/examples/demo.rs index 054b5b44..469bbddf 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -3,7 +3,6 @@ extern crate sdl2_mixer; use std::env; use std::path::Path; -use sdl2::*; use sdl2_mixer::{INIT_MP3, INIT_FLAC, INIT_MOD, INIT_FLUIDSYNTH, INIT_MODPLUG, INIT_OGG, AUDIO_S16LSB}; From bcb4893d3e147befadea9d87d74dd04cbff5da82 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Fri, 26 Feb 2016 11:42:31 +0100 Subject: [PATCH 088/114] Update to sdl2 0.15 --- Cargo.toml | 2 +- src/sdl2_mixer/lib.rs | 55 +++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e8eb5ea5..b3fdf307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.14" +sdl2 = "0.15" sdl2-sys = "0.8" libc = "0.2" diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 4011d834..cceecc6d 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -20,7 +20,6 @@ use libc::{c_int, uint16_t, c_double, c_uint}; use sdl2::get_error; use sdl2::rwops::RWops; use sdl2::version::Version; -use sdl2::SdlResult; // Setup linking for all targets. #[cfg(target_os="macos")] @@ -155,7 +154,7 @@ impl Drop for Sdl2MixerContext { /// Loads dynamic libraries and prepares them for use. Flags should be /// one or more flags from InitFlag. -pub fn init(flags: InitFlag) -> SdlResult { +pub fn init(flags: InitFlag) -> Result { let return_flags = unsafe { let ret = ffi::Mix_Init(flags.bits() as c_int); InitFlag::from_bits_truncate(ret as u32) @@ -164,10 +163,10 @@ pub fn init(flags: InitFlag) -> SdlResult { if !flags.intersects(return_flags) { // Flags not matching won't always set the error message text // according to sdl docs - if get_error() == sdl2::ErrorMessage("".to_string()) { + if get_error() == "" { let un_init_flags = return_flags ^ flags; let error_str = &("Could not init: ".to_string() + &un_init_flags.to_string()); - sdl2::set_error(error_str); + let _ = sdl2::set_error(error_str); } Err(get_error()) } else { @@ -181,7 +180,7 @@ pub fn open_audio(frequency: isize, format: AudioFormat, channels: isize, chunksize: isize) - -> SdlResult<()> { + -> Result<(), String> { let ret = unsafe { ffi::Mix_OpenAudio(frequency as c_int, format, @@ -201,7 +200,7 @@ pub fn close_audio() { } /// Get the actual audio format in use by the opened audio device. -pub fn query_spec() -> SdlResult<(isize, AudioFormat, isize)> { +pub fn query_spec() -> Result<(isize, AudioFormat, isize), String> { let frequency: c_int = 0; let format: uint16_t = 0; let channels: c_int = 0; @@ -245,7 +244,7 @@ impl Drop for Chunk { impl Chunk { /// Load file for use as a sample. - pub fn from_file(path: &Path) -> SdlResult { + pub fn from_file(path: &Path) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(try!(RWops::from_file(path, "rb")).raw(), 0) }; if raw.is_null() { Err(get_error()) @@ -271,12 +270,12 @@ impl Chunk { /// Loader trait for RWops pub trait LoaderRWops { /// Load src for use as a sample. - fn load_wav(&self) -> SdlResult; + fn load_wav(&self) -> Result; } impl<'a> LoaderRWops for RWops<'a> { /// Load src for use as a sample. - fn load_wav(&self) -> SdlResult { + fn load_wav(&self) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw(), 0) }; if raw == ptr::null() { Err(get_error()) @@ -367,11 +366,11 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn play(self, chunk: &Chunk, loops: isize) -> SdlResult { + pub fn play(self, chunk: &Chunk, loops: isize) -> Result { self.play_timed(chunk, loops, -1) } - pub fn play_timed(self, chunk: &Chunk, loops: isize, ticks: isize) -> SdlResult { + pub fn play_timed(self, chunk: &Chunk, loops: isize, ticks: isize) -> Result { let Channel(ch) = self; let ret = unsafe { ffi::Mix_PlayChannelTimed(ch as c_int, chunk.raw, loops as c_int, ticks as c_int) @@ -384,7 +383,7 @@ impl Channel { } /// Play chunk on channel, or if channel is -1, pick the first free unreserved channel. - pub fn fade_in(self, chunk: &Chunk, loops: isize, ms: isize) -> SdlResult { + pub fn fade_in(self, chunk: &Chunk, loops: isize, ms: isize) -> Result { self.fade_in_timed(chunk, loops, ms, -1) } @@ -393,7 +392,7 @@ impl Channel { loops: isize, ms: isize, ticks: isize) - -> SdlResult { + -> Result { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadeInChannelTimed(ch as c_int, @@ -484,7 +483,7 @@ impl Channel { } /// This removes all effects registered to channel. - pub fn unregister_all_effects(self) -> SdlResult<()> { + pub fn unregister_all_effects(self) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_UnregisterAllEffects(ch as c_int) }; if ret == 0 { @@ -496,7 +495,7 @@ impl Channel { /// Sets a panning effect, where left and right is the volume of the left and right channels. /// They range from 0 (silence) to 255 (loud). - pub fn set_panning(self, left: u8, right: u8) -> SdlResult<()> { + pub fn set_panning(self, left: u8, right: u8) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, left, right) }; if ret == 0 { @@ -507,7 +506,7 @@ impl Channel { } /// Unregisters panning effect. - pub fn unset_panning(self) -> SdlResult<()> { + pub fn unset_panning(self) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPanning(ch as c_int, 255, 255) }; if ret == 0 { @@ -519,7 +518,7 @@ impl Channel { /// This effect simulates a simple attenuation of volume due to distance. /// distance ranges from 0 (close/loud) to 255 (far/quiet). - pub fn set_distance(self, distance: u8) -> SdlResult<()> { + pub fn set_distance(self, distance: u8) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, distance) }; if ret == 0 { @@ -530,7 +529,7 @@ impl Channel { } /// Unregisters distance effect. - pub fn unset_distance(self) -> SdlResult<()> { + pub fn unset_distance(self) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetDistance(ch as c_int, 0) }; if ret == 0 { @@ -543,7 +542,7 @@ impl Channel { /// This effect emulates a simple 3D audio effect. /// angle ranges from 0 to 360 degrees going clockwise, where 0 is directly in front. /// distance ranges from 0 (close/loud) to 255 (far/quiet). - pub fn set_position(self, angle: i16, distance: u8) -> SdlResult<()> { + pub fn set_position(self, angle: i16, distance: u8) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, angle, distance) }; if ret == 0 { @@ -554,7 +553,7 @@ impl Channel { } /// Unregisters position effect. - pub fn unset_position(self) -> SdlResult<()> { + pub fn unset_position(self) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetPosition(ch as c_int, 0, 0) }; if ret == 0 { @@ -566,7 +565,7 @@ impl Channel { /// Simple reverse stereo, swaps left and right channel sound. /// true for reverse, false to unregister effect. - pub fn set_reverse_stereo(self, flip: bool) -> SdlResult<()> { + pub fn set_reverse_stereo(self, flip: bool) -> Result<(), String> { let Channel(ch) = self; let ret = unsafe { ffi::Mix_SetReverseStereo(ch as c_int, flip as c_int) }; if ret == 0 { @@ -741,7 +740,7 @@ impl fmt::Debug for Music { impl Music { /// Load music file to use. - pub fn from_file(path: &Path) -> SdlResult { + pub fn from_file(path: &Path) -> Result { let raw = unsafe { ffi::Mix_LoadMUS(CString::new(path.to_str().unwrap()).unwrap().as_ptr()) }; @@ -774,7 +773,7 @@ impl Music { } /// Play the loaded music loop times through from start to finish. - pub fn play(&self, loops: isize) -> SdlResult<()> { + pub fn play(&self, loops: isize) -> Result<(), String> { let ret = unsafe { ffi::Mix_PlayMusic(self.raw, loops as c_int) }; if ret == -1 { Err(get_error()) @@ -785,7 +784,7 @@ impl Music { /// Fade in over ms milliseconds of time, the loaded music, /// playing it loop times through from start to finish. - pub fn fade_in(&self, loops: isize, ms: isize) -> SdlResult<()> { + pub fn fade_in(&self, loops: isize, ms: isize) -> Result<(), String> { let ret = unsafe { ffi::Mix_FadeInMusic(self.raw, loops as c_int, ms as c_int) }; if ret == -1 { Err(get_error()) @@ -795,7 +794,7 @@ impl Music { } /// Fade in over ms milliseconds of time, from position. - pub fn fade_in_from_pos(&self, loops: isize, ms: isize, position: f64) -> SdlResult<()> { + pub fn fade_in_from_pos(&self, loops: isize, ms: isize, position: f64) -> Result<(), String> { let ret = unsafe { ffi::Mix_FadeInMusicPos(self.raw, loops as c_int, ms as c_int, position as c_double) }; @@ -841,7 +840,7 @@ impl Music { } /// Set the position of the currently playing music. - pub fn set_pos(position: f64) -> SdlResult<()> { + pub fn set_pos(position: f64) -> Result<(), String> { let ret = unsafe { ffi::Mix_SetMusicPosition(position as c_double) }; if ret == -1 { Err(get_error()) @@ -851,7 +850,7 @@ impl Music { } /// Setup a command line music player to use to play music. - pub fn set_command(command: &str) -> SdlResult<()> { + pub fn set_command(command: &str) -> Result<(), String> { let ret = unsafe { ffi::Mix_SetMusicCMD(CString::new(command).unwrap().as_ptr()) }; if ret == -1 { Err(get_error()) @@ -868,7 +867,7 @@ impl Music { } /// Gradually fade out the music over ms milliseconds starting from now. - pub fn fade_out(ms: isize) -> SdlResult<()> { + pub fn fade_out(ms: isize) -> Result<(), String> { let ret = unsafe { ffi::Mix_FadeOutMusic(ms as c_int) }; if ret == -1 { Err(get_error()) From a5aadab2fcedfcac86bbc2d2f93c06b0f8922197 Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 1 Mar 2016 11:16:15 +0800 Subject: [PATCH 089/114] bump version v0.15.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b3fdf307..0352109e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.13.0" +version = "0.15.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From 98b510e589d4d93b1377ab4f97aa6f8e364af021 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Fri, 4 Mar 2016 16:21:23 +0100 Subject: [PATCH 090/114] Updated dependencies - Bumped to 0.16.0 --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0352109e..cac160f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.15.0" +version = "0.16.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.3.2" -sdl2 = "0.15" -sdl2-sys = "0.8" +sdl2 = "0.16" +sdl2-sys = "0.8.1" libc = "0.2" # [dependencies.sdl2] From bab5332942043c5cd0efba330160a400e8577648 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Thu, 14 Apr 2016 20:34:45 +0200 Subject: [PATCH 091/114] Updated dependencies - Bumped to 0.17.0 --- Cargo.toml | 6 +++--- src/sdl2_mixer/lib.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cac160f6..9e3165c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.16.0" +version = "0.17.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -12,8 +12,8 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.3.2" -sdl2 = "0.16" +bitflags = "0.5.0" +sdl2 = "0.18" sdl2-sys = "0.8.1" libc = "0.2" diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index cceecc6d..d68af64c 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -106,13 +106,13 @@ pub fn get_linked_version() -> Version { unsafe { Version::from_ll(*ffi::Mix_Linked_Version()) } } -bitflags!(flags InitFlag : u32 { - const INIT_FLAC = ffi::MIX_INIT_FLAC as u32, - const INIT_MOD = ffi::MIX_INIT_MOD as u32, - const INIT_MODPLUG = ffi::MIX_INIT_MODPLUG as u32, - const INIT_MP3 = ffi::MIX_INIT_MP3 as u32, - const INIT_OGG = ffi::MIX_INIT_OGG as u32, - const INIT_FLUIDSYNTH = ffi::MIX_INIT_FLUIDSYNTH as u32 +bitflags!(pub flags InitFlag : u32 { + const INIT_FLAC = ::ffi::MIX_INIT_FLAC as u32, + const INIT_MOD = ::ffi::MIX_INIT_MOD as u32, + const INIT_MODPLUG = ::ffi::MIX_INIT_MODPLUG as u32, + const INIT_MP3 = ::ffi::MIX_INIT_MP3 as u32, + const INIT_OGG = ::ffi::MIX_INIT_OGG as u32, + const INIT_FLUIDSYNTH = ::ffi::MIX_INIT_FLUIDSYNTH as u32 }); impl ToString for InitFlag { From 15c3e9d0c8b8461fb9f1cce96736e9e8cb148dde Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 20 Apr 2016 01:17:56 +0800 Subject: [PATCH 092/114] minor code style changes --- src/sdl2_mixer/lib.rs | 55 ++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index d68af64c..034a1ac3 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -1,4 +1,4 @@ -//! A binding for SDL2_mixer. +//! A binding for `SDL2_mixer`. //! #![crate_name = "sdl2_mixer"] @@ -100,7 +100,7 @@ pub const DEFAULT_FREQUENCY: isize = 22050; /// Maximum value for any volume setting. pub const MAX_VOLUME: isize = 128; -/// Returns the version of the dynamically linked SDL_mixer library +/// Returns the version of the dynamically linked `SDL_mixer` library pub fn get_linked_version() -> Version { unsafe { Version::from_ll(*ffi::Mix_Linked_Version()) } @@ -140,7 +140,7 @@ impl ToString for InitFlag { } } -/// Context manager for sdl2_mixer to manage init and quit +/// Context manager for `sdl2_mixer` to manage init and quit pub struct Sdl2MixerContext; /// Cleans up all dynamically loaded library handles, freeing memory. @@ -153,14 +153,16 @@ impl Drop for Sdl2MixerContext { } /// Loads dynamic libraries and prepares them for use. Flags should be -/// one or more flags from InitFlag. +/// one or more flags from `InitFlag`. pub fn init(flags: InitFlag) -> Result { let return_flags = unsafe { let ret = ffi::Mix_Init(flags.bits() as c_int); InitFlag::from_bits_truncate(ret as u32) }; // Check if all init flags were set - if !flags.intersects(return_flags) { + if flags.intersects(return_flags) { + Ok(Sdl2MixerContext) + } else { // Flags not matching won't always set the error message text // according to sdl docs if get_error() == "" { @@ -169,8 +171,6 @@ pub fn init(flags: InitFlag) -> Result { let _ = sdl2::set_error(error_str); } Err(get_error()) - } else { - Ok(Sdl2MixerContext) } } @@ -214,7 +214,7 @@ pub fn query_spec() -> Result<(isize, AudioFormat, isize), String> { // 4.2 Samples -/// Get the number of sample chunk decoders available from the Mix_GetChunkDecoder function. +/// Get the number of sample chunk decoders available from the `Mix_GetChunkDecoder` function. pub fn get_chunk_decoders_number() -> isize { unsafe { ffi::Mix_GetNumChunkDecoders() as isize } } @@ -267,7 +267,7 @@ impl Chunk { } } -/// Loader trait for RWops +/// Loader trait for `RWops` pub trait LoaderRWops { /// Load src for use as a sample. fn load_wav(&self) -> Result; @@ -325,7 +325,7 @@ extern "C" fn c_channel_finished_callback(ch: c_int) { } } -/// When channel playback is halted, then the specified channel_finished function is called. +/// When channel playback is halted, then the specified `channel_finished` function is called. pub fn set_channel_finished(f: fn(Channel)) { unsafe { channel_finished_callback = Some(f); @@ -461,10 +461,9 @@ impl Channel { let Channel(ch) = self; let ret = unsafe { ffi::Mix_FadingChannel(ch as c_int) as c_uint }; match ret { - ffi::MIX_NO_FADING => Fading::NoFading, - ffi::MIX_FADING_OUT => Fading::FadingOut, - ffi::MIX_FADING_IN => Fading::FadingIn, - _ => Fading::NoFading, + ffi::MIX_FADING_OUT => Fading::FadingOut, + ffi::MIX_FADING_IN => Fading::FadingIn, + ffi::MIX_NO_FADING | _ => Fading::NoFading } } @@ -758,17 +757,16 @@ impl Music { pub fn get_type(&self) -> MusicType { let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as isize } as c_uint; match ret { - ffi::MUS_NONE => MusicType::MusicNone, - ffi::MUS_CMD => MusicType::MusicCmd, - ffi::MUS_WAV => MusicType::MusicWav, - ffi::MUS_MOD => MusicType::MusicMod, - ffi::MUS_MID => MusicType::MusicMid, - ffi::MUS_OGG => MusicType::MusicOgg, - ffi::MUS_MP3 => MusicType::MusicMp3, - ffi::MUS_MP3_MAD => MusicType::MusicMp3Mad, - ffi::MUS_FLAC => MusicType::MusicFlac, - ffi::MUS_MODPLUG => MusicType::MusicModPlug, - _ => MusicType::MusicNone, + ffi::MUS_CMD => MusicType::MusicCmd, + ffi::MUS_WAV => MusicType::MusicWav, + ffi::MUS_MOD => MusicType::MusicMod, + ffi::MUS_MID => MusicType::MusicMid, + ffi::MUS_OGG => MusicType::MusicOgg, + ffi::MUS_MP3 => MusicType::MusicMp3, + ffi::MUS_MP3_MAD => MusicType::MusicMp3Mad, + ffi::MUS_FLAC => MusicType::MusicFlac, + ffi::MUS_MODPLUG => MusicType::MusicModPlug, + ffi::MUS_NONE | _ => MusicType::MusicNone } } @@ -920,10 +918,9 @@ impl Music { pub fn get_fading() -> Fading { let ret = unsafe { ffi::Mix_FadingMusic() as isize } as c_uint; match ret { - ffi::MIX_NO_FADING => Fading::NoFading, - ffi::MIX_FADING_OUT => Fading::FadingOut, - ffi::MIX_FADING_IN => Fading::FadingIn, - _ => Fading::NoFading, + ffi::MIX_FADING_OUT => Fading::FadingOut, + ffi::MIX_FADING_IN => Fading::FadingIn, + ffi::MIX_NO_FADING | _ => Fading::NoFading } } } From 0764a6de85c232b9089708e7874b6579ce570504 Mon Sep 17 00:00:00 2001 From: andelf Date: Wed, 20 Apr 2016 01:26:47 +0800 Subject: [PATCH 093/114] add crates.io ver to README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d4647e46..a0e2ce66 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Rust-SDL2_mixer ============= [![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) +[![crates.io](http://meritbadge.herokuapp.com/sdl2_mixer)](https://crates.io/crates/sdl2_mixer) Rust bindings for SDL2_mixer. @@ -27,7 +28,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.12.0" +sdl2_mixer = "0.17.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From 49ddedbc9173b03f6fd7bb1198fcd079ce562d2b Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Wed, 4 May 2016 11:48:21 +0200 Subject: [PATCH 094/114] Updated dependences - Bumped to 0.18.0 --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9e3165c7..c50adadb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.17.0" +version = "0.18.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -12,9 +12,9 @@ name = "sdl2_mixer" path = "src/sdl2_mixer/lib.rs" [dependencies] -bitflags = "0.5.0" -sdl2 = "0.18" -sdl2-sys = "0.8.1" +bitflags = "0.6.0" +sdl2 = "0.19" +sdl2-sys = "0.19" libc = "0.2" # [dependencies.sdl2] From d9f4b22c4449c8dc30a1e9269684a64efefd9462 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Thu, 9 Jun 2016 10:27:31 +0200 Subject: [PATCH 095/114] Correct constness mismatches in ffi function signatures --- src/sdl2_mixer/ffi.rs | 76 ++++++++++++++++++++----------------------- src/sdl2_mixer/lib.rs | 14 ++++---- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 78be5ed7..11b1bdf9 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -54,45 +54,41 @@ extern "C" { chunksize: c_int) -> c_int; pub fn Mix_AllocateChannels(numchans: c_int) -> c_int; - pub fn Mix_QuerySpec(frequency: *const c_int, - format: *const uint16_t, - channels: *const c_int) + pub fn Mix_QuerySpec(frequency: *mut c_int, + format: *mut uint16_t, + channels: *mut c_int) -> c_int; - pub fn Mix_LoadWAV_RW(src: *const SDL_RWops, freesrc: c_int) -> *const Mix_Chunk; - pub fn Mix_LoadMUS(file: *const c_char) -> *const Mix_Music; - pub fn Mix_LoadMUS_RW(src: *const SDL_RWops, freesrc: c_int) -> *const Mix_Music; - pub fn Mix_LoadMUSType_RW(src: *const SDL_RWops, - _type: Mix_MusicType, + pub fn Mix_LoadWAV_RW(src: *mut SDL_RWops, freesrc: c_int) -> *mut Mix_Chunk; + pub fn Mix_LoadMUS(file: *const c_char) -> *mut Mix_Music; + pub fn Mix_LoadMUS_RW(src: *mut SDL_RWops, freesrc: c_int) -> *mut Mix_Music; + pub fn Mix_LoadMUSType_RW(src: *mut SDL_RWops, + type_: Mix_MusicType, freesrc: c_int) - -> *const Mix_Music; - pub fn Mix_QuickLoad_WAV(mem: *const uint8_t) -> *const Mix_Chunk; - pub fn Mix_QuickLoad_RAW(mem: *const uint8_t, len: uint32_t) -> *const Mix_Chunk; - pub fn Mix_FreeChunk(chunk: *const Mix_Chunk); - pub fn Mix_FreeMusic(music: *const Mix_Music); + -> *mut Mix_Music; + pub fn Mix_QuickLoad_WAV(mem: *mut uint8_t) -> *mut Mix_Chunk; + pub fn Mix_QuickLoad_RAW(mem: *mut uint8_t, len: uint32_t) -> *mut Mix_Chunk; + pub fn Mix_FreeChunk(chunk: *mut Mix_Chunk); + pub fn Mix_FreeMusic(music: *mut Mix_Music); pub fn Mix_GetNumChunkDecoders() -> c_int; pub fn Mix_GetChunkDecoder(index: c_int) -> *const c_char; pub fn Mix_GetNumMusicDecoders() -> c_int; pub fn Mix_GetMusicDecoder(index: c_int) -> *const c_char; pub fn Mix_GetMusicType(music: *const Mix_Music) -> Mix_MusicType; - pub fn Mix_SetPostMix(mix_func: ::std::option::Option, - arg: *const c_void); - pub fn Mix_HookMusic(mix_func: ::std::option::Option, - arg: *const c_void); - pub fn Mix_HookMusicFinished(music_finished: ::std::option::Option); - pub fn Mix_GetMusicHookData() -> *const c_void; - pub fn Mix_ChannelFinished(channel_finished: - ::std::option::Option); + pub fn Mix_SetPostMix(mix_func: Option, + arg: *mut c_void); + pub fn Mix_HookMusic(mix_func: Option, + arg: *mut c_void); + pub fn Mix_HookMusicFinished(music_finished: Option); + pub fn Mix_GetMusicHookData() -> *mut c_void; + pub fn Mix_ChannelFinished(channel_finished: Option); pub fn Mix_RegisterEffect(chan: c_int, f: Mix_EffectFunc_t, d: Mix_EffectDone_t, - arg: *const c_void) + arg: *mut c_void) -> c_int; pub fn Mix_UnregisterEffect(channel: c_int, f: Mix_EffectFunc_t) -> c_int; pub fn Mix_UnregisterAllEffects(channel: c_int) -> c_int; @@ -108,25 +104,25 @@ extern "C" { pub fn Mix_GroupOldest(tag: c_int) -> c_int; pub fn Mix_GroupNewer(tag: c_int) -> c_int; pub fn Mix_PlayChannelTimed(channel: c_int, - chunk: *const Mix_Chunk, + chunk: *mut Mix_Chunk, loops: c_int, ticks: c_int) -> c_int; - pub fn Mix_PlayMusic(music: *const Mix_Music, loops: c_int) -> c_int; - pub fn Mix_FadeInMusic(music: *const Mix_Music, loops: c_int, ms: c_int) -> c_int; - pub fn Mix_FadeInMusicPos(music: *const Mix_Music, + pub fn Mix_PlayMusic(music: *mut Mix_Music, loops: c_int) -> c_int; + pub fn Mix_FadeInMusic(music: *mut Mix_Music, loops: c_int, ms: c_int) -> c_int; + pub fn Mix_FadeInMusicPos(music: *mut Mix_Music, loops: c_int, ms: c_int, position: c_double) -> c_int; pub fn Mix_FadeInChannelTimed(channel: c_int, - chunk: *const Mix_Chunk, + chunk: *mut Mix_Chunk, loops: c_int, ms: c_int, ticks: c_int) -> c_int; pub fn Mix_Volume(channel: c_int, volume: c_int) -> c_int; - pub fn Mix_VolumeChunk(chunk: *const Mix_Chunk, volume: c_int) -> c_int; + pub fn Mix_VolumeChunk(chunk: *mut Mix_Chunk, volume: c_int) -> c_int; pub fn Mix_VolumeMusic(volume: c_int) -> c_int; pub fn Mix_HaltChannel(channel: c_int) -> c_int; pub fn Mix_HaltGroup(tag: c_int) -> c_int; @@ -152,11 +148,9 @@ extern "C" { pub fn Mix_GetSynchroValue() -> c_int; pub fn Mix_SetSoundFonts(paths: *const c_char) -> c_int; pub fn Mix_GetSoundFonts() -> *const c_char; - pub fn Mix_EachSoundFont(function: ::std::option::Option c_int>, - data: *const c_void) - -> c_int; - pub fn Mix_GetChunk(channel: c_int) -> *const Mix_Chunk; + pub fn Mix_EachSoundFont(function: Option c_int>, + data: *mut c_void) -> c_int; + pub fn Mix_GetChunk(channel: c_int) -> *mut Mix_Chunk; pub fn Mix_CloseAudio(); } diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 034a1ac3..892a6ebc 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -201,10 +201,10 @@ pub fn close_audio() { /// Get the actual audio format in use by the opened audio device. pub fn query_spec() -> Result<(isize, AudioFormat, isize), String> { - let frequency: c_int = 0; - let format: uint16_t = 0; - let channels: c_int = 0; - let ret = unsafe { ffi::Mix_QuerySpec(&frequency, &format, &channels) }; + let mut frequency: c_int = 0; + let mut format: uint16_t = 0; + let mut channels: c_int = 0; + let ret = unsafe { ffi::Mix_QuerySpec(&mut frequency, &mut format, &mut channels) }; if ret == 0 { Err(get_error()) } else { @@ -230,7 +230,7 @@ pub fn get_chunk_decoder(index: isize) -> String { /// The internal format for an audio chunk. #[derive(PartialEq)] pub struct Chunk { - pub raw: *const ffi::Mix_Chunk, + pub raw: *mut ffi::Mix_Chunk, pub owned: bool, } @@ -277,7 +277,7 @@ impl<'a> LoaderRWops for RWops<'a> { /// Load src for use as a sample. fn load_wav(&self) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw(), 0) }; - if raw == ptr::null() { + if raw == ptr::null_mut() { Err(get_error()) } else { Ok(Chunk { @@ -718,7 +718,7 @@ extern "C" fn c_music_finished_hook() { /// This is an opaque data type used for Music data. #[derive(PartialEq)] pub struct Music { - pub raw: *const ffi::Mix_Music, + pub raw: *mut ffi::Mix_Music, pub owned: bool, } From 0933ea8a6ab30975b76e6287f23ed391857e0b01 Mon Sep 17 00:00:00 2001 From: Bastien Dejean Date: Thu, 7 Jul 2016 08:53:21 +0200 Subject: [PATCH 096/114] Update dependencies --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c50adadb..89b88660 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.6.0" -sdl2 = "0.19" -sdl2-sys = "0.19" +sdl2 = "0.21.0" +sdl2-sys = "0.21.0" libc = "0.2" # [dependencies.sdl2] From 4dbe8a2c76ff8e4fcfa3bbb06c35ef1ad2635fa0 Mon Sep 17 00:00:00 2001 From: andelf Date: Sun, 10 Jul 2016 11:15:16 +0800 Subject: [PATCH 097/114] bump version, fix #51 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 89b88660..fa8a10aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.18.0" +version = "0.21.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From f7c88af3782492e96e77357c46eab9bfb9cbeecb Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Tue, 23 Aug 2016 23:37:05 +0200 Subject: [PATCH 098/114] Updated dependencies - Bumped to 0.22.0 --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fa8a10aa..372bdda9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.21.0" +version = "0.22.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.6.0" -sdl2 = "0.21.0" -sdl2-sys = "0.21.0" +sdl2 = "0.22.0" +sdl2-sys = "0.22.0" libc = "0.2" # [dependencies.sdl2] From d8d45559325df557c3b0180c633c2de8e007bd78 Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 13 Sep 2016 22:51:18 +0800 Subject: [PATCH 099/114] bump version 0.23.0 --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 372bdda9..43dab523 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.22.0" +version = "0.23.0" license = "MIT" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.6.0" -sdl2 = "0.22.0" -sdl2-sys = "0.22.0" +sdl2 = "0.23.0" +sdl2-sys = "0.23.0" libc = "0.2" # [dependencies.sdl2] From 1926c6bddf6d9ff04dd1201f0b614ac0201c0752 Mon Sep 17 00:00:00 2001 From: andelf Date: Tue, 13 Sep 2016 23:02:16 +0800 Subject: [PATCH 100/114] fix some clippy warnings --- src/sdl2_mixer/ffi.rs | 26 +++++++++++++------------- src/sdl2_mixer/lib.rs | 11 +++++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/sdl2_mixer/ffi.rs b/src/sdl2_mixer/ffi.rs index 11b1bdf9..4cc2b0a2 100644 --- a/src/sdl2_mixer/ffi.rs +++ b/src/sdl2_mixer/ffi.rs @@ -1,6 +1,6 @@ extern crate sdl2_sys as sys; -use libc::*; +use std::os::raw::{c_uint, c_int, c_char, c_double, c_void}; use self::sys::rwops::SDL_RWops; use self::sys::version::SDL_version; @@ -14,9 +14,9 @@ pub const MIX_INIT_FLUIDSYNTH: c_uint = 32; #[repr(C)] pub struct Struct_Mix_Chunk { pub allocated: c_int, - pub abuf: *const uint8_t, - pub alen: uint32_t, - pub volume: uint8_t, + pub abuf: *const u8, + pub alen: u32, + pub volume: u8, } pub type Mix_Chunk = Struct_Mix_Chunk; pub type Mix_Fading = c_uint; @@ -49,13 +49,13 @@ extern "C" { pub fn Mix_Init(flags: c_int) -> c_int; pub fn Mix_Quit(); pub fn Mix_OpenAudio(frequency: c_int, - format: uint16_t, + format: u16, channels: c_int, chunksize: c_int) -> c_int; pub fn Mix_AllocateChannels(numchans: c_int) -> c_int; pub fn Mix_QuerySpec(frequency: *mut c_int, - format: *mut uint16_t, + format: *mut u16, channels: *mut c_int) -> c_int; pub fn Mix_LoadWAV_RW(src: *mut SDL_RWops, freesrc: c_int) -> *mut Mix_Chunk; @@ -65,8 +65,8 @@ extern "C" { type_: Mix_MusicType, freesrc: c_int) -> *mut Mix_Music; - pub fn Mix_QuickLoad_WAV(mem: *mut uint8_t) -> *mut Mix_Chunk; - pub fn Mix_QuickLoad_RAW(mem: *mut uint8_t, len: uint32_t) -> *mut Mix_Chunk; + pub fn Mix_QuickLoad_WAV(mem: *mut u8) -> *mut Mix_Chunk; + pub fn Mix_QuickLoad_RAW(mem: *mut u8, len: u32) -> *mut Mix_Chunk; pub fn Mix_FreeChunk(chunk: *mut Mix_Chunk); pub fn Mix_FreeMusic(music: *mut Mix_Music); pub fn Mix_GetNumChunkDecoders() -> c_int; @@ -75,11 +75,11 @@ extern "C" { pub fn Mix_GetMusicDecoder(index: c_int) -> *const c_char; pub fn Mix_GetMusicType(music: *const Mix_Music) -> Mix_MusicType; pub fn Mix_SetPostMix(mix_func: Option, arg: *mut c_void); pub fn Mix_HookMusic(mix_func: Option, arg: *mut c_void); pub fn Mix_HookMusicFinished(music_finished: Option); @@ -92,9 +92,9 @@ extern "C" { -> c_int; pub fn Mix_UnregisterEffect(channel: c_int, f: Mix_EffectFunc_t) -> c_int; pub fn Mix_UnregisterAllEffects(channel: c_int) -> c_int; - pub fn Mix_SetPanning(channel: c_int, left: uint8_t, right: uint8_t) -> c_int; - pub fn Mix_SetPosition(channel: c_int, angle: int16_t, distance: uint8_t) -> c_int; - pub fn Mix_SetDistance(channel: c_int, distance: uint8_t) -> c_int; + pub fn Mix_SetPanning(channel: c_int, left: u8, right: u8) -> c_int; + pub fn Mix_SetPosition(channel: c_int, angle: i16, distance: u8) -> c_int; + pub fn Mix_SetDistance(channel: c_int, distance: u8) -> c_int; pub fn Mix_SetReverseStereo(channel: c_int, flip: c_int) -> c_int; pub fn Mix_ReserveChannels(num: c_int) -> c_int; pub fn Mix_GroupChannel(which: c_int, tag: c_int) -> c_int; diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index 892a6ebc..e35e2b5e 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -10,7 +10,6 @@ extern crate libc; extern crate sdl2; use std::default; -use std::ptr; use std::fmt; use std::ffi::{CString, CStr}; use std::str::from_utf8; @@ -277,7 +276,7 @@ impl<'a> LoaderRWops for RWops<'a> { /// Load src for use as a sample. fn load_wav(&self) -> Result { let raw = unsafe { ffi::Mix_LoadWAV_RW(self.raw(), 0) }; - if raw == ptr::null_mut() { + if raw.is_null() { Err(get_error()) } else { Ok(Chunk { @@ -741,7 +740,8 @@ impl Music { /// Load music file to use. pub fn from_file(path: &Path) -> Result { let raw = unsafe { - ffi::Mix_LoadMUS(CString::new(path.to_str().unwrap()).unwrap().as_ptr()) + let c_path = CString::new(path.to_str().unwrap()).unwrap(); + ffi::Mix_LoadMUS(c_path.as_ptr()) }; if raw.is_null() { Err(get_error()) @@ -849,7 +849,10 @@ impl Music { /// Setup a command line music player to use to play music. pub fn set_command(command: &str) -> Result<(), String> { - let ret = unsafe { ffi::Mix_SetMusicCMD(CString::new(command).unwrap().as_ptr()) }; + let ret = unsafe { + let c_command = CString::new(command).unwrap(); + ffi::Mix_SetMusicCMD(c_command.as_ptr()) + }; if ret == -1 { Err(get_error()) } else { From 1007cbb2918ff13833fb82990c002002444c08ce Mon Sep 17 00:00:00 2001 From: johnthagen Date: Sat, 17 Sep 2016 15:55:55 -0400 Subject: [PATCH 101/114] Add Windows requirements section. Fixes #54. --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index a0e2ce66..da3e6a05 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,30 @@ Rust-SDL2_mixer uses the MIT licence. #### OSX brew install sdl2_mixer + +#### Windows + +1. Download mingw development libraries from https://www.libsdl.org/projects/SDL_mixer/ +(`SDL2_mixer-devel-2.0.x-mingw.tar.gz`) +2. Unpack to a folder of your choosing (You can delete it afterwards). +3. Copy all lib files from + > SDL2_mixer-2.0.x\x86_64-w64-mingw32\lib\ + + to (for Rust 1.6 and above) + > C:\Program Files\Rust\\**lib**\rustlib\x86_64-pc-windows-gnu\lib + + or to (for Rust versions 1.5 and below) + > C:\Program Files\Rust\\**bin**\rustlib\x86_64-pc-windows-gnu\lib + + or to your library folder of choice, and ensure you have a system environment variable of + > LIBRARY_PATH = C:\your\rust\library\folder + + For Multirust Users, this folder will be in + > C:\Users\{Your Username}\AppData\Local\.multirust\toolchains\{current toolchain}\lib\rustlib\x86_64-pc-windows-gnu\lib +4. Copy `SDL2_mixer.dll` and `smpeg2.dll` from + > SDL2_mixer-2.0.1\x86_64-w64-mingw32\bin\ + + into your cargo project, right next to your `Cargo.toml`. ## Installation From 92f8f5d82c35c4f04bae71fd701c516386ba7174 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Sat, 17 Sep 2016 15:57:21 -0400 Subject: [PATCH 102/114] Fix minor version number to be agnostic. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index da3e6a05..c653e507 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Rust-SDL2_mixer uses the MIT licence. For Multirust Users, this folder will be in > C:\Users\{Your Username}\AppData\Local\.multirust\toolchains\{current toolchain}\lib\rustlib\x86_64-pc-windows-gnu\lib 4. Copy `SDL2_mixer.dll` and `smpeg2.dll` from - > SDL2_mixer-2.0.1\x86_64-w64-mingw32\bin\ + > SDL2_mixer-2.0.x\x86_64-w64-mingw32\bin\ into your cargo project, right next to your `Cargo.toml`. From 02a33bf4ec580d3d80c50b05b25150d03b24fb8e Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 19 Sep 2016 19:38:04 -0400 Subject: [PATCH 103/114] Fix requirement to target stable Rust. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c653e507..a14a15b4 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Rust-SDL2_mixer uses the MIT licence. * [Rust-SDL2](https://github.com/AngryLawyer/rust-sdl2) * SDL2_mixer development libraries -* Rust master or nightly +* Latest stable Rust master #### OSX From 83e84e8163b14a49be624fd9c9d2190c98b71d88 Mon Sep 17 00:00:00 2001 From: Simon Heath Date: Fri, 23 Sep 2016 13:27:47 -0400 Subject: [PATCH 104/114] Added load_music() to LoaderRWops Can't actually test it at the moment but it SHOULD work fine, right? --- src/sdl2_mixer/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sdl2_mixer/lib.rs b/src/sdl2_mixer/lib.rs index e35e2b5e..54bcc155 100644 --- a/src/sdl2_mixer/lib.rs +++ b/src/sdl2_mixer/lib.rs @@ -270,6 +270,8 @@ impl Chunk { pub trait LoaderRWops { /// Load src for use as a sample. fn load_wav(&self) -> Result; + + fn load_music(&self) -> Result; } impl<'a> LoaderRWops for RWops<'a> { @@ -285,6 +287,20 @@ impl<'a> LoaderRWops for RWops<'a> { }) } } + + /// Load src for use as music. + fn load_music(&self) -> Result { + let raw = unsafe { ffi::Mix_LoadMUS_RW(self.raw(), 0) }; + if raw.is_null() { + Err(get_error()) + } else { + Ok(Music { + raw: raw, + owned: true, + }) + } + } + } From 3440efa093d4d57b21d085189fd83a636b2a929e Mon Sep 17 00:00:00 2001 From: andelf Date: Sat, 24 Sep 2016 18:46:28 +0800 Subject: [PATCH 105/114] Relicense under dual MIT/Apache-2.0, fix $43 --- Cargo.toml | 2 +- LICENSE | 20 ----- LICENSE-APACHE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE-MIT | 25 ++++++ README.md | 13 ++++ 5 files changed, 240 insertions(+), 21 deletions(-) delete mode 100644 LICENSE create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT diff --git a/Cargo.toml b/Cargo.toml index 43dab523..0d1dd8b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" version = "0.23.0" -license = "MIT" +license = "MIT/Apache-2.0" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 5e611b20..00000000 --- a/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013 Mozilla Foundation - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 00000000..16fe87b0 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 00000000..25597d58 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2010 The Rust Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index a0e2ce66..b6585875 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,16 @@ A simple demo that plays out a portion of a given music file is included: ```bash cargo run --example demo path/to/music.(mp3|flac|ogg|wav) ``` + +## License + +Licensed under either of + * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any +additional terms or conditions. From b7ace8d61f66d6423de9b3bc7886c2b447095d31 Mon Sep 17 00:00:00 2001 From: andelf Date: Sat, 24 Sep 2016 18:48:51 +0800 Subject: [PATCH 106/114] bump version v0.23.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0d1dd8b0..d3211eb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.23.0" +version = "0.23.1" license = "MIT/Apache-2.0" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] From a397eb4bbde2456893852bd1b13ea2922717060b Mon Sep 17 00:00:00 2001 From: andelf Date: Fri, 7 Oct 2016 23:22:58 +0800 Subject: [PATCH 107/114] bump sdl2 version --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3211eb1..d3107670 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.23.1" +version = "0.24.0" license = "MIT/Apache-2.0" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.6.0" -sdl2 = "0.23.0" -sdl2-sys = "0.23.0" +sdl2 = "0.24.0" +sdl2-sys = "0.24.0" libc = "0.2" # [dependencies.sdl2] From 8a7f3ce3c77e34b6ea38785a9033cc55ea880071 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 24 Oct 2016 17:09:06 -0400 Subject: [PATCH 108/114] Update crate version in README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 304f84e3..963a1605 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Place the following into your project's Cargo.toml file: ```toml [dependencies] -sdl2_mixer = "0.17.0" +sdl2_mixer = "0.24.0" ``` Or, to depend on the newest rust-sdl2_mixer, reference the repository: From 3b53e1f94433eb4de3a26f7d2a6a83f4ab426e9b Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 24 Oct 2016 17:13:09 -0400 Subject: [PATCH 109/114] Provide full brew install instructions for needed extra libraries. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 304f84e3..c0c8c1cc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,9 @@ Rust-SDL2_mixer uses the MIT licence. #### OSX - brew install sdl2_mixer +```bash + brew install sdl2_mixer --with-flac --with-fluid-synth --with-libmikmod --with-libmodplug --with-libvorbis --with-smpeg2 +``` #### Windows From 2b6cf630c5ff144631fb0729f4f8c4e5dce45206 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 24 Oct 2016 17:20:16 -0400 Subject: [PATCH 110/114] Use HTTPS connections in Travis. --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bf6540cc..40e4f20e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,8 @@ env: - LD_LIBRARY_PATH: /usr/local/lib install: - cd .. - - time wget -q http://www.libsdl.org/release/SDL2-2.0.3.tar.gz - - time wget -q http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz + - time wget -q https://www.libsdl.org/release/SDL2-2.0.3.tar.gz + - time wget -q https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.0.tar.gz - time tar xf SDL2-*.tar.gz - time tar xf SDL2_mixer-*.tar.gz - cd SDL2-* @@ -25,4 +25,4 @@ script: - cargo doc -v after_script: - cp -R target/doc doc - - curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh + - curl https://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh From f6b1bc67b70ef68c2d4fce61952ee32b73e7f1ff Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 24 Oct 2016 17:23:28 -0400 Subject: [PATCH 111/114] Add badge for crate license. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 304f84e3..da354c7a 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Rust-SDL2_mixer [![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) [![crates.io](http://meritbadge.herokuapp.com/sdl2_mixer)](https://crates.io/crates/sdl2_mixer) +[![Crates.io](https://img.shields.io/crates/l/sdl2-mixer.svg)](https://github.com/andelf/rust-sdl2_mixer/blob/master/LICENSE-MIT) Rust bindings for SDL2_mixer. From 095baf7f9da5523a991dd76b47cabf2adc3475c7 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Mon, 24 Oct 2016 17:25:02 -0400 Subject: [PATCH 112/114] Fix crate name in badge reference. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index da354c7a..eeae7bda 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Rust-SDL2_mixer [![Build Status](https://travis-ci.org/andelf/rust-sdl2_mixer.svg?branch=master)](https://travis-ci.org/andelf/rust-sdl2_mixer) [![crates.io](http://meritbadge.herokuapp.com/sdl2_mixer)](https://crates.io/crates/sdl2_mixer) -[![Crates.io](https://img.shields.io/crates/l/sdl2-mixer.svg)](https://github.com/andelf/rust-sdl2_mixer/blob/master/LICENSE-MIT) +[![Crates.io](https://img.shields.io/crates/l/sdl2_mixer.svg)](https://github.com/andelf/rust-sdl2_mixer/blob/master/LICENSE-MIT) Rust bindings for SDL2_mixer. From fb2fe75ff73f326b4eb17829aff028adc1542f08 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Tue, 25 Oct 2016 07:10:03 -0400 Subject: [PATCH 113/114] Remove unnecessary indentation. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d9c17f6c..e4d2c2fc 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Rust-SDL2_mixer uses the MIT licence. #### OSX ```bash - brew install sdl2_mixer --with-flac --with-fluid-synth --with-libmikmod --with-libmodplug --with-libvorbis --with-smpeg2 +brew install sdl2_mixer --with-flac --with-fluid-synth --with-libmikmod --with-libmodplug --with-libvorbis --with-smpeg2 ``` #### Windows From 317d9e1e759f3b7c82aa348e693065efa2ab6fea Mon Sep 17 00:00:00 2001 From: icefoxen Date: Sat, 5 Nov 2016 12:16:59 -0400 Subject: [PATCH 114/114] Version bump to 0.25 (#65) fixes complaints about trying to include sdl2-sys 0.24 and 0.25 in the same project. --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3107670..f5812422 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sdl2_mixer" description = "SDL2_mixer bindings for Rust" repository = "https://github.com/andelf/rust-sdl2_mixer" -version = "0.24.0" +version = "0.25.0" license = "MIT/Apache-2.0" authors = ["ShuYu Wang "] keywords = ["SDL", "windowing", "graphics", "music", "sound"] @@ -13,8 +13,8 @@ path = "src/sdl2_mixer/lib.rs" [dependencies] bitflags = "0.6.0" -sdl2 = "0.24.0" -sdl2-sys = "0.24.0" +sdl2 = "0.25.0" +sdl2-sys = "0.25.0" libc = "0.2" # [dependencies.sdl2]