main.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. extern crate sdl2;
  2. use sdl2::pixels::Color;
  3. use sdl2::video::Window;
  4. use sdl2::render::Canvas;
  5. use sdl2::surface::Surface;
  6. use std::time::Duration;
  7. use std::thread::sleep;
  8. use std::path::Path;
  9. // using a different window size than lazyfoo, due to high-density screen
  10. const WIDTH:u32 = 1280;
  11. const HEIGHT:u32 = 960;
  12. // This is all about figuring out what types go where, so that passing
  13. // arguments and return values works properly. We could probably go with
  14. // globals, like the C version, but if so, why use rust anyway?
  15. fn init() -> Canvas<Window> {
  16. let context = match sdl2::init() {
  17. Ok(context) => context,
  18. Err(err) => panic!("Could not initialize SDL2. Error: {}", err),
  19. };
  20. let video = match context.video() {
  21. Ok(video) => video,
  22. Err(err) => panic!("Could not gain access to the SDL2 video subsystem. Error: {}", err),
  23. };
  24. let window = match video.window("SDL Tutorial, lesson 01", WIDTH, HEIGHT)
  25. .position_centered()
  26. .opengl()
  27. .build() {
  28. Ok(window) => window,
  29. Err(err) => panic!("Could not create window. Error: {}", err),
  30. };
  31. let canvas = match window.into_canvas()
  32. .build() {
  33. Ok(canvas) => canvas,
  34. Err(err) => panic!("Could not create renderer from window. Error: {}", err),
  35. };
  36. return canvas
  37. }
  38. fn load_media(filename:&str) -> Surface {
  39. // having difficulty with lifetimes. Going to try to use a TextureCreator
  40. // to get around that problem for now.
  41. // screw lifetimes (for now). going to just load and return the
  42. // surface, to get around TextureCreator lifetime restrictions
  43. let surface = match Surface::load_bmp(&Path::new(filename)) {
  44. Ok(surface) => surface,
  45. Err(err) => panic!("Could not load image: {}", err)
  46. };
  47. return surface
  48. }
  49. fn main() {
  50. let mut canvas = init();
  51. let surface = load_media("../assets/hello_world.bmp");
  52. let tc = canvas.texture_creator();
  53. let texture = match tc.create_texture_from_surface(&surface) {
  54. Ok(texture) => texture,
  55. Err(err) => panic!("Could not slap surface into a texture: {}", err)
  56. };
  57. // These are fairly straightforward draw commands.
  58. // The present() is needed to make the changes visible.
  59. //
  60. // keeping these because, hey, why not?
  61. canvas.set_draw_color(Color::RGB(255,255,128));
  62. canvas.clear();
  63. canvas.present();
  64. // 'copy' takes:
  65. // - the texture
  66. // - None or Some(Rect) -- the subrect of the texture (or the whole thing)
  67. // - None or (not sure) -- if None, stretch to fill the rendering rectangle
  68. match canvas.copy(&texture, None, None) {
  69. Ok(()) => (), // no return value == success
  70. Err(err) => panic!("Could not render texture: {}", err),
  71. };
  72. // and now flip the screen buffers again
  73. canvas.present();
  74. sleep(Duration::new(2,0));
  75. // in the original tutorial, there is also a `close()` function which
  76. // frees up the surface and destroys the window
  77. // afaict, those are handled by scoping rules in rust, so are
  78. // unneeded
  79. }