FS2_Open
Open source remastering of the Freespace 2 engine
floating.cpp
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 #include <stdlib.h>
11 
12 #include "io/timer.h"
13 #include "math/floating.h"
14 
15 
21 float fl_roundoff(float x, int multiple)
22 {
23  float half = (float) multiple / 2.0f;
24 
25  if (x < 0)
26  half = -half;
27 
28  x += half;
29  return (float) (((int) x / multiple) * multiple);
30 }
31 
35 float frand()
36 {
37  int i_rval;
38  do {
39  i_rval = myrand();
40  } while (i_rval == RAND_MAX);
41  float rval = i2fl(i_rval) * RAND_MAX_1f;
42  return rval;
43 }
44 
50 float frand_range(float min, float max)
51 {
52  float rval;
53 
54  rval = frand();
55  rval = rval * (max - min) + min;
56 
57  return rval;
58 }
59 
66 int rand_chance(float frametime, float chance)
67 {
68  while (--chance > 0.0f)
69  if (frand() < frametime)
70  return 1;
71 
72  return frand() < (frametime * (chance + 1.0f));
73 }
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
typedef int(SCP_EXT_CALLCONV *SCPDLL_PFVERSION)(SCPDLL_Version *)
int rand_chance(float frametime, float chance)
Call this in the frame interval to get TRUE chance times per second.
Definition: floating.cpp:66
const float RAND_MAX_1f
Definition: pstypes.h:309
float fl_roundoff(float x, int multiple)
Rounds off a floating point number to a multiple of some number.
Definition: floating.cpp:21
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
typedef float(SCP_EXT_CALLCONV *SCPTRACKIR_PFFLOATVOID)()
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 i2fl(i)
Definition: floating.h:32
int myrand()
Definition: systemvars.cpp:102