Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganizing fix library #195

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ endif()
include_directories(
"cfile" # TODO: Remove after untying all modules
"ddebug" # -*-
"fix" # -*-
"lib" # TODO: Remove after untying all modules
"Descent3"
${PLATFORM_INCLUDES}
Expand Down
5 changes: 5 additions & 0 deletions Descent3/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@
#include "stringtable.h"
#include "hlsoundlib.h"
#include "player.h"
#include "psrand.h"
#include "ambient.h"
#include "matcen.h"
#include "dedicated_server.h"
Expand Down Expand Up @@ -2114,6 +2115,10 @@ void InitD3Systems1(bool editor) {
// This function needs be called before ANY 3d stuff can get done. I mean it.
InitMathTables();

// Initialize a random seed.
ps_srand(time(nullptr));


// This function has to be done before any sound stuff is called
InitSounds();

Expand Down
5 changes: 5 additions & 0 deletions fix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ set(CPPS
fix.cpp)

add_library(fix STATIC ${HEADERS} ${CPPS})
target_include_directories(fix PUBLIC
$<BUILD_INTERFACE:
${PROJECT_SOURCE_DIR}/fix
>
)
24 changes: 9 additions & 15 deletions fix/fix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,29 @@
* $NoKeywords: $
*/

#include <cmath>

#include "fix.h"

#include <time.h>
#include <stdlib.h>
#include "psrand.h"
// Tables for trig functions
float sincos_table[321]; // 256 entries + 64 sin-only + 1 for interpolation
angle asin_table[257]; // 1 quadrants worth, +1 for interpolation
angle acos_table[257];

#define PI 3.141592654

// Generate the data for the trig tables
void InitMathTables() {
int i;
float rad, s, c;

for (i = 0; i < 321; i++) {
rad = (float)((double)i / 256.0 * 2 * PI);
sincos_table[i] = (float)sin(rad);
sincos_table[i] = std::sin(rad);
}

for (i = 0; i < 256; i++) {

s = asin((float)i / 256.0);
c = acos((float)i / 256.0);
s = std::asin((float)i / 256.0f);
c = std::acos((float)i / 256.0f);

s = (s / (PI * 2));
c = (c / (PI * 2));
Expand All @@ -100,9 +97,6 @@ void InitMathTables() {

asin_table[256] = asin_table[255];
acos_table[256] = acos_table[255];

// Initialize a random seed.
ps_srand(time(NULL));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is not needed to initialize the random number generator?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they relocated seed initialization to init.cpp

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random seed was injected into InitMathTables when it was part of vecmat submodule. Logically seed initialization not belongs to vecmat or fixmat components so I decided to move it out to common initialization function.

}

// Returns the sine of the given angle. Linearly interpolates between two entries in a 256-entry table
Expand Down Expand Up @@ -179,7 +173,7 @@ angle FixAsin(float v) {
fix vv;
int i, f, aa;

vv = FloatToFix(fabs(v));
vv = FloatToFix(std::fabs(v));

if (vv >= F1_0) // check for out of range
return 0x4000;
Expand All @@ -201,7 +195,7 @@ angle FixAcos(float v) {
fix vv;
int i, f, aa;

vv = FloatToFix(fabs(v));
vv = FloatToFix(std::fabs(v));

if (vv >= F1_0) // check for out of range
return 0;
Expand Down Expand Up @@ -231,12 +225,12 @@ angle FixAtan2(float cos, float sin) {

q = (sin * sin) + (cos * cos);

m = sqrt(q);
m = std::sqrt(q);

if (m == 0)
return 0;

if (fabs(sin) < fabs(cos)) {
if (std::fabs(sin) < std::fabs(cos)) {
// sin is smaller, use arcsin
t = FixAsin(sin / m);
if (cos < 0)
Expand Down
23 changes: 8 additions & 15 deletions lib/fix.h → fix/fix.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,20 @@
#ifndef _FIX_H
#define _FIX_H

#include "math.h"

// Disable the "possible loss of data" warning
#pragma warning(disable : 4244)
#include <cmath>
#include <cstdint>

// Angles are unsigned shorts
typedef unsigned short angle;
typedef uint16_t angle;

// The basic fixed-point type
typedef long fix;
typedef int32_t fix;

#define PI 3.141592654
#define PI 3.141592654f
#define PIOVER2 1.570796327 // DAJ

// Constants for converted between fix and float
#define FLOAT_SCALER 65536.0
#define FLOAT_SCALER 65536.0f
#define FIX_SHIFT 16

// 1.0 in fixed-point
Expand Down Expand Up @@ -105,15 +103,10 @@ fix FloatToFixFast(float num);
//??#define FloatToFix(num) Round((num) * FLOAT_SCALER)
#define FloatToFix(num) ((fix)((num) * FLOAT_SCALER))
#define IntToFix(num) ((num) << FIX_SHIFT)
#define ShortToFix(num) (((long)(num)) << FIX_SHIFT)
#define ShortToFix(num) (((int32_t)(num)) << FIX_SHIFT)
#define FixToFloat(num) (((float)(num)) / FLOAT_SCALER)
#define FixToInt(num) ((num) >> FIX_SHIFT)
#define FixToShort(num) ((short)((num) >> FIX_SHIFT))

// Fixed-point math functions in inline ASM form
#if defined(WIN32)
#include "win\fixwin32.h"
#endif
#define FixToShort(num) ((int16_t)((num) >> FIX_SHIFT))

// use this instead of:
// for: (int)floor(x+0.5f) use FloatRound(x)
Expand Down
85 changes: 0 additions & 85 deletions lib/win/fixwin32.h

This file was deleted.

1 change: 1 addition & 0 deletions scripts/AIGame3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "AIGame3_External.h"

#include "module.h"
#include "psrand.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ add_custom_target(HogLinuxFull-copy

foreach(SCRIPT ${SCRIPTS})
add_library(${SCRIPT} SHARED ${CPPS} "${SCRIPT}.cpp")
target_link_libraries(${SCRIPT} fix)
add_dependencies(${SCRIPT} HogLinuxFull-copy) # HogLinuxDemo-copy
set_target_properties(${SCRIPT} PROPERTIES PREFIX "")
if (UNIX)
Expand Down
4 changes: 0 additions & 4 deletions scripts/DallasFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5335,8 +5335,6 @@ Can object see the target object
$$END
*/
bool qObjCanSeeObj(int handletarget, int cone, int handlesrc) {
#define PI 3.141592654

vector vsource, vtarget;

msafe_struct mstruct;
Expand Down Expand Up @@ -5383,8 +5381,6 @@ Can object see the target object
$$END
*/
bool qObjCanSeeObjAdvanced(int handletarget, int cone, int handlesrc, int fvi_flags) {
#define PI 3.141592654

vector vsource, vtarget;
int sourceroom;

Expand Down