|
@@ -0,0 +1,116 @@
|
|
|
+extern crate sdl2;
|
|
|
+
|
|
|
+use sdl2::Sdl;
|
|
|
+use sdl2::video::Window;
|
|
|
+use sdl2::render::Canvas;
|
|
|
+use sdl2::surface::Surface;
|
|
|
+use sdl2::event::Event;
|
|
|
+
|
|
|
+use std::path::Path;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// using a different window size than lazyfoo, due to high-density screen
|
|
|
+const WIDTH:u32 = 1280;
|
|
|
+const HEIGHT:u32 = 960;
|
|
|
+
|
|
|
+
|
|
|
+// This is all about figuring out what types go where, so that passing
|
|
|
+// arguments and return values works properly. We could probably go with
|
|
|
+// globals, like the C version, but if so, why use rust anyway?
|
|
|
+fn init() -> (Sdl, Canvas<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("SDL Tutorial, lesson 03", WIDTH, HEIGHT)
|
|
|
+ .position_centered()
|
|
|
+ .opengl()
|
|
|
+ .build() {
|
|
|
+ Ok(window) => window,
|
|
|
+ Err(err) => panic!("Could not create window. Error: {}", err),
|
|
|
+ };
|
|
|
+
|
|
|
+ let canvas = match window.into_canvas()
|
|
|
+ .build() {
|
|
|
+ Ok(canvas) => canvas,
|
|
|
+ Err(err) => panic!("Could not create renderer from window. Error: {}", err),
|
|
|
+ };
|
|
|
+
|
|
|
+ return (context, canvas)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+fn load_media(filename:&str) -> Surface {
|
|
|
+ // having difficulty with lifetimes. Going to try to use a TextureCreator
|
|
|
+ // to get around that problem for now.
|
|
|
+
|
|
|
+ // screw lifetimes (for now). going to just load and return the
|
|
|
+ // surface, to get around TextureCreator lifetime restrictions
|
|
|
+ let surface = match Surface::load_bmp(&Path::new(filename)) {
|
|
|
+ Ok(surface) => surface,
|
|
|
+ Err(err) => panic!("Could not load image: {}", err)
|
|
|
+ };
|
|
|
+
|
|
|
+ return surface
|
|
|
+}
|
|
|
+
|
|
|
+fn main() {
|
|
|
+
|
|
|
+ let mut running: bool = true;
|
|
|
+
|
|
|
+ let (context, mut canvas) = init();
|
|
|
+
|
|
|
+ let surface = load_media("../assets/hello_world.bmp");
|
|
|
+
|
|
|
+ let mut pump = match context.event_pump() {
|
|
|
+ Ok(pump) => pump,
|
|
|
+ Err(err) => panic!("Could not start pumping: {}", err)
|
|
|
+ };
|
|
|
+
|
|
|
+ let tc = canvas.texture_creator();
|
|
|
+
|
|
|
+ let texture = match tc.create_texture_from_surface(&surface) {
|
|
|
+ Ok(texture) => texture,
|
|
|
+ Err(err) => panic!("Could not slap surface into a texture: {}", err)
|
|
|
+ };
|
|
|
+
|
|
|
+ while running {
|
|
|
+ // pull all pending events
|
|
|
+ for event in pump.poll_iter() {
|
|
|
+ match event {
|
|
|
+ // apparently '{..}' means "with whatever fields"
|
|
|
+ Event::Quit {..} => {
|
|
|
+ running = false
|
|
|
+ },
|
|
|
+ _ => {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // keeping these because, hey, why not?
|
|
|
+ // If nothing else, I can look for flicker comparison
|
|
|
+ // As it turns out, there's plenty of flicker, so disabling
|
|
|
+ //canvas.set_draw_color(Color::RGB(255,255,128));
|
|
|
+ //canvas.clear();
|
|
|
+ //canvas.present();
|
|
|
+
|
|
|
+
|
|
|
+ // Also tried this as a "draw once", rather than "draw every frame"
|
|
|
+ // -- no significant difference (on linux, anyway)
|
|
|
+ match canvas.copy(&texture, None, None) {
|
|
|
+ Ok(()) => (), // no return value == success
|
|
|
+ Err(err) => panic!("Could not render texture: {}", err),
|
|
|
+ };
|
|
|
+
|
|
|
+ canvas.present();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|