Commit 29b18757 authored by Richard Gobeille's avatar Richard Gobeille
Browse files

engine: redo vector types to use templates

parent 0ec92adc
......@@ -5,199 +5,116 @@
#include "compat.h"
typedef struct vec2_ {
int32_t x, y;
#ifdef __cplusplus
inline bool operator==(struct vec2_ const &c) { return x == c.x && y == c.y; }
inline bool operator!=(struct vec2_ const &c) { return x != c.x || y != c.y; }
template <typename T> struct vec2
{
T x, y;
struct vec2_ &operator+=(const struct vec2_& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct vec2_ &operator-=(const struct vec2_& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
inline bool operator==(vec2<T> const c) { return (x == c.x) & (y == c.y); }
inline bool operator!=(vec2<T> const c) { return (x != c.x) | (y != c.y); }
const struct vec2_ operator+(const struct vec2_ rhs) const { return { x + rhs.x, y + rhs.y }; }
const struct vec2_ operator-(const struct vec2_ rhs) const { return { x - rhs.x, y - rhs.y }; }
#endif
} MAY_ALIAS vec2_t;
vec2<T> &operator+=(const vec2<T> rhs) { x += rhs.x; y += rhs.y; return *this; }
vec2<T> &operator-=(const vec2<T> rhs) { x -= rhs.x; y -= rhs.y; return *this; }
vec2<T> &operator*=(const vec2<T> rhs) { x *= rhs.x; y *= rhs.y; return *this; }
typedef struct vec2_16_ {
int16_t x, y;
#ifdef __cplusplus
inline bool operator==(struct vec2_16_ const &c) { return x == c.x && y == c.y; }
inline bool operator!=(struct vec2_16_ const &c) { return x != c.x || y != c.y; }
struct vec2_16_ &operator+=(const struct vec2_16_& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct vec2_16_ &operator-=(const struct vec2_16_& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
const struct vec2_16_ operator+(const struct vec2_16_ rhs) const { return { (int16_t)(x + rhs.x), (int16_t)(y + rhs.y) }; }
const struct vec2_16_ operator-(const struct vec2_16_ rhs) const { return { (int16_t)(x - rhs.x), (int16_t)(y - rhs.y) }; }
#endif // __cplusplus
} MAY_ALIAS vec2_16_t;
typedef struct vec2u_ {
uint32_t x, y;
#ifdef __cplusplus
inline bool operator==(struct vec2u_ const &c) { return x == c.x && y == c.y; }
inline bool operator!=(struct vec2u_ const &c) { return x != c.x || y != c.y; }
struct vec2u_ &operator+=(const struct vec2u_& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct vec2u_ &operator-=(const struct vec2u_& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
const struct vec2u_ operator+(const struct vec2u_ rhs) const { return { x + rhs.x, y + rhs.y }; }
const struct vec2u_ operator-(const struct vec2u_ rhs) const { return { x - rhs.x, y - rhs.y }; }
#endif // __cplusplus
} vec2u_t;
typedef struct vec2f_ {
float x, y;
#ifdef __cplusplus
inline bool operator==(struct vec2f_ const &c) { return x == c.x && y == c.y; }
inline bool operator!=(struct vec2f_ const &c) { return x != c.x || y != c.y; }
struct vec2f_ &operator+=(const struct vec2f_& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct vec2f_ &operator-=(const struct vec2f_& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
const vec2<T> operator+(const vec2<T> rhs) const { return { (T)(x + rhs.x), (T)(y + rhs.y) }; }
const vec2<T> operator-(const vec2<T> rhs) const { return { (T)(x - rhs.x), (T)(y - rhs.y) }; }
const vec2<T> operator*(const vec2<T> rhs) const { return { (T)(x * rhs.x), (T)(y * rhs.y) }; }
};
const struct vec2f_ operator+(const struct vec2f_ rhs) const { return { x + rhs.x, y + rhs.y }; }
const struct vec2f_ operator-(const struct vec2f_ rhs) const { return { x - rhs.x, y - rhs.y }; }
#endif // __cplusplus
} vec2f_t;
typedef struct vec2d_ {
double x, y;
#ifdef __cplusplus
inline bool operator==(struct vec2d_ const &c) { return x == c.x && y == c.y; }
inline bool operator!=(struct vec2d_ const &c) { return x != c.x || y != c.y; }
struct vec2d_ &operator+=(const struct vec2d_& rhs) { x += rhs.x; y += rhs.y; return *this; }
struct vec2d_ &operator-=(const struct vec2d_& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
const struct vec2d_ operator+(const struct vec2d_ rhs) const { return { x + rhs.x, y + rhs.y }; }
const struct vec2d_ operator-(const struct vec2d_ rhs) const { return { x - rhs.x, y - rhs.y }; }
#endif // __cplusplus
} vec2d_t;
typedef struct vec3_ {
template <typename T> struct vec3
{
union {
struct { int32_t x, y, z; };
vec2_t xy;
struct { T x, y, z; };
struct { T d, u, v; };
vec2<T> xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec3_ const &c) { return x == c.x && y == c.y && z == c.z; }
inline bool operator!=(struct vec3_ const &c) { return x != c.x || y != c.y || z != c.z; }
struct vec3_ &operator+=(const struct vec3_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; return *this; }
struct vec3_ &operator-=(const struct vec3_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; return *this; }
struct vec3_ &operator+=(const struct vec2_& rhs) { xy += rhs; return *this; }
struct vec3_ &operator-=(const struct vec2_& rhs) { xy -= rhs; return *this; }
const struct vec3_ operator+(const struct vec3_ rhs) const { return { x + rhs.x, y + rhs.y, z + rhs.z }; }
const struct vec3_ operator-(const struct vec3_ rhs) const { return { x - rhs.x, y - rhs.y, z + rhs.z }; }
#endif // __cplusplus
} MAY_ALIAS vec3_t;
typedef struct vec3_16_ {
inline bool operator==(vec3<T> const &c) { return (x == c.x) & (y == c.y) & (z == c.z); }
inline bool operator!=(vec3<T> const &c) { return (x != c.x) | (y != c.y) | (z != c.z); }
vec3<T> &operator+=(const vec3<T>& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; return *this; }
vec3<T> &operator-=(const vec3<T>& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; return *this; }
vec3<T> &operator*=(const vec3<T>& rhs) { x *= rhs.x; y *= rhs.y; z*= rhs.z; return *this; }
const vec3<T> operator+(const vec3<T> &rhs) const { return { (T)(x + rhs.x), (T)(y + rhs.y), (T)(z + rhs.z) }; }
const vec3<T> operator-(const vec3<T> &rhs) const { return { (T)(x - rhs.x), (T)(y - rhs.y), (T)(z - rhs.z) }; }
const vec3<T> operator*(const vec3<T> &rhs) const { return { (T)(x * rhs.x), (T)(y * rhs.y), (T)(z * rhs.z) }; }
vec3<T> &operator+=(const vec2<T>& rhs) { xy += rhs; return *this; }
vec3<T> &operator-=(const vec2<T>& rhs) { xy -= rhs; return *this; }
vec3<T> &operator*=(const vec2<T>& rhs) { xy *= rhs; return *this; }
};
template <typename T> struct vec4
{
union {
struct { int16_t x, y, z; };
vec2_16_t xy;
struct { T x, y, z, a; };
struct { T d, u, v, w; };
vec3<T> xyz;
vec2<T> xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec3_16_ const &c) { return x == c.x && y == c.y && z == c.z; }
inline bool operator!=(struct vec3_16_ const &c) { return x != c.x || y != c.y || z != c.z; }
inline bool operator==(vec4<T> const &c) { return (x == c.x) & (y == c.y) & (z == c.z) & (a == c.a); }
inline bool operator!=(vec4<T> const &c) { return (x != c.x) | (y != c.y) | (z != c.z) | (a != c.a); }
vec4<T> &operator+=(const vec4<T>& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; a += rhs.a; return *this; }
vec4<T> &operator-=(const vec4<T>& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; a -= rhs.a; return *this; }
vec4<T> &operator*=(const vec4<T>& rhs) { x *= rhs.x; y *= rhs.y; z*= rhs.z; a *= rhs.a; return *this; }
struct vec3_16_ &operator+=(const struct vec3_16_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; return *this; }
struct vec3_16_ &operator-=(const struct vec3_16_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; return *this; }
const vec4<T> operator+(const vec4<T> &rhs) const { return { (T)(x + rhs.x), (T)(y + rhs.y), (T)(z + rhs.z), (T)(a + rhs.a) }; }
const vec4<T> operator-(const vec4<T> &rhs) const { return { (T)(x - rhs.x), (T)(y - rhs.y), (T)(z - rhs.z), (T)(a - rhs.a) }; }
const vec4<T> operator*(const vec4<T> &rhs) const { return { (T)(x * rhs.x), (T)(y * rhs.y), (T)(z * rhs.z), (T)(a * rhs.a) }; }
struct vec3_16_ &operator+=(const struct vec2_16_& rhs) { xy += rhs; return *this; }
struct vec3_16_ &operator-=(const struct vec2_16_& rhs) { xy -= rhs; return *this; }
vec4<T> &operator+=(const vec3<T>& rhs) { xyz += rhs; return *this; }
vec4<T> &operator-=(const vec3<T>& rhs) { xyz -= rhs; return *this; }
vec4<T> &operator*=(const vec3<T>& rhs) { xyz *= rhs; return *this; }
vec4<T> &operator+=(const vec2<T>& rhs) { xy += rhs; return *this; }
vec4<T> &operator-=(const vec2<T>& rhs) { xy -= rhs; return *this; }
vec4<T> &operator*=(const vec2<T>& rhs) { xy *= rhs; return *this; }
};
const struct vec3_16_ operator+(const struct vec3_16_ rhs) const { return { (int16_t)(x + rhs.x), (int16_t)(y + rhs.y), (int16_t)(z + rhs.z) }; }
const struct vec3_16_ operator-(const struct vec3_16_ rhs) const { return { (int16_t)(x - rhs.x), (int16_t)(y - rhs.y), (int16_t)(z + rhs.z) }; }
#endif // __cplusplus
} MAY_ALIAS vec3_16_t;
typedef struct vec2<double> vec2d_t;
typedef struct vec3<double> vec3d_t;
typedef struct vec3f_ {
union {
struct {
union { float x, d; };
union { float y, u; };
union { float z, v; };
};
vec2f_t xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec3f_ const &c) { return x == c.x && y == c.y && z == c.z; }
inline bool operator!=(struct vec3f_ const &c) { return x != c.x || y != c.y || z != c.z; }
typedef struct vec2<float> vec2f_t;
typedef struct vec3<float> vec3f_t;
typedef struct vec4<float> vec4f_t;
struct vec3f_ &operator+=(const struct vec3f_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; return *this; }
struct vec3f_ &operator-=(const struct vec3f_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; return *this; }
typedef struct vec2<int16_t> vec2_16_t;
typedef struct vec3<int16_t> vec3_16_t;
struct vec3f_ &operator+=(const struct vec2f_& rhs) { xy += rhs; return *this; }
struct vec3f_ &operator-=(const struct vec2f_& rhs) { xy -= rhs; return *this; }
typedef struct vec2<uint16_t> vec2_u16_t;
typedef struct vec3<uint16_t> vec3_u16_t;
const struct vec3f_ operator+(const struct vec3f_ rhs) const { return { x + rhs.x, y + rhs.y, z + rhs.z }; }
const struct vec3f_ operator-(const struct vec3f_ rhs) const { return { x - rhs.x, y - rhs.y, z + rhs.z }; }
#endif // __cplusplus
} vec3f_t;
typedef struct vec2<int32_t> vec2_t;
typedef struct vec3<int32_t> vec3_t;
typedef struct vec4<int32_t> vec4_t;
EDUKE32_STATIC_ASSERT(sizeof(vec3f_t) == sizeof(float) * 3);
typedef struct vec2<uint32_t> vec2u_t;
typedef struct vec3d_ {
union {
struct {
union { double x, d; };
union { double y, u; };
union { double z, v; };
};
vec2d_t xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec3d_ const &c) { return x == c.x && y == c.y && z == c.z; }
inline bool operator!=(struct vec3d_ const &c) { return x != c.x || y != c.y || z != c.z; }
#else // __cplusplus
struct vec3d_ &operator+=(const struct vec3d_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; return *this; }
struct vec3d_ &operator-=(const struct vec3d_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; return *this; }
typedef struct { int16_t x, y; } vec2_16_t;
typedef struct { union { struct { int16_t x, y, z; }; vec2_16_t xy; }; } vec3_16_t;
struct vec3d_ &operator+=(const struct vec2d_& rhs) { xy += rhs; return *this; }
struct vec3d_ &operator-=(const struct vec2d_& rhs) { xy -= rhs; return *this; }
typedef struct { int32_t x, y; } vec2_t;
typedef struct { union { struct { int32_t x, y, z; }; vec2_t xy; }; } vec3_t;
typedef struct { union { struct { int32_t x, y, z, a; }; vec3_t xyz; vec2_t xy; }; } vec4_t;
const struct vec3d_ operator+(const struct vec3d_ rhs) const { return { x + rhs.x, y + rhs.y, z + rhs.z }; }
const struct vec3d_ operator-(const struct vec3d_ rhs) const { return { x - rhs.x, y - rhs.y, z + rhs.z }; }
#endif // __cplusplus
} vec3d_t;
typedef struct { uint32_t x, y; } vec2u_t;
typedef struct { float x, y; } vec2f_t;
typedef struct { double x, y; } vec2d_t;
EDUKE32_STATIC_ASSERT(sizeof(vec3d_t) == sizeof(double) * 3);
typedef struct { union { union { struct { float x, y, z; }; struct { float d, u, v; }; }; vec2f_t xy; }; } vec3f_t;
typedef struct { union { union { struct { double x, y, z; }; struct { double d, u, v; }; }; vec2d_t xy; }; } vec3d_t;
typedef struct { union { union { struct { float x, y, z, w; }; struct { float d, u, v; }; }; vec3f_t xyz; vec2f_t xy; }; } vec4f_t;
typedef struct vec4_ {
union {
struct { int32_t x, y, z, a; };
vec3_t xyz;
vec2_t xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec4_ const &c) { return x == c.x && y == c.y && z == c.z && a == c.a; }
inline bool operator!=(struct vec4_ const &c) { return x != c.x || y != c.y || z != c.z || a != c.a; }
struct vec4_ &operator+=(const struct vec4_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; a += rhs.a; return *this; }
struct vec4_ &operator-=(const struct vec4_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; a -= rhs.a; return *this; }
#endif // __cplusplus
} vec4_t;
typedef struct vec4f_ {
union {
struct {
union { float x, d; };
union { float y, u; };
union { float z, v; };
float w;
};
vec3f_t xyz;
vec2f_t xy;
};
#ifdef __cplusplus
inline bool operator==(struct vec4f_ const &c) { return x == c.x && y == c.y && z == c.z && w == c.w; }
inline bool operator!=(struct vec4f_ const &c) { return x != c.x || y != c.y || z != c.z || w != c.w; }
struct vec4f_ &operator+=(const struct vec4f_& rhs) { x += rhs.x; y += rhs.y; z+= rhs.z; w += rhs.w; return *this; }
struct vec4f_ &operator-=(const struct vec4f_& rhs) { x -= rhs.x; y -= rhs.y; z-= rhs.z; w -= rhs.w; return *this; }
#endif // __cplusplus
} vec4f_t;
// basic sanity checks
EDUKE32_STATIC_ASSERT(sizeof(vec2_t) == sizeof(int32_t) * 2);
EDUKE32_STATIC_ASSERT(sizeof(vec3_t) == sizeof(int32_t) * 3);
EDUKE32_STATIC_ASSERT(sizeof(vec3f_t) == sizeof(float) * 3);
EDUKE32_STATIC_ASSERT(sizeof(vec3d_t) == sizeof(double) * 3);
#endif // vec_h__
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment