Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error #27

Merged
merged 8 commits into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 63 additions & 76 deletions src/sdl3/sdl.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
use libc::c_char;
use sys::init::{SDL_INIT_AUDIO, SDL_INIT_CAMERA, SDL_INIT_EVENTS, SDL_INIT_GAMEPAD, SDL_INIT_HAPTIC, SDL_INIT_JOYSTICK, SDL_INIT_SENSOR, SDL_INIT_VIDEO};
use std::cell::Cell;
use std::error;
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use std::mem::transmute;
use std::os::raw::c_void;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use sys::init::{
SDL_INIT_AUDIO, SDL_INIT_CAMERA, SDL_INIT_EVENTS, SDL_INIT_GAMEPAD, SDL_INIT_HAPTIC,
SDL_INIT_JOYSTICK, SDL_INIT_SENSOR, SDL_INIT_VIDEO,
};

use crate::sys;
use crate::sys::init::SDL_InitFlags;

#[repr(i32)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Error {
NoMemError = sys::SDL_errorcode::SDL_ENOMEM as i32,
ReadError = sys::SDL_errorcode::SDL_EFREAD as i32,
WriteError = sys::SDL_errorcode::SDL_EFWRITE as i32,
SeekError = sys::SDL_errorcode::SDL_EFSEEK as i32,
UnsupportedError = sys::SDL_errorcode::SDL_UNSUPPORTED as i32,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;

match *self {
NoMemError => write!(f, "Out of memory"),
ReadError => write!(f, "Error reading from datastream"),
WriteError => write!(f, "Error writing to datastream"),
SeekError => write!(f, "Error seeking in datastream"),
UnsupportedError => write!(f, "Unknown SDL error"),
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
use self::Error::*;

match *self {
NoMemError => "out of memory",
ReadError => "error reading from datastream",
WriteError => "error writing to datastream",
SeekError => "error seeking in datastream",
UnsupportedError => "unknown SDL error",
}
}
}
// seems like these are gone?
// #[repr(i32)]
// #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
// pub enum Error {
// NoMemError = sys::SDL_errorcode::SDL_ENOMEM as i32,
// ReadError = SDL_errorcode::SDL_EFREAD as i32,
// WriteError = sys::SDL_errorcode::SDL_EFWRITE as i32,
// SeekError = sys::SDL_errorcode::SDL_EFSEEK as i32,
// UnsupportedError = sys::SDL_errorcode::SDL_UNSUPPORTED as i32,
// }

// impl fmt::Display for Error {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// use self::Error::*;
//
// match *self {
// NoMemError => write!(f, "Out of memory"),
// ReadError => write!(f, "Error reading from datastream"),
// WriteError => write!(f, "Error writing to datastream"),
// SeekError => write!(f, "Error seeking in datastream"),
// UnsupportedError => write!(f, "Unknown SDL error"),
// }
// }
// }

// impl error::Error for Error {
// fn description(&self) -> &str {
// use self::Error::*;
//
// match *self {
// NoMemError => "out of memory",
// ReadError => "error reading from datastream",
// WriteError => "error writing to datastream",
// SeekError => "error seeking in datastream",
// UnsupportedError => "unknown SDL error",
// }
// }
// }

/// True if the main thread has been declared. The main thread is declared when
/// SDL is first initialized.
Expand Down Expand Up @@ -105,10 +109,10 @@ impl Sdl {
let result;

unsafe {
result = sys::SDL_Init(0);
result = sys::init::SDL_Init(0);
}

if result != 0 {
if !result {
SDL_COUNT.store(0, Ordering::Relaxed);
return Err(get_error());
}
Expand Down Expand Up @@ -157,12 +161,6 @@ impl Sdl {
SensorSubsystem::new(self)
}

/// Initializes the timer subsystem.
#[inline]
pub fn timer(&self) -> Result<TimerSubsystem, String> {
TimerSubsystem::new(self)
}

/// Initializes the video subsystem.
#[inline]
pub fn video(&self) -> Result<VideoSubsystem, String> {
Expand Down Expand Up @@ -213,7 +211,7 @@ impl Drop for SdlDrop {
assert!(prev_count > 0);
if prev_count == 1 {
unsafe {
sys::SDL_Quit();
sys::init::SDL_Quit();
}
}
}
Expand Down Expand Up @@ -305,24 +303,14 @@ impl Drop for SubsystemDrop {
assert!(prev_count > 0);
if prev_count == 1 {
unsafe {
sys::SDL_QuitSubSystem(self.flag);
sys::init::SDL_QuitSubSystem(self.flag);
}
}
}
}

subsystem!(
AudioSubsystem,
SDL_INIT_AUDIO as u32,
AUDIO_COUNT,
nosync
);
subsystem!(
VideoSubsystem,
SDL_INIT_VIDEO as u32,
VIDEO_COUNT,
nosync
);
subsystem!(AudioSubsystem, SDL_INIT_AUDIO as u32, AUDIO_COUNT, nosync);
subsystem!(VideoSubsystem, SDL_INIT_VIDEO as u32, VIDEO_COUNT, nosync);
subsystem!(
JoystickSubsystem,
SDL_INIT_JOYSTICK as u32,
Expand All @@ -342,12 +330,7 @@ subsystem!(
nosync
);
// The event queue can be read from other threads.
subsystem!(
EventSubsystem,
SDL_INIT_EVENTS as u32,
EVENT_COUNT,
sync
);
subsystem!(EventSubsystem, SDL_INIT_EVENTS as u32, EVENT_COUNT, sync);
subsystem!(
SensorSubsystem,
SDL_INIT_SENSOR as u32,
Expand Down Expand Up @@ -398,7 +381,11 @@ impl Drop for EventPump {
#[inline]
#[doc(alias = "SDL_GetPlatform")]
pub fn get_platform() -> &'static str {
unsafe { CStr::from_ptr(sys::SDL_GetPlatform()).to_str().unwrap() }
unsafe {
CStr::from_ptr(sys::platform::SDL_GetPlatform())
.to_str()
.unwrap()
}
}

/// Initializes the SDL library.
Expand All @@ -423,7 +410,7 @@ pub fn init() -> Result<Sdl, String> {

pub fn get_error() -> String {
unsafe {
let err = sys::SDL_GetError();
let err = sys::error::SDL_GetError();
CStr::from_ptr(err as *const _).to_str().unwrap().to_owned()
}
}
Expand All @@ -432,24 +419,24 @@ pub fn get_error() -> String {
pub fn set_error(err: &str) -> Result<(), NulError> {
let c_string = CString::new(err)?;
unsafe {
sys::SDL_SetError(
sys::error::SDL_SetError(
b"%s\0".as_ptr() as *const c_char,
c_string.as_ptr() as *const c_char,
);
}
Ok(())
}

#[doc(alias = "SDL_Error")]
pub fn set_error_from_code(err: Error) {
unsafe {
sys::SDL_Error(transmute(err));
}
}
// #[doc(alias = "SDL_Error")]
// pub fn set_error_from_code(err: Error) {
// unsafe {
// sys::error::SDL_Error(transmute(err));
// }
// }

#[doc(alias = "SDL_ClearError")]
pub fn clear_error() {
unsafe {
sys::SDL_ClearError();
sys::error::SDL_ClearError();
}
}
Loading