Skip to content

Commit

Permalink
✨ added .first(), .last() and some QoL improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Nov 29, 2024
1 parent bca1b04 commit e30bf04
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ashura/engine/font_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct CpuFontAtlas

ImageLayerSpan<u8, 1> span() const
{
return ImageLayerSpan<u8, 1>{.channels = ::ash::span(channels),
return ImageLayerSpan<u8, 1>{.channels = ash::span(channels),
.width = extent.x,
.height = extent.y,
.layers = num_layers};
Expand Down
8 changes: 4 additions & 4 deletions ashura/engine/text_compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ struct [[nodiscard]] TextCursor

constexpr Slice32 as_slice(u32 len) const
{
u32 rfirst = (u32)::ash::clamp(first, (i64) 0, (i64) len);
u32 rlast = (u32)::ash::clamp(last, (i64) 0, (i64) len);
u32 rfirst = (u32) ash::clamp(first, (i64) 0, (i64) len);
u32 rlast = (u32) ash::clamp(last, (i64) 0, (i64) len);
if (rfirst > rlast)
{
swap(rfirst, rlast);
Expand Down Expand Up @@ -138,8 +138,8 @@ struct [[nodiscard]] TextCursor
{
return TextCursor{};
}
return TextCursor{::ash::clamp(first, (i64) 0, (i64) (len - 1)),
::ash::clamp(last, (i64) 0, (i64) (len - 1))};
return TextCursor{ash::clamp(first, (i64) 0, (i64) (len - 1)),
ash::clamp(last, (i64) 0, (i64) (len - 1))};
}
};

Expand Down
8 changes: 4 additions & 4 deletions ashura/std/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -828,17 +828,17 @@ TaskInfo to_task_info(F &frame)
fn(&frame, [](F *frame, void *mem) { new (mem) F{(F &&) (*frame)}; });

TaskInfo::Uninit uninit = [](void *f) {
F *frame = (F *) f;
F *frame = reinterpret_cast<F *>(f);
frame->~F();
};

TaskInfo::Poll poll = [](void *f) -> bool {
F *frame = (F *) f;
F *frame = reinterpret_cast<F *>(f);
return frame->poll();
};

TaskInfo::Run run = [](void *f) -> bool {
F *frame = (F *) f;
F *frame = reinterpret_cast<F *>(f);
return frame->run();
};

Expand Down Expand Up @@ -1060,7 +1060,7 @@ template <Callable F, Callable... F1, Poll P = Ready>
void once(Tuple<F, F1...> fns, P poll = {}, TaskSchedule schedule = {})
{
TaskBody body{[fns = std::move(fns)]() mutable -> bool {
::ash::fold(fns);
ash::fold(fns);
return false;
},
std::move(poll)};
Expand Down
3 changes: 1 addition & 2 deletions ashura/std/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ Result<Void, IoErr> read_file(Span<char const> path, Vec<u8> &buff);

inline Result<> path_append(Vec<char> &path, Span<char const> tail)
{
if (!path.is_empty() && path[path.size() - 1] != '/' &&
path[path.size() - 1] != '\\')
if (!path.is_empty() && path.last() != '/' && path.last() != '\\')
{
if (!path.push('/'))
{
Expand Down
113 changes: 102 additions & 11 deletions ashura/std/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ constexpr f64 to_radians(f64 degree)
return PI * degree * 0.00555555555;
}

inline f32 sqrt(f32 x)
{
return std::sqrt(x);
}

inline f64 sqrt(f64 x)
{
return std::sqrt(x);
}

constexpr f32 invsqrt(f32 x)
{
// (enable only on IEEE 754)
static_assert(std::numeric_limits<f32>::is_iec559);
f32 const y = std::bit_cast<f32>(0x5F3759DF - (std::bit_cast<u32>(x) >> 1));
return y * (1.5F - (x * 0.5F * y * y));
}

/// @brief Calculate log base 2 of an unsigned integer. Undefined behaviour if
/// value is 0
constexpr u8 ulog2(u8 value)
Expand Down Expand Up @@ -241,6 +259,87 @@ constexpr T catmull_rom(T const &p0, T const &p1, T const &p2, T const &p3,
(-p0 + 3 * p1 - 3 * p2 + p3) * t * t * t);
}

/// @brief Elastic Easing
/// @param amplitude strength of the elastic effect (default = 1.0)
/// @param period length of the oscillation (default = 0.3)
/// @note Based on Robert Penner's elastic easing
/// (http://robertpenner.com/easing/)
inline f32 elastic(f32 amplitude, f32 period, f32 t)
{
constexpr f32 TWO_PI = 2.0F * PI;
f32 const s = (period / TWO_PI) * std::asin(1 / amplitude);
f32 const factor = amplitude * std::pow(2.0F, -10.0F * t) *
std::sin((t - s) * (TWO_PI / period)) +
1.0F;
return factor;
}

/// @brief EaseOut Bounce
/// @param strength strength of the bounce effect (default = 1.0)
/// @note Based on Robert Penner's easeOutBounce
/// (http://robertpenner.com/easing/)
constexpr f32 bounce(f32 strength, f32 t)
{
// Inverse the time to create an ease-out effect
t = 1.0F - t;

if (t < (1.0F / 2.75F))
{
return 1.0F - (7.5625F * t * t * strength);
}
else if (t < (2.0F / 2.75F))
{
t -= 1.5F / 2.75F;
return 1.0F - (7.5625F * t * t * strength + 0.75F);
}
else if (t < (2.5F / 2.75F))
{
t -= 2.25F / 2.75F;
return 1.0F - (7.5625F * t * t * strength + 0.9375F);
}
else
{
t -= 2.625F / 2.75F;
return 1.0F - (7.5625F * t * t * strength + 0.984375F);
}
}

/// @brief Spring-based Elastic Easing based on simple harmonic motion with
/// damping
///
/// The default behavior is a tight spring effect, tune the parameters to give
/// a desired effect.
/// @param mass: Oscillator mass (default: 1.0)
/// @param stiffness: Spring constant (default: 20.0)
/// @param damping: Damping coefficient (default: 10.0F)
///
/// @note https://www.ryanjuckett.com/damped-springs/
///
inline f32 spring(f32 mass, f32 stiffness, f32 damping, f32 t)
{
// Calculate critical damping factors
f32 const omega0 = std::sqrt(stiffness / mass);
f32 const critical_damping = 2.0F * std::sqrt(mass * stiffness);
f32 const damping_ratio = damping / critical_damping;

// Underdamped
if (damping_ratio < 1.0F)
{
f32 const omega_d =
omega0 * std::sqrt(1.0F - damping_ratio * damping_ratio);
return 1.0F -
std::exp(-damping_ratio * omega0 * t) *
(std::cos(omega_d * t) +
(damping_ratio * omega0 / omega_d) * std::sin(omega_d * t));
}

// Overdamped or critically damped
f32 const alpha = -damping_ratio * omega0;
f32 const beta = omega0 * std::sqrt(damping_ratio * damping_ratio - 1.0F);
return 1.0F - (std::exp(alpha * t) *
(std::cosh(beta * t) + (alpha / beta) * std::sinh(beta * t)));
}

constexpr f32 step(f32 a, f32 t)
{
return (t < a) ? 0.0F : 1.0F;
Expand Down Expand Up @@ -1384,27 +1483,19 @@ constexpr Vec3I cross(Vec3I a, Vec3I b)
a.x * b.y - a.y * b.x};
}

constexpr f32 inverse_sqrt(f32 num)
{
// (enable only on IEEE 754)
static_assert(std::numeric_limits<f32>::is_iec559);
f32 const y = std::bit_cast<f32>(0x5F3759DF - (std::bit_cast<u32>(num) >> 1));
return y * (1.5F - (num * 0.5F * y * y));
}

constexpr Vec2 normalize(Vec2 a)
{
return a * inverse_sqrt(dot(a, a));
return a * invsqrt(dot(a, a));
}

constexpr Vec3 normalize(Vec3 a)
{
return a * inverse_sqrt(dot(a, a));
return a * invsqrt(dot(a, a));
}

constexpr Vec4 normalize(Vec4 a)
{
return a * inverse_sqrt(dot(a, a));
return a * invsqrt(dot(a, a));
}

struct Mat2
Expand Down
2 changes: 1 addition & 1 deletion ashura/std/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Result<> join(Span<Span<C const> const> strings, Span<C const> delimiter,
}
}

if (!out.extend_copy(strings[strings.size() - 1]))
if (!out.extend_copy(strings.last()))
{
out.resize_uninit(initial_size).unwrap();
return Err{};
Expand Down
Loading

0 comments on commit e30bf04

Please sign in to comment.