mirror of
https://github.com/ZDoom/raze-gles.git
synced 2024-12-25 03:00:46 +00:00
Factor out [l]dist() implementations into static inline functions in common.h.
And use these in jmact/mathutil.c's FindDistance2D()/FindDistance3D(). The main use is to allow passing dx/dy instead of sprite positions; the code that actually uses this is not committed. git-svn-id: https://svn.eduke32.com/eduke32@4579 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
parent
1eaf60b222
commit
77dc743f09
3 changed files with 45 additions and 42 deletions
|
@ -7,8 +7,12 @@
|
||||||
#ifndef EDUKE32_COMMON_H_
|
#ifndef EDUKE32_COMMON_H_
|
||||||
#define EDUKE32_COMMON_H_
|
#define EDUKE32_COMMON_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include "scriptfile.h"
|
#include "scriptfile.h"
|
||||||
#include "cache1d.h"
|
#include "cache1d.h"
|
||||||
|
#include "pragmas.h" // klabs
|
||||||
|
#include "build.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef EXTERNC
|
#ifdef EXTERNC
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -82,6 +86,42 @@ int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
|
||||||
char *dup_filename(const char *fn);
|
char *dup_filename(const char *fn);
|
||||||
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext);
|
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext);
|
||||||
|
|
||||||
|
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
|
||||||
|
// in jmact/mathutil.c: "Ken's reverse-engineering job".
|
||||||
|
// Note that jmact/mathutil.c contains practically the same code, but where the
|
||||||
|
// individual x/y(/z) distances are passed instead.
|
||||||
|
static inline int32_t sepldist(const int32_t dx, const int32_t dy)
|
||||||
|
{
|
||||||
|
int32_t x = klabs(dx);
|
||||||
|
int32_t y = klabs(dy);
|
||||||
|
|
||||||
|
if (x < y)
|
||||||
|
swaplong(&x, &y);
|
||||||
|
|
||||||
|
{
|
||||||
|
int32_t t = y + (y>>1);
|
||||||
|
return x - (x>>5) - (x>>7) + (t>>2) + (t>>6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dz: in Build coordinates
|
||||||
|
static inline int32_t sepdist(int32_t dx, int32_t dy, int32_t dz)
|
||||||
|
{
|
||||||
|
int32_t x = klabs(dx);
|
||||||
|
int32_t y = klabs(dy);
|
||||||
|
int32_t z = klabs(dz>>4);
|
||||||
|
|
||||||
|
if (x < y)
|
||||||
|
swaplong(&x, &y);
|
||||||
|
if (x < z)
|
||||||
|
swaplong(&x, &z);
|
||||||
|
|
||||||
|
{
|
||||||
|
int32_t t = y + z;
|
||||||
|
return x - (x>>4) + (t>>2) + (t>>3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int32_t ldist(const spritetype *s1, const spritetype *s2);
|
int32_t ldist(const spritetype *s1, const spritetype *s2);
|
||||||
int32_t dist(const spritetype *s1, const spritetype *s2);
|
int32_t dist(const spritetype *s1, const spritetype *s2);
|
||||||
|
|
||||||
|
|
|
@ -197,36 +197,14 @@ int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
|
|
||||||
// in jmact/mathutil.c: "Ken's reverse-engineering job".
|
|
||||||
// Note that jmact/mathutil.c contains practically the same code, but where the
|
|
||||||
// individual x/y(/z) distances are passed instead.
|
|
||||||
int32_t ldist(const spritetype *s1, const spritetype *s2)
|
int32_t ldist(const spritetype *s1, const spritetype *s2)
|
||||||
{
|
{
|
||||||
int32_t x = klabs(s1->x-s2->x);
|
return sepldist(s1->x-s2->x, s1->y-s2->y);
|
||||||
int32_t y = klabs(s1->y-s2->y);
|
|
||||||
|
|
||||||
if (x<y) swaplong(&x,&y);
|
|
||||||
|
|
||||||
{
|
|
||||||
int32_t t = y + (y>>1);
|
|
||||||
return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t dist(const spritetype *s1, const spritetype *s2)
|
int32_t dist(const spritetype *s1, const spritetype *s2)
|
||||||
{
|
{
|
||||||
int32_t x = klabs(s1->x-s2->x);
|
return sepdist(s1->x-s2->x, s1->y-s2->y, s1->z-s2->z);
|
||||||
int32_t y = klabs(s1->y-s2->y);
|
|
||||||
int32_t z = klabs((s1->z-s2->z)>>4);
|
|
||||||
|
|
||||||
if (x<y) swaplong(&x,&y);
|
|
||||||
if (x<z) swaplong(&x,&z);
|
|
||||||
|
|
||||||
{
|
|
||||||
int32_t t = y + z;
|
|
||||||
return (x - (x>>4) + (t>>2) + (t>>3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,29 +31,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <limits.h>
|
#include "common.h"
|
||||||
#include "compat.h"
|
|
||||||
#include "pragmas.h"
|
|
||||||
|
|
||||||
// I wonder if it's faster to use Ken's functions here...
|
|
||||||
|
|
||||||
int32_t FindDistance2D(int32_t x, int32_t y)
|
int32_t FindDistance2D(int32_t x, int32_t y)
|
||||||
{
|
{
|
||||||
if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y);
|
return sepldist(x, y);
|
||||||
|
|
||||||
{
|
|
||||||
int32_t t = y + (y>>1);
|
|
||||||
return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t FindDistance3D(int32_t x, int32_t y, int32_t z)
|
int32_t FindDistance3D(int32_t x, int32_t y, int32_t z)
|
||||||
{
|
{
|
||||||
if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y);
|
return sepdist(x, y, z);
|
||||||
if (x < (z=klabs(z))) swaplong(&x,&z);
|
|
||||||
|
|
||||||
{
|
|
||||||
int32_t t = y + z;
|
|
||||||
return (x - (x>>4) + (t>>2) + (t>>3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue