BosonX/src/util.h

90 lines
2.2 KiB
C++

#ifndef __UTIL_H__
# define __UTIL_H__
#include <num/num.h>
#include <num/vec.h>
#include <random>
using namespace libnum;
/* A platform rectangle, aligned with the camera's view */
struct prect
{
vec3 nl, nr; /* Near left and near right points */
vec3 fl, fr; /* Far left and far right points */
};
/* A side rectangle, aligned with the camera's view */
struct srect
{
vec3 nt, nb; /* Near top and near bottom points */
vec3 ft, fb; /* Far top and far bottom points */
};
/* A flat rectangle, normal to the camera's view */
struct frect
{
vec3 tl, tr; /* Top left an top right points */
vec3 bl, br; /* Bottom left and bottom right points */
};
/* Approximations of the sine and cosine of an angle in radians. */
num num_cos(num a);
num num_sin(num a);
/* Clamp a num between two bounds. */
num num_clamp(num t, num lower_bound, num upper_bound);
/* Uniform random num between 0 and 1. */
num num_rand(void);
/* Uniform random num between low and high. */
num num_rand_between(num low, num high);
/* Random number functions based on C++ standard PRNGs. */
template<typename T> requires(std::uniform_random_bit_generator<T>)
num num_rand_std(T &engine)
{
num r;
std::uniform_int_distribution<> d(0, 0xffff);
r.v = d(engine);
return r;
}
template<typename T> requires(std::uniform_random_bit_generator<T>)
num num_rand_between_std(T &engine, num low, num high)
{
num r;
std::uniform_int_distribution<> d(low.v, high.v);
r.v = d(engine);
return r;
}
template<typename T> requires(std::uniform_random_bit_generator<T>)
vec2 num_rand_vec2_std(T &engine)
{
vec2 r;
do {
r.x = num_rand_between_std(engine, -1, 1);
r.y = num_rand_between_std(engine, -1, 1);
}
while(r.x * r.x + r.y * r.y > 1);
return r.normalize();
}
/* String representation of various objects (static strings rotating; can use
up to 8 of them at once). */
char const *str(num x);
char const *str(vec2 u);
char const *str(vec3 u);
/* Rotate v by θ around z, when rotator=(cos(θ), sin(θ)) */
vec3 vec_rotate_around_z(vec3 v, vec2 rotator);
#define RGB24(hex) \
(((hex & 0xf80000) >> 8) | \
((hex & 0x00fc00) >> 5) | \
((hex & 0x0000f8) >> 3))
#endif /* __UTIL_H__ */