Merge pull request #1043 from GuillaumeGomez/doc-aliases

Add doc aliases for C equivalents
This commit is contained in:
Cobrand
2020-12-07 21:56:50 +01:00
committed by GitHub
29 changed files with 337 additions and 0 deletions
+24
View File
@@ -101,6 +101,7 @@ impl AudioSubsystem {
AudioQueue::open_queue(self, device, spec)
}
#[doc(alias = "SDL_GetCurrentAudioDriver")]
pub fn current_audio_driver(&self) -> &'static str {
unsafe {
let buf = sys::SDL_GetCurrentAudioDriver();
@@ -110,6 +111,7 @@ impl AudioSubsystem {
}
}
#[doc(alias = "SDL_GetNumAudioDevices")]
pub fn num_audio_playback_devices(&self) -> Option<u32> {
let result = unsafe { sys::SDL_GetNumAudioDevices(0) };
if result < 0 {
@@ -120,6 +122,7 @@ impl AudioSubsystem {
}
}
#[doc(alias = "SDL_GetAudioDeviceName")]
pub fn audio_playback_device_name(&self, index: u32) -> Result<String, String> {
unsafe {
let dev_name = sys::SDL_GetAudioDeviceName(index as c_int, 0);
@@ -176,6 +179,7 @@ impl AudioFormat {
}
}
#[doc(alias = "SDL_AudioFormat")]
fn to_ll(self) -> sys::SDL_AudioFormat {
self as sys::SDL_AudioFormat
}
@@ -228,6 +232,7 @@ impl TryFrom<u32> for AudioStatus {
}
}
#[doc(alias = "SDL_GetAudioDriver")]
#[derive(Copy, Clone)]
pub struct DriverIterator {
length: i32,
@@ -262,6 +267,7 @@ impl Iterator for DriverIterator {
impl ExactSizeIterator for DriverIterator { }
/// Gets an iterator of all audio drivers compiled into the SDL2 library.
#[doc(alias = "SDL_GetAudioDriver")]
#[inline]
pub fn drivers() -> DriverIterator {
// This function is thread-safe and doesn't require the audio subsystem to be initialized.
@@ -290,6 +296,7 @@ impl AudioSpecWAV {
}
/// Loads a WAVE from the data source.
#[doc(alias = "SDL_LoadWAV_RW")]
pub fn load_wav_rw(src: &mut RWops) -> Result<AudioSpecWAV, String> {
use std::mem::MaybeUninit;
use std::ptr::null_mut;
@@ -325,6 +332,7 @@ impl AudioSpecWAV {
}
impl Drop for AudioSpecWAV {
#[doc(alias = "SDL_FreeWAV")]
fn drop(&mut self) {
unsafe { sys::SDL_FreeWAV(self.audio_buf); }
}
@@ -532,6 +540,7 @@ impl AudioDeviceID {
}
impl Drop for AudioDeviceID {
#[doc(alias = "SDL_CloseAudioDevice")]
fn drop(&mut self) {
//! Shut down audio processing and close the audio device.
unsafe { sys::SDL_CloseAudioDevice(self.id()) }
@@ -548,6 +557,7 @@ pub struct AudioQueue<Channel: AudioFormatNum> {
impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {
/// Opens a new audio device given the desired parameters and callback.
#[doc(alias = "SDL_OpenAudioDevice")]
pub fn open_queue<D: Into<Option<&'a str>>>(a: &AudioSubsystem, device: D, spec: &AudioSpecDesired) -> Result<AudioQueue<Channel>, String> {
use std::mem::MaybeUninit;
@@ -590,6 +600,7 @@ impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {
}
#[inline]
#[doc(alias = "SDL_GetAudioDeviceStatus")]
pub fn subsystem(&self) -> &AudioSubsystem { &self.subsystem }
#[inline]
@@ -603,26 +614,31 @@ impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {
}
/// Pauses playback of the audio device.
#[doc(alias = "SDL_PauseAudioDevice")]
pub fn pause(&self) {
unsafe { sys::SDL_PauseAudioDevice(self.device_id.id(), 1) }
}
/// Starts playback of the audio device.
#[doc(alias = "SDL_PauseAudioDevice")]
pub fn resume(&self) {
unsafe { sys::SDL_PauseAudioDevice(self.device_id.id(), 0) }
}
/// Adds data to the audio queue.
#[doc(alias = "SDL_QueueAudio")]
pub fn queue(&self, data: &[Channel]) -> bool {
let result = unsafe {sys::SDL_QueueAudio(self.device_id.id(), data.as_ptr() as *const c_void, (data.len() * mem::size_of::<Channel>()) as u32)};
result == 0
}
#[doc(alias = "SDL_GetQueuedAudioSize")]
pub fn size(&self) -> u32 {
unsafe {sys::SDL_GetQueuedAudioSize(self.device_id.id())}
}
/// Clears all data from the current audio queue.
#[doc(alias = "SDL_ClearQueuedAudio")]
pub fn clear(&self) {
unsafe {sys::SDL_ClearQueuedAudio(self.device_id.id());}
}
@@ -639,6 +655,7 @@ pub struct AudioDevice<CB: AudioCallback> {
impl<CB: AudioCallback> AudioDevice<CB> {
/// Opens a new audio device for playback or capture (given the desired parameters and callback).
#[doc(alias = "SDL_OpenAudioDevice")]
fn open<'a, F, D>(a: &AudioSubsystem, device: D, spec: &AudioSpecDesired, get_callback: F, capture: bool) -> Result<AudioDevice <CB>, String>
where
F: FnOnce(AudioSpec) -> CB,
@@ -713,6 +730,7 @@ impl<CB: AudioCallback> AudioDevice<CB> {
}
#[inline]
#[doc(alias = "SDL_GetAudioDeviceStatus")]
pub fn subsystem(&self) -> &AudioSubsystem { &self.subsystem }
#[inline]
@@ -726,11 +744,13 @@ impl<CB: AudioCallback> AudioDevice<CB> {
}
/// Pauses playback of the audio device.
#[doc(alias = "SDL_PauseAudioDevice")]
pub fn pause(&self) {
unsafe { sys::SDL_PauseAudioDevice(self.device_id.id(), 1) }
}
/// Starts playback of the audio device.
#[doc(alias = "SDL_PauseAudioDevice")]
pub fn resume(&self) {
unsafe { sys::SDL_PauseAudioDevice(self.device_id.id(), 0) }
}
@@ -740,6 +760,7 @@ impl<CB: AudioCallback> AudioDevice<CB> {
/// When the returned lock guard is dropped, `SDL_UnlockAudioDevice` is
/// called.
/// Use this method to read and mutate callback data.
#[doc(alias = "SDL_LockAudioDevice")]
pub fn lock(&mut self) -> AudioDeviceLockGuard<CB> {
unsafe { sys::SDL_LockAudioDevice(self.device_id.id()) };
AudioDeviceLockGuard {
@@ -766,6 +787,7 @@ pub struct AudioDeviceLockGuard<'a, CB> where CB: AudioCallback, CB: 'a {
impl<'a, CB: AudioCallback> Deref for AudioDeviceLockGuard<'a, CB> {
type Target = CB;
#[doc(alias = "SDL_UnlockAudioDevice")]
fn deref(&self) -> &CB { (*self.device.userdata).as_ref().expect("Missing callback") }
}
@@ -785,6 +807,7 @@ pub struct AudioCVT {
}
impl AudioCVT {
#[doc(alias = "SDL_BuildAudioCVT")]
pub fn new(src_format: AudioFormat, src_channels: u8, src_rate: i32,
dst_format: AudioFormat, dst_channels: u8, dst_rate: i32) -> Result<AudioCVT, String>
{
@@ -805,6 +828,7 @@ impl AudioCVT {
}
}
#[doc(alias = "SDL_ConvertAudio")]
pub fn convert(&self, mut src: Vec<u8>) -> Vec<u8> {
//! Convert audio data to a desired audio format.
//!
+3
View File
@@ -29,6 +29,7 @@ impl crate::VideoSubsystem {
}
impl ClipboardUtil {
#[doc(alias = "SDL_SetClipboardText")]
pub fn set_clipboard_text(&self, text: &str) -> Result<(), String> {
unsafe {
let text = CString::new(text).unwrap();
@@ -42,6 +43,7 @@ impl ClipboardUtil {
}
}
#[doc(alias = "SDL_GetClipboardText")]
pub fn clipboard_text(&self) -> Result<String, String> {
unsafe {
let buf = sys::SDL_GetClipboardText();
@@ -56,6 +58,7 @@ impl ClipboardUtil {
}
}
#[doc(alias = "SDL_HasClipboardText")]
pub fn has_clipboard_text(&self) -> bool {
unsafe { sys::SDL_HasClipboardText() == sys::SDL_bool::SDL_TRUE }
}
+22
View File
@@ -50,6 +50,7 @@ impl error::Error for AddMappingError {
impl GameControllerSubsystem {
/// Retrieve the total number of attached joysticks *and* controllers identified by SDL.
#[doc(alias = "SDL_NumJoysticks")]
pub fn num_joysticks(&self) -> Result<u32, String> {
let result = unsafe { sys::SDL_NumJoysticks() };
@@ -62,6 +63,7 @@ impl GameControllerSubsystem {
/// Return true if the joystick at index `joystick_index` is a game controller.
#[inline]
#[doc(alias = "SDL_IsGameController")]
pub fn is_game_controller(&self, joystick_index: u32) -> bool {
match validate_int(joystick_index, "joystick_index") {
Ok(joystick_index) => unsafe { sys::SDL_IsGameController(joystick_index) != sys::SDL_bool::SDL_FALSE },
@@ -72,6 +74,7 @@ impl GameControllerSubsystem {
/// Attempt to open the controller at index `joystick_index` and return it.
/// Controller IDs are the same as joystick IDs and the maximum number can
/// be retrieved using the `SDL_NumJoysticks` function.
#[doc(alias = "SDL_GameControllerOpen")]
pub fn open(&self, joystick_index: u32) -> Result<GameController, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let joystick_index = validate_int(joystick_index, "joystick_index")?;
@@ -88,6 +91,7 @@ impl GameControllerSubsystem {
}
/// Return the name of the controller at index `joystick_index`.
#[doc(alias = "SDL_GameControllerNameForIndex")]
pub fn name_for_index(&self, joystick_index: u32) -> Result<String, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let joystick_index = validate_int(joystick_index, "joystick_index")?;
@@ -104,17 +108,20 @@ impl GameControllerSubsystem {
/// If state is `true` controller events are processed, otherwise
/// they're ignored.
#[doc(alias = "SDL_GameControllerEventState")]
pub fn set_event_state(&self, state: bool) {
unsafe { sys::SDL_GameControllerEventState(state as i32) };
}
/// Return `true` if controller events are processed.
#[doc(alias = "SDL_GameControllerEventState")]
pub fn event_state(&self) -> bool {
unsafe { sys::SDL_GameControllerEventState(sys::SDL_QUERY as i32)
== sys::SDL_ENABLE as i32 }
}
/// Add a new controller input mapping from a mapping string.
#[doc(alias = "SDL_GameControllerAddMapping")]
pub fn add_mapping(&self, mapping: &str)
-> Result<MappingStatus, AddMappingError> {
use self::AddMappingError::*;
@@ -153,6 +160,7 @@ impl GameControllerSubsystem {
}
/// Load controller input mappings from an SDL [`RWops`] object.
#[doc(alias = "SDL_GameControllerAddMappingsFromRW")]
pub fn load_mappings_from_rw<'a>(&self, rw: RWops<'a>) -> Result<i32, AddMappingError> {
use self::AddMappingError::*;
@@ -163,6 +171,7 @@ impl GameControllerSubsystem {
}
}
#[doc(alias = "SDL_GameControllerMappingForGUID")]
pub fn mapping_for_guid(&self, guid: joystick::Guid) -> Result<String, String> {
let c_str = unsafe { sys::SDL_GameControllerMappingForGUID(guid.raw()) };
@@ -171,6 +180,7 @@ impl GameControllerSubsystem {
#[inline]
/// Force controller update when not using the event loop
#[doc(alias = "SDL_GameControllerUpdate")]
pub fn update(&self) {
unsafe { sys::SDL_GameControllerUpdate() };
}
@@ -190,6 +200,7 @@ pub enum Axis {
impl Axis {
/// Return the Axis from a string description in the same format
/// used by the game controller mapping strings.
#[doc(alias = "SDL_GameControllerGetAxisFromString")]
pub fn from_string(axis: &str) -> Option<Axis> {
let id = match CString::new(axis) {
Ok(axis) => unsafe { sys::SDL_GameControllerGetAxisFromString(axis.as_ptr() as *const c_char) },
@@ -202,6 +213,7 @@ impl Axis {
/// Return a string for a given axis in the same format using by
/// the game controller mapping strings
#[doc(alias = "SDL_GameControllerGetStringForAxis")]
pub fn string(self) -> String {
let axis: sys::SDL_GameControllerAxis;
unsafe { axis = transmute(self); }
@@ -259,6 +271,7 @@ pub enum Button {
impl Button {
/// Return the Button from a string description in the same format
/// used by the game controller mapping strings.
#[doc(alias = "SDL_GameControllerGetButtonFromString")]
pub fn from_string(button: &str) -> Option<Button> {
let id = match CString::new(button) {
Ok(button) => unsafe { sys::SDL_GameControllerGetButtonFromString(button.as_ptr() as *const c_char) },
@@ -271,6 +284,7 @@ impl Button {
/// Return a string for a given button in the same format using by
/// the game controller mapping strings
#[doc(alias = "SDL_GameControllerGetStringForButton")]
pub fn string(self) -> String {
let button: sys::SDL_GameControllerButton;
unsafe { button = transmute(self); }
@@ -342,6 +356,7 @@ impl GameController {
/// Return the name of the controller or an empty string if no
/// name is found.
#[doc(alias = "SDL_GameControllerName")]
pub fn name(&self) -> String {
let name = unsafe { sys::SDL_GameControllerName(self.raw) };
@@ -350,6 +365,7 @@ impl GameController {
/// Return a String describing the controller's button and axis
/// mappings
#[doc(alias = "SDL_GameControllerMapping")]
pub fn mapping(&self) -> String {
let mapping = unsafe { sys::SDL_GameControllerMapping(self.raw) };
@@ -358,11 +374,13 @@ impl GameController {
/// Return true if the controller has been opened and currently
/// connected.
#[doc(alias = "SDL_GameControllerGetAttached")]
pub fn attached(&self) -> bool {
unsafe { sys::SDL_GameControllerGetAttached(self.raw) != sys::SDL_bool::SDL_FALSE }
}
/// Return the joystick instance id of this controller
#[doc(alias = "SDL_GameControllerGetJoystick")]
pub fn instance_id(&self) -> u32 {
let result = unsafe {
let joystick = sys::SDL_GameControllerGetJoystick(self.raw);
@@ -378,6 +396,7 @@ impl GameController {
}
/// Get the position of the given `axis`
#[doc(alias = "SDL_GameControllerGetAxis")]
pub fn axis(&self, axis: Axis) -> i16 {
// This interface is a bit messed up: 0 is a valid position
// but can also mean that an error occured.
@@ -391,6 +410,7 @@ impl GameController {
}
/// Returns `true` if `button` is pressed.
#[doc(alias = "SDL_GameControllerGetButton")]
pub fn button(&self, button: Button) -> bool {
// This interface is a bit messed up: 0 is a valid position
// but can also mean that an error occured.
@@ -414,6 +434,7 @@ impl GameController {
/// the rumble effect to keep playing for a long time, as this results in
/// the effect ending immediately after starting due to an overflow.
/// Use some smaller, "huge enough" number instead.
#[doc(alias = "SDL_GameControllerRumble")]
pub fn set_rumble(&mut self,
low_frequency_rumble: u16,
high_frequency_rumble: u16,
@@ -436,6 +457,7 @@ impl GameController {
}
impl Drop for GameController {
#[doc(alias = "SDL_GameControllerClose")]
fn drop(&mut self) {
unsafe { sys::SDL_GameControllerClose(self.raw) }
}
+15
View File
@@ -3,62 +3,77 @@ use crate::sys::SDL_bool;
pub const CACHELINESIZE: u8 = 128;
#[doc(alias = "SDL_GetCPUCount")]
pub fn cpu_count() -> i32 {
unsafe { sys::SDL_GetCPUCount() }
}
#[doc(alias = "SDL_GetCPUCacheLineSize")]
pub fn cpu_cache_line_size() -> i32 {
unsafe { sys::SDL_GetCPUCacheLineSize() }
}
#[doc(alias = "SDL_HasRDTSC")]
pub fn has_rdtsc() -> bool {
unsafe { sys::SDL_HasRDTSC() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasAltiVec")]
pub fn has_alti_vec() -> bool {
unsafe { sys::SDL_HasAltiVec() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasMMX")]
pub fn has_mmx() -> bool {
unsafe { sys::SDL_HasMMX() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_Has3DNow")]
pub fn has_3d_now() -> bool {
unsafe { sys::SDL_Has3DNow() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasSSE")]
pub fn has_sse() -> bool {
unsafe { sys::SDL_HasSSE() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasSSE2")]
pub fn has_sse2() -> bool {
unsafe { sys::SDL_HasSSE2() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasSSE3")]
pub fn has_sse3() -> bool {
unsafe { sys::SDL_HasSSE3() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasSSE41")]
pub fn has_sse41() -> bool {
unsafe { sys::SDL_HasSSE41() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasSSE42")]
pub fn has_sse42() -> bool {
unsafe { sys::SDL_HasSSE42() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasAVX")]
pub fn has_avx() -> bool {
unsafe { sys::SDL_HasAVX() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasAVX2")]
pub fn has_avx2() -> bool {
unsafe { sys::SDL_HasAVX2() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_HasAVX512F")]
pub fn has_avx512f() -> bool {
unsafe { sys::SDL_HasAVX512F() == SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_GetSystemRAM")]
pub fn system_ram() -> i32 {
unsafe { sys::SDL_GetSystemRAM() }
}
+9
View File
@@ -50,11 +50,13 @@ lazy_static! {
impl crate::EventSubsystem {
/// Removes all events in the event queue that match the specified event type.
#[doc(alias = "SDL_FlushEvent")]
pub fn flush_event(&self, event_type: EventType) {
unsafe { sys::SDL_FlushEvent(event_type as u32) };
}
/// Removes all events in the event queue that match the specified type range.
#[doc(alias = "SDL_FlushEvents")]
pub fn flush_events(&self, min_type: u32, max_type: u32) {
unsafe { sys::SDL_FlushEvents(min_type, max_type) };
}
@@ -79,6 +81,7 @@ impl crate::EventSubsystem {
/// println!("{:?}", event);
/// }
/// ```
#[doc(alias = "SDL_PeepEvents")]
pub fn peek_events<B>(&self, max_amount: u32) -> B
where B: FromIterator<Event>
{
@@ -770,6 +773,7 @@ unsafe impl Sync for Event {}
/// Helper function to make converting scancodes
/// and keycodes to primitive `SDL_Keysym` types.
#[doc(alias = "SDL_Keysym")]
fn mk_keysym<S, K>(scancode: S,
keycode: K,
keymod: Mod) -> sys::SDL_Keysym
@@ -2359,6 +2363,7 @@ unsafe fn wait_event_timeout(timeout: u32) -> Option<Event> {
impl crate::EventPump {
/// Query if an event type is enabled.
#[doc(alias = "SDL_EventState")]
pub fn is_event_enabled(&self, event_type: EventType) -> bool {
let result = unsafe { sys::SDL_EventState(event_type as u32, sys::SDL_QUERY) };
@@ -2366,6 +2371,7 @@ impl crate::EventPump {
}
/// Enable an event type. Returns if the event type was enabled before the call.
#[doc(alias = "SDL_EventState")]
pub fn enable_event(&mut self, event_type: EventType) -> bool {
let result = unsafe { sys::SDL_EventState(event_type as u32, sys::SDL_ENABLE as c_int) };
@@ -2373,6 +2379,7 @@ impl crate::EventPump {
}
/// Disable an event type. Returns if the event type was enabled before the call.
#[doc(alias = "SDL_EventState")]
pub fn disable_event(&mut self, event_type: EventType) -> bool {
let result = unsafe { sys::SDL_EventState(event_type as u32, sys::SDL_DISABLE as c_int) };
@@ -2409,6 +2416,7 @@ impl crate::EventPump {
}
/// Pumps the event loop, gathering events from the input devices.
#[doc(alias = "SDL_PumpEvents")]
pub fn pump_events(&mut self) {
unsafe { sys::SDL_PumpEvents(); };
}
@@ -2768,6 +2776,7 @@ pub struct EventSender {
impl EventSender {
/// Pushes an event to the event queue.
#[doc(alias = "SDL_PushEvent")]
pub fn push_event(&self, event: Event) -> Result<(), String> {
match event.to_ll() {
Some(mut raw_event) => {
+2
View File
@@ -7,6 +7,7 @@ use libc::c_char;
use crate::sys;
#[doc(alias = "SDL_GetBasePath")]
pub fn base_path() -> Result<String, String> {
let result = unsafe {
let buf = sys::SDL_GetBasePath();
@@ -56,6 +57,7 @@ impl error::Error for PrefPathError {
// TODO: Change to OsStr or something?
/// Return the preferred directory for the application to write files on this
/// system, based on the given organization and application name.
#[doc(alias = "SDL_GetPrefPath")]
pub fn pref_path(org_name: &str, app_name: &str)
-> Result<String, PrefPathError> {
use self::PrefPathError::*;
+4
View File
@@ -7,6 +7,7 @@ use crate::get_error;
impl HapticSubsystem {
/// Attempt to open the joystick at index `joystick_index` and return its haptic device.
#[doc(alias = "SDL_JoystickOpen")]
pub fn open_from_joystick_id(&self, joystick_index: u32) -> Result<Haptic, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let joystick_index = validate_int(joystick_index, "joystick_index")?;
@@ -37,6 +38,7 @@ pub struct Haptic {
impl Haptic {
#[inline]
#[doc(alias = "SDL_HapticRumblePlay")]
pub fn subsystem(&self) -> &HapticSubsystem { &self.subsystem }
/// Run a simple rumble effect on the haptic device.
@@ -45,6 +47,7 @@ impl Haptic {
}
/// Stop the simple rumble on the haptic device.
#[doc(alias = "SDL_HapticRumbleStop")]
pub fn rumble_stop(&mut self) {
unsafe { sys::SDL_HapticRumbleStop(self.raw) };
}
@@ -52,6 +55,7 @@ impl Haptic {
impl Drop for Haptic {
#[doc(alias = "SDL_HapticClose")]
fn drop(&mut self) {
unsafe { sys::SDL_HapticClose(self.raw) }
}
+3
View File
@@ -73,6 +73,7 @@ pub fn get_video_minimize_on_focus_loss() -> bool {
}
}
#[doc(alias = "SDL_SetHint")]
pub fn set(name: &str, value: &str) -> bool{
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
@@ -81,6 +82,7 @@ pub fn set(name: &str, value: &str) -> bool{
}
}
#[doc(alias = "SDL_GetHint")]
pub fn get(name: &str) -> Option<String> {
use std::str;
@@ -97,6 +99,7 @@ pub fn get(name: &str) -> Option<String> {
}
}
#[doc(alias = "SDL_SetHintWithPriority")]
pub fn set_with_priority(name: &str, value: &str, priority: &Hint) -> bool {
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
+1
View File
@@ -152,6 +152,7 @@ impl<T> LoadTexture for TextureCreator<T> {
}
}
#[doc(alias = "IMG_LoadTexture")]
fn load_texture_bytes(&self, buf: &[u8]) -> Result<Texture, String> {
//! Loads an SDL Texture from a buffer that the format must be something supported by SDL2_image (png, jpeg, ect, but NOT RGBA8888 bytes for instance)
unsafe {
+24
View File
@@ -11,6 +11,7 @@ use crate::common::{validate_int, IntegerOrSdlError};
impl JoystickSubsystem {
/// Retrieve the total number of attached joysticks *and* controllers identified by SDL.
#[doc(alias = "SDL_NumJoysticks")]
pub fn num_joysticks(&self) -> Result<u32, String> {
let result = unsafe { sys::SDL_NumJoysticks() };
@@ -22,6 +23,7 @@ impl JoystickSubsystem {
}
/// Attempt to open the joystick at index `joystick_index` and return it.
#[doc(alias = "SDL_JoystickOpen")]
pub fn open(&self, joystick_index: u32)
-> Result<Joystick, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
@@ -40,6 +42,7 @@ impl JoystickSubsystem {
}
/// Return the name of the joystick at index `joystick_index`.
#[doc(alias = "SDL_JoystickNameForIndex")]
pub fn name_for_index(&self, joystick_index: u32) -> Result<String, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let joystick_index = validate_int(joystick_index, "joystick_index")?;
@@ -56,6 +59,7 @@ impl JoystickSubsystem {
}
/// Get the GUID for the joystick at index `joystick_index`
#[doc(alias = "SDL_JoystickGetDeviceGUID")]
pub fn device_guid(&self, joystick_index: u32) -> Result<Guid, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let joystick_index = validate_int(joystick_index, "joystick_index")?;
@@ -73,11 +77,13 @@ impl JoystickSubsystem {
/// If state is `true` joystick events are processed, otherwise
/// they're ignored.
#[doc(alias = "SDL_JoystickEventState")]
pub fn set_event_state(&self, state: bool) {
unsafe { sys::SDL_JoystickEventState(state as i32) };
}
/// Return `true` if joystick events are processed.
#[doc(alias = "SDL_JoystickEventState")]
pub fn event_state(&self) -> bool {
unsafe { sys::SDL_JoystickEventState(sys::SDL_QUERY as i32)
== sys::SDL_ENABLE as i32 }
@@ -85,6 +91,7 @@ impl JoystickSubsystem {
/// Force joystick update when not using the event loop
#[inline]
#[doc(alias = "SDL_JoystickUpdate")]
pub fn update(&self) {
unsafe { sys::SDL_JoystickUpdate() };
}
@@ -140,6 +147,7 @@ impl Joystick {
/// Return the name of the joystick or an empty string if no name
/// is found.
#[doc(alias = "SDL_JoystickName")]
pub fn name(&self) -> String {
let name = unsafe { sys::SDL_JoystickName(self.raw) };
@@ -148,10 +156,12 @@ impl Joystick {
/// Return true if the joystick has been opened and currently
/// connected.
#[doc(alias = "SDL_JoystickGetAttached")]
pub fn attached(&self) -> bool {
unsafe { sys::SDL_JoystickGetAttached(self.raw) != sys::SDL_bool::SDL_FALSE }
}
#[doc(alias = "SDL_JoystickInstanceID")]
pub fn instance_id(&self) -> u32 {
let result = unsafe { sys::SDL_JoystickInstanceID(self.raw) };
@@ -164,6 +174,7 @@ impl Joystick {
}
/// Retrieve the joystick's GUID
#[doc(alias = "SDL_JoystickGetGUID")]
pub fn guid(&self) -> Guid {
let raw = unsafe { sys::SDL_JoystickGetGUID(self.raw) };
@@ -178,6 +189,7 @@ impl Joystick {
}
/// Retrieve the battery level of this joystick
#[doc(alias = "SDL_JoystickCurrentPowerLevel")]
pub fn power_level(&self) -> Result<PowerLevel, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
clear_error();
@@ -200,6 +212,7 @@ impl Joystick {
}
/// Retrieve the number of axes for this joystick
#[doc(alias = "SDL_JoystickNumAxes")]
pub fn num_axes(&self) -> u32 {
let result = unsafe { sys::SDL_JoystickNumAxes(self.raw) };
@@ -214,6 +227,7 @@ impl Joystick {
/// Gets the position of the given `axis`.
///
/// The function will fail if the joystick doesn't have the provided axis.
#[doc(alias = "SDL_JoystickGetAxis")]
pub fn axis(&self, axis: u32) -> Result<i16, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
// This interface is a bit messed up: 0 is a valid position
@@ -239,6 +253,7 @@ impl Joystick {
}
/// Retrieve the number of buttons for this joystick
#[doc(alias = "SDL_JoystickNumButtons")]
pub fn num_buttons(&self) -> u32 {
let result = unsafe { sys::SDL_JoystickNumButtons(self.raw) };
@@ -253,6 +268,7 @@ impl Joystick {
/// Return `Ok(true)` if `button` is pressed.
///
/// The function will fail if the joystick doesn't have the provided button.
#[doc(alias = "SDL_JoystickGetButton")]
pub fn button(&self, button: u32) -> Result<bool, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
// Same deal as axis, 0 can mean both unpressed or
@@ -280,6 +296,7 @@ impl Joystick {
}
/// Retrieve the number of balls for this joystick
#[doc(alias = "SDL_JoystickNumBalls")]
pub fn num_balls(&self) -> u32 {
let result = unsafe { sys::SDL_JoystickNumBalls(self.raw) };
@@ -293,6 +310,7 @@ impl Joystick {
/// Return a pair `(dx, dy)` containing the difference in axis
/// position since the last poll
#[doc(alias = "SDL_JoystickGetBall")]
pub fn ball(&self, ball: u32) -> Result<(i32, i32), IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
let mut dx = 0;
@@ -309,6 +327,7 @@ impl Joystick {
}
/// Retrieve the number of balls for this joystick
#[doc(alias = "SDL_JoystickNumHats")]
pub fn num_hats(&self) -> u32 {
let result = unsafe { sys::SDL_JoystickNumHats(self.raw) };
@@ -321,6 +340,7 @@ impl Joystick {
}
/// Return the position of `hat` for this joystick
#[doc(alias = "SDL_JoystickGetHat")]
pub fn hat(&self, hat: u32) -> Result<HatState, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
// Guess what? This function as well uses 0 to report an error
@@ -357,6 +377,7 @@ impl Joystick {
/// the rumble effect to keep playing for a long time, as this results in
/// the effect ending immediately after starting due to an overflow.
/// Use some smaller, "huge enough" number instead.
#[doc(alias = "SDL_JoystickRumble")]
pub fn set_rumble(&mut self,
low_frequency_rumble: u16,
high_frequency_rumble: u16,
@@ -379,6 +400,7 @@ impl Joystick {
}
impl Drop for Joystick {
#[doc(alias = "SDL_JoystickClose")]
fn drop(&mut self) {
if self.attached() {
unsafe { sys::SDL_JoystickClose(self.raw) }
@@ -403,6 +425,7 @@ impl Eq for Guid {}
impl Guid {
/// Create a GUID from a string representation.
#[doc(alias = "SDL_JoystickGetGUIDFromString")]
pub fn from_string(guid: &str) -> Result<Guid, NulError> {
let guid = CString::new(guid)?;
@@ -423,6 +446,7 @@ impl Guid {
}
/// Return a String representation of GUID
#[doc(alias = "SDL_JoystickGetGUIDString")]
pub fn string(&self) -> String {
// Doc says "buf should supply at least 33bytes". I took that
// to mean that 33bytes should be enough in all cases, but
+3
View File
@@ -504,6 +504,7 @@ use crate::keyboard::Scancode;
impl Keycode {
/// Gets the virtual key from a scancode. Returns None if there is no corresponding virtual key.
#[doc(alias = "SDL_GetKeyFromScancode")]
pub fn from_scancode(scancode: Scancode) -> Option<Keycode> {
const UNKNOWN: i32 = sys::SDLK_UNKNOWN as i32;
unsafe {
@@ -514,6 +515,7 @@ impl Keycode {
}
}
#[doc(alias = "SDL_GetKeyFromName")]
pub fn from_name(name: &str) -> Option<Keycode> {
const UNKNOWN: i32 = sys::SDLK_UNKNOWN as i32;
unsafe {
@@ -528,6 +530,7 @@ impl Keycode {
}
}
#[doc(alias = "SDL_GetKeyName")]
pub fn name(self) -> String {
// The name string pointer's contents _might_ change, depending on the last call to SDL_GetKeyName.
// Knowing this, we must always return a new string.
+10
View File
@@ -41,6 +41,7 @@ pub struct KeyboardState<'a> {
}
impl<'a> KeyboardState<'a> {
#[doc(alias = "SDL_GetKeyboardState")]
pub fn new(_e: &'a EventPump) -> KeyboardState<'a> {
let keyboard_state = unsafe {
let mut count = 0;
@@ -179,6 +180,7 @@ pub struct KeyboardUtil {
impl KeyboardUtil {
/// Gets the id of the window which currently has keyboard focus.
#[doc(alias = "SDL_GetKeyboardFocus")]
pub fn focused_window_id(&self) -> Option<u32> {
let raw = unsafe { sys::SDL_GetKeyboardFocus() };
if raw.is_null() {
@@ -189,10 +191,12 @@ impl KeyboardUtil {
}
}
#[doc(alias = "SDL_GetModState")]
pub fn mod_state(&self) -> Mod {
unsafe { Mod::from_bits(sys::SDL_GetModState() as u16).unwrap() }
}
#[doc(alias = "SDL_SetModState")]
pub fn set_mod_state(&self, flags: Mod) {
unsafe { sys::SDL_SetModState(transmute::<u32, sys::SDL_Keymod>(flags.bits() as u32)); }
}
@@ -214,26 +218,32 @@ pub struct TextInputUtil {
}
impl TextInputUtil {
#[doc(alias = "SDL_StartTextInput")]
pub fn start(&self) {
unsafe { sys::SDL_StartTextInput(); }
}
#[doc(alias = "SDL_IsTextInputActive")]
pub fn is_active(&self, ) -> bool {
unsafe { sys::SDL_IsTextInputActive() == sys::SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_StopTextInput")]
pub fn stop(&self) {
unsafe { sys::SDL_StopTextInput(); }
}
#[doc(alias = "SDL_SetTextInputRect")]
pub fn set_rect(&self, rect: Rect) {
unsafe { sys::SDL_SetTextInputRect(rect.raw() as *mut sys::SDL_Rect); }
}
#[doc(alias = "SDL_HasScreenKeyboardSupport")]
pub fn has_screen_keyboard_support(&self) -> bool {
unsafe { sys::SDL_HasScreenKeyboardSupport() == sys::SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_IsScreenKeyboardShown")]
pub fn is_screen_keyboard_shown(&self, window: &Window) -> bool {
unsafe { sys::SDL_IsScreenKeyboardShown(window.raw()) == sys::SDL_bool::SDL_TRUE }
}
+3
View File
@@ -517,6 +517,7 @@ use crate::keyboard::Keycode;
impl Scancode {
/// Gets the scancode from a virtual key. Returns None if there is no corresponding scancode.
#[doc(alias = "SDL_GetScancodeFromKey")]
pub fn from_keycode(keycode: Keycode) -> Option<Scancode> {
unsafe {
match sys::SDL_GetScancodeFromKey(keycode as i32) {
@@ -526,6 +527,7 @@ impl Scancode {
}
}
#[doc(alias = "SDL_GetScancodeFromName")]
pub fn from_name(name: &str) -> Option<Scancode> {
unsafe {
match CString::new(name) {
@@ -539,6 +541,7 @@ impl Scancode {
}
}
#[doc(alias = "SDL_GetScancodeName")]
pub fn name(self) -> &'static str {
// The name string pointer lives in static, read-only memory.
// Knowing this, we can always return a string slice.
+2
View File
@@ -85,6 +85,7 @@ unsafe extern "C" fn rust_sdl2_log_fn(_userdata: *mut libc::c_void,
custom_log_fn(priority, category, &*message);
}
#[doc(alias = "SDL_LogSetOutputFunction")]
pub fn set_output_function(callback : fn(Priority, Category, &str)) {
unsafe {
custom_log_fn = callback;
@@ -94,6 +95,7 @@ pub fn set_output_function(callback : fn(Priority, Category, &str)) {
/// Standard log function which takes as priority INFO and
/// as category APPLICATION
#[doc(alias = "SDL_Log")]
pub fn log(message: &str) {
let message = message.replace('%', "%%");
let message = CString::new(message).unwrap();
+2
View File
@@ -141,6 +141,7 @@ impl error::Error for ShowMessageError {
/// There is no way to know if the user clicked "Ok" or closed the message box,
/// If you want to retrieve which button was clicked and customize a bit more
/// your message box, use `show_message_box` instead.
#[doc(alias = "SDL_ShowSimpleMessageBox")]
pub fn show_simple_message_box<'a, W>(flags: MessageBoxFlag, title: &str,
message: &str, window: W)
-> Result<(), ShowMessageError>
@@ -180,6 +181,7 @@ where W: Into<Option<&'a Window>>
/// Note that the variant of the `ClickedButton` enum will also be returned if the message box
/// has been forcefully closed (Alt-F4, ...)
///
#[doc(alias = "SDL_ShowMessageBox")]
pub fn show_message_box<'a, 'b, W, M>(flags:MessageBoxFlag, buttons:&'a [ButtonData], title:&str,
message:&str, window: W, scheme: M)
-> Result<ClickedButton<'a>,ShowMessageError>
+1
View File
@@ -791,6 +791,7 @@ impl<'a> Music<'a> {
}
/// Load music from a static byte buffer.
#[doc(alias = "SDL_RWFromConstMem")]
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Music<'static>, String> {
let rw = unsafe {
sys::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int)
+13
View File
@@ -33,12 +33,14 @@ pub struct Cursor {
impl Drop for Cursor {
#[inline]
#[doc(alias = "SDL_FreeCursor")]
fn drop(&mut self) {
unsafe { sys::SDL_FreeCursor(self.raw) };
}
}
impl Cursor {
#[doc(alias = "SDL_CreateCursor")]
pub fn new(data: &[u8], mask: &[u8], width: i32, height: i32, hot_x: i32, hot_y: i32) -> Result<Cursor, String> {
unsafe {
let raw = sys::SDL_CreateCursor(data.as_ptr(),
@@ -55,6 +57,7 @@ impl Cursor {
}
// TODO: figure out how to pass Surface in here correctly
#[doc(alias = "SDL_CreateColorCursor")]
pub fn from_surface<S: AsRef<SurfaceRef>>(surface: S, hot_x: i32, hot_y: i32) -> Result<Cursor, String> {
unsafe {
let raw = sys::SDL_CreateColorCursor(surface.as_ref().raw(), hot_x, hot_y);
@@ -67,6 +70,7 @@ impl Cursor {
}
}
#[doc(alias = "SDL_CreateSystemCursor")]
pub fn from_system(cursor: SystemCursor) -> Result<Cursor, String> {
unsafe {
let raw = sys::SDL_CreateSystemCursor(transmute(cursor as u32));
@@ -79,6 +83,7 @@ impl Cursor {
}
}
#[doc(alias = "SDL_SetCursor")]
pub fn set(&self) {
unsafe { sys::SDL_SetCursor(self.raw); }
}
@@ -160,6 +165,7 @@ pub struct MouseState {
}
impl MouseState {
#[doc(alias = "SDL_GetMouseState")]
pub fn new(_e: &EventPump) -> MouseState {
let mut x = 0;
let mut y = 0;
@@ -338,6 +344,7 @@ pub struct MouseUtil {
impl MouseUtil {
/// Gets the id of the window which currently has mouse focus.
#[doc(alias = "SDL_GetMouseFocus")]
pub fn focused_window_id(&self) -> Option<u32> {
let raw = unsafe { sys::SDL_GetMouseFocus() };
if raw.is_null() {
@@ -348,27 +355,33 @@ impl MouseUtil {
}
}
#[doc(alias = "SDL_WarpMouseInWindow")]
pub fn warp_mouse_in_window(&self, window: &video::Window, x: i32, y: i32) {
unsafe { sys::SDL_WarpMouseInWindow(window.raw(), x, y); }
}
#[doc(alias = "SDL_SetRelativeMouseMode")]
pub fn set_relative_mouse_mode(&self, on: bool) {
let on = if on { sys::SDL_bool::SDL_TRUE } else { sys::SDL_bool::SDL_FALSE };
unsafe { sys::SDL_SetRelativeMouseMode(on); }
}
#[doc(alias = "SDL_GetRelativeMouseMode")]
pub fn relative_mouse_mode(&self) -> bool {
unsafe { sys::SDL_GetRelativeMouseMode() == sys::SDL_bool::SDL_TRUE }
}
#[doc(alias = "SDL_ShowCursor")]
pub fn is_cursor_showing(&self) -> bool {
unsafe { sys::SDL_ShowCursor(crate::sys::SDL_QUERY) == 1 }
}
#[doc(alias = "SDL_ShowCursor")]
pub fn show_cursor(&self, show: bool) {
unsafe { sys::SDL_ShowCursor(show as i32); }
}
#[doc(alias = "SDL_CaptureMouse")]
pub fn capture(&self, enable: bool) {
let enable = if enable { sys::SDL_bool::SDL_TRUE } else { sys::SDL_bool::SDL_FALSE };
unsafe { sys::SDL_CaptureMouse(enable); }
+1
View File
@@ -12,6 +12,7 @@ pub struct RelativeMouseState {
}
impl RelativeMouseState {
#[doc(alias = "SDL_GetRelativeMouseState")]
pub fn new(_e: &EventPump) -> RelativeMouseState {
let mut x = 0;
let mut y = 0;
+8
View File
@@ -11,6 +11,7 @@ pub struct Palette {
impl Palette {
#[inline]
/// Creates a new, uninitialized palette
#[doc(alias = "SDL_AllocPalette")]
pub fn new(mut capacity: usize) -> Result<Self, String> {
use crate::common::*;
@@ -39,6 +40,7 @@ impl Palette {
}
/// Creates a palette from the provided colors
#[doc(alias = "SDL_SetPaletteColors")]
pub fn with_colors(colors: &[Color]) -> Result<Self, String> {
let pal = Self::new(colors.len())?;
@@ -72,6 +74,7 @@ impl Palette {
}
impl Drop for Palette {
#[doc(alias = "SDL_FreePalette")]
fn drop(&mut self) {
unsafe { sys::SDL_FreePalette(self.raw); }
}
@@ -111,10 +114,12 @@ impl Color {
Color { r, g, b, a }
}
#[doc(alias = "SDL_MapRGBA")]
pub fn to_u32(self, format: &PixelFormat) -> u32 {
unsafe { sys::SDL_MapRGBA(format.raw, self.r, self.g, self.b, self.a) }
}
#[doc(alias = "SDL_GetRGBA")]
pub fn from_u32(format: &PixelFormat, pixel: u32) -> Color {
let (mut r, mut g, mut b, mut a) = (0, 0, 0, 0);
@@ -261,6 +266,7 @@ impl PixelFormatEnum {
}
impl PixelFormatEnum {
#[doc(alias = "SDL_MasksToPixelFormatEnum")]
pub fn from_masks(masks: PixelMasks) -> PixelFormatEnum {
unsafe {
let format = sys::SDL_MasksToPixelFormatEnum(masks.bpp as i32, masks.rmask, masks.gmask, masks.bmask, masks.amask);
@@ -268,6 +274,7 @@ impl PixelFormatEnum {
}
}
#[doc(alias = "SDL_PixelFormatEnumToMasks")]
pub fn into_masks(self) -> Result<PixelMasks, String> {
let format: u32 = self as u32;
let mut bpp = 0;
@@ -457,6 +464,7 @@ impl TryFrom<u32> for PixelFormatEnum {
impl TryFrom<PixelFormatEnum> for PixelFormat {
type Error = String;
#[doc(alias = "SDL_AllocFormat")]
fn try_from(pfe: PixelFormatEnum) -> Result<Self, Self::Error> {
unsafe {
let pf_ptr = sys::SDL_AllocFormat(pfe as u32);
+1
View File
@@ -4,6 +4,7 @@ use self::raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use crate::{sys::SDL_Window, video::Window};
unsafe impl HasRawWindowHandle for Window {
#[doc(alias = "SDL_GetVersion")]
fn raw_window_handle(&self) -> RawWindowHandle {
use self::SDL_SYSWM_TYPE::*;

Some files were not shown because too many files have changed in this diff Show More