main.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. extern crate sdl2;
  2. use sdl2::Sdl;
  3. use sdl2::video::{Window};
  4. use sdl2::image::LoadTexture;
  5. use sdl2::event::Event;
  6. use sdl2::keyboard::Keycode;
  7. use sdl2::rect::Rect;
  8. use std::path::Path;
  9. const WIDTH:u32 = 1280;
  10. const HEIGHT:u32 = 960;
  11. // it looks like returning the Canvas is still the best route to take
  12. fn init() -> (Sdl, Window) {
  13. let context = match sdl2::init() {
  14. Ok(context) => context,
  15. Err(err) => panic!("Could not initialize SDL2. Error: {}", err),
  16. };
  17. let video = match context.video() {
  18. Ok(video) => video,
  19. Err(err) => panic!("Could not gain access to the SDL2 video subsystem. Error: {}", err),
  20. };
  21. let window = match video.window("Drop Game", WIDTH, HEIGHT)
  22. .position_centered()
  23. .opengl()
  24. .build() {
  25. Ok(window) => window,
  26. Err(err) => panic!("Could not create window. Error: {}", err),
  27. };
  28. return (context, window)
  29. }
  30. fn main() {
  31. let mut running: bool = true;
  32. let mut blitx: i32 = 7;
  33. let mut blity: i32 = 5;
  34. let mut blitrect = Rect::new(blitx * 80, blity * 80, 80, 80);
  35. let (context, window) = init();
  36. let mut canvas = match window.into_canvas()
  37. .build() {
  38. Ok(canvas) => canvas,
  39. Err(err) => panic!("Could not create canvas from window. Error: {}", err),
  40. };
  41. let tc = canvas.texture_creator();
  42. let texture = match tc.load_texture(&Path::new("assets/blip1.png")) {
  43. Ok(texture) => texture,
  44. Err(err) => panic!("Could not load png: {}", err)
  45. };
  46. let mut pump = match context.event_pump() {
  47. Ok(pump) => pump,
  48. Err(err) => panic!("Could not start pumping: {}", err)
  49. };
  50. while running {
  51. // pull all pending events
  52. for event in pump.poll_iter() {
  53. match event {
  54. // apparently '{..}' means "with whatever fields"
  55. Event::Quit {..} => {
  56. running = false
  57. },
  58. Event::KeyDown { keycode: k, .. } => match k {
  59. Some(Keycode::Escape) | Some(Keycode::Q) => {
  60. running = false
  61. },
  62. Some(Keycode::Up) => {
  63. if blity >= 1 { blity -= 1; }
  64. },
  65. Some(Keycode::Down) => {
  66. if blity < 11 { blity += 1; }
  67. },
  68. Some(Keycode::Left) => {
  69. if blitx >= 1 { blitx -= 1; }
  70. },
  71. Some(Keycode::Right) => {
  72. if blitx < 15 { blitx += 1; }
  73. },
  74. Some(_) => { },
  75. None => {}
  76. },
  77. _ => {}
  78. }
  79. }
  80. blitrect.set_x(blitx * 80);
  81. blitrect.set_y(blity * 80);
  82. canvas.clear();
  83. match canvas.copy(&texture, None, blitrect) {
  84. Ok(()) => (), // no return value == success
  85. Err(err) => panic!("Could not render texture: {}", err),
  86. };
  87. canvas.present();
  88. }
  89. }