Skip to content

Commit

Permalink
Some random work on animations and mathematics
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Jul 10, 2024
1 parent 3f10610 commit 80f294d
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 144 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ bitflags = "2.1.0"
common = { package = "surreal-common", path = "./common" }
editor = { package = "surreal-editor", path = "./editor", optional = true }

# backends
# framework
sdl = { package = "surreal-backend-sdl", path = "./backends/sdl", optional = true }

# modules
audio = { package = "surreal-audio", path = "./modules/audio", optional = true }
graphics = { package = "surreal-graphics", path = "./modules/graphics", optional = true }
input = { package = "surreal-input", path = "./modules/input", optional = true }
scripting = { package = "surreal-scripting", path = "./modules/scripting", optional = true }

[[example]]
name = "hello-world"
required-features = ["sdl", "graphics"]

[[example]]
name = "sprites"
required-features = ["sdl", "graphics"]
2 changes: 1 addition & 1 deletion common/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ pub type FastMultiMap<K, V> = MultiMap<K, V, BuildHasherDefault<rustc_hash::FxHa
pub type FastAnyMap = AnyMap<BuildHasherDefault<rustc_hash::FxHasher>>;

/// A faster spatial hash grid that is not resilient to DoS attacks.
pub type FastSpatialHashGrid<T> = SpatialHashGrid<T, BuildHasherDefault<rustc_hash::FxHasher>>;
pub type FastSpatialHashMap<T> = SpatialHashMap<T, BuildHasherDefault<rustc_hash::FxHasher>>;
30 changes: 29 additions & 1 deletion common/src/maths/lerp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Scalar;
use super::{Quat, Scalar, Vec2, Vec3};

/// Allows linear interpolation of arbitrary values.
pub trait Lerp {
Expand All @@ -15,6 +15,34 @@ impl<T: Scalar> Lerp for T {
}
}

impl Lerp for Vec2 {
#[inline(always)]
fn lerp(a: Self, b: Self, t: f32) -> Self {
let x = f32::lerp(a.x, b.x, t);
let y = f32::lerp(a.y, b.y, t);

Self::new(x, y)
}
}

impl Lerp for Vec3 {
#[inline(always)]
fn lerp(a: Self, b: Self, t: f32) -> Self {
let x = f32::lerp(a.x, b.x, t);
let y = f32::lerp(a.y, b.y, t);
let z = f32::lerp(a.z, b.z, t);

Self::new(x, y, z)
}
}

