From 9c8593018b62d18d3a82d282ffd4bf9f63b5a245 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Sun, 2 Aug 2020 22:08:35 +1000 Subject: [PATCH] - define new function `calcSinTableValue()` to calculate an equivalent `sintable[]` entry. * Returns a true double result. * Also supports fractional input for fractional input. That is, if `sintable[1]` = 50 and `sintable[2]` = 100, `calcSinTableValue(1.5)` = 75. * Increased precision of `BANG2RAD` define to use double and not float. * As such, revert addition of `sintablef[]`. --- source/build/include/build.h | 3 ++- source/build/src/engine.cpp | 19 ++++++++------- source/games/duke/src/hudweapon_d.cpp | 34 +++++++++++++-------------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 94d144e15..88f6dc6bb 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -370,7 +370,6 @@ static inline int32_t BGetTime(void) { return (int32_t) totalclock; } EXTERN int32_t numframes, randomseed; EXTERN int16_t sintable[2048]; -EXTERN double sintablef[2048]; EXTERN int16_t numshades; EXTERN uint8_t paletteloaded; @@ -939,6 +938,8 @@ int32_t videoSetRenderMode(int32_t renderer); void renderSetRollAngle(float rolla); #endif +double calcSinTableValue(double index); + // pal: pass -1 to invalidate all palettes for the tile, or >=0 for a particular palette // how: pass -1 to invalidate all instances of the tile in texture memory, or a bitfield // bit 0: opaque or masked (non-translucent) texture, using repeating diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 45d54b197..3ae659ae4 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -588,20 +588,11 @@ static int32_t engineLoadTables(void) reciptable[i] = divscale30(2048, i+2048); for (i=0; i<=512; i++) - { sintable[i] = (int16_t)(16384.f * sinf((float)i * BANG2RAD) + 0.0001f); - sintablef[i] = 16384. * sin(BANG2RAD * i); - } for (i=513; i<1024; i++) - { sintable[i] = sintable[1024-i]; - sintablef[i] = sintablef[1024-i]; - } for (i=1024; i<2048; i++) - { sintable[i] = -sintable[i-1024]; - sintablef[i] = -sintablef[i-1024]; - } for (i=0; i<640; i++) radarang[i] = (int16_t)(atanf(((float)(640-i)-0.5f) * (1.f/160.f)) * (-64.f * (1.f/BANG2RAD)) + 0.0001f); @@ -4331,3 +4322,13 @@ void renderSetRollAngle(float rolla) gtang = rolla * (fPI * (1.f/1024.f)); } #endif + +// +// Calculates and returns a sintable[] value of the equivilent index (and supports fractional indexes also) +// +double calcSinTableValue(double index) +{ + double value = fabs(16384. * sin(BANG2RAD * (index - (fmod(index, 512) * 2)))); + return (index >= 1024 ? -value : value); +} + diff --git a/source/games/duke/src/hudweapon_d.cpp b/source/games/duke/src/hudweapon_d.cpp index a679c92f1..85087d5c0 100644 --- a/source/games/duke/src/hudweapon_d.cpp +++ b/source/games/duke/src/hudweapon_d.cpp @@ -72,7 +72,7 @@ void displayloogie(short snum) { a = abs(sintable[((ps[snum].loogcnt + i) << 5) & 2047]) >> 5; z = 4096 + ((ps[snum].loogcnt + i) << 9); - x = (-getavel(snum)) + (sintablef[((ps[snum].loogcnt + i) << 6) & 2047] / 1024.); + x = (-getavel(snum)) + (calcSinTableValue(((ps[snum].loogcnt + i) << 6) & 2047) / 1024.); hud_drawsprite( (ps[snum].loogiex[i] + x), (200 + ps[snum].loogiey[i] - y), z - (i << 8), 256 - a, @@ -103,7 +103,7 @@ int animatefist(int gs, int snum) fistzoom = 90612L; if (fistzoom < 40920) fistzoom = 40290; - fistz = 194 + (sintablef[((6 + fisti) << 7) & 2047] / 512.); + fistz = 194 + (calcSinTableValue(((6 + fisti) << 7) & 2047) / 512.); if (sprite[ps[snum].i].pal == 1) fistpal = 1; @@ -303,11 +303,11 @@ void displayweapon_d(int snum, double smoothratio) gun_pos = 80-(p->weapon_pos*p->weapon_pos); weapon_xoffset = (160)-90; - weapon_xoffset -= (sintablef[((xs_CRoundToInt(weapon_sway)>>1)+512)&2047] / (1024. + 512.)); + weapon_xoffset -= calcSinTableValue(fmod((weapon_sway / 2.) + 512, 2048)) / (1024. + 512.); weapon_xoffset -= 58 + p->weapon_ang; if( sprite[p->i].xrepeat < 32 ) - gun_pos -= fabs(sintablef[(xs_CRoundToInt(weapon_sway)<<2)&2047] / 512.); - else gun_pos -= fabs(sintablef[(xs_CRoundToInt(weapon_sway)>>1)&2047] / 1024.); + gun_pos -= fabs(calcSinTableValue(fmod(weapon_sway * 4., 2048)) / 512.); + else gun_pos -= fabs(calcSinTableValue(fmod(weapon_sway / 2., 2048)) / 1024.); gun_pos -= (p->hard_landing<<3); @@ -367,14 +367,14 @@ void displayweapon_d(int snum, double smoothratio) fistsign += i >> 1; } cw = weapon_xoffset; - weapon_xoffset += sintablef[(fistsign) & 2047] / 1024.; + weapon_xoffset += calcSinTableValue(fistsign & 2047) / 1024.; hud_draw(weapon_xoffset + 250 - (p->q16look_ang / (2. * FRACUNIT)), - looking_arc + 258 - (fabs(sintablef[(fistsign) & 2047] / 256.)), + looking_arc + 258 - (fabs(calcSinTableValue(fistsign & 2047) / 256.)), FIST, gs, o); weapon_xoffset = cw; - weapon_xoffset -= sintablef[(fistsign) & 2047] / 1024.; + weapon_xoffset -= calcSinTableValue(fistsign & 2047) / 1024.; hud_draw(weapon_xoffset + 40 - (p->q16look_ang / (2. * FRACUNIT)), - looking_arc + 200 + (fabs(sintablef[(fistsign) & 2047] / 256.)), + looking_arc + 200 + (fabs(calcSinTableValue(fistsign & 2047) / 256.)), FIST, gs, o | 4); } else @@ -458,8 +458,8 @@ void displayweapon_d(int snum, double smoothratio) pal = 1; else pal = sector[p->cursectnum].floorpal; - weapon_xoffset -= sintablef[(768 + (p->kickback_pic << 7)) & 2047] / 2048.; - gun_pos += sintablef[(768 + (p->kickback_pic << 7) & 2047)] / 2048.; + weapon_xoffset -= calcSinTableValue((768 + (p->kickback_pic << 7)) & 2047) / 2048.; + gun_pos += calcSinTableValue((768 + (p->kickback_pic << 7)) & 2047) / 2048.; if (*kb > 0) { @@ -510,7 +510,7 @@ void displayweapon_d(int snum, double smoothratio) if (*kb > 0) { - gun_pos -= sintablef[p->kickback_pic << 7] / 4096.; + gun_pos -= calcSinTableValue(p->kickback_pic << 7) / 4096.; } if (*kb > 0 && sprite[p->i].pal != 1) @@ -655,7 +655,7 @@ void displayweapon_d(int snum, double smoothratio) pal = sector[p->cursectnum].floorpal; if (*kb > 0) - gun_pos -= sintablef[p->kickback_pic << 7] / 4096.; + gun_pos -= calcSinTableValue(p->kickback_pic << 7) / 4096.; if (*kb > 0 && sprite[p->i].pal != 1) weapon_xoffset += 1 - (rand() & 3); @@ -768,7 +768,7 @@ void displayweapon_d(int snum, double smoothratio) pal = sector[p->cursectnum].floorpal; if (*kb > 0) - gun_pos -= sintablef[p->kickback_pic << 7] / 4096.; + gun_pos -= calcSinTableValue(p->kickback_pic << 7) / 4096.; if (*kb > 0 && sprite[p->i].pal != 1) weapon_xoffset += 1 - (rand() & 3); @@ -1125,7 +1125,7 @@ void displayweapon_d(int snum, double smoothratio) hud_drawpal(weapon_xoffset + 184 - (p->q16look_ang / (2. * FRACUNIT)), looking_arc + 240 - gun_pos, SHRINKER + 2, - 16 - (sintablef[p->random_club_frame & 2047] / 1024.), + 16 - (calcSinTableValue(p->random_club_frame & 2047) / 1024.), o, 0); hud_drawpal(weapon_xoffset + 188 - (p->q16look_ang / (2. * FRACUNIT)), @@ -1282,7 +1282,7 @@ void displayweapon_d(int snum, double smoothratio) { hud_drawpal(weapon_xoffset + 184 - (p->q16look_ang / (2. * FRACUNIT)), looking_arc + 240 - gun_pos, SHRINKER + 2, - 16 - (sintablef[p->random_club_frame & 2047] / 1024.), + 16 - (calcSinTableValue(p->random_club_frame & 2047) / 1024.), o, 2); hud_drawpal(weapon_xoffset + 188 - (p->q16look_ang / (2. * FRACUNIT)), @@ -1292,7 +1292,7 @@ void displayweapon_d(int snum, double smoothratio) { hud_drawpal(weapon_xoffset + 184 - (p->q16look_ang / (2. * FRACUNIT)), looking_arc + 240 - gun_pos, SHRINKER + 2, - 16 - (sintablef[p->random_club_frame & 2047] / 1024.), + 16 - (calcSinTableValue(p->random_club_frame & 2047) / 1024.), o, 0); hud_drawpal(weapon_xoffset + 188 - (p->q16look_ang / (2. * FRACUNIT)),