| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/Lib/fix.h $ |
| 21 | * $Revision: 4 $ |
| 22 | * $Date: 10/21/99 9:27p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Header for fixed-point math functions |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/Lib/fix.h $ |
| 28 | * |
| 29 | * 4 10/21/99 9:27p Jeff |
| 30 | * B.A. Macintosh code merge |
| 31 | * |
| 32 | * 3 9/09/97 3:53p Chris |
| 33 | * Added a #define PI to fix.h |
| 34 | * |
| 35 | * 2 8/22/97 12:34p Jason |
| 36 | * added FixCeil and FixFloor |
| 37 | * |
| 38 | * 1 6/23/97 9:25p Samir |
| 39 | * added because source safe sucks |
| 40 | * |
| 41 | * 7 2/27/97 4:57p Samir |
| 42 | * include fixwin32.h from win\fixwin32.h |
| 43 | * |
| 44 | * 6 2/27/97 4:52 PM Jeremy |
| 45 | * moved inline asm fixmul/div functions into separate header files for |
| 46 | * mac and pc. |
| 47 | * |
| 48 | * 5 2/12/97 5:27p Jason |
| 49 | * implemented asin,acos,atan2 |
| 50 | * |
| 51 | * 4 2/10/97 1:57p Jason |
| 52 | * changed FloatToFix back to what it originally was |
| 53 | * |
| 54 | * 3 2/07/97 5:44p Matt |
| 55 | * Moved fixed-point math funcs from vecmat to fix |
| 56 | * Changed trig functions to use small table like D2 |
| 57 | * |
| 58 | * $NoKeywords: $ |
| 59 | */ |
| 60 | |
| 61 | #ifndef _FIX_H |
| 62 | #define _FIX_H |
| 63 | |
| 64 | #include <cmath> |
| 65 | #include <cstdint> |
| 66 | |
| 67 | // Angles are unsigned shorts |
| 68 | typedef uint16_t angle; |
| 69 | |
| 70 | // The basic fixed-point type |
| 71 | typedef int32_t fix; |
| 72 | |
| 73 | #define PI 3.141592654f |
| 74 | #define PIOVER2 1.570796327 // DAJ |
| 75 | |
| 76 | // Constants for converted between fix and float |
| 77 | #define FLOAT_SCALER 65536.0f |
| 78 | #define FIX_SHIFT 16 |
| 79 | |
| 80 | // 1.0 in fixed-point |
| 81 | #define F1_0 (1 << FIX_SHIFT) |
| 82 | |
| 83 | // Generate the data for the trig tables. Must be called before trig functions |
| 84 | void InitMathTables(); |
| 85 | |
| 86 | // Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table |
| 87 | float FixSin(angle a); |
| 88 | |
| 89 | // Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table |
| 90 | float FixCos(angle a); |
| 91 | |
| 92 | // Returns the sine of the given angle, but does no interpolation |
| 93 | float FixSinFast(angle a); |
| 94 | |
| 95 | // Returns the cosine of the given angle, but does no interpolation |
| 96 | float FixCosFast(angle a); |
| 97 | |
| 98 | #define Round(x) ((int)(x + 0.5)) |
| 99 | |
| 100 | fix FloatToFixFast(float num); |
| 101 | |
| 102 | // Conversion macros |
| 103 | //??#define FloatToFix(num) Round((num) * FLOAT_SCALER) |
| 104 | #define FloatToFix(num) ((fix)((num) * FLOAT_SCALER)) |
| 105 | #define IntToFix(num) ((num) << FIX_SHIFT) |
| 106 | #define ShortToFix(num) (((int32_t)(num)) << FIX_SHIFT) |
| 107 | #define FixToFloat(num) (((float)(num)) / FLOAT_SCALER) |
| 108 | #define FixToInt(num) ((num) >> FIX_SHIFT) |
| 109 | #define FixToShort(num) ((int16_t)((num) >> FIX_SHIFT)) |
| 110 | |
| 111 | // use this instead of: |
| 112 | // for: (int)floor(x+0.5f) use FloatRound(x) |
| 113 | // (int)ceil(x-0.5f) use FloatRound(x) |
| 114 | // (int)floor(x-0.5f) use FloatRound(x-1.0f) |
| 115 | // (int)floor(x) use FloatRound(x-0.5f) |
| 116 | // for values in the range -2048 to 2048 |
| 117 | int FloatRound(float x); |
| 118 | |
| 119 | angle FixAtan2(float cos, float sin); |
| 120 | angle FixAsin(float v); |
| 121 | angle FixAcos(float v); |
| 122 | |
| 123 | // Does a ceiling operation on a fixed number |
| 124 | fix FixCeil(fix num); |
| 125 | |
| 126 | // Floors a fixed number |
| 127 | fix FixFloor(fix num); |
| 128 | |
| 129 | #endif |