move typedefs to twn_types.h, specify that twn_vec.h is C user only, remove Vec2sh

This commit is contained in:
veclav talica 2024-10-07 15:55:53 +03:00
parent 8463ed4440
commit e2f5d44959
2 changed files with 42 additions and 53 deletions

View File

@ -39,4 +39,42 @@ typedef struct Matrix4 {
Vec4 row[4]; Vec4 row[4];
} Matrix4; } Matrix4;
/* a point in some space (integer) */
typedef struct Vec2i {
_Alignas(8)
int32_t x;
int32_t y;
} Vec2i;
/* a point in some space (floating point) */
typedef struct Vec2 {
_Alignas(8)
float x;
float y;
} Vec2;
/* a point in some three dimension space (floating point) */
/* y goes up, x goes to the right */
typedef struct Vec3 {
_Alignas(16)
float x;
float y;
float z;
} Vec3;
/* a point in some three dimension space (floating point) */
/* y goes up, x goes to the right */
typedef struct Vec4 {
_Alignas(16)
float x;
float y;
float z;
float w;
} Vec4;
#endif #endif

View File

@ -1,55 +1,14 @@
#ifndef TWN_VEC_H #ifndef TWN_VEC_H
#define TWN_VEC_H #define TWN_VEC_H
/* vector ops for C users */
#include "twn_types.h"
#include <stdint.h> #include <stdint.h>
#include <math.h> #include <math.h>
/* a point in some space (integer) */
typedef struct Vec2i {
_Alignas(8)
int32_t x;
int32_t y;
} Vec2i;
/* a point in some space (floating point) */
typedef struct Vec2 {
_Alignas(8)
float x;
float y;
} Vec2;
/* a point in some three dimension space (floating point) */
/* y goes up, x goes to the right */
typedef struct Vec3 {
_Alignas(16)
float x;
float y;
float z;
} Vec3;
/* a point in some three dimension space (floating point) */
/* y goes up, x goes to the right */
typedef struct Vec4 {
_Alignas(16)
float x;
float y;
float z;
float w;
} Vec4;
/* a point in some space (short) */
typedef struct Vec2sh {
_Alignas(4)
int16_t x;
int16_t y;
} Vec2sh;
/* aren't macros to prevent double evaluation with side effects */ /* aren't macros to prevent double evaluation with side effects */
/* maybe could be inlined? i hope LTO will resolve this */ /* maybe could be inlined? i hope LTO will resolve this */
static inline Vec2 vec2_from_vec2i(Vec2i vec) { static inline Vec2 vec2_from_vec2i(Vec2i vec) {
@ -59,13 +18,6 @@ static inline Vec2 vec2_from_vec2i(Vec2i vec) {
}; };
} }
static inline Vec2 vec2_from_vec2sh(Vec2sh vec) {
return (Vec2) {
.x = (float)vec.x,
.y = (float)vec.y,
};
}
static inline Vec3 vec3_add(Vec3 a, Vec3 b) { static inline Vec3 vec3_add(Vec3 a, Vec3 b) {
return (Vec3) { a.x + b.x, a.y + b.y, a.z + b.z }; return (Vec3) { a.x + b.x, a.y + b.y, a.z + b.z };
} }
@ -133,7 +85,6 @@ static inline Vec3 vec3_rotate(Vec3 v, float angle, Vec3 axis) {
#define m_vec2_from(p_any_vec2) (_Generic((p_any_vec2), \ #define m_vec2_from(p_any_vec2) (_Generic((p_any_vec2), \
Vec2i: vec2_from_vec2i, \ Vec2i: vec2_from_vec2i, \
Vec2sh: vec2_from_vec2sh \
)(p_any_vec2)) )(p_any_vec2))
#define m_vec_sub(p_any_vec0, p_any_vec1) (_Generic((p_any_vec0), \ #define m_vec_sub(p_any_vec0, p_any_vec1) (_Generic((p_any_vec0), \