Skip to content

Commit

Permalink
lazy-foo: 6th tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Jul 18, 2020
1 parent 43a859f commit a703417
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lazy-foo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ edition = "2018"

[dependencies]
[dependencies.sdl2]
features = ["ttf"]
version="0.34.2"
default-features = false
features = ["image", "ttf"]

[[bin]]
name = "02_image_on_screen"
Expand All @@ -20,6 +21,11 @@ path = "src/03_event_driven.rs"
[[bin]]
name = "04_key_presses"
path = "src/04_key_presses.rs"

[[bin]]
name = "05_optimized_loading_soft_stretching"
path = "src/05_optimized_loading_soft_stretching.rs"

[[bin]]
name = "06_loading_pngs"
path = "src/06_loading_pngs.rs"
24 changes: 24 additions & 0 deletions lazy-foo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ cargo run --bin 05_optimized_loading_soft_stretching
- [SDL_BlitScaled](https://wiki.libsdl.org/SDL_BlitScaled)
- [WindowSurfaceRef::update_window](https://rust-sdl2.github.io/rust-sdl2/sdl2/video/struct.WindowSurfaceRef.html#method.update_window)

#### 06 Loading PNGs with SDL_image

```sh
cargo run --bin 06_loading_pngs
```

Note that I got this warning initially:
> // libpng warning: iCCP: known incorrect sRGB profile
which was easily fixed via: `convert loaded.png fixed.png` and using the
_fixed_ png instead which also turned out to be half the size.

- [tutorials installation
instructions](http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index.php)
- not needed if you followed the [main readme](../README.md) instructions
- [tutorial](http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index2.php)
- [rust-sdl2 load_texture](https://rust-sdl2.github.io/rust-sdl2/sdl2/render
/struct.TextureCreator.html#method.load_texture)
- [rust-sdl2 image demo](https://github.com/Rust-SDL2/rust-sdl2/blob/master
/examples/image-demo.rs)
- [SDL2_Image docs](https://www.libsdl.org/projects/SDL_image/docs
/SDL_image_frame.html)
- [`SDL_Surface *IMG_Load(const char *file)`](https://www.libsdl.org/projects/SDL_image/docs/SDL_image_frame.html)

## Related Projects

- [rust-sdl2-lazyfoo](https://github.com/bombless/rust-sdl2-lazyfoo) from 2015 up to tutorial 18
Expand Down
Binary file added lazy-foo/assets/06_loading_pngs/fixed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions lazy-foo/src/06_loading_pngs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use lazy_foo::init_renderer;
use sdl2::event::Event;
use sdl2::image;
use sdl2::image::{LoadSurface, LoadTexture};
use sdl2::pixels::PixelFormat;
use sdl2::render::{Texture, TextureCreator};
use sdl2::surface::Surface;
use sdl2::video::WindowContext;
use std::convert::TryFrom;
use std::error::Error;

// The way we've been loading images and converting to textures all along.
// A bit more verbose than the `direct_texture_load` way, but allows converting
// surface to PixelFormat
fn load_image<'a>(
path: &str,
format: PixelFormat,
texture_creator: &'a TextureCreator<WindowContext>,
) -> Result<Texture<'a>, Box<dyn Error>> {
let image_surface: Surface = LoadSurface::from_file(path)?;
let image_surface = image_surface.convert(&format)?;

let texture = image_surface.as_texture(texture_creator)?;
Ok(texture)
}

// Shortcut to load a texture used in rust-sdl2 image demo example.
// Does not allow setting the PixelFormat.
#[allow(dead_code)]
fn direct_texture_load<'a>(
path: &str,
texture_creator: &'a TextureCreator<WindowContext>,
) -> Result<Texture<'a>, String> {
texture_creator.load_texture(path)
}

fn main() {
let (sdl_context, mut canvas) =
init_renderer().expect("FATAL: failed to initialize window and canvas.");

let init_flags = image::InitFlag::PNG;
let _image_ctx = image::init(init_flags).expect("FATAL: failed to initialize sdl2:image");

let texture_creator = canvas.texture_creator();

// Alternative to the below 7 lines is to use the shortcut to load a texture:
// let texture = direct_texture_load("assets/06_loading_pngs/loaded.png", &texture_creator)
let window_pixel_format = PixelFormat::try_from(canvas.window().window_pixel_format())
.expect("FATAL: failed to obtain window pixel format");
let texture = load_image(
"assets/06_loading_pngs/fixed.png",
window_pixel_format,
&texture_creator,
)
.expect("FATAL: failed to load png texture");

let mut event_pump = sdl_context
.event_pump()
.expect("FATAL: failed to init event_pump");
'running: loop {
if let Some(Event::Quit { .. }) = event_pump.poll_event() {
break 'running;
}

canvas.clear();
canvas
.copy(&texture, None, None)
.expect("FATAL: failed to draw to canvas.");
canvas.present();
}
}

0 comments on commit a703417

Please sign in to comment.