Initial commit
This commit is contained in:
68
src/main.rs
Normal file
68
src/main.rs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user