use boll::*;
use common::{Point2D, Rect};
+use core::controller::ControllerManager;
use point; // defined in common, but loaded from main...
use rand::Rng;
use sdl2::event::{Event, WindowEvent};
let context = sdl2::init().unwrap();
sdl2::image::init(sdl2::image::InitFlag::PNG)?;
let video = context.video()?;
-
self.print_video_display_modes(&video);
let window = video
let event_pump = context.event_pump()?;
let sprites = SpriteManager::new(canvas.texture_creator());
let screen = canvas.output_size().unwrap();
+ let ctrl = context.game_controller()?;
+ ctrl.set_event_state(true);
Ok(App {
canvas,
event_pump,
sprites,
state: self.state.unwrap_or_else(|| Box::new(ActiveState::new(screen))),
+ ctrl_man: ControllerManager::new(ctrl, context.haptic()?),
})
}
pub event_pump: EventPump,
pub sprites: SpriteManager,
pub state: Box<dyn AppState>,
+ pub ctrl_man: ControllerManager,
}
impl App {
if frame_count == FPS as u64 {
let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64
/ 1_000_000_000.0;
- println!("fps: {}", frame_count as f64 / duration);
+ // println!("fps: {}", frame_count as f64 / duration);
frame_count = 0;
fps_time = PreciseTime::now();
}
fn handle_events(&mut self) -> Result<(), ()> {
for event in self.event_pump.poll_iter() {
+ self.ctrl_man.handle_event(&event);
match event {
+ Event::ControllerButtonDown { .. } => {
+ let c = self.ctrl_man.controllers[0].clone();
+ c.rumble(0.75, 100);
+ }
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
--- /dev/null
+use std::cell::RefCell;
+use sdl2::haptic::Haptic;
+use sdl2::HapticSubsystem;
+use sdl2::GameControllerSubsystem;
+use sdl2::event::Event;
+use sdl2::controller::GameController;
+use std::rc::Rc;
+
+//#[derive(Debug)]
+pub struct ControllerManager {
+ ctrl: GameControllerSubsystem,
+ haptic: Rc<HapticSubsystem>,
+ pub controllers: Vec<Rc<Controller>>,
+}
+
+//#[derive(Debug)]
+pub struct Controller {
+ id: u32,
+ pub ctrl: GameController,
+ haptic: Option<Rc<RefCell<Haptic>>>,
+}
+
+impl ControllerManager {
+ pub fn new(ctrl: GameControllerSubsystem, haptic: HapticSubsystem) -> Self {
+ ControllerManager {
+ ctrl,
+ haptic: Rc::new(haptic),
+ controllers: vec![],
+ }
+ }
+
+ pub fn handle_event(&mut self, event: &Event) {
+ match event {
+ Event::ControllerDeviceAdded { which: id, .. } => { self.add_device(*id) }
+ Event::ControllerDeviceRemoved { which: id, .. } => { self.remove_device(*id) }
+ Event::ControllerDeviceRemapped { which: id, .. } => { println!("device remapped ({})!", *id) }
+ Event::ControllerButtonDown { button: btn, .. } => { println!("button {} down!", btn.string()) }
+ Event::ControllerButtonUp { button: btn, .. } => { println!("button {} up!", btn.string()) }
+ Event::ControllerAxisMotion { axis: ax, .. } => { println!("axis motion {}!", ax.string()) }
+ _ => {}
+ }
+ }
+
+ pub fn add_device(&mut self, id: u32) {
+ println!("device added ({})!", id);
+ let mut ctrl = self.ctrl.open(id).unwrap();
+ println!("opened {}", ctrl.name());
+ let haptic = match ctrl.set_rumble(500, 1000, 500) {
+ Ok(_) => self.haptic.open_from_joystick_id(id).ok(),
+ Err(_) => None
+ };
+
+ let c = Rc::new(Controller {id, ctrl, haptic: haptic.map(|h| Rc::new(RefCell::new(h)))});
+ c.rumble(0.5, 300);
+ self.controllers.push(c);
+ }
+
+ pub fn remove_device(&mut self, id: i32) {
+ println!("device removed ({})!", id);
+ }
+}
+
+impl Controller {
+ /// strength [0 - 1]
+ pub fn rumble(&self, strength: f32, duration_ms: u32) {
+ if let Some(h) = &self.haptic {
+ h.borrow_mut().rumble_play(strength, duration_ms);
+ }
+ }
+}