2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 11:19:39 +01:00
hlsdk-xash3d/cl_dll/util.cpp

133 lines
2.5 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// util.cpp
//
// implementation of class-less helper functions
//
2019-10-13 15:37:32 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
2016-06-04 15:24:23 +02:00
#include "hud.h"
#include "cl_util.h"
#include <string.h>
2021-06-20 00:53:07 +02:00
#if !defined(M_PI)
2016-06-04 15:24:23 +02:00
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
#endif
2021-06-20 00:53:07 +02:00
#if !defined(M_PI_F)
2019-10-13 13:49:25 +02:00
#define M_PI_F (float)M_PI
#endif
2024-01-15 03:16:49 +01:00
// extern vec3_t vec3_origin;
2016-06-04 15:24:23 +02:00
// if C++ mangling differs from C symbol name
2021-06-20 00:53:07 +02:00
#if _MSC_VER || __WATCOMC__
2024-01-15 03:31:16 +01:00
float vec3_origin[3];
2017-01-10 10:45:52 +01:00
#endif
2016-07-03 15:39:55 +02:00
float Length( const float *v )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
int i;
2016-06-04 15:24:23 +02:00
float length;
2016-07-03 15:39:55 +02:00
2019-10-13 13:49:25 +02:00
length = 0.0f;
2016-07-03 15:39:55 +02:00
for( i = 0; i < 3; i++ )
length += v[i] * v[i];
length = sqrt( length ); // FIXME
2016-06-04 15:24:23 +02:00
return length;
}
void VectorAngles( const float *forward, float *angles )
{
2016-07-03 15:39:55 +02:00
float tmp, yaw, pitch;
2019-10-13 13:49:25 +02:00
if( forward[1] == 0.0f && forward[0] == 0.0f )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
yaw = 0.0f;
if( forward[2] > 0.0f )
pitch = 90.0f;
2016-06-04 15:24:23 +02:00
else
2019-10-13 13:49:25 +02:00
pitch = 270.0f;
2016-06-04 15:24:23 +02:00
}
else
{
2019-10-13 13:49:25 +02:00
yaw = ( atan2( forward[1], forward[0]) * 180.0f / M_PI_F );
if( yaw < 0.0f )
yaw += 360.0f;
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
tmp = sqrt( forward[0] * forward[0] + forward[1] * forward[1] );
2019-10-13 13:49:25 +02:00
pitch = ( atan2( forward[2], tmp ) * 180.0f / M_PI_F );
if( pitch < 0.0f )
pitch += 360.0f;
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
angles[0] = pitch;
angles[1] = yaw;
2019-10-13 13:49:25 +02:00
angles[2] = 0.0f;
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
float VectorNormalize( float *v )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
float length, ilength;
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
length = sqrt( length ); // FIXME
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
if( length )
2016-06-04 15:24:23 +02:00
{
2019-10-13 13:49:25 +02:00
ilength = 1.0f / length;
2016-06-04 15:24:23 +02:00
v[0] *= ilength;
v[1] *= ilength;
v[2] *= ilength;
}
2016-07-03 15:39:55 +02:00
return length;
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
void VectorInverse( float *v )
2016-06-04 15:24:23 +02:00
{
v[0] = -v[0];
v[1] = -v[1];
v[2] = -v[2];
}
2016-07-03 15:39:55 +02:00
void VectorScale( const float *in, float scale, float *out )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
out[0] = in[0] * scale;
out[1] = in[1] * scale;
out[2] = in[2] * scale;
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
void VectorMA( const float *veca, float scale, const float *vecb, float *vecc )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
vecc[0] = veca[0] + scale * vecb[0];
vecc[1] = veca[1] + scale * vecb[1];
vecc[2] = veca[2] + scale * vecb[2];
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
HSPRITE LoadSprite( const char *pszName )
2016-06-04 15:24:23 +02:00
{
int i = GetSpriteRes( ScreenWidth, ScreenHeight );
2016-07-03 15:39:55 +02:00
char sz[256];
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
sprintf( sz, pszName, i );
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
return SPR_Load( sz );
2016-06-04 15:24:23 +02:00
}