makefile: support a MinGW cross-compilation environment
[blobwars-mingw.git] / src / CMath.cpp
blobd622ba1ef0f025d6177669f2d4257ac74d85196c
1 /*
2 Copyright (C) 2004 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "headers.h"
23 void Math::limitChar(signed char *in, int low, int high)
25 if (*in < low)
26 *in = low;
27 if (*in > high)
28 *in = high;
31 void Math::limitChar(unsigned char *in, int low, int high)
33 if (*in < low)
34 *in = low;
35 if (*in > high)
36 *in = high;
39 void Math::limitInt(int *in, int low, int high)
41 if (*in < low)
42 *in = low;
43 if (*in > high)
44 *in = high;
47 void Math::limitFloat(float *in, float low, float high)
49 if (*in < low)
50 *in = low;
51 if (*in > high)
52 *in = high;
55 void Math::wrapChar(signed char *in, signed char low, signed char high)
57 if (*in < low)
58 *in = high;
59 if (*in > high)
60 *in = low;
63 void Math::wrapInt(int *in, int low, int high)
65 if (*in < low)
66 *in = high;
67 if (*in > high)
68 *in = low;
71 void Math::wrapFloat(float *in, float low, float high)
73 if (*in < low)
74 *in = high;
75 if (*in > high)
76 *in = low;
79 int Math::prand()
81 pSeed = pSeed * 7654321 + 12345;
82 pSeed = (unsigned int)(pSeed / 65536) % 32768;
84 return pSeed;
87 int Math::rrand(int min, int max)
89 int r = min;
91 max++;
93 if ((max - min) == 0)
95 return min;
98 return r += prand() % (max - min);
101 void Math::addBit(long *currentBits, long newBits)
103 if (!(*currentBits & newBits))
104 *currentBits += newBits;
107 void Math::removeBit(long *currentBits, long oldBits)
109 if (*currentBits & oldBits)
110 *currentBits -= oldBits;
113 void Math::calculateSlope(float x, float y, float x2, float y2, float *dx, float *dy)
115 int steps = (int)max(fabs(x - x2), fabs(y - y2));
116 if (steps == 0)
118 *dx = 0;
119 *dy = 0;
120 return;
123 *dx = (x - x2);
124 *dx /= steps;
125 *dy = (y - y2);
126 *dy /= steps;
129 char *Math::formatTime(int t)
131 static char time[1024];
133 strcpy(time, "");
135 int hours = t / 3600;
136 int minutes = (t % 3600) / 60;
137 int seconds = (t % 60);
139 sprintf(time, "%dh %dm %ds", hours, minutes, seconds);
141 return time;
144 unsigned int Math::pSeed;