impl Lerp for Quat {
#[inline(always)]
fn lerp(a: Self, b: Self, t: f32) -> Self {
a.slerp(b, t)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions common/src/maths/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ mod planes;
mod rays;
mod scalars;
mod vectors;

/// Represents a numerical space with identity constants
pub trait Identity {
const ZERO: Self;
const ONE: Self;
const MIN: Self;
const MAX: Self;
}
18 changes: 9 additions & 9 deletions common/src/maths/linear/scalars.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use crate::Identity;

/// Represents a scalar type that allows standard equality and arithmetic.
pub trait Scalar:
Copy
+ Default
+ Identity
+ PartialOrd
+ PartialEq
+ Add<Output = Self>
Expand All @@ -16,11 +19,6 @@ pub trait Scalar:
+ DivAssign
+ Sized
{
const ZERO: Self;
const ONE: Self;
const MIN: Self;
const MAX: Self;

/// Converts a value from a 32-bit floating point number.
fn from_f32(value: f32) -> Self;

Expand All @@ -33,13 +31,15 @@ pub trait Scalar:

/// Implements the numeric traits for standard purpose a numeric type.
macro_rules! impl_scalar {
($type:ty) => {
impl Scalar for $type {
($name:ty) => {
impl Identity for $name {
const ZERO: Self = 0 as Self;
const ONE: Self = 1 as Self;
const MIN: Self = <$type>::MIN;
const MAX: Self = <$type>::MAX;
const MIN: Self = <$name>::MIN;
const MAX: Self = <$name>::MAX;
}

impl Scalar for $name {
#[inline(always)]
fn from_f32(value: f32) -> Self {
value as Self
Expand Down
10 changes: 4 additions & 6 deletions common/src/maths/linear/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub trait Vector:
Copy
+ Clone
+ Default
+ Identity
+ Add<Output = Self>
+ AddAssign
+ Add<Self::Scalar, Output = Self>
Expand All @@ -46,11 +47,6 @@ pub trait Vector:
+ DivAssign<Self::Scalar>
+ Sized
{
const ZERO: Self;
const ONE: Self;
const MIN: Self;
const MAX: Self;

/// The type of the space that this vector is in.
type Space: Space;

Expand All @@ -60,12 +56,14 @@ pub trait Vector:

macro_rules! impl_vector {
($name:ident, $space:ident, $scalar:ident) => {
impl Vector for $name {
impl Identity for $name {
const ZERO: Self = Self::splat($scalar::ZERO);
const ONE: Self = Self::splat($scalar::ONE);
const MIN: Self = Self::splat($scalar::MIN);
const MAX: Self = Self::splat($scalar::MAX);
}

impl Vector for $name {
type Space = $space;
type Scalar = $scalar;
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/maths/time/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl DeltaClock {
let delta_time = current_time - self.last_time;

self.last_time = current_time;
self.last_delta_time = delta_time.total_seconds().min(self.max_delta_time);
self.last_delta_time = delta_time.as_seconds().min(self.max_delta_time);

self.last_delta_time
}
Expand All @@ -47,6 +47,6 @@ impl DeltaClock {
pub fn total_time(&self) -> f32 {
let now = TimeStamp::now();

(now - self.start_time).total_seconds()
(now - self.start_time).as_seconds()
}
}
2 changes: 1 addition & 1 deletion common/src/maths/time/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl IntervalTimer {

pub fn tick(&mut self, delta_time: f32) -> bool {
self.time_elapsed += delta_time;
self.time_elapsed >= self.interval.total_seconds()
self.time_elapsed >= self.interval.as_seconds()
}

pub fn reset(&mut self) {
Expand Down
38 changes: 19 additions & 19 deletions common/src/maths/time/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ impl TimeSpan {
}

#[inline]
pub fn total_millis(&self) -> f32 {
self.total_seconds() * 1000.
pub fn as_millis(&self) -> f32 {
self.as_seconds() * 1000.
}

#[inline]
pub fn total_seconds(&self) -> f32 {
pub fn as_seconds(&self) -> f32 {
self.seconds
}

#[inline]
pub fn total_minutes(&self) -> f32 {
self.total_seconds() / 60.
pub fn as_minutes(&self) -> f32 {
self.as_seconds() / 60.
}

#[inline]
pub fn total_hours(&self) -> f32 {
self.total_minutes() / 60.
pub fn as_hours(&self) -> f32 {
self.as_minutes() / 60.
}

#[inline]
pub fn total_days(&self) -> f32 {
self.total_hours() / 24.
pub fn as_days(&self) -> f32 {
self.as_hours() / 24.
}
}

Expand Down Expand Up @@ -145,11 +145,11 @@ impl From<TimeSpan> for Duration {
impl Display for TimeSpan {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
match self {
_ if self.total_days() > 1. => write!(formatter, "{} days", self.total_days()),
_ if self.total_hours() > 1. => write!(formatter, "{} hours", self.total_hours()),
_ if self.total_minutes() > 1. => write!(formatter, "{} minutes", self.total_minutes()),
_ if self.total_seconds() > 1. => write!(formatter, "{} seconds", self.total_seconds()),
_ => write!(formatter, "{} milliseconds", self.total_millis()),
_ if self.as_days() > 1. => write!(formatter, "{} days", self.as_days()),
_ if self.as_hours() > 1. => write!(formatter, "{} hours", self.as_hours()),
_ if self.as_minutes() > 1. => write!(formatter, "{} minutes", self.as_minutes()),
_ if self.as_seconds() > 1. => write!(formatter, "{} seconds", self.as_seconds()),
_ => write!(formatter, "{} milliseconds", self.as_millis()),
}
}
}
Expand Down Expand Up @@ -216,35 +216,35 @@ mod tests {
fn test_time_span_from_millis() {
let time_span = TimeSpan::from_millis(100.0);

assert_eq!(time_span.total_millis(), 100.0);
assert_eq!(time_span.as_millis(), 100.0);
}

#[test]
fn test_time_span_from_seconds() {
let time_span = TimeSpan::from_seconds(2.5);

assert_eq!(time_span.total_millis(), 2500.0);
assert_eq!(time_span.as_millis(), 2500.0);
}

#[test]
fn test_time_span_from_minutes() {
let time_span = TimeSpan::from_minutes(1.5);

assert_eq!(time_span.total_millis(), 90000.0);
assert_eq!(time_span.as_millis(), 90000.0);
}

#[test]
fn test_time_span_from_hours() {
let time_span = TimeSpan::from_hours(0.5);

assert_eq!(time_span.total_millis(), 1800000.0);
assert_eq!(time_span.as_millis(), 1800000.0);
}

#[test]
fn test_u8_into_time_span() {
let value: u8 = 10;
let time_span = value.milliseconds();

assert_eq!(time_span.total_millis(), 10.0);
assert_eq!(time_span.as_millis(), 10.0);
}
}
2 changes: 0 additions & 2 deletions editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
pub use documents::*;
pub use hosting::*;
pub use projects::*;
pub use windows::*;

mod documents;
mod hosting;
mod projects;
mod windows;
2 changes: 2 additions & 0 deletions editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use surreal_editor::*;

mod windows;

fn main() {
start_editor(EditorConfig {
hosting_mode: HostingModel::OutOfProcess {
Expand Down
39 changes: 0 additions & 39 deletions examples/sprites.rs

This file was deleted.

Loading

0 comments on commit 80f294d

Please sign in to comment.