Struct tetra::Context[][src]

pub struct Context { /* fields omitted */ }
Expand description

A struct containing all of the ‘global’ state within the framework.

Implementations

impl Context[src]

pub fn run<S, F, E>(&mut self, init: F) -> Result<(), E> where
    S: State<E>,
    F: FnOnce(&mut Context) -> Result<S, E>,
    E: From<TetraError>, 
[src]

Runs the game.

The init parameter takes a function or closure that creates a State implementation. A common pattern is to use method references to pass in your state’s constructor directly - see the example below for how this works.

The error type returned by your init closure currently must match the error type returned by your State methods. This limitation may be lifted in the future.

Errors

If the State returns an error from update, draw or event, the game will stop running and this method will return the error.

Examples

use tetra::{Context, ContextBuilder, State};

struct GameState;

impl GameState {
    fn new(ctx: &mut Context) -> tetra::Result<GameState> {
        Ok(GameState)
    }
}

impl State for GameState { }

fn main() -> tetra::Result {
    // Because GameState::new takes `&mut Context` and returns a `State` implementation
    // wrapped in a `Result`, you can use it without a closure wrapper:
    ContextBuilder::new("Hello, world!", 1280, 720)
        .build()?
        .run(GameState::new)
}

Auto Trait Implementations

impl !RefUnwindSafe for Context

impl !Send for Context

impl !Sync for Context

impl Unpin for Context

impl !UnwindSafe for Context

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.