123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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();
- }
- }
|