• src/main.rs
+15 -1src/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 10
#[derive(Debug)]
11 11
struct Player {
12 12
    position: Point,
13 13
    sprite: Rect,
  14
    speed: i32,
14 15
}
15 16

16 17
fn render(
17 18
    canvas: &mut WindowCanvas,
18 19
    color: Color,
19 20
    texture: &Texture,
20 21
    player: &Player,
21 22
) -> Result<(), String> {
22 23
    canvas.set_draw_color(color);
23 24
    canvas.clear();
24 25

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

27 28
    // Treat the center of the screen as the (0, 0) coordinate
28 29
    let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
29 30
    let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height());
30 31
    canvas.copy(texture, player.sprite, screen_rect)?;
31 32

32 33
    canvas.present();
33 34

34 35
    Ok(())
35 36
}
36 37

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

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

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

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

56  
    let player = Player {
  57
    let mut player = Player {
57 58
        position: Point::new(0, 0),
58 59
        sprite: Rect::new(0, 0, 26, 36),
  60
        speed: 5,
59 61
    };
60 62

61 63
    let mut event_pump = sdl_context.event_pump()?;
62 64
    let mut i = 0;
63 65
    'running: loop {
64 66
        // Handle events
65 67
        for event in event_pump.poll_iter() {
66 68
            match event {
67 69
                Event::Quit {..} |
68 70
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
69 71
                    break 'running;
70 72
                },
  73
                Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
  74
                    player.position = player.position.offset(-player.speed, 0);
  75
                },
  76
                Event::KeyDown { keycode: Some(Keycode::Right), .. } => {
  77
                    player.position = player.position.offset(player.speed, 0);
  78
                },
  79
                Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
  80
                    player.position = player.position.offset(0, -player.speed);
  81
                },
  82
                Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
  83
                    player.position = player.position.offset(0, player.speed);
  84
                },
71 85
                _ => {}
72 86
            }
73 87
        }
74 88

75 89
        // Update
76 90
        i = (i + 1) % 255;
77 91

78 92
        // Render
79 93
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
80 94

81 95
        // Time management!
82 96
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
83 97
    }
84 98

85 99
    Ok(())
86 100
}
+15 -1src/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)]
10
#[derive(Debug)]
11
struct Player {
11
struct Player {
12
    position: Point,
12
    position: Point,
13
    sprite: Rect,
13
    sprite: Rect,
    14
    speed: i32,
14
}
15
}
15

16

16
fn render(
17
fn render(
17
    canvas: &mut WindowCanvas,
18
    canvas: &mut WindowCanvas,
18
    color: Color,
19
    color: Color,
19
    texture: &Texture,
20
    texture: &Texture,
20
    player: &Player,
21
    player: &Player,
21
) -> Result<(), String> {
22
) -> Result<(), String> {
22
    canvas.set_draw_color(color);
23
    canvas.set_draw_color(color);
23
    canvas.clear();
24
    canvas.clear();
24

25

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

27

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

32

32
    canvas.present();
33
    canvas.present();
33

34

34
    Ok(())
35
    Ok(())
35
}
36
}
36

37

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

45

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

50

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

53

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

56

56
    let player = Player {
57
    let mut player = Player {
57
        position: Point::new(0, 0),
58
        position: Point::new(0, 0),
58
        sprite: Rect::new(0, 0, 26, 36),
59
        sprite: Rect::new(0, 0, 26, 36),
    60
        speed: 5,
59
    };
61
    };
60

62

61
    let mut event_pump = sdl_context.event_pump()?;
63
    let mut event_pump = sdl_context.event_pump()?;
62
    let mut i = 0;
64
    let mut i = 0;
63
    'running: loop {
65
    'running: loop {
64
        // Handle events
66
        // Handle events
65
        for event in event_pump.poll_iter() {
67
        for event in event_pump.poll_iter() {
66
            match event {
68
            match event {
67
                Event::Quit {..} |
69
                Event::Quit {..} |
68
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
70
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
69
                    break 'running;
71
                    break 'running;
70
                },
72
                },
    73
                Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
    74
                    player.position = player.position.offset(-player.speed, 0);
    75
                },
    76
                Event::KeyDown { keycode: Some(Keycode::Right), .. } => {
    77
                    player.position = player.position.offset(player.speed, 0);
    78
                },
    79
                Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
    80
                    player.position = player.position.offset(0, -player.speed);
    81
                },
    82
                Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
    83
                    player.position = player.position.offset(0, player.speed);
    84
                },
71
                _ => {}
85
                _ => {}
72
            }
86
            }
73
        }
87
        }
74

88

75
        // Update
89
        // Update
76
        i = (i + 1) % 255;
90
        i = (i + 1) % 255;
77

91

78
        // Render
92
        // Render
79
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
93
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
80

94

81
        // Time management!
95
        // Time management!
82
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
96
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
83
    }
97
    }
84

98

85
    Ok(())
99
    Ok(())
86
}
100
}
/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,
    speed: i32,
}

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 mut player = Player {
        position: Point::new(0, 0),
        sprite: Rect::new(0, 0, 26, 36),
        speed: 5,
    };

    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;
                },
                Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
                    player.position = player.position.offset(-player.speed, 0);
                },
                Event::KeyDown { keycode: Some(Keycode::Right), .. } => {
                    player.position = player.position.offset(player.speed, 0);
                },
                Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
                    player.position = player.position.offset(0, -player.speed);
                },
                Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
                    player.position = player.position.offset(0, player.speed);
                },
                _ => {}
            }
        }

        // 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(())
}