Skip to main content

InitContext

Struct InitContext 

Source
pub struct InitContext<'a, G: Game> { /* private fields */ }
Expand description

The work the startup closure run takes may do.

Implementations§

Source§

impl<'a, G: Game> InitContext<'a, G>

Source

pub fn startup(&mut self) -> &mut Startup<'a>

The startup work that is typed by no vocabulary of the game’s own.

Required if you want to start two games the same way: Startup is one type whatever the game, so code shared between them takes &mut Startup<'_>.

Examples found in repository?
examples/sprite-adventure.rs (line 1008)
1007    fn init(ctx: &mut InitContext<'_, Keep>) -> Result<Self, Error> {
1008        let startup = ctx.startup();
1009        let gem_taken = startup.saved(Flag::GemTaken);
1010        let area = if startup.saved(Flag::InCave) {
1011            Area::Cave
1012        } else {
1013            Area::Overworld
1014        };
1015        let position = Vec3::new(
1016            startup.saved(Position::X) as f32,
1017            0.0,
1018            startup.saved(Position::Z) as f32,
1019        );
1020
1021        Ok(Self {
1022            area,
1023            position,
1024            previous: position,
1025            facing: Facing::Toward,
1026            walk_ticks: 0,
1027            simulated: Duration::ZERO,
1028            door_opening: gem_taken,
1029            swing_ticks: if gem_taken { DOOR_SWING_TICKS } else { 0 },
1030            gem_taken,
1031            ghost: 0.0,
1032            reset_requested: false,
1033        })
1034    }
More examples
Hide additional examples
examples/ui-fonts.rs (line 603)
602    fn init(ctx: &mut InitContext<'_, Self>) -> Result<Self, Error> {
603        let startup = ctx.startup();
604        let mut fonts = egui::FontDefinitions::default();
605        for (family, names) in [
606            (egui::FontFamily::Proportional, &[PROPORTIONAL_FONT][..]),
607            (egui::FontFamily::Monospace, &[MONOSPACE_FONT][..]),
608            (
609                egui::FontFamily::Name(DISPLAY_FAMILY.into()),
610                &[DISPLAY_FONT][..],
611            ),
612            (
613                egui::FontFamily::Name(PIXEL_ONLY_FAMILY.into()),
614                &[PROPORTIONAL_FONT][..],
615            ),
616            (
617                egui::FontFamily::Name(PROMPT_FAMILY.into()),
618                &[PROMPT_FONT, PROPORTIONAL_FONT][..],
619            ),
620        ] {
621            install_family(&mut fonts, startup, family, names)?;
622        }
623        startup.set_fonts(fonts)?;
624
625        Ok(Self {
626            elapsed: Duration::ZERO,
627            orbit: Orbit::default(),
628            hailed: None,
629            dialogue: None,
630            sheet_open: false,
631        })
632    }
Source

pub fn duration(&mut self, sound: G::Sounds) -> Duration

How long sound plays for at its own pitch: the whole clip, before any trim.

Required if you want to time a game against a sound. Every value the vocabulary catalogs is built before this runs, and a streamed one reports what the decode at startup measured.

Source

pub fn durations(&mut self) -> HashMap<G::Sounds, Duration>

How long each value that the vocabulary catalogs plays for, keyed by the value.

Required if you want to time a game against a whole vocabulary. A value the catalog leaves out is absent from the map; read that one through duration.

Examples found in repository?
examples/sound-lab.rs (line 413)
412    fn init(ctx: &mut InitContext<'_, SoundCheck>) -> Result<Self, Error> {
413        let durations = ctx.durations();
414
415        let picked = Sound::Bounce;
416        let trim_end = durations.get(&picked).copied().unwrap_or_default();
417
418        Ok(Self {
419            master_volume: 1.0,
420
421            picked,
422            one_shot_gain: 1.0,
423            one_shot_pitch: 1.0,
424            one_shot_fade: SoundCue::<Sound>::DEFAULT_FADE.as_secs_f32(),
425            trim_start: 0.0,
426            trim_end: trim_end.as_secs_f32(),
427            one_shot_loop_from: 0.0,
428
429            theme_on: false,
430            menu_on: false,
431            pulse_on: false,
432            cue_fade: 1.0,
433
434            merge_demo: false,
435            ring_demo: false,
436
437            player: Vec2::ZERO,
438            player_prev: Vec2::ZERO,
439            sources: [
440                Source::new(-2.5, -2.0, Sound::Bounce, 4.0, false),
441                Source::new(2.5, -2.0, Sound::Serve, 4.0, false),
442                Source::new(0.0, 2.8, Sound::Theme, 7.0, true),
443            ],
444            dragging: None,
445
446            durations,
447        })
448    }
Source

pub fn prepare<M>(&mut self, mesh: M)
where G::Meshes: Holds<M> + From<M>,

Builds and uploads mesh now, instead of on its first draw; the copy is then held like any drawn mesh’s under Config::with_mesh_memory, and no longer than that.

Takes a mesh of Game::Meshes and no other.

Examples found in repository?
examples/isometric-board.rs (line 369)
367    fn init(ctx: &mut InitContext<'_, Board>) -> Result<Self, Error> {
368        for &(_, _, seed, _) in &ROCKS {
369            ctx.prepare(Rock { seed });
370        }
371
372        let (sprite_lift, _) = unit_geometry(Turn::Sprite);
373        let (block_lift, _) = unit_geometry(Turn::Block);
374        Ok(Self {
375            sprite: Unit::resting((1, 1), sprite_lift),
376            block: Unit::resting((BOARD_TILES - 2, BOARD_TILES - 2), block_lift),
377            turn: Turn::Sprite,
378            selected: false,
379        })
380    }

Auto Trait Implementations§

§

impl<'a, G> !RefUnwindSafe for InitContext<'a, G>

§

impl<'a, G> !Send for InitContext<'a, G>

§

impl<'a, G> !Sync for InitContext<'a, G>

§

impl<'a, G> !UnwindSafe for InitContext<'a, G>

§

impl<'a, G> Freeze for InitContext<'a, G>
where &'a mut Renderer<<G as Game>::Meshes, <G as Game>::Skyboxes>: Freeze, &'a mut Audio<<G as Game>::Sounds>: Freeze,

§

impl<'a, G> Unpin for InitContext<'a, G>
where &'a mut Renderer<<G as Game>::Meshes, <G as Game>::Skyboxes>: Unpin, &'a mut Audio<<G as Game>::Sounds>: Unpin,

§

impl<'a, G> UnsafeUnpin for InitContext<'a, G>
where &'a mut Renderer<<G as Game>::Meshes, <G as Game>::Skyboxes>: UnsafeUnpin, &'a mut Audio<<G as Game>::Sounds>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more