aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/interpolation.rs50
-rw-r--r--src/main.rs1
-rw-r--r--src/render.rs1
3 files changed, 52 insertions, 0 deletions
diff --git a/src/interpolation.rs b/src/interpolation.rs
new file mode 100644
index 0000000..de6b6df
--- /dev/null
+++ b/src/interpolation.rs
@@ -0,0 +1,50 @@
+use std::ops::{Add, Mul, Sub};
+
+pub fn raw_lerp<T>(a: T, b: T, t: f64) -> T
+where
+ T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f64, Output = T>,
+{
+ a + (b - a) * t
+}
+
+pub enum TimingFunction {
+ Lerp,
+}
+pub struct KeyFrame<ValueType> {
+ // We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this
+ pub time: f64,
+ pub val: ValueType,
+}
+impl<ValueType> KeyFrame<ValueType> {
+ pub fn new(time: f64, val: ValueType) -> KeyFrame<ValueType> {
+ KeyFrame { time, val }
+ }
+}
+impl<
+ ValueType: PartialOrd
+ + Copy
+ + Add<Output = ValueType>
+ + Sub<Output = ValueType>
+ + Mul<f64, Output = ValueType>,
+ > KeyFrame<ValueType>
+{
+ /** Simply passes the data to the timing function involved. Does not do bounding. */
+ pub fn raw_value_at(
+ from: &KeyFrame<ValueType>,
+ to: &KeyFrame<ValueType>,
+ time: f64,
+ timing_function: TimingFunction,
+ ) -> ValueType {
+ // Order them so `from` is always the lower bound
+ let (from, to) = if from.time < to.time {
+ (from, to)
+ } else {
+ (to, from)
+ };
+ let length = to.time - from.time;
+ let position = (time - from.time) / length;
+ match timing_function {
+ TimingFunction::Lerp => raw_lerp(from.val, to.val, position),
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 5a653cd..f9c52f9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ extern crate sdl2;
mod font;
pub mod generated;
+mod interpolation;
mod render;
use std::time::{Duration, SystemTime};
diff --git a/src/render.rs b/src/render.rs
index ab9e48e..c4d207f 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,5 +1,6 @@
use crate::font::BakedFont;
use crate::generated::fonts::{FONT_CHERRY_BOMB_ONE, FONT_GALMURI};
+use crate::interpolation::KeyFrame;
use sdl2::pixels::{Color, PixelFormatEnum};
use sdl2::rect::Rect;
use sdl2::render::{Canvas, Texture, TextureCreator};