Struct Game

Source
pub struct Game<W: Write, S = ()> { /* private fields */ }
Expand description

The driving type of a game.

A Game consists of a number of components that are executed in a loop and act on some shared state.

§Example

use teng::{install_panic_handler, terminal_cleanup, terminal_setup, Game};

terminal_setup().unwrap();
install_panic_handler();

let mut game: Game<_, ()> = Game::new_with_custom_buf_writer();
game.install_recommended_components();
game.run().unwrap();

terminal_cleanup().unwrap();

Implementations§

Source§

impl<S: Default + 'static> Game<CustomBufWriter, S>

Source

pub fn new_with_custom_buf_writer() -> Self

Creates a new game with a sink that only flushes once every frame. This is the recommended sink.

Examples found in repository?
examples/simple.rs (line 29)
25fn main() -> io::Result<()> {
26    terminal_setup()?;
27    install_panic_handler();
28
29    let mut game = Game::new_with_custom_buf_writer();
30    // If you don't install the recommended components, you will need to have your own
31    // component that exits the process, since Ctrl-C does not work in raw mode.
32    game.install_recommended_components();
33    game.add_component(Box::new(MyComponent));
34    game.run()?;
35
36    terminal_cleanup()?;
37
38    Ok(())
39}
Source§

impl<S: Default + 'static> Game<Stdout, S>

Source

pub fn new_with_stdout() -> Self

Source§

impl<W: Write, S: Default + 'static> Game<W, S>

Source

pub fn new(sink: W) -> Self

