Guest User

Untitled

a guest
Jun 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. extern crate piston;
  2. extern crate graphics;
  3. extern crate glutin_window;
  4. extern crate opengl_graphics;
  5. extern crate rand;
  6.  
  7. use piston::window::WindowSettings;
  8. use piston::event_loop::{ EventSettings, Events };
  9. use piston::input::{ RenderEvent, RenderArgs, MouseCursorEvent };
  10. use glutin_window::GlutinWindow;
  11. use opengl_graphics::{ GlGraphics, OpenGL };
  12. use graphics::{clear, rectangle};
  13.  
  14. const OPENGL_VERSION: OpenGL = OpenGL::V3_2;
  15.  
  16. pub struct App {
  17. gl: GlGraphics, // OpenGL drawing backend.
  18. cursor_pos: [f64; 2]
  19. }
  20.  
  21. impl App {
  22. fn new(size: [u32; 2]) -> App {
  23. App {
  24. gl: GlGraphics::new(OPENGL_VERSION),
  25. cursor_pos: [0.0, 0.0]
  26. }
  27. }
  28.  
  29. fn render(&mut self, args: &RenderArgs) {
  30. let context = self.gl.draw_begin(args.viewport());
  31. clear([1.0, 1.0, 1.0, 1.0], &mut self.gl);
  32. let [cx, cy] = self.cursor_pos;
  33. rectangle([1.0, 0.0, 0.0, 1.0], [cx - 5.0, cy - 5.0, 10.0, 10.0], context.transform, &mut self.gl);
  34. self.gl.draw_end();
  35. }
  36.  
  37. fn handle_mouse_move(&mut self, args: [f64; 2]) {
  38. self.cursor_pos = args;
  39. }
  40. }
  41.  
  42. fn main() {
  43.  
  44. let size = [1280, 720];
  45. // Create an Glutin window.
  46. let mut window: GlutinWindow = WindowSettings::new(
  47. "Mouse Only",
  48. size
  49. )
  50. .opengl(OPENGL_VERSION)
  51. .exit_on_esc(true)
  52. .vsync(true)
  53. .build()
  54. .unwrap();
  55.  
  56. // Create a new game and run it.
  57. let mut app = App::new(size);
  58. let mut events = Events::new(EventSettings::new());
  59.  
  60. while let Some(e) = events.next(&mut window) {
  61. if let Some(mouse_pos) = e.mouse_cursor_args() {
  62. app.handle_mouse_move(mouse_pos);
  63. }
  64.  
  65. if let Some(r) = e.render_args() {
  66. app.render(&r);
  67. }
  68. }
  69. }
Add Comment
Please, Sign In to add comment