• src/main.rs
+15 -9src/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 5
use sdl2::rect::{Point, Rect};
6 6
// "self" imports the "image" module itself as well as everything else we listed
7 7
use sdl2::image::{self, LoadTexture, InitFlag};
8 8
use std::time::Duration;
9 9

  10
#[derive(Debug)]
  11
struct Player {
  12
    position: Point,
  13
    sprite: Rect,
  14
}
  15

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

20 25
    let (width, height) = canvas.output_size()?;
21 26

22 27
    // 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)?;
  28
    let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
  29
    let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
  30
    canvas.copy(texture, player.sprite, screen_rect)?;
26 31

27 32
    canvas.present();
28 33

29 34
    Ok(())
30 35
}
31 36

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

40 45
    let window = video_subsystem.window("game tutorial", 800, 600)
41 46
        .position_centered()
42 47
        .build()
43 48
        .expect("could not initialize video subsystem");
44 49

45 50
    let mut canvas = window.into_canvas().build()
46 51
        .expect("could not make a canvas");
47 52

48 53
    let texture_creator = canvas.texture_creator();
49 54
    let texture = texture_creator.load_texture("assets/bardo.png")?;
50 55

51  
    let position = Point::new(0, 0);
52  
    // src position in the spritesheet
53  
    let sprite = Rect::new(0, 0, 26, 36);
  56
    let player = Player {
  57
        position: Point::new(0, 0),
  58
        sprite: Rect::new(0, 0, 26, 36),
  59
    };
54 60

55 61
    let mut event_pump = sdl_context.event_pump()?;
56 62
    let mut i = 0;
57 63
    'running: loop {
58 64
        // Handle events
59 65
        for event in event_pump.poll_iter() {
60 66
            match event {
61 67
                Event::Quit {..} |
62 68
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
63 69
                    break 'running;
64 70
                },
65 71
                _ => {}
66 72
            }
67 73
        }
68 74

69 75
        // Update
70 76
        i = (i + 1) % 255;
71 77

72 78
        // Render
73  
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, position, sprite)?;
  79
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
74 80

75 81
        // Time management!
76 82
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
77 83
    }
78 84

79 85
    Ok(())
80 86
}
+15 -9src/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
use sdl2::rect::{Point, Rect};
6
// "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
7
use sdl2::image::{self, LoadTexture, InitFlag};
7
use sdl2::image::{self, LoadTexture, InitFlag};
8
use std::time::Duration;
8
use std::time::Duration;
9

9

    10
#[derive(Debug)]
    11
struct Player {
    12
    position: Point,
    13
    sprite: Rect,
    14
}
    15

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

24

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

26

22
    // Treat the center of the screen as the (0, 0) coordinate
27
    // 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);
28
    let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
24
    let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
29
    let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
25
    canvas.copy(texture, sprite, screen_rect)?;
30
    canvas.copy(texture, player.sprite, screen_rect)?;
26

31

27
    canvas.present();
32
    canvas.present();
28

33

29
    Ok(())
34
    Ok(())
30
}
35
}
31

36

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

44

40
    let window = video_subsystem.window("game tutorial", 800, 600)
45
    let window = video_subsystem.window("game tutorial", 800, 600)
41
        .position_centered()
46
        .position_centered()
42
        .build()
47
        .build()
43
        .expect("could not initialize video subsystem");
48
        .expect("could not initialize video subsystem");
44

49

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

52

48
    let texture_creator = canvas.texture_creator();
53
    let texture_creator = canvas.texture_creator();
49
    let texture = texture_creator.load_texture("assets/bardo.png")?;
54
    let texture = texture_creator.load_texture("assets/bardo.png")?;
50

55

51
    let position = Point::new(0, 0);
56
    let player = Player {
52
    // src position in the spritesheet
57
        position: Point::new(0, 0),
53
    let sprite = Rect::new(0, 0, 26, 36);
58
        sprite: Rect::new(0, 0, 26, 36),
    59
    };
54

60

55
    let mut event_pump = sdl_context.event_pump()?;
61
    let mut event_pump = sdl_context.event_pump()?;
56
    let mut i = 0;
62
    let mut i = 0;
57
    'running: loop {
63
    'running: loop {
58
        // Handle events
64
        // Handle events
59
        for event in event_pump.poll_iter() {
65
        for event in event_pump.poll_iter() {
60
            match event {
66
            match event {
61
                Event::Quit {..} |
67
                Event::Quit {..} |
62
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
68
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
63
                    break 'running;
69
                    break 'running;
64
                },
70
                },
65
                _ => {}
71
                _ => {}
66
            }
72
            }
67
        }
73
        }
68

74

69
        // Update
75
        // Update
70
        i = (i + 1) % 255;
76
        i = (i + 1) % 255;
71

77

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

80

75
        // Time management!
81
        // Time management!
76
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
82
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
77
    }
83
    }
78

84

79
    Ok(())
85
    Ok(())
80
}
86
}
/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;

#[derive(Debug)]
struct Player {
    position: Point,
    sprite: Rect,
}

fn render(
    canvas: &mut WindowCanvas,
    color: Color,
    texture: &Texture,
    player: &Player,
) -> 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 = player.position + Point::new(width as i32 / 2, height as i32 / 2);
    let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
    canvas.copy(texture, player.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 player = Player {
        position: Point::new(0, 0),
        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, &player)?;

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

    Ok(())
}