OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
43
44#ifndef OPENSNES_MATH_H
45#define OPENSNES_MATH_H
46
47#include <snes/types.h>
48
49/*============================================================================
50 * Fixed-Point Type and Macros
51 *============================================================================*/
52
59typedef s16 fixed;
60
71#define FIX(x) ((fixed)((x) << 8))
72
83#define UNFIX(x) ((s16)((x) >> 8))
84
95#define UNFIX_ROUND(x) ((s16)(((x) + 128) >> 8))
96
102#define FIX_FRAC(x) ((u8)((x) & 0xFF))
103
115#define FIX_MAKE(i, f) ((fixed)(((i) << 8) | (f)))
116
117/*============================================================================
118 * Fixed-Point Arithmetic
119 *============================================================================*/
120
144
161
162/*============================================================================
163 * Trigonometry (Lookup Tables)
164 *============================================================================*/
165
179extern const s16 sine_table[256];
180
182 return sine_table[angle];
183}
184
198 /* cos(x) = sin(x + 90°) = sin(x + 64) */
199 return sine_table[(u8)(angle + 64)];
200}
201
202/*============================================================================
203 * Integer Math (Safe Alternatives)
204 *============================================================================*/
205
222
236u16 div16(u16 dividend, u16 divisor);
237
249u16 mod16(u16 dividend, u16 divisor);
250
251/*============================================================================
252 * Utility Functions
253 *============================================================================*/
254
262
272
288
289/*============================================================================
290 * Square Root and Inverse Trigonometry
291 *============================================================================*/
292
316
342
380
381/*============================================================================
382 * Easing Curves (B6 final, 2026-05-22)
383 *============================================================================*/
384
397extern const u8 ease_quad_table[256];
398
415inline u8 ease_in_quad(u8 t) {
416 return ease_quad_table[t];
417}
418
427inline u8 ease_out_quad(u8 t) {
428 return 255 - ease_quad_table[255 - t];
429}
430
431#endif /* OPENSNES_MATH_H */
static u16 b
Definition main.c:157
static u16 a
Definition main.c:157
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:50
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
fixed fixCos(u8 angle)
Get cosine value for angle.
Definition math.h:197
const u8 ease_quad_table[256]
256-byte quadratic easing LUT — i² normalised to [0, 255]
fixed fixClamp(fixed x, fixed min, fixed max)
Clamp value to range.
fixed fixSqrt(fixed x)
Square root in 8.8 fixed-point.
u16 div16(u16 dividend, u16 divisor)
Safe 16-bit division.
fixed fixLerp(fixed a, fixed b, u8 t)
Linear interpolation.
u8 ease_in_quad(u8 t)
Ease-in quadratic: t² curve, output [0, 255].
Definition math.h:415
u8 ease_out_quad(u8 t)
Ease-out quadratic: 1 - (1-t)² curve, output [0, 255].
Definition math.h:427
u16 sqrt16(u16 n)
Integer square root.
fixed fixAbs(fixed x)
Absolute value of fixed-point.
const s16 sine_table[256]
Get sine value for angle.
fixed fixMul(fixed a, fixed b)
Multiply two fixed-point values.
fixed fixSin(u8 angle)
Definition math.h:181
u8 atan2_8(s16 dy, s16 dx)
8-bit two-argument arctangent
s16 fixed
8.8 signed fixed-point type
Definition math.h:59
fixed fixDiv(fixed a, fixed b)
Divide two fixed-point values.
u16 mod16(u16 dividend, u16 divisor)
Get remainder of division.
u16 mul16(u16 a, u16 b)
Safe 16-bit multiplication.
static u16 angle
Rotation step index, 0..47.
Definition main.c:53
OpenSNES Standard Types.