extern crate sdl2; use sdl2::Sdl; use sdl2::video::{Window}; use sdl2::image::LoadTexture; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::rect::Rect; use std::path::Path; const WIDTH:u32 = 1280; const HEIGHT:u32 = 960; // it looks like returning the Canvas is still the best route to take fn init() -> (Sdl, Window) { let context = match sdl2::init() { Ok(context) => context, Err(err) => panic!("Could not initialize SDL2. Error: {}", err), }; let video = match context.video() { Ok(video) => video, Err(err) => panic!("Could not gain access to the SDL2 video subsystem. Error: {}", err), }; let window = match video.window("Drop Game", WIDTH, HEIGHT) .position_centered() .opengl() .build() { Ok(window) => window, Err(err) => panic!("Could not create window. Error: {}", err), }; return (context, window) } fn main() { let mut running: bool = true; let mut blitx: i32 = 7; let mut blity: i32 = 5; let mut blitrect = Rect::new(blitx * 80, blity * 80, 80, 80); let (context, window) = init(); let mut canvas = match window.into_canvas() .build() { Ok(canvas) => canvas, Err(err) => panic!("Could not create canvas from window. Error: {}", err), }; let tc = canvas.texture_creator(); let texture = match tc.load_texture(&Path::new("assets/blip1.png")) { Ok(texture) => texture, Err(err) => panic!("Could not load png: {}", err) }; let mut pump = match context.event_pump() { Ok(pump) => pump, Err(err) => panic!("Could not start pumping: {}", err) }; while running { // pull all pending events for event in pump.poll_iter() { match event { // apparently '{..}' means "with whatever fields" Event::Quit {..} => { running = false }, Event::KeyDown { keycode: k, .. } => match k { Some(Keycode::Escape) | Some(Keycode::Q) => { running = false }, Some(Keycode::Up) => { if blity >= 1 { blity -= 1; } }, Some(Keycode::Down) => { if blity < 11 { blity += 1; } }, Some(Keycode::Left) => { if blitx >= 1 { blitx -= 1; } }, Some(Keycode::Right) => { if blitx < 15 { blitx += 1; } }, Some(_) => { }, None => {} }, _ => {} } } blitrect.set_x(blitx * 80); blitrect.set_y(blity * 80); canvas.clear(); match canvas.copy(&texture, None, blitrect) { Ok(()) => (), // no return value == success Err(err) => panic!("Could not render texture: {}", err), }; canvas.present(); } }