117 lines
3.0 KiB
C
117 lines
3.0 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "dusk.h"
|
|
#include "util/math.h"
|
|
|
|
/**
|
|
* Signed 16-bit integer used to represent normalised directional values.
|
|
* Range: -SHORT_MAX to SHORT_MAX (-32767 to 32767).
|
|
* INT16_MIN (-32768) is intentionally excluded to keep the range symmetric.
|
|
*/
|
|
typedef int16_t short_t;
|
|
|
|
#define SHORT_MAX ((short_t)INT16_MAX)
|
|
#define SHORT_MIN ((short_t)-INT16_MAX)
|
|
#define SHORT_ZERO ((short_t)0)
|
|
|
|
/**
|
|
* Converts a float in [-1, 1] to a short_t.
|
|
*
|
|
* @param f Float value in [-1, 1].
|
|
* @returns Scaled short_t in [-SHORT_MAX, SHORT_MAX].
|
|
*/
|
|
#define shortFromFloat(f) \
|
|
((short_t)mathClamp((int32_t)((f) * (float_t)SHORT_MAX), SHORT_MIN, SHORT_MAX))
|
|
|
|
/**
|
|
* Converts a short_t to a float in [-1, 1].
|
|
*
|
|
* @param s short_t value.
|
|
* @returns Float in [-1, 1].
|
|
*/
|
|
#define shortToFloat(s) ((float_t)(s) / (float_t)SHORT_MAX)
|
|
|
|
/**
|
|
* Adds two short_t values, clamping to [-SHORT_MAX, SHORT_MAX].
|
|
*
|
|
* @param a First operand.
|
|
* @param b Second operand.
|
|
* @returns Clamped a + b.
|
|
*/
|
|
#define shortAdd(a, b) \
|
|
((short_t)mathClamp((int32_t)(a) + (int32_t)(b), SHORT_MIN, SHORT_MAX))
|
|
|
|
/**
|
|
* Subtracts two short_t values, clamping to [-SHORT_MAX, SHORT_MAX].
|
|
*
|
|
* @param a First operand.
|
|
* @param b Second operand.
|
|
* @returns Clamped a - b.
|
|
*/
|
|
#define shortSub(a, b) \
|
|
((short_t)mathClamp((int32_t)(a) - (int32_t)(b), SHORT_MIN, SHORT_MAX))
|
|
|
|
/**
|
|
* Negates a short_t, clamping to [-SHORT_MAX, SHORT_MAX].
|
|
*
|
|
* @param s Value to negate.
|
|
* @returns Clamped -s.
|
|
*/
|
|
#define shortNeg(s) ((short_t)mathClamp(-(int32_t)(s), SHORT_MIN, SHORT_MAX))
|
|
|
|
/**
|
|
* Returns the absolute value of a short_t.
|
|
*
|
|
* @param s Value.
|
|
* @returns |s|, in [0, SHORT_MAX].
|
|
*/
|
|
#define shortAbs(s) ((short_t)((s) < 0 ? -(s) : (s)))
|
|
|
|
/**
|
|
* Clamps a short_t between lo and hi.
|
|
*
|
|
* @param s Value to clamp.
|
|
* @param lo Lower bound.
|
|
* @param hi Upper bound.
|
|
* @returns Clamped value.
|
|
*/
|
|
#define shortClamp(s, lo, hi) ((short_t)mathClamp((int32_t)(s), (int32_t)(lo), (int32_t)(hi)))
|
|
|
|
/**
|
|
* Returns the sine of an angle as a short_t.
|
|
* Angle is a binary angle: the full uint16_t range (0..65535) = one full
|
|
* rotation. Key values: 0=0°, 16384=90°, 32767≈180°, -16384=270°.
|
|
* Result is in [-SHORT_MAX, SHORT_MAX].
|
|
*
|
|
* @param angle Binary angle.
|
|
* @returns Sine scaled to [-SHORT_MAX, SHORT_MAX].
|
|
*/
|
|
short_t shortSin(short_t angle);
|
|
|
|
/**
|
|
* Returns the cosine of an angle as a short_t.
|
|
* Same angle convention as shortSin.
|
|
*
|
|
* @param angle Binary angle.
|
|
* @returns Cosine scaled to [-SHORT_MAX, SHORT_MAX].
|
|
*/
|
|
short_t shortCos(short_t angle);
|
|
|
|
/**
|
|
* Linearly interpolates between two short_t values.
|
|
* t is a short_t in [0, SHORT_MAX] representing 0..1.
|
|
*
|
|
* @param a Start value.
|
|
* @param b End value.
|
|
* @param t Interpolation factor in [0, SHORT_MAX].
|
|
* @returns Interpolated short_t.
|
|
*/
|
|
#define shortLerp(a, b, t) \
|
|
((short_t)((int32_t)(a) + (((int32_t)((b) - (a)) * (int32_t)(t)) / SHORT_MAX)))
|