aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/interpolation.rs188
-rw-r--r--src/main.rs146
-rw-r--r--src/render.rs116
3 files changed, 320 insertions, 130 deletions
diff --git a/src/interpolation.rs b/src/interpolation.rs
new file mode 100644
index 0000000..ecb2c85
--- /dev/null
+++ b/src/interpolation.rs
@@ -0,0 +1,188 @@
+// NOTE: This entire file would need to be reworked to handle functions like bezier curves/any other fn involving multiple points.
+// Someone should do this at some point.
+//
+// This likely would only be slightly breaking; ie the most commonly used portions of AnimationTrack would remain in-tact
+// however, the internals of a KeyFrame would change immensely and be (more or less) impossible to keep
+
+use std::ops::{Add, Index, Mul, Sub};
+
+// For when RFC1733 finally passes
+// trait AcceptableValueType<ValueType> = PartialOrd
+// + Copys
+// + Add<Output = ValueType>
+// + Sub<Output = ValueType>
+// + Mul<f64, Output = ValueType>;
+
+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 fn raw_bezier<T>(v: Vec<T>, t: f64) -> T
+where
+ T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f64, Output = T>,
+{
+ todo!()
+}
+
+#[derive(Clone, Copy)]
+pub enum TwoValueTimingFunction {
+ Lerp,
+}
+#[derive(Clone, Copy)]
+pub enum ManyValueTimingFunction {
+ Bezier,
+}
+
+#[derive(Clone, Copy, PartialEq, PartialOrd)]
+// We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this
+pub struct KeyFrame<ValueType> {
+ 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: TwoValueTimingFunction,
+ ) -> 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 {
+ TwoValueTimingFunction::Lerp => raw_lerp(from.val, to.val, position),
+ }
+ }
+}
+
+#[derive(Clone)]
+pub struct AnimationTrack<ValueType> {
+ pub keyframes: Vec<KeyFrame<ValueType>>,
+ pub loop_count: u32,
+}
+impl<ValueType: Copy> AnimationTrack<ValueType> {
+ fn get_sorted_keyframes(&self) -> Vec<KeyFrame<ValueType>> {
+ let mut kf = self.keyframes.clone();
+ kf.sort_by(|a, b| a.time.total_cmp(&b.time));
+ kf
+ }
+ fn get_first_last(&self) -> Option<(KeyFrame<ValueType>, KeyFrame<ValueType>)> {
+ let okf = self.get_sorted_keyframes();
+ if okf.len() == 0 {
+ None
+ } else {
+ Some((*okf.first().unwrap(), *okf.last().unwrap()))
+ }
+ }
+ fn length(fl: (KeyFrame<ValueType>, KeyFrame<ValueType>)) -> f64 {
+ fl.1.time - fl.0.time
+ }
+}
+impl<
+ ValueType: PartialOrd
+ + Copy
+ + Add<Output = ValueType>
+ + Sub<Output = ValueType>
+ + Mul<f64, Output = ValueType>,
+ > AnimationTrack<ValueType>
+{
+ /** Get the 2 keyframes surrounding the current position in time. */
+ fn get_current_keyframes(
+ &self,
+ // We have the user pass this, as to prevent needing to constantly re-calculate it.
+ sorted_keyframes: Vec<KeyFrame<ValueType>>,
+ time: f64,
+ ) -> Option<(KeyFrame<ValueType>, KeyFrame<ValueType>)> {
+ let length = AnimationTrack::length((
+ *sorted_keyframes.first().unwrap(),
+ *sorted_keyframes.last().unwrap(),
+ ));
+ // This can be removed if size restrictions call for it
+ match if time > length {
+ if time > length * f64::from(self.loop_count) {
+ None
+ } else {
+ Some(time % length)
+ }
+ } else {
+ Some(time)
+ } {
+ None => None,
+ Some(time) => {
+ // TODO: maybe refactor the internals of this to be faster
+ let idx = {
+ let mut idx = 0;
+ let mut found = false;
+ for f in &sorted_keyframes {
+ if f.time > time {
+ found = true;
+ break;
+ } else {
+ idx += 1;
+ }
+ }
+ if found {
+ Some(idx)
+ } else {
+ None
+ }
+ };
+ match idx {
+ None => None,
+ Some(idx) => {
+ // If it's the last item, we don't have a 2nd
+ if idx + 1 == sorted_keyframes.len() {
+ None
+ } else {
+ Some((
+ *sorted_keyframes.index(idx),
+ *sorted_keyframes.index(idx + 1),
+ ))
+ }
+ }
+ }
+ }
+ }
+ }
+ /** Gets the current value */
+ pub fn get_current_value(
+ &self,
+ // We have the user pass this, as to prevent needing to constantly re-calculate it.
+ sorted_keyframes: Vec<KeyFrame<ValueType>>,
+ time: f64,
+ timing_function: TwoValueTimingFunction,
+ ) -> Option<ValueType> {
+ let frames = self.get_current_keyframes(sorted_keyframes, time);
+ match frames {
+ Some(frames) => Some(KeyFrame::raw_value_at(
+ &frames.0,
+ &frames.1,
+ time,
+ timing_function,
+ )),
+ None => None,
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index deb1151..9c65333 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,19 +1,14 @@
extern crate sdl2;
mod font;
+
pub mod generated;
+mod interpolation;
+mod render;
+
+use std::time::{Duration, SystemTime};
-use font::BakedFont;
-use generated::fonts::{FONT_CHERRY_BOMB_ONE, FONT_GALMURI};
-use rand::Rng;
-use sdl2::event::Event;
-use sdl2::keyboard::Keycode;
-use sdl2::pixels::{Color, PixelFormatEnum};
-use sdl2::rect::Rect;
-use sdl2::render::Texture;
-use sdl2::surface::Surface;
-use std::ops::DerefMut;
-use std::time::Duration;
+use sdl2::{event::Event, keyboard::Keycode};
pub fn main() {
let sdl_context = sdl2::init().unwrap();
@@ -27,130 +22,21 @@ pub fn main() {
let mut canvas = window.clone().into_canvas().build().unwrap();
let texture_creator = canvas.texture_creator();
- // let uwu_font = font::UwUFont::new(&texture_creator);
- canvas.clear();
+
+ render::render(&mut canvas, &texture_creator, 0.001);
+
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
- let mut i = 0;
- // amount to incr sin_offset by per frame
- // TODO: do not bind to framerate u silly bean
- let sin_speed = 0.003;
- let mut sin_offset = 0.5;
+ let start_time = SystemTime::now();
'running: loop {
- i = (i + 1) % 255;
+ let time = SystemTime::now()
+ .duration_since(start_time)
+ .expect("Time went back between frames");
+ let time = f64::from(time.as_millis() as u32) / 1000.0;
- if sin_offset > 1.0 {
- sin_offset -= 1.0;
- }
-
- canvas.set_draw_color(Color::RGB(12, 12, 12));
- canvas.clear();
-
- let mut offset: f32 = 0.0;
- for c in "UwU-Space".chars() {
- let char = FONT_CHERRY_BOMB_ONE.get_char(c);
- canvas
- .copy(
- &char
- .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
- .unwrap(),
- None,
- char.to_rect(offset as i32 + 16, 16),
- )
- .unwrap();
- offset += char.advance_width;
- }
+ render::render(&mut canvas, &texture_creator, time);
- offset = 0.0;
- let mut rng = rand::thread_rng();
-
- for c in "sorry for shit demo we have adhd".chars() {
- let char = FONT_GALMURI.get_char(c);
- canvas
- .copy(
- &char
- .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
- .unwrap(),
- None,
- char.to_rect(
- offset as i32 + 18 + rng.gen_range(-2..2),
- 16 + 36 * 2 + rng.gen_range(-2..2),
- ),
- )
- .unwrap();
- offset += char.advance_width / 1.0;
- }
-
- {
- let mut sin_surface = Surface::new(512, 256, PixelFormatEnum::RGBA32).unwrap();
-
- let w = sin_surface.width();
- let h = sin_surface.height();
- let f = &mut sin_surface.deref_mut().without_lock_mut().unwrap();
-
- for x in 0..w {
- let f64_w = f64::from(w);
- let f64_h = f64::from(h);
- let sin_x = {
- let mut sin_x = f64::from(x) + (sin_offset * f64_w);
- if sin_x > f64_w {
- sin_x = sin_x - f64_w;
- }
- sin_x
- };
- let sin_y =
- ((f64::sin(sin_x * (3.141 * 2.0) / f64_w) + 1.0) * (f64_h / 2.0)).floor() as usize;
- // let sin_idx = (sin_y * w as usize + x as usize) * 4;
-
- for y in 0..h {
- let idx = (y * w + x) as usize * 4;
- f[idx] = 122 - (x / 8) as u8;
- f[idx + 1] = 255 - (x / 2) as u8;
- f[idx + 2] = (x / 2) as u8;
- f[idx + 3] = if sin_y < y as usize {
- if idx % 5 == 0 {
- 255
- } else {
- 122
- }
- } else {
- 0
- };
- }
- }
- sin_offset += sin_speed;
-
- let sin_texture = Texture::from_surface(&sin_surface, &texture_creator).unwrap();
-
- canvas
- .copy(&sin_texture, None, Rect::new(0, 0, 512, 256))
- .unwrap();
- }
- offset = 0.0;
- for c in "Come to Cosin25 :3".chars() {
- let char = FONT_GALMURI.get_char(c);
- canvas
- .copy(
- &char
- .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
- .unwrap(),
- None,
- char.to_rect(offset as i32 + 18, 16 + 36),
- )
- .unwrap();
- offset += char.advance_width;
- }
- for event in event_pump.poll_iter() {
- match event {
- Event::Quit { .. }
- | Event::KeyDown {
- keycode: Some(Keycode::Escape),
- ..
- } => break 'running,
- _ => {}
- }
- }
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
@@ -163,6 +49,6 @@ pub fn main() {
}
// The rest of the game loop goes here...
canvas.present();
- ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 120));
+ ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 12000));
}
}
diff --git a/src/render.rs b/src/render.rs
new file mode 100644
index 0000000..938d1ec
--- /dev/null
+++ b/src/render.rs
@@ -0,0 +1,116 @@
+use crate::font::BakedFont;
+use crate::generated::fonts::{FONT_CHERRY_BOMB_ONE, FONT_GALMURI};
+use crate::interpolation::KeyFrame;
+use rand::Rng;
+use sdl2::pixels::{Color, PixelFormatEnum};
+use sdl2::rect::Rect;
+use sdl2::render::{Canvas, Texture, TextureCreator};
+use sdl2::surface::Surface;
+use sdl2::video::{Window, WindowContext};
+use std::ops::DerefMut;
+
+pub fn render(
+ canvas: &mut Canvas<Window>,
+ texture_creator: &TextureCreator<WindowContext>,
+ time_seconds: f64,
+) {
+ let i = ((time_seconds * 60.0) % 255.0).round() as u8;
+ let sin_offset = time_seconds * 0.1;
+
+ canvas.set_draw_color(Color::RGB(12, 12, 12));
+ canvas.clear();
+
+ let mut offset: f32 = 0.0;
+ for c in "UwU-Space".chars() {
+ let char = FONT_CHERRY_BOMB_ONE.get_char(c);
+ canvas
+ .copy(
+ &char
+ .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
+ .unwrap(),
+ None,
+ char.to_rect(offset as i32 + 16, 16),
+ )
+ .unwrap();
+ offset += char.advance_width;
+ }
+
+ offset = 0.0;
+ let mut rng = rand::thread_rng();
+
+ for c in "sorry for shit demo we have adhd".chars() {
+ let char = FONT_GALMURI.get_char(c);
+ canvas
+ .copy(
+ &char
+ .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
+ .unwrap(),
+ None,
+ char.to_rect(
+ offset as i32 + 18 + rng.gen_range(-2..2),
+ 16 + 36 * 2 + rng.gen_range(-2..2),
+ ),
+ )
+ .unwrap();
+ offset += char.advance_width / 1.0;
+ }
+
+ {
+ let mut sin_surface = Surface::new(512, 256, PixelFormatEnum::RGBA32).unwrap();
+
+ let w = sin_surface.width();
+ let h = sin_surface.height();
+ let f = &mut sin_surface.deref_mut().without_lock_mut().unwrap();
+
+ for x in 0..w {
+ let f64_w = f64::from(w);
+ let f64_h = f64::from(h);
+ let sin_x = {
+ let mut sin_x = f64::from(x) + (sin_offset * f64_w);
+ if sin_x > f64_w {
+ sin_x = sin_x - f64_w;
+ }
+ sin_x
+ };
+ let sin_y =
+ ((f64::sin(sin_x * (3.141 * 2.0) / f64_w) + 1.0) * (f64_h / 2.0)).floor() as usize;
+ // let sin_idx = (sin_y * w as usize + x as usize) * 4;
+
+ for y in 0..h {
+ let idx = (y * w + x) as usize * 4;
+ f[idx] = 122 - (x / 8) as u8;
+ f[idx + 1] = 255 - (x / 2) as u8;
+ f[idx + 2] = (x / 2) as u8;
+ f[idx + 3] = if sin_y < y as usize {
+ if idx % 5 == 0 {
+ 255
+ } else {
+ 122
+ }
+ } else {
+ 0
+ };
+ }
+ }
+
+ let sin_texture = Texture::from_surface(&sin_surface, &texture_creator).unwrap();
+
+ canvas
+ .copy(&sin_texture, None, Rect::new(0, 0, 512, 256))
+ .unwrap();
+ }
+ offset = 0.0;
+ for c in "Come to Cosin25 :3".chars() {
+ let char = FONT_GALMURI.get_char(c);
+ canvas
+ .copy(
+ &char
+ .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i))
+ .unwrap(),
+ None,
+ char.to_rect(offset as i32 + 18, 16 + 36),
+ )
+ .unwrap();
+ offset += char.advance_width;
+ }
+}