use tetra::graphics::{self, Color, Texture};
use tetra::math::Vec2;
use tetra::time::{self, Timestep};
use tetra::{Context, ContextBuilder, State};
struct GameState {
texture: Texture,
velocity: Vec2<f32>,
position_none: Vec2<f32>,
position_ex: Vec2<f32>,
position_in_prev: Vec2<f32>,
position_in_curr: Vec2<f32>,
}
impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
Ok(GameState {
texture: Texture::new(ctx, "./examples/resources/player.png")?,
velocity: Vec2::new(16.0, 0.0),
position_none: Vec2::new(16.0, 16.0),
position_ex: Vec2::new(16.0, 32.0),
position_in_prev: Vec2::new(16.0, 48.0),
position_in_curr: Vec2::new(16.0, 48.0),
})
}
}
impl State for GameState {
fn update(&mut self, _: &mut Context) -> tetra::Result {
self.position_none += self.velocity;
self.position_ex += self.velocity;
self.position_in_prev = self.position_in_curr;
self.position_in_curr += self.velocity;
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.769, 0.812, 0.631));
let blend_factor = time::get_blend_factor(ctx);
self.texture.draw(ctx, self.position_none);
self.texture
.draw(ctx, self.position_ex + (self.velocity * blend_factor));
self.texture.draw(
ctx,
Vec2::lerp(self.position_in_prev, self.position_in_curr, blend_factor),
);
Ok(())
}
}
fn main() -> tetra::Result {
ContextBuilder::new("Interpolation", 640, 480)
.timestep(Timestep::Fixed(5.0))
.quit_on_escape(true)
.build()?
.run(GameState::new)
}