refactor: move main game loop to the bottom
This commit is contained in:
85
src/main.rs
85
src/main.rs
@@ -22,48 +22,6 @@ struct Tcod {
|
||||
root: Root,
|
||||
con: Offscreen,
|
||||
}
|
||||
fn main() {
|
||||
let root = Root::initializer()
|
||||
.font("arial10x10.png", FontLayout::Tcod)
|
||||
.font_type(FontType::Greyscale)
|
||||
.size(SCREEN_WIDTH, SCREEN_HEIGHT)
|
||||
.title("Rust/libtcod tutorial")
|
||||
.init();
|
||||
|
||||
let con = Offscreen::new(MAP_WIDTH, MAP_HEIGHT);
|
||||
|
||||
let mut tcod = Tcod { root, con };
|
||||
tcod::system::set_fps(LIMIT_FPS);
|
||||
|
||||
let mut player_x = SCREEN_WIDTH / 2;
|
||||
let mut player_y = SCREEN_HEIGHT / 2;
|
||||
|
||||
// create object representing the player
|
||||
let player = Object::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, '@', WHITE);
|
||||
// create an NPC
|
||||
let npc = Object::new(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2, '@', YELLOW);
|
||||
// the list of objects with the two above
|
||||
let mut objects = [player, npc];
|
||||
|
||||
// Generate map
|
||||
let game = Game { map: make_map() };
|
||||
|
||||
// Main game loop
|
||||
while !tcod.root.window_closed() {
|
||||
tcod.con.clear();
|
||||
render_all(&mut tcod, &game, &objects);
|
||||
|
||||
tcod.root.flush();
|
||||
tcod.root.wait_for_keypress(true);
|
||||
|
||||
// handle kepresses
|
||||
let player = &mut objects[0];
|
||||
let exit = handle_keys(&mut tcod, &game, player);
|
||||
if exit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle_keys() handles inputs; returns a boolean - "should we exit the game?"
|
||||
fn handle_keys(tcod: &mut Tcod, game: &Game, player: &mut Object) -> bool {
|
||||
@@ -193,3 +151,46 @@ fn make_map() -> Map {
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let root = Root::initializer()
|
||||
.font("arial10x10.png", FontLayout::Tcod)
|
||||
.font_type(FontType::Greyscale)
|
||||
.size(SCREEN_WIDTH, SCREEN_HEIGHT)
|
||||
.title("Rust/libtcod tutorial")
|
||||
.init();
|
||||
|
||||
let con = Offscreen::new(MAP_WIDTH, MAP_HEIGHT);
|
||||
|
||||
let mut tcod = Tcod { root, con };
|
||||
tcod::system::set_fps(LIMIT_FPS);
|
||||
|
||||
let mut player_x = SCREEN_WIDTH / 2;
|
||||
let mut player_y = SCREEN_HEIGHT / 2;
|
||||
|
||||
// create object representing the player
|
||||
let player = Object::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, '@', WHITE);
|
||||
// create an NPC
|
||||
let npc = Object::new(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2, '@', YELLOW);
|
||||
// the list of objects with the two above
|
||||
let mut objects = [player, npc];
|
||||
|
||||
// Generate map
|
||||
let game = Game { map: make_map() };
|
||||
|
||||
// Main game loop
|
||||
while !tcod.root.window_closed() {
|
||||
tcod.con.clear();
|
||||
render_all(&mut tcod, &game, &objects);
|
||||
|
||||
tcod.root.flush();
|
||||
tcod.root.wait_for_keypress(true);
|
||||
|
||||
// handle kepresses
|
||||
let player = &mut objects[0];
|
||||
let exit = handle_keys(&mut tcod, &game, player);
|
||||
if exit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user