FS2_Open
Open source remastering of the Freespace 2 engine
floating.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _FLOATING_H
13 #define _FLOATING_H
14 
15 #include <math.h>
16 #include <float.h>
17 
18 #include "globalincs/pstypes.h"
19 
20 extern float frand();
21 extern int rand_chance(float frametime, float chance = 1.0f);
22 float frand_range(float min, float max);
23 
24 // determine if a floating point number is NaN (Not a Number)
25 #define fl_is_nan(fl) _isnan((double)(fl))
26 
27 // Handy macros to prevent type casting all over the place
28 
29 #define fl_sqrt(fl) sqrtf(fl)
30 #define fl_isqrt(fl) (1.0f/sqrtf(fl))
31 #define fl_abs(fl) fabsf(fl)
32 #define i2fl(i) ((float)(i))
33 #define fl2i(fl) ((int)(fl))
34 #define fl2ir(fl) ((int)(fl + ((fl < 0.0f) ? -0.5f : 0.5f)))
35 #define flceil(fl) (int)ceil(fl)
36 #define flfloor(fl) (int)floor(fl)
37 #define f2fl(fx) ((float)(fx)/65536.0f)
38 #define fl2f(fl) (int)((fl)*65536.0f)
39 #define fl_tan(fl) tanf(fl)
40 
41 // convert a measurement in degrees to radians
42 #define fl_radians(fl) ((float)((fl) * (PI / 180.0f)))
43 
44 // convert a measurement in radians to degrees
45 #define fl_degrees(fl) ((float)((fl) * (180.0f / PI)))
46 
47 // use this instead of:
48 // for: (int)floor(x+0.5f) use fl_round_2048(x)
49 // (int)ceil(x-0.5f) use fl_round_2048(x)
50 // (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
51 // (int)floor(x) use fl_round_2048(x-0.5f)
52 // for values in the range -2048 to 2048
53 // use this instead of:
54 // for: (int)floor(x+0.5f) use fl_round_2048(x)
55 // (int)ceil(x-0.5f) use fl_round_2048(x)
56 // (int)floor(x-0.5f) use fl_round_2048(x-1.0f)
57 // (int)floor(x) use fl_round_2048(x-0.5f)
58 // for values in the range -2048 to 2048
59 
60 /*
61 extern const float *p_fl_magic;
62 
63 inline int fl_round_2048( float x )
64 {
65  double tmp_quad;
66  tmp_quad = x + *p_fl_magic;
67  return *((int *)&tmp_quad);
68 }
69 
70 inline float fl_sqrt( float x)
71 {
72  float retval;
73 
74  _asm fld x
75  _asm fsqrt
76  _asm fstp retval
77 
78  return retval;
79 }
80 
81 float fl_isqrt( float x )
82 {
83  float retval;
84 
85  _asm fld x
86  _asm fsqrt
87  _asm fstp retval
88 
89  return 1.0f / retval;
90 }
91 */
92 
93 // sees if two floating point numbers are within the minimum tolerance
94 inline bool fl_equal(float a, float b)
95 {
96  return fl_abs(a - b) <= FLT_EPSILON * MAX(1.0f, MAX(fl_abs(a), fl_abs(b)));
97 }
98 
99 // rounds off a floating point number to a multiple of some number
100 extern float fl_roundoff(float x, int multiple);
101 
102 
103 #endif
float frand_range(float min, float max)
Return a floating point number in the range min..max.
Definition: floating.cpp:50
GLclampf f
Definition: Glext.h:7097
bool fl_equal(float a, float b)
Definition: floating.h:94
GLboolean GLboolean GLboolean GLboolean a
Definition: Glext.h:5781
#define fl_abs(fl)
Definition: floating.h:31
int rand_chance(float frametime, float chance=1.0f)
Call this in the frame interval to get TRUE chance times per second.
Definition: floating.cpp:66
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
float fl_roundoff(float x, int multiple)
Rounds off a floating point number to a multiple of some number.
Definition: floating.cpp:21
float frand()
Return random value in range 0.0..1.0- (1.0- means the closest number less than 1.0)
Definition: floating.cpp:35
#define MAX(a, b)
Definition: pstypes.h:299