mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
apply relevant attributes to relevant funcitons
This commit is contained in:
10
src/error.h
10
src/error.h
@@ -28,10 +28,10 @@ enum {
|
||||
};
|
||||
typedef int8_t gamestatus;
|
||||
|
||||
void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored.
|
||||
void info(char const*, ...); // prints an info message to stdout
|
||||
void warn(char const*, ...); // prints a warning message to stderr
|
||||
void error(char const*, ...); // prints an warning message to stderr
|
||||
__attribute__((format(printf, 1, 2))) void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored.
|
||||
__attribute__((format(printf, 1, 2))) void info(char const*, ...); // prints an info message to stdout
|
||||
__attribute__((format(printf, 1, 2))) void warn(char const*, ...); // prints a warning message to stderr
|
||||
__attribute__((format(printf, 1, 2))) void error(char const*, ...); // prints an warning message to stderr
|
||||
|
||||
// prints an error message to stderr before exiting
|
||||
noreturn void fatal(gamestatus, char const* file_name, uint32_t line, char const* fmt, ...);
|
||||
__attribute__((format(printf, 4, 5))) noreturn void fatal(gamestatus, char const* file_name, uint32_t line, char const* fmt, ...);
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../util/attributes.h"
|
||||
|
||||
typedef struct {
|
||||
struct timespec ts; // stores the time at the current update
|
||||
double sec; // stores the current time in seconds
|
||||
@@ -11,7 +13,7 @@ typedef struct {
|
||||
} gametime;
|
||||
|
||||
// initializes the gametime struct
|
||||
static inline gametime gametime_new(void) {
|
||||
atrb_const static inline gametime gametime_new(void) {
|
||||
struct timespec ts;
|
||||
timespec_get(&ts, TIME_UTC);
|
||||
|
||||
@@ -37,6 +39,6 @@ static inline void gametime_update(gametime* gt) {
|
||||
}
|
||||
|
||||
// gets how many times the game updates per second
|
||||
static inline float gametime_get_ups(gametime* gt) {
|
||||
atrb_const static inline float gametime_get_ups(gametime* gt) {
|
||||
return 1.0F / gt->delta;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
|
||||
// stores a 2D point using a floating-point number
|
||||
typedef struct {
|
||||
float x;
|
||||
@@ -9,57 +11,57 @@ typedef struct {
|
||||
} float2;
|
||||
|
||||
// adds one float2 to another
|
||||
static inline float2 float2_add(float2 v1, float2 v2) {
|
||||
atrb_const static inline float2 float2_add(float2 v1, float2 v2) {
|
||||
return (float2){v1.x + v2.x, v1.y + v2.y};
|
||||
}
|
||||
|
||||
// subtracts one float2 from another
|
||||
static inline float2 float2_sub(float2 v1, float2 v2) {
|
||||
atrb_const static inline float2 float2_sub(float2 v1, float2 v2) {
|
||||
return (float2){v1.x - v2.x, v1.y - v2.y};
|
||||
}
|
||||
|
||||
// multiplies one float2 by another
|
||||
static inline float2 float2_mul(float2 v1, float2 v2) {
|
||||
atrb_const static inline float2 float2_mul(float2 v1, float2 v2) {
|
||||
return (float2){v1.x * v2.x, v1.y * v2.y};
|
||||
}
|
||||
|
||||
// divides one float2 by another
|
||||
static inline float2 float2_div(float2 v1, float2 v2) {
|
||||
atrb_const static inline float2 float2_div(float2 v1, float2 v2) {
|
||||
return (float2){v1.x / v2.x, v1.y / v2.y};
|
||||
}
|
||||
|
||||
// preforms a scalar multiplication upon the float2 (multiplies the float2 by some value)
|
||||
static inline float2 float2_mul_s(float2 v, float n) {
|
||||
atrb_const static inline float2 float2_mul_s(float2 v, float n) {
|
||||
return (float2){v.x * n, v.y * n};
|
||||
}
|
||||
|
||||
// preforms a scalar division upon the float2 (divides the float2 by some value)
|
||||
static inline float2 float2_div_s(float2 v, float n) {
|
||||
atrb_const static inline float2 float2_div_s(float2 v, float n) {
|
||||
return (float2){v.x / n, v.y / n};
|
||||
}
|
||||
|
||||
// negates the float2 (-v)
|
||||
static inline float2 float2_neg(float2 v) {
|
||||
atrb_const static inline float2 float2_neg(float2 v) {
|
||||
return (float2){-v.x, -v.y};
|
||||
}
|
||||
|
||||
// gets the squared magnitude/length of float2
|
||||
static inline float float2_mag2(float2 v) {
|
||||
atrb_const static inline float float2_mag2(float2 v) {
|
||||
return (v.x * v.x) + (v.y * v.y);
|
||||
}
|
||||
|
||||
// gets the length of float2 (length)
|
||||
static inline float float2_mag(float2 v) {
|
||||
atrb_const static inline float float2_mag(float2 v) {
|
||||
return sqrtf(float2_mag2(v));
|
||||
}
|
||||
|
||||
// normalizes the float2
|
||||
static inline float2 float2_norm(float2 v) {
|
||||
atrb_const static inline float2 float2_norm(float2 v) {
|
||||
float s = 1.0F / float2_mag(v); // get the scaling factor
|
||||
return float2_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
|
||||
}
|
||||
|
||||
// gets the dot product of two float2s
|
||||
static inline float float2_dot(float2 v1, float2 v2) {
|
||||
atrb_const static inline float float2_dot(float2 v1, float2 v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
|
||||
// stores a 3D point using a floating-point number
|
||||
typedef struct {
|
||||
float x;
|
||||
@@ -10,56 +12,56 @@ typedef struct {
|
||||
} float3;
|
||||
|
||||
// adds one float3 to another
|
||||
static inline float3 float3_add(float3 v1, float3 v2) {
|
||||
atrb_const static inline float3 float3_add(float3 v1, float3 v2) {
|
||||
return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
|
||||
}
|
||||
|
||||
// subtracts one float3 from another
|
||||
static inline float3 float3_sub(float3 v1, float3 v2) {
|
||||
atrb_const static inline float3 float3_sub(float3 v1, float3 v2) {
|
||||
return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
|
||||
}
|
||||
|
||||
// multiplies one float3 by another
|
||||
static inline float3 float3_mul(float3 v1, float3 v2) {
|
||||
atrb_const static inline float3 float3_mul(float3 v1, float3 v2) {
|
||||
return (float3){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z};
|
||||
}
|
||||
|
||||
// divides one float3 by another
|
||||
static inline float3 float3_div(float3 v1, float3 v2) {
|
||||
atrb_const static inline float3 float3_div(float3 v1, float3 v2) {
|
||||
return (float3){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z};
|
||||
}
|
||||
|
||||
// preforms a scalar multiplication upon the float3 (multiplies the float3 by some value)
|
||||
static inline float3 float3_mul_s(float3 v, float n) {
|
||||
atrb_const static inline float3 float3_mul_s(float3 v, float n) {
|
||||
return (float3){v.x * n, v.y * n, v.z * n};
|
||||
}
|
||||
|
||||
// preforms a scalar division upon the float3 (divides the float3 by some value)
|
||||
static inline float3 float3_div_s(float3 v, float n) {
|
||||
atrb_const static inline float3 float3_div_s(float3 v, float n) {
|
||||
return (float3){v.x / n, v.y / n, v.z / n};
|
||||
}
|
||||
|
||||
static inline float3 float3_neg(float3 v) {
|
||||
atrb_const static inline float3 float3_neg(float3 v) {
|
||||
return (float3){-v.x, -v.y, -v.z};
|
||||
}
|
||||
|
||||
// gets the squared magnitude/length of float3
|
||||
static inline float float3_mag2(float3 v) {
|
||||
atrb_const static inline float float3_mag2(float3 v) {
|
||||
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
|
||||
}
|
||||
|
||||
// gets the length of float3 (length)
|
||||
static inline float float3_mag(float3 v) {
|
||||
atrb_const static inline float float3_mag(float3 v) {
|
||||
return sqrtf(float3_mag2(v));
|
||||
}
|
||||
|
||||
// normalizes the float3
|
||||
static inline float3 float3_norm(float3 v) {
|
||||
atrb_const static inline float3 float3_norm(float3 v) {
|
||||
float s = 1.0F / float3_mag(v); // get the scaling factor
|
||||
return float3_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
|
||||
}
|
||||
|
||||
// gets the dot product of two float3s
|
||||
static inline float float3_dot(float3 v1, float3 v2) {
|
||||
atrb_const static inline float float3_dot(float3 v1, float3 v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
|
||||
// stores a 3D point using a floating-point number
|
||||
typedef struct {
|
||||
float x;
|
||||
@@ -11,56 +13,56 @@ typedef struct {
|
||||
} float4;
|
||||
|
||||
// adds one float4 to another
|
||||
static inline float4 float4_add(float4 v1, float4 v2) {
|
||||
atrb_const static inline float4 float4_add(float4 v1, float4 v2) {
|
||||
return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w};
|
||||
}
|
||||
|
||||
// subtracts one float4 from another
|
||||
static inline float4 float4_sub(float4 v1, float4 v2) {
|
||||
atrb_const static inline float4 float4_sub(float4 v1, float4 v2) {
|
||||
return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w};
|
||||
}
|
||||
|
||||
// multiplies one float4 by another
|
||||
static inline float4 float4_mul(float4 v1, float4 v2) {
|
||||
atrb_const static inline float4 float4_mul(float4 v1, float4 v2) {
|
||||
return (float4){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w};
|
||||
}
|
||||
|
||||
// divides one float4 by another
|
||||
static inline float4 float4_div(float4 v1, float4 v2) {
|
||||
atrb_const static inline float4 float4_div(float4 v1, float4 v2) {
|
||||
return (float4){v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w};
|
||||
}
|
||||
|
||||
// preforms a scalar multiplication upon the float4 (multiplies the float4 by some value)
|
||||
static inline float4 float4_mul_s(float4 v, float n) {
|
||||
atrb_const static inline float4 float4_mul_s(float4 v, float n) {
|
||||
return (float4){v.x * n, v.y * n, v.z * n, v.w * n};
|
||||
}
|
||||
|
||||
// preforms a scalar division upon the float4 (divides the float4 by some value)
|
||||
static inline float4 float4_div_s(float4 v, float n) {
|
||||
atrb_const static inline float4 float4_div_s(float4 v, float n) {
|
||||
return (float4){v.x / n, v.y / n, v.z / n, v.w / n};
|
||||
}
|
||||
|
||||
static inline float4 float4_neg(float4 v) {
|
||||
atrb_const static inline float4 float4_neg(float4 v) {
|
||||
return (float4){-v.x, -v.y, -v.z, -v.w};
|
||||
}
|
||||
|
||||
// gets the squared magnitude/length of float4
|
||||
static inline float float4_mag2(float4 v) {
|
||||
atrb_const static inline float float4_mag2(float4 v) {
|
||||
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w);
|
||||
}
|
||||
|
||||
// gets the length of float4 (length)
|
||||
static inline float float4_mag(float4 v) {
|
||||
atrb_const static inline float float4_mag(float4 v) {
|
||||
return sqrtf(float4_mag2(v));
|
||||
}
|
||||
|
||||
// normalizes the float4
|
||||
static inline float4 float4_norm(float4 v) {
|
||||
atrb_const static inline float4 float4_norm(float4 v) {
|
||||
float s = 1.0F / float4_mag(v); // get the scaling factor
|
||||
return float4_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
|
||||
}
|
||||
|
||||
// gets the dot product of two float4s
|
||||
static inline float float4_dot(float4 v1, float4 v2) {
|
||||
atrb_const static inline float float4_dot(float4 v1, float4 v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <math.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "float3.h"
|
||||
#include "float4.h"
|
||||
|
||||
@@ -14,7 +15,7 @@ static inline float4 quat_from_float3(float3 v) {
|
||||
}
|
||||
|
||||
// converts euler angles into quaternion (ordered roll, pitch, yaw) (in radians)
|
||||
static inline float4 quat_from_euler(float3 euler) {
|
||||
atrb_const static inline float4 quat_from_euler(float3 euler) {
|
||||
euler = float3_mul_s(euler, 0.5F); // half the angles due to quaternions using θ/2 in the formula
|
||||
float cx = cosf(euler.x), sx = sinf(euler.x);
|
||||
float cy = cosf(euler.y), sy = sinf(euler.y);
|
||||
@@ -29,7 +30,7 @@ static inline float4 quat_from_euler(float3 euler) {
|
||||
}
|
||||
|
||||
// converts quaternion into euler angles (ordered as roll, pitch, yaw)
|
||||
static inline float3 quat_to_euler(float4 q) {
|
||||
atrb_const static inline float3 quat_to_euler(float4 q) {
|
||||
// warn: do not read from these variables until set
|
||||
float3 euler;
|
||||
float a, b;
|
||||
@@ -53,7 +54,7 @@ static inline float3 quat_to_euler(float4 q) {
|
||||
}
|
||||
|
||||
// multiplies two quaternions
|
||||
static inline float4 quat_mul(float4 q1, float4 q2) {
|
||||
atrb_const static inline float4 quat_mul(float4 q1, float4 q2) {
|
||||
return (float4){
|
||||
.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
|
||||
.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
|
||||
@@ -63,7 +64,7 @@ static inline float4 quat_mul(float4 q1, float4 q2) {
|
||||
}
|
||||
|
||||
// get the conjugate of the quaternion (negates the vector portion)
|
||||
static inline float4 quat_conj(float4 q) {
|
||||
atrb_const static inline float4 quat_conj(float4 q) {
|
||||
return (float4){
|
||||
.w = q.w,
|
||||
.x = -q.x,
|
||||
@@ -73,7 +74,7 @@ static inline float4 quat_conj(float4 q) {
|
||||
}
|
||||
|
||||
// get the multiplicative inverse of the quaternion (conj / mag²)
|
||||
static inline float4 quat_inv(float4 q) {
|
||||
atrb_const static inline float4 quat_inv(float4 q) {
|
||||
float mag2 = float4_mag2(q);
|
||||
if (mag2 == 0.0F) return (float4){0};
|
||||
mag2 = 1.0F / mag2;
|
||||
@@ -81,13 +82,13 @@ static inline float4 quat_inv(float4 q) {
|
||||
}
|
||||
|
||||
// rotates a vector by the quaternion (q must be a unit quaternion (normalized))
|
||||
static inline float3 quat_rot(float4 q, float3 v) {
|
||||
atrb_const static inline float3 quat_rot(float4 q, float3 v) {
|
||||
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_conj(q)); // q•v•q¯¹ (using conjugate for q⁻¹, as for unit quaternions this is the same as the multiplicative inverse)
|
||||
return (float3){q.x, q.y, q.z};
|
||||
}
|
||||
|
||||
// rotates a vector by the quaternion, q may be non-normalized
|
||||
static inline float3 quat_rot_s(float4 q, float3 v) {
|
||||
atrb_const static inline float3 quat_rot_s(float4 q, float3 v) {
|
||||
q = quat_mul(quat_mul(q, quat_from_float3(v)), quat_inv(q)); // q•v•q¯¹
|
||||
return (float3){q.x, q.y, q.z};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user