• src/main.rs
+19 -3src/main.rs
1 1
use sdl2::pixels::Color;
2 2
use sdl2::event::Event;
3 3
use sdl2::keyboard::Keycode;
4 4
use sdl2::render::{WindowCanvas, Texture};
  5
use sdl2::rect::{Point, Rect};
5 6
// "self" imports the "image" module itself as well as everything else we listed
6 7
use sdl2::image::{self, LoadTexture, InitFlag};
7 8
use std::time::Duration;
8 9

9  
fn render(canvas: &mut WindowCanvas, color: Color, texture: &Texture) -> Result<(), String> {
  10
fn render(
  11
    canvas: &mut WindowCanvas,
  12
    color: Color,
  13
    texture: &Texture,
  14
    position: Point,
  15
    sprite: Rect,
  16
) -> Result<(), String> {
10 17
    canvas.set_draw_color(color);
11 18
    canvas.clear();
12 19

13  
    canvas.copy(texture, None, None)?;
  20
    let (width, height) = canvas.output_size()?;
  21

  22
    // Treat the center of the screen as the (0, 0) coordinate
  23
    let screen_position = position + Point::new(width as i32 / 2, height as i32 / 2);
  24
    let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
  25
    canvas.copy(texture, sprite, screen_rect)?;
14 26

15 27
    canvas.present();
16 28

17 29
    Ok(())
18 30
}
19 31

20 32
fn main() -> Result<(), String> {
21 33
    let sdl_context = sdl2::init()?;
22 34
    let video_subsystem = sdl_context.video()?;
23 35
    // Leading "_" tells Rust that this is an unused variable that we don't care about. It has to
24 36
    // stay unused because if we don't have any variable at all then Rust will treat it as a
25 37
    // temporary value and drop it right away!
26 38
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
27 39

28 40
    let window = video_subsystem.window("game tutorial", 800, 600)
29 41
        .position_centered()
30 42
        .build()
31 43
        .expect("could not initialize video subsystem");
32 44

33 45
    let mut canvas = window.into_canvas().build()
34 46
        .expect("could not make a canvas");
35 47

36 48
    let texture_creator = canvas.texture_creator();
37 49
    let texture = texture_creator.load_texture("assets/bardo.png")?;
38 50

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

39 55
    let mut event_pump = sdl_context.event_pump()?;
40 56
    let mut i = 0;
41 57
    'running: loop {
42 58
        // Handle events
43 59
        for event in event_pump.poll_iter() {
44 60
            match event {
45 61
                Event::Quit {..} |
46 62
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
47 63
                    break 'running;
48 64
                },
49 65
                _ => {}
50 66
            }
51 67
        }
52 68

53 69
        // Update
54 70
        i = (i + 1) % 255;
55 71

56 72
        // Render
57  
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture)?;
  73
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, position, sprite)?;
58 74

59 75
        // Time management!
60 76
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
61 77
    }
62 78

63 79
    Ok(())
64 80
}
+19 -3src/main.rs
1
use sdl2::pixels::Color;
1
use sdl2::pixels::Color;
2
use sdl2::event::Event;
2
use sdl2::event::Event;
3
use sdl2::keyboard::Keycode;
3
use sdl2::keyboard::Keycode;
4
use sdl2::render::{WindowCanvas, Texture};
4
use sdl2::render::{WindowCanvas, Texture};
    5
use sdl2::rect::{Point, Rect};
5
// "self" imports the "image" module itself as well as everything else we listed
6
// "self" imports the "image" module itself as well as everything else we listed
6
use sdl2::image::{self, LoadTexture, InitFlag};
7
use sdl2::image::{self, LoadTexture, InitFlag};
7
use std::time::Duration;
8
use std::time::Duration;
8

9

