-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from Luke-Nukem/master
For review: implementing `mouse_state()`
- Loading branch information
Showing
8 changed files
with
502 additions
and
95 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
extern crate sdl2; | ||
use std::path::Path; | ||
|
||
use sdl2::event::Event; | ||
use sdl2::keyboard::Keycode; | ||
use sdl2::rect::Rect; | ||
use sdl2::rect::Point; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
let sdl_context = sdl2::init().unwrap(); | ||
let video_subsystem = sdl_context.video().unwrap(); | ||
|
||
let window = video_subsystem.window("SDL2", 640, 480) | ||
.position_centered().build().unwrap(); | ||
|
||
let mut renderer = window.renderer() | ||
.accelerated().build().unwrap(); | ||
|
||
renderer.set_draw_color(sdl2::pixels::Color::RGBA(0,0,0,255)); | ||
|
||
let mut timer = sdl_context.timer().unwrap(); | ||
|
||
let mut event_pump = sdl_context.event_pump().unwrap(); | ||
|
||
let temp_surface = sdl2::surface::Surface::load_bmp(Path::new("assets/animate.bmp")).unwrap(); | ||
let texture = renderer.create_texture_from_surface(&temp_surface).unwrap(); | ||
let texture_query = texture.query(); | ||
|
||
let mut center = Point::new(320,240); | ||
let mut source_rect = Rect::new(0, 0, 128, 82); | ||
let mut dest_rect = Rect::new(0,0, 128, 82); | ||
dest_rect.center_on(center); | ||
|
||
let mut running = true; | ||
while running { | ||
for event in event_pump.poll_iter() { | ||
match event { | ||
Event::Quit {..} | Event::KeyDown {keycode: Some(Keycode::Escape), ..} => { | ||
running = false; | ||
}, | ||
_ => {} | ||
} | ||
} | ||
|
||
let ticks = timer.ticks(); | ||
|
||
source_rect.set_x((128 * ((ticks / 100) % 6) ) as i32); | ||
renderer.clear(); | ||
renderer.copy_ex(&texture, Some(source_rect), Some(dest_rect), 10.0, None, true, false); | ||
renderer.present(); | ||
|
||
std::thread::sleep(Duration::from_millis(100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
extern crate sdl2; | ||
|
||
use sdl2::event::Event; | ||
use sdl2::mouse::MouseButton; | ||
use std::collections::HashSet; | ||
use std::time::Duration; | ||
|
||
pub fn main() { | ||
let sdl_context = sdl2::init().unwrap(); | ||
let video_subsystem = sdl_context.video().unwrap(); | ||
|
||
let _window = video_subsystem.window("Mouse", 800, 600) | ||
.position_centered() | ||
.build() | ||
.unwrap(); | ||
|
||
let mut events = sdl_context.event_pump().unwrap(); | ||
|
||
let mut prev_buttons = HashSet::new(); | ||
|
||
'running: loop { | ||
for event in events.poll_iter() { | ||
match event { | ||
Event::Quit {..} => break 'running, | ||
_ => () | ||
} | ||
} | ||
|
||
// get a mouse state | ||
let state = events.mouse_state(); | ||
|
||
// Create a set of pressed Keys. | ||
let buttons = state.pressed_mouse_buttons().collect(); | ||
|
||
// Get the difference between the new and old sets. | ||
let new_buttons = &buttons - &prev_buttons; | ||
let old_buttons = &prev_buttons - &buttons; | ||
|
||
if !new_buttons.is_empty() || !old_buttons.is_empty() { | ||
println!("X = {:?}, Y = {:?} : {:?} -> {:?}", state.x(), state.y(), new_buttons, old_buttons); | ||
} | ||
|
||
prev_buttons = buttons; | ||
|
||
std::thread::sleep(Duration::from_millis(100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
extern crate sdl2; | ||
|
||
use sdl2::event::Event; | ||
use sdl2::mouse::MouseButton; | ||
use std::time::Duration; | ||
|
||
pub fn main() { | ||
let sdl_context = sdl2::init().unwrap(); | ||
let video_subsystem = sdl_context.video().unwrap(); | ||
|
||
let _window = video_subsystem.window("Mouse", 800, 600) | ||
.position_centered() | ||
.build() | ||
.unwrap(); | ||
|
||
let mut events = sdl_context.event_pump().unwrap(); | ||
|
||
let mut state = events.relative_mouse_state(); | ||
let mut old_x = 0; | ||
let mut old_y = 0; | ||
|
||
'running: loop { | ||
for event in events.poll_iter() { | ||
match event { | ||
Event::Quit {..} => break 'running, | ||
_ => () | ||
} | ||
} | ||
|
||
// get a mouse state using mouse_state() so as not to call | ||
// relative_mouse_state() twice and get a false position reading | ||
if events.mouse_state().is_mouse_button_pressed(MouseButton::Left) { | ||
state = events.relative_mouse_state(); | ||
println!("Relative - X = {:?}, Y = {:?}", state.x(), state.y()); | ||
} | ||
|
||
std::thread::sleep(Duration::from_millis(100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.