[package]
name = "Example"
version = "0.1.0"
edition = "2021"
[dependencies]
macroquad = "0.4.13"use interpolation::lerp;
use macroquad::color::{BLACK, LIGHTGRAY, WHITE};
use macroquad::prelude::{
clear_background, draw_rectangle, draw_rectangle_lines, next_frame, screen_width,
};
use macroquad::time::get_frame_time;
use macroquad::window::screen_height;
use miniquad::conf::Conf;
#[derive(Clone, Copy)]
struct Pos {
x: f32,
y: f32,
}
pub fn conf() -> Conf {
Conf::default()
}
#[macroquad::main(conf)]
async fn main() {
const STEP: f32 = 1. / 60.;
let mut acc: f32 = 0.;
let mut pos1 = Pos {
x: screen_width() / 2.0,
y: screen_height() / 2. - 100.,
};
let mut last_pos1 = pos1;
let mut pos2 = Pos {
x: screen_width() / 2.0,
y: screen_height() / 2.,
};
loop {
clear_background(LIGHTGRAY);
acc += get_frame_time();
last_pos1 = pos1;
while acc >= STEP {
pos1.x += 100. * STEP;
acc -= STEP;
}
pos2.x += 100. * get_frame_time();
let alpha = acc / STEP;
draw_rectangle(
lerp(&last_pos1.x, &pos1.x, &alpha),
lerp(&last_pos1.y, &pos1.y, &alpha),
200.,
50.,
WHITE,
);
draw_rectangle_lines(
lerp(&last_pos1.x, &pos1.x, &alpha),
lerp(&last_pos1.y, &pos1.y, &alpha),
200.,
50.,
2.,
BLACK,
);
draw_rectangle(pos2.x, pos2.y, 200., 50., WHITE);
draw_rectangle_lines(pos2.x, pos2.y, 200., 50., 2., BLACK);
next_frame().await
}
} frameRate(60);