9
fn render(canvas: &mut WindowCanvas, color: Color, texture: &Texture) -> Result<(), String> {
10
fn render(
    11
    canvas: &mut WindowCanvas,
    12
    color: Color,
    13
    texture: &Texture,
    14
    position: Point,
    15
    sprite: Rect,
    16
) -> Result<(), String> {
10
    canvas.set_draw_color(color);
17
    canvas.set_draw_color(color);
11
    canvas.clear();
18
    canvas.clear();
12

19

13
    canvas.copy(texture, None, None)?;
20
    let (width, height) = canvas.output_size()?;
    21

    22
    // Treat the center of the screen as the (0, 0) coordinate
    23
    let screen_position = position + Point::new(width as i32 / 2, height as i32 / 2);
    24
    let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
    25
    canvas.copy(texture, sprite, screen_rect)?;
14

26

15
    canvas.present();
27
    canvas.present();
16

28

17
    Ok(())
29
    Ok(())
18
}
30
}
19

31

20
fn main() -> Result<(), String> {
32
fn main() -> Result<(), String> {
21
    let sdl_context = sdl2::init()?;
33
    let sdl_context = sdl2::init()?;
22
    let video_subsystem = sdl_context.video()?;
34
    let video_subsystem = sdl_context.video()?;
23
    // Leading "_" tells Rust that this is an unused variable that we don't care about. It has to
35
    // Leading "_" tells Rust that this is an unused variable that we don't care about. It has to
24
    // stay unused because if we don't have any variable at all then Rust will treat it as a
36
    // stay unused because if we don't have any variable at all then Rust will treat it as a
25
    // temporary value and drop it right away!
37
    // temporary value and drop it right away!
26
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
38
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
27

39

28
    let window = video_subsystem.window("game tutorial", 800, 600)
40
    let window = video_subsystem.window("game tutorial", 800, 600)
29
        .position_centered()
41
        .position_centered()
30
        .build()
42
        .build()
31
        .expect("could not initialize video subsystem");
43
        .expect("could not initialize video subsystem");
32

44

33
    let mut canvas = window.into_canvas().build()
45
    let mut canvas = window.into_canvas().build()
34
        .expect("could not make a canvas");
46
        .expect("could not make a canvas");
35

47

36
    let texture_creator = canvas.texture_creator();
48
    let texture_creator = canvas.texture_creator();
37
    let texture = texture_creator.load_texture("assets/bardo.png")?;
49
    let texture = texture_creator.load_texture("assets/bardo.png")?;
38

50

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

39
    let mut event_pump = sdl_context.event_pump()?;
55
    let mut event_pump = sdl_context.event_pump()?;
40
    let mut i = 0;
56
    let mut i = 0;
41
    'running: loop {
57
    'running: loop {
42
        // Handle events
58
        // Handle events
43
        for event in event_pump.poll_iter() {
59
        for event in event_pump.poll_iter() {
44
            match event {
60
            match event {
45
                Event::Quit {..} |
61
                Event::Quit {..} |
46
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
62
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
47
                    break 'running;
63
                    break 'running;
48
                },
64
                },
49
                _ => {}
65
                _ => {}
50
            }
66
            }
51
        }
67
        }
52

68

53
        // Update
69
        // Update
54
        i = (i + 1) % 255;
70
        i = (i + 1) % 255;
55

71

56
        // Render
72
        // Render
57
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture)?;
73
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, position, sprite)?;
58

74

59
        // Time management!
75
        // Time management!
60
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
76
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
61
    }
77
    }
62

78

63
    Ok(())
79
    Ok(())
64
}
80
}
/target
**/*.rs.bk
[package]
name = "game-tutorial"
version = "0.1.0"
authors = ["Sunjay Varma <[email protected]>"]
edition = "2018"

[dependencies]

[dependencies.sdl2]
version = "0.32.1"
default-features = false
features = ["image"]
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::rect::{Point, Rect};
// "self" imports the "image" module itself as well as everything else we listed
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> {
    canvas.set_draw_color(color);
    canvas.clear();

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

    // Treat the center of the screen as the (0, 0) coordinate
    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)?;

    canvas.present();

    Ok(())
}

fn main() -> Result<(), String> {
    let sdl_context = sdl2::init()?;
    let video_subsystem = sdl_context.video()?;
    // Leading "_" tells Rust that this is an unused variable that we don't care about. It has to
    // stay unused because if we don't have any variable at all then Rust will treat it as a
    // temporary value and drop it right away!
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;

    let window = video_subsystem.window("game tutorial", 800, 600)
        .position_centered()
        .build()
        .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(i, 64, 255 - i), &texture, position, sprite)?;

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

    Ok(())
}