forked from Rust-SDL2/rust-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
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 Rust-SDL2#739 from martinlindhe/master
add an example using a custom mouse cursor
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,68 @@ | ||
extern crate sdl2; | ||
|
||
use std::env; | ||
use std::path::Path; | ||
use sdl2::event::Event; | ||
use sdl2::image::{LoadSurface, INIT_PNG, INIT_JPG}; | ||
use sdl2::keyboard::Keycode; | ||
use sdl2::mouse::Cursor; | ||
use sdl2::pixels::Color; | ||
use sdl2::rect::Rect; | ||
use sdl2::surface::Surface; | ||
|
||
pub fn run(png: &Path) { | ||
|
||
let sdl_context = sdl2::init().unwrap(); | ||
let video_subsystem = sdl_context.video().unwrap(); | ||
let _image_context = sdl2::image::init(INIT_PNG | INIT_JPG).unwrap(); | ||
let window = video_subsystem.window("rust-sdl2 demo: Cursor", 800, 600) | ||
.position_centered() | ||
.build() | ||
.unwrap(); | ||
|
||
let mut canvas = window.into_canvas().software().build().unwrap(); | ||
|
||
let surface = match Surface::from_file(png) { | ||
Ok(surface) => surface, | ||
Err(err) => panic!("failed to load cursor image: {}", err) | ||
}; | ||
let cursor = match Cursor::from_surface(surface, 0, 0) { | ||
Ok(cursor) => cursor, | ||
Err(err) => panic!("failed to load cursor: {}", err) | ||
}; | ||
cursor.set(); | ||
|
||
canvas.clear(); | ||
canvas.present(); | ||
|
||
canvas.set_draw_color(Color::RGBA(255, 255, 255, 255)); | ||
|
||
let mut events = sdl_context.event_pump().unwrap(); | ||
|
||
'mainloop: loop { | ||
for event in events.poll_iter() { | ||
match event { | ||
Event::Quit{..} | | ||
Event::KeyDown {keycode: Option::Some(Keycode::Escape), ..} => | ||
break 'mainloop, | ||
Event::MouseButtonDown {x, y, ..} => { | ||
canvas.fill_rect(Rect::new(x, y, 1, 1)).unwrap(); | ||
canvas.present(); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
fn main() { | ||
|
||
let args: Vec<_> = env::args().collect(); | ||
|
||
if args.len() < 2 { | ||
println!("Usage: cargo run /path/to/image.(png|jpg)") | ||
} else { | ||
run(Path::new(&args[1])); | ||
} | ||
} |