Examples found in repository?
examples/flappy-bird/main.rs (line 6)
5fn main() -> io::Result<()> {
6    let mut game = Game::<_, ()>::new(stdout());
7    game.run()
8}
More examples
Hide additional examples
examples/boundschecker.rs (line 18)
14fn main() -> io::Result<()> {
15    terminal_setup()?;
16    install_panic_handler();
17
18    let mut game = Game::new(stdout());
19    game.install_recommended_components();
20    game.add_component(Box::new(BoundsCheckerComponent::new()));
21    game.run()?;
22
23    terminal_cleanup()?;
24
25    Ok(())
26}
examples/falling-sand/main.rs (line 16)
12fn main() -> std::io::Result<()> {
13    terminal_setup()?;
14    install_panic_handler();
15
16    let mut game = Game::new(stdout());
17    game.install_recommended_components();
18    game.add_component(Box::new(FallingSimulationComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/exponentialgrowingboundschecker.rs (line 20)
16fn main() -> io::Result<()> {
17    terminal_setup()?;
18    install_panic_handler();
19
20    let mut game = Game::new(stdout());
21    game.install_recommended_components();
22    game.add_component(Box::new(ExponentialBoundsCheckerComponent::new()));
23    game.run()?;
24
25    terminal_cleanup()?;
26
27    Ok(())
28}
examples/fpschecker.rs (line 15)
11fn main() -> io::Result<()> {
12    terminal_setup()?;
13    install_panic_handler();
14
15    let mut game = Game::new(stdout());
16    game.install_recommended_components();
17    // game.add_component(Box::new(FpsCheckerComponent::new()));
18    game.add_component(Box::new(FpsCheckerFrameCountComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/ecs/main.rs (line 75)
71fn main() -> io::Result<()> {
72    teng::terminal_setup()?;
73    teng::install_panic_handler();
74
75    let mut game = Game::new(stdout());
76    game.install_recommended_components();
77    game.add_component(Box::new(EcsComponent::default()));
78    game.add_component(Box::new(DrawSystem));
79    game.add_component(Box::new(PhysicsSystem));
80    game.run()?;
81
82    teng::terminal_cleanup()?;
83    Ok(())
84}
Source

pub fn add_component(&mut self, component: Box<dyn Component<S>>)

Examples found in repository?
examples/boundschecker.rs (line 20)
14fn main() -> io::Result<()> {
15    terminal_setup()?;
16    install_panic_handler();
17
18    let mut game = Game::new(stdout());
19    game.install_recommended_components();
20    game.add_component(Box::new(BoundsCheckerComponent::new()));
21    game.run()?;
22
23    terminal_cleanup()?;
24
25    Ok(())
26}
More examples
Hide additional examples
examples/falling-sand/main.rs (line 18)
12fn main() -> std::io::Result<()> {
13    terminal_setup()?;
14    install_panic_handler();
15
16    let mut game = Game::new(stdout());
17    game.install_recommended_components();
18    game.add_component(Box::new(FallingSimulationComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/exponentialgrowingboundschecker.rs (line 22)
16fn main() -> io::Result<()> {
17    terminal_setup()?;
18    install_panic_handler();
19
20    let mut game = Game::new(stdout());
21    game.install_recommended_components();
22    game.add_component(Box::new(ExponentialBoundsCheckerComponent::new()));
23    game.run()?;
24
25    terminal_cleanup()?;
26
27    Ok(())
28}
examples/fpschecker.rs (line 18)
11fn main() -> io::Result<()> {
12    terminal_setup()?;
13    install_panic_handler();
14
15    let mut game = Game::new(stdout());
16    game.install_recommended_components();
17    // game.add_component(Box::new(FpsCheckerComponent::new()));
18    game.add_component(Box::new(FpsCheckerFrameCountComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/ecs/main.rs (line 77)
71fn main() -> io::Result<()> {
72    teng::terminal_setup()?;
73    teng::install_panic_handler();
74
75    let mut game = Game::new(stdout());
76    game.install_recommended_components();
77    game.add_component(Box::new(EcsComponent::default()));
78    game.add_component(Box::new(DrawSystem));
79    game.add_component(Box::new(PhysicsSystem));
80    game.run()?;
81
82    teng::terminal_cleanup()?;
83    Ok(())
84}
examples/simple.rs (line 33)
25fn main() -> io::Result<()> {
26    terminal_setup()?;
27    install_panic_handler();
28
29    let mut game = Game::new_with_custom_buf_writer();
30    // If you don't install the recommended components, you will need to have your own
31    // component that exits the process, since Ctrl-C does not work in raw mode.
32    game.install_recommended_components();
33    game.add_component(Box::new(MyComponent));
34    game.run()?;
35
36    terminal_cleanup()?;
37
38    Ok(())
39}
Source

pub fn add_component_with( &mut self, init_fn: impl FnOnce(usize, usize) -> Box<dyn Component<S>>, )

Source

pub fn run(&mut self) -> Result<()>

Examples found in repository?
examples/flappy-bird/main.rs (line 7)
5fn main() -> io::Result<()> {
6    let mut game = Game::<_, ()>::new(stdout());
7    game.run()
8}
More examples
Hide additional examples
examples/boundschecker.rs (line 21)
14fn main() -> io::Result<()> {
15    terminal_setup()?;
16    install_panic_handler();
17
18    let mut game = Game::new(stdout());
19    game.install_recommended_components();
20    game.add_component(Box::new(BoundsCheckerComponent::new()));
21    game.run()?;
22
23    terminal_cleanup()?;
24
25    Ok(())
26}
examples/falling-sand/main.rs (line 19)
12fn main() -> std::io::Result<()> {
13    terminal_setup()?;
14    install_panic_handler();
15
16    let mut game = Game::new(stdout());
17    game.install_recommended_components();
18    game.add_component(Box::new(FallingSimulationComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/exponentialgrowingboundschecker.rs (line 23)
16fn main() -> io::Result<()> {
17    terminal_setup()?;
18    install_panic_handler();
19
20    let mut game = Game::new(stdout());
21    game.install_recommended_components();
22    game.add_component(Box::new(ExponentialBoundsCheckerComponent::new()));
23    game.run()?;
24
25    terminal_cleanup()?;
26
27    Ok(())
28}
examples/fpschecker.rs (line 19)
11fn main() -> io::Result<()> {
12    terminal_setup()?;
13    install_panic_handler();
14
15    let mut game = Game::new(stdout());
16    game.install_recommended_components();
17    // game.add_component(Box::new(FpsCheckerComponent::new()));
18    game.add_component(Box::new(FpsCheckerFrameCountComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/ecs/main.rs (line 80)
71fn main() -> io::Result<()> {
72    teng::terminal_setup()?;
73    teng::install_panic_handler();
74
75    let mut game = Game::new(stdout());
76    game.install_recommended_components();
77    game.add_component(Box::new(EcsComponent::default()));
78    game.add_component(Box::new(DrawSystem));
79    game.add_component(Box::new(PhysicsSystem));
80    game.run()?;
81
82    teng::terminal_cleanup()?;
83    Ok(())
84}
Examples found in repository?
examples/boundschecker.rs (line 19)
14fn main() -> io::Result<()> {
15    terminal_setup()?;
16    install_panic_handler();
17
18    let mut game = Game::new(stdout());
19    game.install_recommended_components();
20    game.add_component(Box::new(BoundsCheckerComponent::new()));
21    game.run()?;
22
23    terminal_cleanup()?;
24
25    Ok(())
26}
More examples
Hide additional examples
examples/falling-sand/main.rs (line 17)
12fn main() -> std::io::Result<()> {
13    terminal_setup()?;
14    install_panic_handler();
15
16    let mut game = Game::new(stdout());
17    game.install_recommended_components();
18    game.add_component(Box::new(FallingSimulationComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/exponentialgrowingboundschecker.rs (line 21)
16fn main() -> io::Result<()> {
17    terminal_setup()?;
18    install_panic_handler();
19
20    let mut game = Game::new(stdout());
21    game.install_recommended_components();
22    game.add_component(Box::new(ExponentialBoundsCheckerComponent::new()));
23    game.run()?;
24
25    terminal_cleanup()?;
26
27    Ok(())
28}
examples/fpschecker.rs (line 16)
11fn main() -> io::Result<()> {
12    terminal_setup()?;
13    install_panic_handler();
14
15    let mut game = Game::new(stdout());
16    game.install_recommended_components();
17    // game.add_component(Box::new(FpsCheckerComponent::new()));
18    game.add_component(Box::new(FpsCheckerFrameCountComponent::new()));
19    game.run()?;
20
21    terminal_cleanup()?;
22
23    Ok(())
24}
examples/ecs/main.rs (line 76)
71fn main() -> io::Result<()> {
72    teng::terminal_setup()?;
73    teng::install_panic_handler();
74
75    let mut game = Game::new(stdout());
76    game.install_recommended_components();
77    game.add_component(Box::new(EcsComponent::default()));
78    game.add_component(Box::new(DrawSystem));
79    game.add_component(Box::new(PhysicsSystem));
80    game.run()?;
81
82    teng::terminal_cleanup()?;
83    Ok(())
84}
examples/simple.rs (line 32)
25fn main() -> io::Result<()> {
26    terminal_setup()?;
27    install_panic_handler();
28
29    let mut game = Game::new_with_custom_buf_writer();
30    // If you don't install the recommended components, you will need to have your own
31    // component that exits the process, since Ctrl-C does not work in raw mode.
32    game.install_recommended_components();
33    game.add_component(Box::new(MyComponent));
34    game.run()?;
35
36    terminal_cleanup()?;
37
38    Ok(())
39}

Auto Trait Implementations§

§

impl<W, S> Freeze for Game<W, S>
where W: Freeze, S: Freeze,

§

impl<W, S = ()> !RefUnwindSafe for Game<W, S>

§

impl<W, S = ()> !Send for Game<W, S>

§

impl<W, S = ()> !Sync for Game<W, S>

§

impl<W, S> Unpin for Game<W, S>
where W: Unpin, S: Unpin,

§

impl<W, S = ()> !UnwindSafe for Game<W, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Any for T
where T: Any,