Files
itgmania212121/stepmania/src/RageUtil.h
T

204 lines
6.6 KiB
C
Raw Normal View History

2002-08-23 00:52:12 +00:00
#ifndef RAGEUTIL_H
#define RAGEUTIL_H
/*
-----------------------------------------------------------------------------
File: RageUtil
2002-07-23 01:41:40 +00:00
Desc: Miscellaneous helper macros and functions.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
2002-01-20 07:51:09 +00:00
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// SAFE_ Macros
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
2002-07-23 01:41:40 +00:00
#define ZERO(x) memset(&x, 0, sizeof(x))
2002-07-28 20:28:37 +00:00
#define COPY(a,b) { ASSERT(sizeof(a)==sizeof(b)); memcpy(&a, &b, sizeof(a)); }
2001-11-03 10:52:42 +00:00
#define RECTWIDTH(rect) ((rect).right - (rect).left)
#define RECTHEIGHT(rect) ((rect).bottom - (rect).top)
2002-05-19 01:59:48 +00:00
inline int RECTCENTERX(RECT rect) { return rect.left + (rect.right-rect.left)/2; }
inline int RECTCENTERY(RECT rect) { return rect.top + (rect.bottom-rect.top)/2; }
2001-11-03 10:52:42 +00:00
/* Common harmless mismatches. */
inline float min(float a, int b) { return a < b? a:b; }
inline float max(float a, int b) { return a > b? a:b; }
inline float min(int a, float b) { return a < b? a:b; }
inline float max(int a, float b) { return a > b? a:b; }
/* Traditional defines. Only use this if you absolutely need
* a constant value. */
2002-07-28 20:28:37 +00:00
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
2002-01-16 10:01:32 +00:00
#define clamp(val,low,high) ( max( (low), min((val),(high)) ) )
#define PI ((float)3.1415926535897932384626433832795)
#define DegreeToRadian( degree ) ((degree) * (PI / 180.0f))
#define RadianToDegree( radian ) ((radian) * (180.0f / PI))
2002-07-23 01:41:40 +00:00
// Scales x so that l1 corresponds to l2 and h1 corresponds to h2. Does not modify x, MUST assign the result to something!
#define SCALE(x, l1, h1, l2, h2) (((x) - (l1)) / ((h1) - (l1)) * ((h2) - (l2)) + (l2))
// Clamps x
#define CLAMP(x, l, h) {if (x > h) x = h; else if (x < l) x = l;}
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Misc helper functions
//-----------------------------------------------------------------------------
2002-07-23 01:41:40 +00:00
// Fast random number generators
// Taken from "Numerical Recipes in C"
extern ULONG randseed;
inline ULONG Random()
{
randseed = 1664525L * randseed + 1013904223L;
return randseed;
}
inline float RandomFloat()
{
randseed = 1664525L * randseed + 1013904223L;
ULONG itemp = 0x3f800000 | (0x007fffff & randseed);
return (*(float *)&itemp) - 1.0f;
}
// Returns a float between dLow and dHigh inclusive
inline float RandomFloat(float fLow, float fHigh)
{
return RandomFloat() * (fHigh - fLow) + fLow;
}
// Returns an integer between nLow and nHigh inclusive
inline int RandomInt(int nLow, int nHigh)
{
return ((Random() >> 2) % (nHigh - nLow + 1)) + nLow;
}
// Debug new for memory leak tracing
//#ifdef _DEBUG
//#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
//#endif
2001-11-03 10:52:42 +00:00
// Simple function for generating random numbers
inline float randomf( const float low=-1.0f, const float high=1.0f )
2002-02-28 19:40:40 +00:00
{
return low + ( high - low ) * ( (FLOAT)rand() ) / RAND_MAX;
}
/* XXX: These are C99 functions (except for the roundf(double) overload); what's
* the C99 define we can test for? */
inline double trunc( double f ) { return float(int(f)); };
inline float truncf( float f ) { return float(int(f)); };
inline float roundf( float f ) { if(f < 0) return truncf(f-0.5f); return truncf(f+0.5f); };
inline double roundf( double f ){ if(f < 0) return trunc(f-0.5); return trunc(f+0.5); };
2002-04-16 17:31:00 +00:00
inline float froundf( const float f, const float fRoundInterval )
{
return int( (f + fRoundInterval/2)/fRoundInterval ) * fRoundInterval;
}
2001-11-03 10:52:42 +00:00
2002-08-29 21:02:04 +00:00
bool IsAnInt( const char *s );
2002-04-28 20:42:32 +00:00
float TimeToSeconds( CString sHMS );
2002-08-01 13:42:56 +00:00
CString SecondsToTime( float fSecs );
2002-02-28 19:40:40 +00:00
2002-08-29 21:02:04 +00:00
CString ssprintf( const char *fmt, ...);
CString vssprintf( const char *fmt, va_list argList );
2001-11-03 10:52:42 +00:00
#ifdef WIN32
CString hr_ssprintf( int hr, const char *fmt, ...);
#endif
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
// Splits a Path into 4 parts (Directory, Drive, Filename, Extention). Supports UNC path names.
// param1: Whether the Supplied Path (PARAM2) contains a directory name only
// or a file name (Reason: some directories will end with "aaa.bbb"
2002-03-06 08:25:09 +00:00
// which is like a file name).
// We should just make sure all pathnames end with a slash, not special case it here.
// -glenn
void splitpath(
bool UsingDirsOnly,
const CString &Path,
CString &Drive,
CString &Dir,
CString &FName,
CString &Ext
);
2001-11-03 10:52:42 +00:00
void splitrelpath(
const CString &Path,
CString& Dir,
CString& FName,
CString& Ext
);
2001-12-28 10:15:59 +00:00
2002-04-16 17:31:00 +00:00
// Splits a CString into an CStringArray according the Deliminator.
void split(
const CString &Source,
const CString &Deliminator,
CStringArray& AddIt,
const bool bIgnoreEmpty = true
);
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
// Joins a CStringArray to create a CString according the Deliminator.
CString join(
const CString &Deliminator,
const CStringArray& Source
);
2001-11-03 10:52:42 +00:00
2002-08-30 00:28:20 +00:00
bool CreateDirectories( CString Path );
2002-05-19 06:13:16 +00:00
void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs=false, bool bReturnPathToo=false );
2002-09-01 00:37:10 +00:00
bool GetExtDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo, ... );
bool GetExtDirListingV( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo, const char *masks[] );
void FlushDirCache();
2002-08-20 21:00:56 +00:00
unsigned int GetHashForString( CString s );
unsigned int GetHashForFile( CString sPath );
unsigned int GetHashForDirectory( CString sDir ); // a hash value that remains the same as long as nothing in the directory has changed
2001-11-03 10:52:42 +00:00
bool DoesFileExist( const CString &sPath );
2002-07-11 19:02:26 +00:00
bool IsAFile( const CString &sPath );
bool IsADirectory( const CString &sPath );
DWORD GetFileSizeInBytes( const CString &sFilePath );
2001-11-03 10:52:42 +00:00
2002-10-24 08:17:09 +00:00
bool CompareCStringsAsc(const CString &str1, const CString &str2);
bool CompareCStringsDesc(const CString &str1, const CString &str2);
void SortCStringArray( CStringArray &AddTo, const bool bSortAcsending = true );
2001-11-03 10:52:42 +00:00
2002-03-06 08:25:09 +00:00
2002-08-29 21:02:04 +00:00
LONG GetRegKey(HKEY key, const char *subkey, LPTSTR retdata);
HINSTANCE GotoURL(const char *url);
2001-11-03 10:52:42 +00:00
/* Find the mean and standard deviation of all numbers in [start,end). */
float calc_mean(const float *start, const float *end);
float calc_stddev(const float *start, const float *end);
void TrimLeft(string &str, const char *s = "\r\n\t ");
void TrimRight(string &str, const char *s = "\r\n\t ");
2002-08-29 21:02:04 +00:00
/* Fix Windows breakage ... */
#ifdef WIN32
2002-08-29 21:02:04 +00:00
#define getcwd _getcwd
#define wgetcwd _wgetcwd
#define chdir _chdir
#define wchdir _wchdir
#define alloca _alloca
2002-10-31 05:38:03 +00:00
#define stat _stat
2002-08-29 21:02:04 +00:00
#endif
2002-11-16 06:56:25 +00:00
#endif