- File Changes
- .gitignore
- Cargo.toml
- assets/bardo.png
- assets/darkdimension.png
- assets/reaper.png
- src/main.rs
- assets
-
src
- main.rs
- .gitignore
- Cargo.toml
+15
-9
src/main.rs
1 | 1 |
|
2 | 2 |
|
3 | 3 |
|
4 | 4 |
|
5 | 5 |
|
6 | 6 |
|
7 | 7 |
|
8 | 8 |
|
9 | 9 |
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
10 | 16 |
|
11 | 17 |
|
12 | 18 |
|
13 | 19 |
|
14 |
|
|
15 |
|
|
20 |
|
|
16 | 21 |
|
17 | 22 |
|
18 | 23 |
|
19 | 24 |
|
20 | 25 |
|
21 | 26 |
|
22 | 27 |
|
23 |
|
|
24 |
|
|
25 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
26 | 31 |
|
27 | 32 |
|
28 | 33 |
|
29 | 34 |
|
30 | 35 |
|
31 | 36 |
|
32 | 37 |
|
33 | 38 |
|
34 | 39 |
|
35 | 40 |
|
36 | 41 |
|
37 | 42 |
|
38 | 43 |
|
39 | 44 |
|
40 | 45 |
|
41 | 46 |
|
42 | 47 |
|
43 | 48 |
|
44 | 49 |
|
45 | 50 |
|
46 | 51 |
|
47 | 52 |
|
48 | 53 |
|
49 | 54 |
|
50 | 55 |
|
51 |
|
|
52 |
|
|
53 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
54 | 60 |
|
55 | 61 |
|
56 | 62 |
|
57 | 63 |
|
58 | 64 |
|
59 | 65 |
|
60 | 66 |
|
61 | 67 |
|
62 | 68 |
|
63 | 69 |
|
64 | 70 |
|
65 | 71 |
|
66 | 72 |
|
67 | 73 |
|
68 | 74 |
|
69 | 75 |
|
70 | 76 |
|
71 | 77 |
|
72 | 78 |
|
73 |
|
|
79 |
|
|
74 | 80 |
|
75 | 81 |
|
76 | 82 |
|
77 | 83 |
|
78 | 84 |
|
79 | 85 |
|
80 | 86 |
|
+15
-9
src/main.rs
1 |
|
1 |
|
2 |
|
2 |
|
3 |
|
3 |
|
4 |
|
4 |
|
5 |
|
5 |
|
6 |
|
6 |
|
7 |
|
7 |
|
8 |
|
8 |
|
9 |
|
9 |
|
10 |
|
||
11 |
|
||
12 |
|
||
13 |
|
||
14 |
|
||
15 |
|
||
10 |
|
16 |
|
11 |
|
17 |
|
12 |
|
18 |
|
13 |
|
19 |
|
14 |
|
20 |
|
15 |
|
||
16 |
|
21 |
|
17 |
|
22 |
|
18 |
|
23 |
|
19 |
|
24 |
|
20 |
|
25 |
|
21 |
|
26 |
|
22 |
|
27 |
|
23 |
|
28 |
|
24 |
|
29 |
|
25 |
|
30 |
|
26 |
|
31 |
|
27 |
|
32 |
|
28 |
|
33 |
|
29 |
|
34 |
|
30 |
|
35 |
|
31 |
|
36 |
|
32 |
|
37 |
|
33 |
|
38 |
|
34 |
|
39 |
|
35 |
|
40 |
|
36 |
|
41 |
|
37 |
|
42 |
|
38 |
|
43 |
|
39 |
|
44 |
|
40 |
|
45 |
|
41 |
|
46 |
|
42 |
|
47 |
|
43 |
|
48 |
|
44 |
|
49 |
|
45 |
|
50 |
|
46 |
|
51 |
|
47 |
|
52 |
|
48 |
|
53 |
|
49 |
|
54 |
|
50 |
|
55 |
|
51 |
|
56 |
|
52 |
|
57 |
|
53 |
|
58 |
|
59 |
|
||
54 |
|
60 |
|
55 |
|
61 |
|
56 |
|
62 |
|
57 |
|
63 |
|
58 |
|
64 |
|
59 |
|
65 |
|
60 |
|
66 |
|
61 |
|
67 |
|
62 |
|
68 |
|
63 |
|
69 |
|
64 |
|
70 |
|
65 |
|
71 |
|
66 |
|
72 |
|
67 |
|
73 |
|
68 |
|
74 |
|
69 |
|
75 |
|
70 |
|
76 |
|
71 |
|
77 |
|
72 |
|
78 |
|
73 |
|
79 |
|
74 |
|
80 |
|
75 |
|
81 |
|
76 |
|
82 |
|
77 |
|
83 |
|
78 |
|
84 |
|
79 |
|
85 |
|
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(())
}