Skip to content

Commit

Permalink
Initial port to SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
17cupsofcoffee committed Nov 27, 2024
1 parent eb53253 commit e708150
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
# Core
fermium = { version = "22605", default-features = false }
sdl3-sys = { version = "0.1.3" }
glow = "0.16"
bytemuck = { version = "1.20", features = ["derive"] }
glam = { version = "0.29", features = ["bytemuck"] }
Expand All @@ -24,5 +24,5 @@ rand = "0.8"
[features]
default = ["ldtk"]
ldtk = ["serde", "serde_json"]
static_bundled_build = ["fermium/static_bundled_build"]
# static_bundled_build = ["fermium/static_bundled_build"]
serde = ["dep:serde", "glam/serde"]
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fermium::prelude::SDL_QUIT;
use sdl3_sys::events::SDL_EVENT_QUIT;

use crate::graphics::Graphics;
use crate::input::{Event, Input};
Expand Down Expand Up @@ -70,7 +70,7 @@ impl App {
pub fn handle_events(&mut self, event_handler: &mut impl EventHandler) {
while let Some(event) = self.window.next_event() {
unsafe {
if event.type_ == SDL_QUIT {
if event.r#type == SDL_EVENT_QUIT.0 {
self.is_running = false;
}
}
Expand Down
78 changes: 38 additions & 40 deletions src/input/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use fermium::prelude::*;
use glam::Vec2;
use sdl3_sys::events::*;
use sdl3_sys::gamepad::*;
use sdl3_sys::joystick::*;

/// This is a unique ID for a joystick for the time it is connected to the
/// system.
Expand All @@ -11,15 +13,12 @@ use glam::Vec2;
pub struct JoystickID(SDL_JoystickID);

impl JoystickID {
fn from_raw(id: i32) -> JoystickID {
JoystickID(SDL_JoystickID(id))
fn from_raw(id: SDL_JoystickID) -> JoystickID {
JoystickID(id)
}
fn from_controller_handle(handle: *mut SDL_GameController) -> JoystickID {
unsafe {
JoystickID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(
handle,
)))
}

fn from_controller_handle(handle: *mut SDL_Gamepad) -> JoystickID {
unsafe { JoystickID(SDL_GetJoystickID(SDL_GetGamepadJoystick(handle))) }
}
}

Expand Down Expand Up @@ -69,39 +68,39 @@ pub enum Event {
impl Event {
pub fn try_from_sdl_event(event: &SDL_Event) -> Option<Self> {
unsafe {
match event.type_ {
SDL_KEYDOWN if event.key.repeat == 0 => {
if let Some(key) = Key::from_raw(event.key.keysym.scancode) {
match SDL_EventType(event.r#type) {
SDL_EVENT_KEY_DOWN if event.key.repeat => {
if let Some(key) = Key::from_raw(event.key.scancode) {
return Some(Event::KeyDown(key));
}
}

SDL_KEYUP if event.key.repeat == 0 => {
if let Some(key) = Key::from_raw(event.key.keysym.scancode) {
SDL_EVENT_KEY_UP if event.key.repeat => {
if let Some(key) = Key::from_raw(event.key.scancode) {
return Some(Event::KeyUp(key));
}
}

SDL_MOUSEBUTTONDOWN => {
if let Some(button) = MouseButton::from_raw(event.button.button as u32) {
SDL_EVENT_MOUSE_BUTTON_DOWN => {
if let Some(button) = MouseButton::from_raw(event.button.button as i32) {
return Some(Event::MouseButtonDown(button));
}
}

SDL_MOUSEBUTTONUP => {
if let Some(button) = MouseButton::from_raw(event.button.button as u32) {
SDL_EVENT_MOUSE_BUTTON_UP => {
if let Some(button) = MouseButton::from_raw(event.button.button as i32) {
return Some(Event::MouseButtonUp(button));
}
}

SDL_MOUSEMOTION => {
SDL_EVENT_MOUSE_MOTION => {
return Some(Event::MouseMotion {
new_position: Vec2::new(event.motion.x as f32, event.motion.y as f32),
new_position: Vec2::new(event.motion.x, event.motion.y),
});
}

SDL_CONTROLLERDEVICEADDED => {
let handle = SDL_GameControllerOpen(event.cdevice.which);
SDL_EVENT_GAMEPAD_ADDED => {
let handle = SDL_OpenGamepad(event.cdevice.which);

if handle.is_null() {
// TODO: Should probably log here
Expand All @@ -115,42 +114,42 @@ impl Event {
return Some(Event::ControllerDeviceAdded { joystick, gamepad });
}

SDL_CONTROLLERDEVICEREMOVED => {
SDL_EVENT_GAMEPAD_REMOVED => {
return Some(Event::ControllerDeviceRemoved {
joystick: JoystickID::from_raw(event.cdevice.which),
});
}

SDL_CONTROLLERBUTTONDOWN => {
if let Some(button) = GamepadButton::from_raw(SDL_GameControllerButton(
event.cbutton.button as i32,
)) {
SDL_EVENT_GAMEPAD_BUTTON_DOWN => {
if let Some(button) =
GamepadButton::from_raw(SDL_GamepadButton(event.gbutton.button as i32))
{
return Some(Event::ControllerButtonDown {
joystick: JoystickID::from_raw(event.cdevice.which),
button,
});
}
}

SDL_CONTROLLERBUTTONUP => {
if let Some(button) = GamepadButton::from_raw(SDL_GameControllerButton(
event.cbutton.button as i32,
)) {
SDL_EVENT_GAMEPAD_BUTTON_UP => {
if let Some(button) =
GamepadButton::from_raw(SDL_GamepadButton(event.gbutton.button as i32))
{
return Some(Event::ControllerButtonUp {
joystick: JoystickID::from_raw(event.cdevice.which),
button,
});
}
}

SDL_CONTROLLERAXISMOTION => {
SDL_EVENT_GAMEPAD_AXIS_MOTION => {
if let Some(axis) =
GamepadAxis::from_raw(SDL_GameControllerAxis(event.caxis.axis as i32))
GamepadAxis::from_raw(SDL_GamepadAxis(event.gaxis.axis as i32))
{
let mut value = if event.caxis.value > 0 {
event.caxis.value as f32 / 32767.0
let mut value = if event.gaxis.value > 0 {
event.gaxis.value as f32 / 32767.0
} else {
event.caxis.value as f32 / 32768.0
event.gaxis.value as f32 / 32768.0
};

// TODO: Add less hacky deadzone logic
Expand All @@ -165,7 +164,7 @@ impl Event {
}
}

SDL_WINDOWEVENT => {
SDL_EVENT_WINDOW_RESIZED => {
let e = &event.window;
if e.data1 > 0 && e.data2 > 0 {
let width = e.data1 as u32;
Expand All @@ -174,9 +173,8 @@ impl Event {
}
}

SDL_TEXTINPUT => {
let e = &event.text.text;
let text = std::ffi::CStr::from_ptr(e.as_ptr())
SDL_EVENT_TEXT_INPUT => {
let text = std::ffi::CStr::from_ptr(event.text.text)
.to_string_lossy()
.into_owned();
return Some(Event::TextInput { text });
Expand Down
54 changes: 27 additions & 27 deletions src/input/gamepad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, rc::Rc};

use fermium::prelude::*;
use sdl3_sys::gamepad::*;

#[derive(Clone)]
pub struct Gamepad(#[allow(dead_code)] Rc<GamepadInner>);
Expand All @@ -18,19 +18,19 @@ impl fmt::Debug for Gamepad {
}

struct GamepadInner {
handle: *mut SDL_GameController,
handle: *mut SDL_Gamepad,
}

impl Gamepad {
pub fn from_raw(raw: *mut SDL_GameController) -> Gamepad {
pub fn from_raw(raw: *mut SDL_Gamepad) -> Gamepad {
Gamepad(Rc::new(GamepadInner { handle: raw }))
}
}

impl Drop for GamepadInner {
fn drop(&mut self) {
unsafe {
SDL_GameControllerClose(self.handle);
SDL_CloseGamepad(self.handle);
}
}
}
Expand All @@ -55,23 +55,23 @@ pub enum GamepadButton {
}

impl GamepadButton {
pub(crate) fn from_raw(raw: SDL_GameControllerButton) -> Option<GamepadButton> {
pub(crate) fn from_raw(raw: SDL_GamepadButton) -> Option<GamepadButton> {
match raw {
SDL_CONTROLLER_BUTTON_A => Some(GamepadButton::A),
SDL_CONTROLLER_BUTTON_B => Some(GamepadButton::B),
SDL_CONTROLLER_BUTTON_X => Some(GamepadButton::X),
SDL_CONTROLLER_BUTTON_Y => Some(GamepadButton::Y),
SDL_CONTROLLER_BUTTON_BACK => Some(GamepadButton::Back),
SDL_CONTROLLER_BUTTON_GUIDE => Some(GamepadButton::Guide),
SDL_CONTROLLER_BUTTON_START => Some(GamepadButton::Start),
SDL_CONTROLLER_BUTTON_LEFTSTICK => Some(GamepadButton::LeftStick),
SDL_CONTROLLER_BUTTON_RIGHTSTICK => Some(GamepadButton::RightStick),
SDL_CONTROLLER_BUTTON_LEFTSHOULDER => Some(GamepadButton::LeftShoulder),
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER => Some(GamepadButton::RightShoulder),
SDL_CONTROLLER_BUTTON_DPAD_UP => Some(GamepadButton::Up),
SDL_CONTROLLER_BUTTON_DPAD_DOWN => Some(GamepadButton::Down),
SDL_CONTROLLER_BUTTON_DPAD_LEFT => Some(GamepadButton::Left),
SDL_CONTROLLER_BUTTON_DPAD_RIGHT => Some(GamepadButton::Right),
SDL_GAMEPAD_BUTTON_SOUTH => Some(GamepadButton::A),
SDL_GAMEPAD_BUTTON_EAST => Some(GamepadButton::B),
SDL_GAMEPAD_BUTTON_WEST => Some(GamepadButton::X),
SDL_GAMEPAD_BUTTON_NORTH => Some(GamepadButton::Y),
SDL_GAMEPAD_BUTTON_BACK => Some(GamepadButton::Back),
SDL_GAMEPAD_BUTTON_GUIDE => Some(GamepadButton::Guide),
SDL_GAMEPAD_BUTTON_START => Some(GamepadButton::Start),
SDL_GAMEPAD_BUTTON_LEFT_STICK => Some(GamepadButton::LeftStick),
SDL_GAMEPAD_BUTTON_RIGHT_STICK => Some(GamepadButton::RightStick),
SDL_GAMEPAD_BUTTON_LEFT_SHOULDER => Some(GamepadButton::LeftShoulder),
SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER => Some(GamepadButton::RightShoulder),
SDL_GAMEPAD_BUTTON_DPAD_UP => Some(GamepadButton::Up),
SDL_GAMEPAD_BUTTON_DPAD_DOWN => Some(GamepadButton::Down),
SDL_GAMEPAD_BUTTON_DPAD_LEFT => Some(GamepadButton::Left),
SDL_GAMEPAD_BUTTON_DPAD_RIGHT => Some(GamepadButton::Right),
_ => None,
}
}
Expand All @@ -88,14 +88,14 @@ pub enum GamepadAxis {
}

impl GamepadAxis {
pub(crate) fn from_raw(raw: SDL_GameControllerAxis) -> Option<GamepadAxis> {
pub(crate) fn from_raw(raw: SDL_GamepadAxis) -> Option<GamepadAxis> {
match raw {
SDL_CONTROLLER_AXIS_LEFTX => Some(GamepadAxis::LeftStickX),
SDL_CONTROLLER_AXIS_LEFTY => Some(GamepadAxis::LeftStickY),
SDL_CONTROLLER_AXIS_RIGHTX => Some(GamepadAxis::RightStickX),
SDL_CONTROLLER_AXIS_RIGHTY => Some(GamepadAxis::RightStickY),
SDL_CONTROLLER_AXIS_TRIGGERLEFT => Some(GamepadAxis::LeftTrigger),
SDL_CONTROLLER_AXIS_TRIGGERRIGHT => Some(GamepadAxis::RightTrigger),
SDL_GAMEPAD_AXIS_LEFTX => Some(GamepadAxis::LeftStickX),
SDL_GAMEPAD_AXIS_LEFTY => Some(GamepadAxis::LeftStickY),
SDL_GAMEPAD_AXIS_RIGHTX => Some(GamepadAxis::RightStickX),
SDL_GAMEPAD_AXIS_RIGHTY => Some(GamepadAxis::RightStickY),
SDL_GAMEPAD_AXIS_LEFT_TRIGGER => Some(GamepadAxis::LeftTrigger),
SDL_GAMEPAD_AXIS_RIGHT_TRIGGER => Some(GamepadAxis::RightTrigger),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/input/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fermium::prelude::*;
use sdl3_sys::scancode::*;

macro_rules! keys {
($($key:ident => $raw:ident),*$(,)?) => {
Expand Down
4 changes: 2 additions & 2 deletions src/input/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fermium::prelude::*;
use sdl3_sys::mouse::*;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Expand All @@ -10,7 +10,7 @@ pub enum MouseButton {
}

impl MouseButton {
pub(crate) fn from_raw(raw: u32) -> Option<MouseButton> {
pub(crate) fn from_raw(raw: i32) -> Option<MouseButton> {
match raw {
SDL_BUTTON_LEFT => Some(MouseButton::Left),
SDL_BUTTON_MIDDLE => Some(MouseButton::Middle),
Expand Down
Loading

0 comments on commit e708150

Please sign in to comment.