commit 5b7a331c7fd6a359bdfe333eda4491e574beaf9c Author: Michael Smith Date: Mon Jul 12 17:35:57 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..395ec46 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,55 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a6577517ecd0ee0934f48a7295a89aaef3e6dfafeac404f94c0b3448518ddfe" + +[[package]] +name = "cc" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" + +[[package]] +name = "lazy_static" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417" + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "roguelike" +version = "0.1.0" +dependencies = [ + "tcod", +] + +[[package]] +name = "tcod" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44e518b0661949712e8dbc6d6d9d7c00a405dd88bc539102c1edfc2d22e5e144" +dependencies = [ + "bitflags", + "lazy_static", + "tcod-sys", +] + +[[package]] +name = "tcod-sys" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "730fb27d2261f7c860ae7a61c3ae70277f0836173ceeccb594193abb4fa5f4aa" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0d440b6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "roguelike" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tcod = "*" diff --git a/arial10x10.png b/arial10x10.png new file mode 100644 index 0000000..64691f1 Binary files /dev/null and b/arial10x10.png differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0e2f64d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,68 @@ +use tcod::colors::*; +use tcod::console::*; + +const SCREEN_WIDTH: i32 = 80; +const SCREEN_HEIGHT: i32 = 50; + +const FPS: i32 = 20; + +struct Tcod { + root: Root, +} + +fn handle_keys(tcod: &mut Tcod, player_x: &mut i32, player_y: &mut i32) -> bool { + use tcod::input::Key; + use tcod::input::KeyCode::*; + + // todo: handle keys + let key = tcod.root.wait_for_keypress(true); + match key { + Key { + code: Enter, + alt: true, + .. + } => { + // Alt+Enter: toggle fullscreen + let fullscreen = tcod.root.is_fullscreen(); + tcod.root.set_fullscreen(!fullscreen); + } + Key { code: Escape, .. } => return true, // exit game + + Key { code: Up, .. } => *player_y -= 1, + Key { code: Down, .. } => *player_y += 1, + Key { code: Left, .. } => *player_x -= 1, + Key { code: Right, .. } => *player_x += 1, + + _ => {} + } + false +} + +fn main() { + tcod::system::set_fps(FPS); + + let root = Root::initializer() + .font("arial10x10.png", FontLayout::Tcod) + .font_type(FontType::Greyscale) + .size(SCREEN_WIDTH, SCREEN_HEIGHT) + .title("Rust/libtcod tutorial") + .init(); + + let mut tcod = Tcod { root }; + + let mut player_x = SCREEN_WIDTH / 2; + let mut player_y = SCREEN_HEIGHT / 2; + + while !tcod.root.window_closed() { + tcod.root.set_default_foreground(WHITE); + tcod.root.clear(); + tcod.root + .put_char(player_x, player_y, '@', BackgroundFlag::None); + tcod.root.flush(); + tcod.root.wait_for_keypress(true); + let exit = handle_keys(&mut tcod, &mut player_x, &mut player_y); + if exit { + break; + } + } +}