Bastien Sevajol 5 years ago
commit
8c81a33739
5 changed files with 138 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 55 0
      Cargo.lock
  3. 8 0
      Cargo.toml
  4. BIN
      arial10x10.png
  5. 73 0
      src/main.rs

+ 2 - 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/target
2
+**/*.rs.bk

+ 55 - 0
Cargo.lock View File

@@ -0,0 +1,55 @@
1
+# This file is automatically @generated by Cargo.
2
+# It is not intended for manual editing.
3
+[[package]]
4
+name = "bitflags"
5
+version = "0.1.1"
6
+source = "registry+https://github.com/rust-lang/crates.io-index"
7
+
8
+[[package]]
9
+name = "cc"
10
+version = "1.0.47"
11
+source = "registry+https://github.com/rust-lang/crates.io-index"
12
+
13
+[[package]]
14
+name = "lazy_static"
15
+version = "0.1.16"
16
+source = "registry+https://github.com/rust-lang/crates.io-index"
17
+
18
+[[package]]
19
+name = "pkg-config"
20
+version = "0.3.17"
21
+source = "registry+https://github.com/rust-lang/crates.io-index"
22
+
23
+[[package]]
24
+name = "rust_rogue"
25
+version = "0.1.0"
26
+dependencies = [
27
+ "tcod 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
28
+]
29
+
30
+[[package]]
31
+name = "tcod"
32
+version = "0.15.0"
33
+source = "registry+https://github.com/rust-lang/crates.io-index"
34
+dependencies = [
35
+ "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
36
+ "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
37
+ "tcod-sys 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
38
+]
39
+
40
+[[package]]
41
+name = "tcod-sys"
42
+version = "5.0.1"
43
+source = "registry+https://github.com/rust-lang/crates.io-index"
44
+dependencies = [
45
+ "cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)",
46
+ "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
47
+]
48
+
49
+[metadata]
50
+"checksum bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2a6577517ecd0ee0934f48a7295a89aaef3e6dfafeac404f94c0b3448518ddfe"
51
+"checksum cc 1.0.47 (registry+https://github.com/rust-lang/crates.io-index)" = "aa87058dce70a3ff5621797f1506cb837edd02ac4c0ae642b4542dce802908b8"
52
+"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
53
+"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
54
+"checksum tcod 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44e518b0661949712e8dbc6d6d9d7c00a405dd88bc539102c1edfc2d22e5e144"
55
+"checksum tcod-sys 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "730fb27d2261f7c860ae7a61c3ae70277f0836173ceeccb594193abb4fa5f4aa"

+ 8 - 0
Cargo.toml View File

@@ -0,0 +1,8 @@
1
+[package]
2
+name = "rust_rogue"
3
+version = "0.1.0"
4
+authors = ["Bastien Sevajol <bastien.sevajol@algoo.fr>"]
5
+edition = "2018"
6
+
7
+[dependencies]
8
+tcod = "0.15"

BIN
arial10x10.png View File


+ 73 - 0
src/main.rs View File

@@ -0,0 +1,73 @@
1
+// This file is generated automatically. Do not edit it directly.
2
+// See the Contributing section in README on how to make changes to it.
3
+use tcod::colors::*;
4
+use tcod::console::*;
5
+
6
+// actual size of the window
7
+const SCREEN_WIDTH: i32 = 80;
8
+const SCREEN_HEIGHT: i32 = 50;
9
+
10
+const LIMIT_FPS: i32 = 20; // 20 frames-per-second maximum
11
+
12
+struct Tcod {
13
+    root: Root,
14
+}
15
+
16
+fn handle_keys(tcod: &mut Tcod, player_x: &mut i32, player_y: &mut i32) -> bool {
17
+    use tcod::input::Key;
18
+    use tcod::input::KeyCode::*;
19
+
20
+    let key = tcod.root.wait_for_keypress(true);
21
+    match key {
22
+        Key {
23
+            code: Enter,
24
+            alt: true,
25
+            ..
26
+        } => {
27
+            // Alt+Enter: toggle fullscreen
28
+            let fullscreen = tcod.root.is_fullscreen();
29
+            tcod.root.set_fullscreen(!fullscreen);
30
+        }
31
+        Key { code: Escape, .. } => return true, // exit game
32
+
33
+        // movement keys
34
+        Key { code: Up, .. } => *player_y -= 1,
35
+        Key { code: Down, .. } => *player_y += 1,
36
+        Key { code: Left, .. } => *player_x -= 1,
37
+        Key { code: Right, .. } => *player_x += 1,
38
+
39
+        _ => {}
40
+    }
41
+
42
+    false
43
+}
44
+
45
+fn main() {
46
+    tcod::system::set_fps(LIMIT_FPS);
47
+
48
+    let root = Root::initializer()
49
+        .font("arial10x10.png", FontLayout::Tcod)
50
+        .font_type(FontType::Greyscale)
51
+        .size(SCREEN_WIDTH, SCREEN_HEIGHT)
52
+        .title("Rust/libtcod tutorial")
53
+        .init();
54
+
55
+    let mut tcod = Tcod { root };
56
+
57
+    let mut player_x = SCREEN_WIDTH / 2;
58
+    let mut player_y = SCREEN_HEIGHT / 2;
59
+
60
+    while !tcod.root.window_closed() {
61
+        tcod.root.set_default_foreground(WHITE);
62
+        tcod.root.clear();
63
+        tcod.root
64
+            .put_char(player_x, player_y, '@', BackgroundFlag::None);
65
+        tcod.root.flush();
66
+
67
+        // handle keys and exit game if needed
68
+        let exit = handle_keys(&mut tcod, &mut player_x, &mut player_y);
69
+        if exit {
70
+            break;
71
+        }
72
+    }
73
+}