Descent3/lib/fix.h

137 lines
3.6 KiB
C
Raw Normal View History

/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-04-16 03:43:29 +00:00
/*
* $Logfile: /DescentIII/Main/Lib/fix.h $
* $Revision: 4 $
* $Date: 10/21/99 9:27p $
* $Author: Jeff $
*
* Header for fixed-point math functions
*
* $Log: /DescentIII/Main/Lib/fix.h $
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 4 10/21/99 9:27p Jeff
* B.A. Macintosh code merge
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 3 9/09/97 3:53p Chris
* Added a #define PI to fix.h
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 2 8/22/97 12:34p Jason
* added FixCeil and FixFloor
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 1 6/23/97 9:25p Samir
* added because source safe sucks
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 7 2/27/97 4:57p Samir
* include fixwin32.h from win\fixwin32.h
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 6 2/27/97 4:52 PM Jeremy
* moved inline asm fixmul/div functions into separate header files for
* mac and pc.
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 5 2/12/97 5:27p Jason
* implemented asin,acos,atan2
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 4 2/10/97 1:57p Jason
* changed FloatToFix back to what it originally was
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 3 2/07/97 5:44p Matt
* Moved fixed-point math funcs from vecmat to fix
* Changed trig functions to use small table like D2
*
* $NoKeywords: $
*/
#ifndef _FIX_H
#define _FIX_H
#include "math.h"
// Disable the "possible loss of data" warning
2024-04-16 18:56:40 +00:00
#pragma warning(disable : 4244)
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Angles are unsigned shorts
2024-04-16 03:43:29 +00:00
typedef unsigned short angle;
2024-04-16 18:56:40 +00:00
// The basic fixed-point type
2024-04-16 03:43:29 +00:00
typedef long fix;
#define PI 3.141592654
2024-04-16 18:56:40 +00:00
#define PIOVER2 1.570796327 // DAJ
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Constants for converted between fix and float
#define FLOAT_SCALER 65536.0
#define FIX_SHIFT 16
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// 1.0 in fixed-point
#define F1_0 (1 << FIX_SHIFT)
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Generate the data for the trig tables. Must be called before trig functions
void InitMathTables();
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table
2024-04-16 03:43:29 +00:00
float FixSin(angle a);
2024-04-16 18:56:40 +00:00
// Returns the cosine of the given angle. Linearly interpolates between two entries in a 256-entry table
2024-04-16 03:43:29 +00:00
float FixCos(angle a);
2024-04-16 18:56:40 +00:00
// Returns the sine of the given angle, but does no interpolation
2024-04-16 03:43:29 +00:00
float FixSinFast(angle a);
2024-04-16 18:56:40 +00:00
// Returns the cosine of the given angle, but does no interpolation
2024-04-16 03:43:29 +00:00
float FixCosFast(angle a);
2024-04-16 18:56:40 +00:00
#define Round(x) ((int)(x + 0.5))
2024-04-16 03:43:29 +00:00
fix FloatToFixFast(float num);
2024-04-16 18:56:40 +00:00
// Conversion macros
2024-04-16 03:43:29 +00:00
//??#define FloatToFix(num) Round((num) * FLOAT_SCALER)
2024-04-16 20:59:11 +00:00
#define FloatToFix(num) ((fix)((num) * FLOAT_SCALER))
2024-04-16 03:43:29 +00:00
#define IntToFix(num) ((num) << FIX_SHIFT)
2024-04-16 18:56:40 +00:00
#define ShortToFix(num) (((long)(num)) << FIX_SHIFT)
#define FixToFloat(num) (((float)(num)) / FLOAT_SCALER)
2024-04-16 03:43:29 +00:00
#define FixToInt(num) ((num) >> FIX_SHIFT)
2024-04-16 18:56:40 +00:00
#define FixToShort(num) ((short)((num) >> FIX_SHIFT))
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Fixed-point math functions in inline ASM form
#if defined(WIN32)
2024-04-16 18:56:40 +00:00
#include "win\fixwin32.h"
2024-04-16 03:43:29 +00:00
#endif
// use this instead of:
// for: (int)floor(x+0.5f) use FloatRound(x)
// (int)ceil(x-0.5f) use FloatRound(x)
// (int)floor(x-0.5f) use FloatRound(x-1.0f)
// (int)floor(x) use FloatRound(x-0.5f)
// for values in the range -2048 to 2048
2024-04-16 18:56:40 +00:00
int FloatRound(float x);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
angle FixAtan2(float cos, float sin);
2024-04-16 03:43:29 +00:00
angle FixAsin(float v);
angle FixAcos(float v);
// Does a ceiling operation on a fixed number
2024-04-16 18:56:40 +00:00
fix FixCeil(fix num);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// Floors a fixed number
fix FixFloor(fix num);
2024-04-16 03:43:29 +00:00
#endif