… | |
… | |
18 | |
18 | |
19 | #include <shstr.h> |
19 | #include <shstr.h> |
20 | #include <traits.h> |
20 | #include <traits.h> |
21 | |
21 | |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
23 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
23 | #define auto(var,expr) typeof(expr) var = (expr) |
24 | |
24 | |
25 | // very ugly macro that basicaly declares and initialises a variable |
25 | // very ugly macro that basicaly declares and initialises a variable |
26 | // that is in scope for the next statement only |
26 | // that is in scope for the next statement only |
27 | // works only for stuff that can be assigned 0 and converts to false |
27 | // works only for stuff that can be assigned 0 and converts to false |
28 | // (note: works great for pointers) |
28 | // (note: works great for pointers) |
… | |
… | |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
47 | |
47 | |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
49 | |
|
|
50 | template<typename T> |
|
|
51 | static inline T |
|
|
52 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
53 | { |
|
|
54 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
55 | } |
49 | |
56 | |
50 | // lots of stuff taken from FXT |
57 | // lots of stuff taken from FXT |
51 | |
58 | |
52 | /* Rotate right. This is used in various places for checksumming */ |
59 | /* Rotate right. This is used in various places for checksumming */ |
53 | //TODO: that sucks, use a better checksum algo |
60 | //TODO: that sucks, use a better checksum algo |
… | |
… | |
262 | |
269 | |
263 | void seed (uint32_t seed); |
270 | void seed (uint32_t seed); |
264 | uint32_t next (); |
271 | uint32_t next (); |
265 | |
272 | |
266 | // uniform distribution |
273 | // uniform distribution |
267 | uint32_t operator ()(uint32_t r_max) |
274 | uint32_t operator ()(uint32_t num) |
268 | { |
275 | { |
269 | return is_constant (r_max) |
276 | return is_constant (num) |
270 | ? this->next () % r_max |
277 | ? (next () * (uint64_t)num) >> 32U |
271 | : get_range (r_max); |
278 | : get_range (num); |
272 | } |
279 | } |
273 | |
280 | |
274 | // return a number within (min .. max) |
281 | // return a number within (min .. max) |
275 | int operator () (int r_min, int r_max) |
282 | int operator () (int r_min, int r_max) |
276 | { |
283 | { |
277 | return is_constant (r_min) && is_constant (r_max) |
284 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
278 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
285 | ? r_min + operator ()(r_max - r_min + 1) |
279 | : get_range (r_min, r_max); |
286 | : get_range (r_min, r_max); |
280 | } |
287 | } |
281 | |
288 | |
282 | double operator ()() |
289 | double operator ()() |
283 | { |
290 | { |
… | |
… | |
434 | // return current time as timestampe |
441 | // return current time as timestampe |
435 | tstamp now (); |
442 | tstamp now (); |
436 | |
443 | |
437 | int similar_direction (int a, int b); |
444 | int similar_direction (int a, int b); |
438 | |
445 | |
|
|
446 | // like printf, but returns a std::string |
|
|
447 | const std::string format (const char *format, ...); |
|
|
448 | |
439 | #endif |
449 | #endif |
440 | |
450 | |