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 * 80; let mut blity: i32 = 5 * 80; let mut direction = ""; let stepsize = 8; let ticksize = 20; let mut next_tick: u32; let mut blitrect = Rect::new(blitx * 80, blity * 80, 80, 80); // grab the context and window, then set the window up for drawing 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), }; // load the png into a texture 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) }; // initialize timer subsystem let mut timer = match context.timer() { Ok(timer) => timer, Err(err) => panic!("Could not start the timer: {}", err) }; // prime the event pump let mut pump = match context.event_pump() { Ok(pump) => pump, Err(err) => panic!("Could not start pumping: {}", err) }; while running { // determine next frame time next_tick = timer.ticks() + ticksize; // 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 (blitx % 80) == 0 { direction = "up" } }, Some(Keycode::Down) => { if (blitx % 80) == 0 { direction = "down" } }, Some(Keycode::Left) => { if (blity % 80) == 0 { direction = "left" } }, Some(Keycode::Right) => { if (blity % 80) == 0 { direction = "right" } }, Some(_) => { }, None => {} }, _ => {} } } match direction { "up" => { if blity <= 0 { blity = 0; direction = ""; } else { blity -= stepsize; if (blity % 80) == 0 { direction = "" } } }, "down" => { if blity >= (HEIGHT - 80) as i32 { blity = (HEIGHT - 80) as i32; direction = ""; } else { blity += stepsize; if (blity % 80) == 0 { direction = "" } } }, "left" => { if blitx <= 0 { blitx = 0; direction = ""; } else { blitx -= stepsize; if (blitx % 80) == 0 { direction = "" } } }, "right" => { if blitx >= (WIDTH - 80) as i32 { blitx = (WIDTH - 80) as i32; direction = ""; } else { blitx += stepsize; if (blitx % 80) == 0 { direction = "" } } }, _ => { } } blitrect.set_x(blitx); blitrect.set_y(blity); canvas.clear(); match canvas.copy(&texture, None, blitrect) { Ok(()) => (), // no return value == success Err(err) => panic!("Could not render texture: {}", err), }; // wait til time to flip the frame let now = timer.ticks(); if now < next_tick { timer.delay( next_tick - now ) } canvas.present(); } }