Skip to content

Commit

Permalink
Remove unsafe from app layer
Browse files Browse the repository at this point in the history
  • Loading branch information
17cupsofcoffee committed Dec 2, 2024
1 parent 1773d02 commit 5cfd751
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use sdl3_sys::events::SDL_EVENT_QUIT;

use crate::graphics::Graphics;
use crate::input::{Event, Input};
use crate::time::Timer;
Expand Down Expand Up @@ -69,13 +67,11 @@ impl App {

pub fn handle_events(&mut self, event_handler: &mut impl EventHandler) {
while let Some(event) = self.window.next_event() {
unsafe {
if event.r#type == SDL_EVENT_QUIT.0 {
if let Some(event) = Event::try_from_sdl_event(&event) {
if let Event::Quit = event {
self.is_running = false;
}
}

if let Some(event) = crate::input::Event::try_from_sdl_event(&event) {
self.input.event(&event);

event_handler.event(self, event);
Expand Down
8 changes: 7 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Input {
Event::MouseButtonDown(button) => self.mouse_buttons.set_down(*button),
Event::MouseButtonUp(button) => self.mouse_buttons.set_up(*button),
Event::MouseMotion { new_position } => self.mouse_position = *new_position,

Event::ControllerDeviceAdded { joystick, gamepad } => {
let empty_slot = self.gamepads.iter().position(Option::is_none);

Expand All @@ -63,21 +64,25 @@ impl Input {

self.joystick_ids.insert(*joystick, gamepad_id);
}

Event::ControllerDeviceRemoved { joystick } => {
if let Some(gamepad_id) = self.joystick_ids.remove(joystick) {
self.gamepads[gamepad_id] = None;
}
}

Event::ControllerButtonDown { joystick, button } => {
if let Some(gamepad_id) = self.joystick_ids.get(joystick) {
self.gamepad_buttons.set_down((*gamepad_id, *button));
}
}

Event::ControllerButtonUp { joystick, button } => {
if let Some(gamepad_id) = self.joystick_ids.get(joystick) {
self.gamepad_buttons.set_up((*gamepad_id, *button));
}
}

Event::ControllerAxisMotion {
joystick,
axis,
Expand All @@ -87,7 +92,8 @@ impl Input {
self.axes.set_value(*gamepad_id, *axis, *value);
}
}
Event::WindowResized { .. } | Event::TextInput { .. } => {}

_ => {}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/input/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use super::{Gamepad, GamepadAxis, GamepadButton, Key, MouseButton};

#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Quit,
KeyDown(Key),
KeyUp(Key),
MouseButtonDown(MouseButton),
Expand Down Expand Up @@ -69,6 +70,10 @@ impl Event {
pub fn try_from_sdl_event(event: &SDL_Event) -> Option<Self> {
unsafe {
match SDL_EventType(event.r#type) {
SDL_EVENT_QUIT => {
return Some(Event::Quit);
}

SDL_EVENT_KEY_DOWN if !event.key.repeat => {
if let Some(key) = Key::from_raw(event.key.scancode) {
return Some(Event::KeyDown(key));
Expand Down

0 comments on commit 5cfd751

Please sign in to comment.