Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate piston;
- extern crate graphics;
- extern crate glutin_window;
- extern crate opengl_graphics;
- extern crate rand;
- use piston::window::WindowSettings;
- use piston::event_loop::{ EventSettings, Events };
- use piston::input::{ RenderEvent, RenderArgs, MouseCursorEvent };
- use glutin_window::GlutinWindow;
- use opengl_graphics::{ GlGraphics, OpenGL };
- use graphics::{clear, rectangle};
- const OPENGL_VERSION: OpenGL = OpenGL::V3_2;
- pub struct App {
- gl: GlGraphics, // OpenGL drawing backend.
- cursor_pos: [f64; 2]
- }
- impl App {
- fn new(size: [u32; 2]) -> App {
- App {
- gl: GlGraphics::new(OPENGL_VERSION),
- cursor_pos: [0.0, 0.0]
- }
- }
- fn render(&mut self, args: &RenderArgs) {
- let context = self.gl.draw_begin(args.viewport());
- clear([1.0, 1.0, 1.0, 1.0], &mut self.gl);
- let [cx, cy] = self.cursor_pos;
- rectangle([1.0, 0.0, 0.0, 1.0], [cx - 5.0, cy - 5.0, 10.0, 10.0], context.transform, &mut self.gl);
- self.gl.draw_end();
- }
- fn handle_mouse_move(&mut self, args: [f64; 2]) {
- self.cursor_pos = args;
- }
- }
- fn main() {
- let size = [1280, 720];
- // Create an Glutin window.
- let mut window: GlutinWindow = WindowSettings::new(
- "Mouse Only",
- size
- )
- .opengl(OPENGL_VERSION)
- .exit_on_esc(true)
- .vsync(true)
- .build()
- .unwrap();
- // Create a new game and run it.
- let mut app = App::new(size);
- let mut events = Events::new(EventSettings::new());
- while let Some(e) = events.next(&mut window) {
- if let Some(mouse_pos) = e.mouse_cursor_args() {
- app.handle_mouse_move(mouse_pos);
- }
- if let Some(r) = e.render_args() {
- app.render(&r);
- }
- }
- }
Add Comment
Please, Sign In to add comment