• src/main.rs
+16 -9src/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 5
use std::time::Duration;
5 6

  7
fn render(canvas: &mut WindowCanvas, color: Color) {
  8
    canvas.set_draw_color(color);
  9
    canvas.clear();
  10
    canvas.present();
  11
}
  12

6 13
fn main() -> Result<(), String> {
7 14
    let sdl_context = sdl2::init()?;
8 15
    let video_subsystem = sdl_context.video()?;
9 16

10  
    let window = video_subsystem.window("rust-sdl2 demo", 800, 600)
  17
    let window = video_subsystem.window("game tutorial", 800, 600)
11 18
        .position_centered()
12 19
        .build()
13 20
        .expect("could not initialize video subsystem");
14 21

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

18  
    canvas.set_draw_color(Color::RGB(0, 255, 255));
19  
    canvas.clear();
20  
    canvas.present();
21 25
    let mut event_pump = sdl_context.event_pump()?;
22 26
    let mut i = 0;
23 27
    'running: loop {
24  
        i = (i + 1) % 255;
25  
        canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
26  
        canvas.clear();
  28
        // Handle events
27 29
        for event in event_pump.poll_iter() {
28 30
            match event {
29 31
                Event::Quit {..} |
30 32
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
31 33
                    break 'running;
32 34
                },
33 35
                _ => {}
34 36
            }
35 37
        }
36  
        // The rest of the game loop goes here...
  38

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

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

38  
        canvas.present();
  45
        // Time management!
39 46
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
40 47
    }
41 48

42 49
    Ok(())
43 50
}
+16 -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;
4
use std::time::Duration;
5
use std::time::Duration;
5

6

    7
fn render(canvas: &mut WindowCanvas, color: Color) {
    8
    canvas.set_draw_color(color);
    9
    canvas.clear();
    10
    canvas.present();
    11
}
    12

6
fn main() -> Result<(), String> {
13
fn main() -> Result<(), String> {
7
    let sdl_context = sdl2::init()?;
14
    let sdl_context = sdl2::init()?;
8
    let video_subsystem = sdl_context.video()?;
15
    let video_subsystem = sdl_context.video()?;
9

16

10
    let window = video_subsystem.window("rust-sdl2 demo", 800, 600)
17
    let window = video_subsystem.window("game tutorial", 800, 600)
11
        .position_centered()
18
        .position_centered()
12
        .build()
19
        .build()
13
        .expect("could not initialize video subsystem");
20
        .expect("could not initialize video subsystem");
14

21

15
    let mut canvas = window.into_canvas().build()
22
    let mut canvas = window.into_canvas().build()
16
        .expect("could not make a canvas");
23
        .expect("could not make a canvas");
17

24

18
    canvas.set_draw_color(Color::RGB(0, 255, 255));
   
19
    canvas.clear();
   
20
    canvas.present();
   
21
    let mut event_pump = sdl_context.event_pump()?;
25
    let mut event_pump = sdl_context.event_pump()?;
22
    let mut i = 0;
26
    let mut i = 0;
23
    'running: loop {
27
    'running: loop {
24
        i = (i + 1) % 255;
28
        // Handle events
25
        canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
   
26
        canvas.clear();
   
27
        for event in event_pump.poll_iter() {
29
        for event in event_pump.poll_iter() {
28
            match event {
30
            match event {
29
                Event::Quit {..} |
31
                Event::Quit {..} |
30
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
32
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
31
                    break 'running;
33
                    break 'running;
32
                },
34
                },
33
                _ => {}
35
                _ => {}
34
            }
36
            }
35
        }
37
        }
36
        // The rest of the game loop goes here...
38

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

    42
        // Render
    43
        render(&mut canvas, Color::RGB(i, 64, 255 - i));
37

44

38
        canvas.present();
45
        // Time management!
39
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
46
        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
40
    }
47
    }
41

48

42
    Ok(())
49
    Ok(())
43
}
50
}
/target
**/*.rs.bk
[package]
name = "game-tutorial"
version = "0.1.0"
authors = ["Sunjay Varma <[email protected]>"]
edition = "2018"

[dependencies]
sdl2 = "0.32.1"
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::WindowCanvas;
use std::time::Duration;

fn render(canvas: &mut WindowCanvas, color: Color) {
    canvas.set_draw_color(color);
    canvas.clear();
    canvas.present();
}

fn main() -> Result<(), String> {
    let sdl_context = sdl2::init()?;
    let video_subsystem = sdl_context.video()?;

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

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

    Ok(())
}