Rust and Sdl2 crate, can't see sprite on window

I'm following a tutorial to develope a game while learning rust. The problem is that can't see the sprite on the window, here is the code.

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::rect::{Point, Rect};
use sdl2::image::{self, LoadTexture, InitFlag};
use std::time::Duration;

fn render(
    canvas: &mut WindowCanvas,

    color: Color,
    texture: &Texture,
    position: Point,
    sprite: Rect,
) -> Result<(), String> {

    let (width, height) = canvas.output_size()?;

    let screen_position = position + Point::new(width as i32 / 2, height as i32 / 2);
    let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
    canvas.copy(texture, sprite, screen_rect)?;



fn main() -> Result<(), String> {
    let sdl_context = sdl2::init()?;
    let video_subsystem =;
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;

    let window = video_subsystem.window("game tutorial", 800, 600)
        .expect("could not initialize video subsystem");

    let mut canvas = window.into_canvas().build()
        .expect("could not make a canvas");

    let texture_creator = canvas.texture_creator();
    let texture = texture_creator.load_texture("../assets/bardo.png")?;

    let position = Point::new(0, 0);
    // src position in the spritesheet
    let sprite = Rect::new(0, 0, 26, 36);

    let mut event_pump = sdl_context.event_pump()?;
    let mut i = 0;
    'running: loop {
        // Handle events
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                    break 'running;
                _ => {}

        // Update
        i = (i + 1) % 255;

        // Render
        render(&mut canvas, Color::RGB(250, 64, 255), &texture, position, sprite)?;

        // Time management!
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));


I was able to see the sprite when I ran your code. The only catch is that you have to run cargo r from within the src directory so that load_texture can find ../assets as a relative path.

Do you get an error message?


Thanks for the answer.
I don't get any errors.
I get the window changing colors
yes, I'm runing cargo in the src folder

I wonder if you're somehow running different code than you posted above. The code above does not change the window color. i is updated in your loop, but the value is not used to color the canvas. The tutorial uses it like so:

render(&mut canvas, Color::RGB(250, 64, 255 - i), &texture, position, sprite)?;

You might also double check your copy of the bardo.png spritesheet (open in image editor) to make sure you have the correct image in your project.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.