Skip to content

Instantly share code, notes, and snippets.

@Darksecond
Created November 9, 2018 10:50
Show Gist options
  • Save Darksecond/73c70b04f79d4b4cf85d55fd5baff511 to your computer and use it in GitHub Desktop.
Save Darksecond/73c70b04f79d4b4cf85d55fd5baff511 to your computer and use it in GitHub Desktop.
Example for how specs and tcod would work together
extern crate tcod;
extern crate specs;
use tcod::console::{Root};
use specs::{System, VecStorage, Component, World, Builder, WriteStorage, DispatcherBuilder};
#[derive(Debug)]
enum Command {
PutCharacter(char,i32,i32),
Clear,
}
#[derive(Debug)]
struct Surface {
commands: Vec<Command>,
}
impl Component for Surface {
type Storage = VecStorage<Self>;
}
struct RootSystem {
root: Root,
}
impl<'a> System<'a> for RootSystem {
type SystemData = WriteStorage<'a, Surface>;
fn run(&mut self, mut surfaces: Self::SystemData) {
use specs::Join;
use tcod::colors;
use tcod::console::*;
self.root.set_default_foreground(colors::WHITE); // Command
for surface in (&mut surfaces).join() { // should be surfaces
for command in &surface.commands {
match command {
Command::Clear => self.root.clear(),
Command::PutCharacter(ascii, x, y) => self.root.put_char(*x, *y, *ascii, BackgroundFlag::None),
}
}
surface.commands.clear();
}
self.root.flush();
self.root.wait_for_keypress(true); // Should be check instead of wait and should push into a Key resource
//TODO resource for self.root.window_closed()
}
// fn setup(&mut self, res: &mut Resources) {
// use specs::prelude::SystemData;
// Self::SystemData::setup(res);
// }
}
impl RootSystem {
fn new() -> RootSystem {
use tcod::console::*;
let root = Root::initializer()
.font("arial10x10.png", FontLayout::Tcod)
.font_type(FontType::Greyscale)
.size(80, 50)
.title("Rust/libtcod tutorial")
.init();
RootSystem {
root
}
}
}
fn main() {
tcod::system::set_fps(20);
let mut world = World::new();
world.register::<Surface>();
world.create_entity().with(Surface{commands: vec![Command::Clear, Command::PutCharacter('@', 1, 1)]}).build();
let mut dispatcher = DispatcherBuilder::new()
.with_thread_local(RootSystem::new())
.build();
loop {
dispatcher.dispatch(&mut world.res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment