• assets/reaper.png
  • assets/bardo.png
  • assets/darkdimension.png
  • Cargo.toml
  • src/main.rs
+5 -1Cargo.toml
1 1
[package]
2 2
name = "game-tutorial"
3 3
version = "0.1.0"
4 4
authors = ["Sunjay Varma <[email protected]>"]
5 5
edition = "2018"
6 6

7 7
[dependencies]
8  
sdl2 = "0.32.1"
  8

  9
[dependencies.sdl2]
  10
version = "0.32.1"
  11
default-features = false
  12
features = ["image"]
+17 -3src/main.rs
1 1
use sdl2::pixels::Color;
2 2
use sdl2::event::Event;
3 3
use sdl2::keyboard::Keycode;
4  
use sdl2::render::WindowCanvas;
  4
use sdl2::render::{WindowCanvas, Texture};
  5
// "self" imports the "image" module itself as well as everything else we listed
  6
use sdl2::image::{self, LoadTexture, InitFlag};
5 7
use std::time::Duration;
6 8

7  
fn render(canvas: &mut WindowCanvas, color: Color) {
  9
fn render(canvas: &mut WindowCanvas, color: Color, texture: &Texture) -> Result<(), String> {
8 10
    canvas.set_draw_color(color);
9 11
    canvas.clear();
  12

  13
    canvas.copy(texture, None, None)?;
  14

10 15
    canvas.present();
  16

  17
    Ok(())
11 18
}
12 19

13 20
fn main() -> Result<(), String> {
14 21
    let sdl_context = sdl2::init()?;
15 22
    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
  24
    // 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!
  26
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
16 27

17 28
    let window = video_subsystem.window("game tutorial", 800, 600)
18 29
        .position_centered()
19 30
        .build()
20 31
        .expect("could not initialize video subsystem");
21 32

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

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

25 39
    let mut event_pump = sdl_context.event_pump()?;
26 40
    let mut i = 0;
27 41
    'running: loop {
28 42
        // Handle events
29 43
        for event in event_pump.poll_iter() {
30 44
            match event {
31 45
                Event::Quit {..} |
32 46
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
33 47
                    break 'running;
34 48
                },
35 49
                _ => {}
36 50
            }
37 51
        }
38 52

39 53
        // Update
40 54
        i = (i + 1) % 255;
41 55

42 56
        // Render
43  
        render(&mut canvas, Color::RGB(i, 64, 255 - i));
  57
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture)?;
44 58

45 59
        // Time management!
46 60
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
47 61
    }
48 62

49 63
    Ok(())
50 64
}
+5 -1Cargo.toml
1
[package]
1
[package]
2
name = "game-tutorial"
2
name = "game-tutorial"
3
version = "0.1.0"
3
version = "0.1.0"
4
authors = ["Sunjay Varma <[email protected]>"]
4
authors = ["Sunjay Varma <[email protected]>"]
5
edition = "2018"
5
edition = "2018"
6

6

7
[dependencies]
7
[dependencies]
8
sdl2 = "0.32.1"
8

    9
[dependencies.sdl2]
    10
version = "0.32.1"
    11
default-features = false
    12
features = ["image"]
+17 -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;
4
use sdl2::render::{WindowCanvas, Texture};
    5
// "self" imports the "image" module itself as well as everything else we listed
    6
use sdl2::image::{self, LoadTexture, InitFlag};
5
use std::time::Duration;
7
use std::time::Duration;
6

8

7
fn render(canvas: &mut WindowCanvas, color: Color) {
9
fn render(canvas: &mut WindowCanvas, color: Color, texture: &Texture) -> Result<(), String> {
8
    canvas.set_draw_color(color);
10
    canvas.set_draw_color(color);
9
    canvas.clear();
11
    canvas.clear();
    12

    13
    canvas.copy(texture, None, None)?;
    14

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

    17
    Ok(())
11
}
18
}
12

19

13
fn main() -> Result<(), String> {
20
fn main() -> Result<(), String> {
14
    let sdl_context = sdl2::init()?;
21
    let sdl_context = sdl2::init()?;
15
    let video_subsystem = sdl_context.video()?;
22
    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
    24
    // 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!
    26
    let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;
16

27

17
    let window = video_subsystem.window("game tutorial", 800, 600)
28
    let window = video_subsystem.window("game tutorial", 800, 600)
18
        .position_centered()
29
        .position_centered()
19
        .build()
30
        .build()
20
        .expect("could not initialize video subsystem");
31
        .expect("could not initialize video subsystem");
21

32

22
    let mut canvas = window.into_canvas().build()
33
    let mut canvas = window.into_canvas().build()
23
        .expect("could not make a canvas");
34
        .expect("could not make a canvas");
24

35

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

25
    let mut event_pump = sdl_context.event_pump()?;
39
    let mut event_pump = sdl_context.event_pump()?;
26
    let mut i = 0;
40
    let mut i = 0;
27
    'running: loop {
41
    'running: loop {
28
        // Handle events
42
        // Handle events
29
        for event in event_pump.poll_iter() {
43
        for event in event_pump.poll_iter() {
30
            match event {
44
            match event {
31
                Event::Quit {..} |
45
                Event::Quit {..} |
32
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
46
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
33
                    break 'running;
47
                    break 'running;
34
                },
48
                },
35
                _ => {}
49
                _ => {}
36
            }
50
            }
37
        }
51
        }
38

52

39
        // Update
53
        // Update
40
        i = (i + 1) % 255;
54
        i = (i + 1) % 255;
41

55

42
        // Render
56
        // Render
43
        render(&mut canvas, Color::RGB(i, 64, 255 - i));
57
        render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture)?;
44

58

45
        // Time management!
59
        // Time management!
46
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
60
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
47
    }
61
    }
48

62

49
    Ok(())
63
    Ok(())
50
}
64
}
/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};
// "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) -> Result<(), String> {
    canvas.set_draw_color(color);
    canvas.clear();

    canvas.copy(texture, None, None)?;

    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 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)?;

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

    Ok(())
}