main.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 * 80;
  33. let mut blity: i32 = 5 * 80;
  34. let mut direction = "";
  35. let stepsize = 8;
  36. let ticksize = 20;
  37. let mut next_tick: u32;
  38. let mut blitrect = Rect::new(blitx * 80, blity * 80, 80, 80);
  39. // grab the context and window, then set the window up for drawing
  40. let (context, window) = init();
  41. let mut canvas = match window.into_canvas()
  42. .build() {
  43. Ok(canvas) => canvas,
  44. Err(err) => panic!("Could not create canvas from window. Error: {}", err),
  45. };
  46. // load the png into a texture
  47. let tc = canvas.texture_creator();
  48. let texture = match tc.load_texture(&Path::new("assets/blip1.png")) {
  49. Ok(texture) => texture,
  50. Err(err) => panic!("Could not load png: {}", err)
  51. };
  52. // initialize timer subsystem
  53. let mut timer = match context.timer() {
  54. Ok(timer) => timer,
  55. Err(err) => panic!("Could not start the timer: {}", err)
  56. };
  57. // prime the event pump
  58. let mut pump = match context.event_pump() {
  59. Ok(pump) => pump,
  60. Err(err) => panic!("Could not start pumping: {}", err)
  61. };
  62. while running {
  63. // determine next frame time
  64. next_tick = timer.ticks() + ticksize;
  65. // pull all pending events
  66. for event in pump.poll_iter() {
  67. match event {
  68. // apparently '{..}' means "with whatever fields"
  69. Event::Quit {..} => {
  70. running = false
  71. },
  72. Event::KeyDown { keycode: k, .. } => match k {
  73. Some(Keycode::Escape) | Some(Keycode::Q) => {
  74. running = false
  75. },
  76. Some(Keycode::Up) => {
  77. if (blitx % 80) == 0 {
  78. direction = "up"
  79. }
  80. },
  81. Some(Keycode::Down) => {
  82. if (blitx % 80) == 0 {
  83. direction = "down"
  84. }
  85. },
  86. Some(Keycode::Left) => {
  87. if (blity % 80) == 0 {
  88. direction = "left"
  89. }
  90. },
  91. Some(Keycode::Right) => {
  92. if (blity % 80) == 0 {
  93. direction = "right"
  94. }
  95. },
  96. Some(_) => { },
  97. None => {}
  98. },
  99. _ => {}
  100. }
  101. }
  102. match direction {
  103. "up" => {
  104. if blity <= 0 {
  105. blity = 0;
  106. direction = "";
  107. } else {
  108. blity -= stepsize;
  109. if (blity % 80) == 0 {
  110. direction = ""
  111. }
  112. }
  113. },
  114. "down" => {
  115. if blity >= (HEIGHT - 80) as i32 {
  116. blity = (HEIGHT - 80) as i32;
  117. direction = "";
  118. } else {
  119. blity += stepsize;
  120. if (blity % 80) == 0 {
  121. direction = ""
  122. }
  123. }
  124. },
  125. "left" => {
  126. if blitx <= 0 {
  127. blitx = 0;
  128. direction = "";
  129. } else {
  130. blitx -= stepsize;
  131. if (blitx % 80) == 0 {
  132. direction = ""
  133. }
  134. }
  135. },
  136. "right" => {
  137. if blitx >= (WIDTH - 80) as i32 {
  138. blitx = (WIDTH - 80) as i32;
  139. direction = "";
  140. } else {
  141. blitx += stepsize;
  142. if (blitx % 80) == 0 {
  143. direction = ""
  144. }
  145. }
  146. },
  147. _ => { }
  148. }
  149. blitrect.set_x(blitx);
  150. blitrect.set_y(blity);
  151. canvas.clear();
  152. match canvas.copy(&texture, None, blitrect) {
  153. Ok(()) => (), // no return value == success
  154. Err(err) => panic!("Could not render texture: {}", err),
  155. };
  156. // wait til time to flip the frame
  157. let now = timer.ticks();
  158. if now < next_tick {
  159. timer.delay( next_tick - now )
  160. }
  161. canvas.present();
  162. }
  163. }