Skip to content

Commit

Permalink
lazy-foo: second tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Jul 16, 2020
1 parent cfdabe2 commit eee361e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lazy-foo/README.md
Expand Up @@ -18,6 +18,8 @@ I recommend to also refer to instructions in the [main readme](../README.md).
- [tutorial](https://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php)
- `cargo run --bin 02_image_on_screen`
- [rust code](./src/02_image_on_screen.rs)
- [read up on high-dpi-mode](https://nlguillemot.wordpress.com/2016/12/11
/high-dpi-rendering/) which is an option when creating the window


## Related Projects
Expand Down
Binary file added lazy-foo/assets/hello_rust_sdl.bmp
Binary file not shown.
74 changes: 73 additions & 1 deletion lazy-foo/src/02_image_on_screen.rs
@@ -1,3 +1,75 @@
use std::error::Error;
use std::thread::sleep;
use std::time::Duration;

use sdl2::render::{Texture, TextureCreator, WindowCanvas};
use sdl2::surface::Surface;
use sdl2::video::WindowContext;
use sdl2::Sdl;

const WIDTH: u32 = 640;
const HEIGHT: u32 = 480;

fn init() -> Result<(Sdl, WindowCanvas), Box<dyn Error>> {
// Initialize SDL
let sdl_context: Sdl = sdl2::init()?;
let video = sdl_context.video()?;

// Create Window
let window = video
.window("SDL Tutorial", WIDTH, HEIGHT)
.position_centered()
// .allow_highdpi() creates the window in high-DPI mode if available
.build()?;

// Create Canvas to render
let canvas = window
.into_canvas()
// .accelerated() if we want to leverage the GPU to render
// .present_vsync() if we want to synchronize renderer `present` with display refresh rate
// .software() to enforce Software rendering (opposite of accelerated and the default)
.build()?;

// window is still accessible via `canvas.window()` since it 'owns' it, so returning
// sdl_context and canvas is sufficient
Ok((sdl_context, canvas))
}

fn load_media(texture_creator: &TextureCreator<WindowContext>) -> Result<Texture, Box<dyn Error>> {
// Load Image
let surface = Surface::load_bmp("assets/hello_rust_sdl.bmp")?;
// Convert to Texture so we can render it
// Same as: let texture = surface.as_texture(texture_creator)?;
let texture = texture_creator.create_texture_from_surface(surface)?;
Ok(texture)
}

fn main() {
println!("Hello, world!");
let (sdl_context, mut canvas) = init().expect("FATAL: failed to initialize window and canvas.");
let texture_creator = canvas.texture_creator();
let img_hello_world = load_media(&texture_creator).expect("FATAL: failed to load image.");

canvas.clear();

// Providing None, None for src and dst as we want to fill the window with the
// entire image.
// If `src` is `None`, the entire texture is copied.
// If `dst` is `None`, the texture will be stretched to fill the given rectangle.
canvas
.copy(&img_hello_world, None, None)
.expect("FATAL: failed to draw to canvas.");

canvas.present();

// If we don't init the event pump and pump events at least once, nothing will
// happen and no window even show.
let mut event_pump = sdl_context
.event_pump()
.expect("FATAL: failed to init event_pump");

event_pump.pump_events();
sleep(Duration::new(2, 0));

// We do not need to implement a `close` method like the original tutorial
// does since Rust cleans up things for us.
}

0 comments on commit eee361e

Please sign in to comment.