42 #ifndef INCLUDED_GLS_RUNTIME_UTIL_H
43 #define INCLUDED_GLS_RUNTIME_UTIL_H
74 #if defined( LINUX ) || defined( __VXWORKS__ ) || defined( __APPLE__ ) || defined( QNX )
79 # include <android/asset_manager.h>
82 const int DIALOG_MAX_DIRECTORY_LENGTH = 1024;
87 #if defined( GLS_DEBUG ) || !defined( NDEBUG )
88 # define GLS_DEBUG_CODE( x ) x
90 # define GLS_DEBUG_CODE( x )
108 inline int strcasecmp(
const char*
const x,
const char*
const y )
110 return _stricmp( x, y );
118 const char PARSER_CLEARSTRING_DELIMETER_START[] =
"#$STRING_START$#";
119 const char PARSER_CLEARSTRING_DELIMETER_END[] =
"#$STRING_END$#";
121 const double HIT_TOLERANCE = 5.0;
124 template<
class X,
class X1,
class X2>
125 bool BETWEEN(
const X& x,
const X1& x1,
const X2& x2 )
127 return ( x >= x1 && x <= x2 )
128 || ( x >= x2 && x <= x1 );
133 bool IS_NEGATIVE(
const X& x ) {
return std::fabs( x ) > 0.0001 ? ( x < 0.0 ) : 0; }
137 bool IS_POSITIVE(
const X& x ) {
return std::fabs( x ) > 0.0001 ? ( x > 0.0 ) : 0; }
143 return std::fabs( x ) < threshold;
161 # define MIN( A, B ) ( ( A ) < ( B ) ? ( A ) : ( B ) )
167 # define MAX( A, B ) ( ( A ) > ( B ) ? ( A ) : ( B ) )
176 GLS_EXPORT
bool gltIsExtSupported(
const char* extension );
181 # define GLSTUDIO_CLAMP_TO_EDGE ( OpenGLVersion() >= 1.2f ? 0x812F : GL_CLAMP )
183 # define GLSTUDIO_CLAMP_TO_EDGE GL_CLAMP_TO_EDGE
211 if( ( index < 0 ) || ( index >= GLS_COLOR_MAX ) )
212 return glsDefinedColors[ 0 ];
214 return glsDefinedColors[ index ];
222 unsigned char alpha )
224 if( ( index >= 0 ) && ( index < GLS_COLOR_MAX ) )
226 glsDefinedColors[ index ][ 0 ] = red;
227 glsDefinedColors[ index ][ 1 ] = green;
228 glsDefinedColors[ index ][ 2 ] = blue;
229 glsDefinedColors[ index ][ 3 ] = alpha;
245 template<
class T1,
class T2>
246 bool Equal( T1 x, T2 y,
float precision = 0.001f )
248 return ( x - precision ) <= y && y <= ( x + precision );
257 const T&
Min(
const T& x,
const T& y )
259 return x < y ? x : y;
268 const T&
Max(
const T& x,
const T& y )
270 return x < y ? y : x;
278 template<
class T,
class Pred>
279 const T&
Min(
const T& x,
const T& y, Pred pr )
281 return pr( x, y ) ? x : y;
289 template<
class T,
class Pred>
290 const T&
Max(
const T& x,
const T& y, Pred pr )
292 return pr( x, y ) ? y : x;
357 rotation_scaling( 0, 0 ) = new_i.x;
358 rotation_scaling( 0, 1 ) = new_j.x;
359 rotation_scaling( 0, 2 ) = new_k.x;
360 rotation_scaling( 1, 0 ) = new_i.y;
361 rotation_scaling( 1, 1 ) = new_j.y;
362 rotation_scaling( 1, 2 ) = new_k.y;
363 rotation_scaling( 2, 0 ) = new_i.z;
364 rotation_scaling( 2, 1 ) = new_j.z;
365 rotation_scaling( 2, 2 ) = new_k.z;
368 rotation_scaling.
Invert();
371 return rotation_scaling * transform;
377 GlsMatrixAffine<T> transform;
378 GlsMatrixAffine<T> rotation_scaling;
382 transform.Translate( -new_origin );
386 rotation_scaling( 0, 0 ) = new_i.x;
387 rotation_scaling( 0, 1 ) = new_j.x;
388 rotation_scaling( 0, 2 ) = new_k.x;
389 rotation_scaling( 1, 0 ) = new_i.y;
390 rotation_scaling( 1, 1 ) = new_j.y;
391 rotation_scaling( 1, 2 ) = new_k.y;
392 rotation_scaling( 2, 0 ) = new_i.z;
393 rotation_scaling( 2, 1 ) = new_j.z;
394 rotation_scaling( 2, 2 ) = new_k.z;
397 rotation_scaling.Invert();
400 result = rotation_scaling * transform;
472 int& index1,
int& index2,
int& index3,
473 bool isVectorArray =
true );
496 GLS_EXPORT
int EncodeString(
char* dest,
const char* src,
const int dest_str_length );
502 GLS_EXPORT std::string
EncodeString(
const std::string& src );
515 GLS_EXPORT
int C_EncodeString(
char* dest,
const char* src,
const int dest_str_length );
529 GLS_EXPORT
int DecodeString(
char* dest,
const char* src,
const int dest_str_length );
535 GLS_EXPORT std::string
DecodeString(
const std::string& src );
543 unsigned char* pixels;
546 GLS_EXPORT
char* MakeRelativePath(
const char* originalPath,
const char* relativePath );
547 GLS_EXPORT
void ConvertBackslashToSlash(
char* str );
548 GLS_EXPORT
void ConvertBackslashToSlash( std::string& str );
565 GLS_EXPORT
int range_check(
int num,
double val, ... );
574 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, FILE** f );
576 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, std::fstream& outstr );
585 GLS_EXPORT
void EnableDirectAssetLoading(
bool enableDirectAssetLoading );
593 GLS_EXPORT
bool HasAssetExtension(
const char* filename );
601 GLS_EXPORT
bool AssetExists(
const char* filename );
608 GLS_EXPORT
void SetAssetManager( AAssetManager* assetManager );
611 #if defined( ANDROID ) || defined( __APPLE__ ) || ( defined( LINUX ) && defined( GLES ) ) || defined( QNX ) || defined( INTEGRITY )
617 GLS_EXPORT std::string
ResolvePath(
const char* filename_ );
623 GLS_EXPORT
void SetResourcePath(
const char* resourcePath );
634 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
659 GLS_EXPORT std::string
ResolvePath(
const char* path );
662 inline std::string
ResolvePath(
const std::string& path )
673 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
683 GLS_EXPORT std::string GetAbsolutePathAndFileNameInDefaultResourceBundle(
const char* fileName );
696 GLS_EXPORT
bool FileExists(
const char* filename );
697 GLS_EXPORT
bool FileExists(
const std::string& filename );
702 GLS_EXPORT
bool IsDirectory(
const char* filename );
703 GLS_EXPORT
bool IsDirectory(
const std::string& filename );
708 GLS_EXPORT std::string
FileExtension(
const std::string& filepath );
713 GLS_EXPORT std::string
FileName(
const std::string& filepath );
719 GLS_EXPORT std::string
FilePath(
const std::string& filepath );
727 GLS_EXPORT
const char*
GetFilePath(
const char* name );
753 GLS_EXPORT
char*
PathToOS(
const char* path );
760 GLS_EXPORT std::string
GetExtension(
const std::string& filename );
766 GLS_EXPORT
const char*
GetBaseName(
const char* path );
800 GLS_EXPORT
bool ContainsNonBlank(
const std::string& val );
802 GLS_EXPORT
bool GetNoSpaces( FILE* f,
char* result,
int maxLen );
803 GLS_EXPORT
bool GetToEnd( std::istream& instr, std::string& result,
bool decode );
804 GLS_EXPORT
bool GetVertex( std::istream& instr, Vertex* vert,
bool getColor );
809 GLS_EXPORT
bool GetComponentClassNames(
const char* dllFileName, DynamicArray<std::string>& nameList,
const char* createClassTag =
"CreateComponent_" );
825 #if !defined( GLES ) || defined( GLES_ANGLE )
826 GLS_EXPORT
bool OpenFileDialog( Fl_Window* win,
char* filePath,
unsigned int filePathSize,
827 char* directory = NULL,
const char* filterStr = NULL,
828 const char* defaultExt = NULL,
const char* title = NULL,
829 bool multiSelect =
false,
bool createFile =
false,
830 bool fileMustExist =
false,
bool pathMustExist =
false,
831 bool noChangeDirectory =
false );
833 GLS_EXPORT
bool SaveFileDialog( Fl_Window* win,
char* filePath,
unsigned int filePathSize,
834 char* directory = NULL,
const char* filterStr = NULL,
835 const char* defaultExt = NULL,
const char* title = NULL,
836 bool createFile =
false,
837 bool fileMustExist =
false,
bool pathMustExist =
false,
838 bool noChangeDirectory =
false );
840 GLS_EXPORT
void CheckGLError(
void );
850 GLS_EXPORT std::string
Uppercase(
const std::string& str );
852 GLS_EXPORT std::string ReplaceEnvironmentVariables(
const char* originalPath );
859 GLS_EXPORT
void ReadCommandLine(
int argc,
char** argv );
860 GLS_EXPORT
void Usage();
868 static GLS_EXPORT glsCommandLine* Instance();
884 GLS_EXPORT InterfaceDescriptionClass(
const char* code,
const char* usage,
const char* comment );
886 GLS_EXPORT ~InterfaceDescriptionClass();
887 GLS_EXPORT InterfaceDescriptionClass(
const InterfaceDescriptionClass& source );
888 GLS_EXPORT InterfaceDescriptionClass& operator=(
const InterfaceDescriptionClass& source );
890 GLS_EXPORT
void Code(
const char* );
891 GLS_EXPORT
void Usage(
const char* );
892 GLS_EXPORT
void Comment(
const char* );
894 const char* Code()
const {
return _code; }
895 const char* Usage()
const {
return _usage; }
896 const char* Comment()
const {
return _comment; }
898 #if defined( DISTI_HAS_RVAL_REFS )
899 InterfaceDescriptionClass( InterfaceDescriptionClass&& source )
907 InterfaceDescriptionClass& operator=( InterfaceDescriptionClass&& source )
909 if(
this != &source )
922 void StealFrom( InterfaceDescriptionClass& source )
925 _code = source._code;
926 _usage = source._usage;
927 _comment = source._comment;
931 source._usage = NULL;
932 source._comment = NULL;
946 typedef std::ostream ostreamType;
947 typedef std::istream istreamType;
949 virtual void StreamOut( ostreamType& outstr )
const = 0;
950 virtual void StreamIn( istreamType& instr ) = 0;
984 if(
typeid( T2 ) !=
typeid(
void* ) )
988 if(
typeid( T3 ) !=
typeid(
void* ) )
992 if(
typeid( T4 ) !=
typeid(
void* ) )
996 if(
typeid( T5 ) !=
typeid(
void* ) )
1000 if(
typeid( T6 ) !=
typeid(
void* ) )
1004 if(
typeid( T7 ) !=
typeid(
void* ) )
1008 if(
typeid( T8 ) !=
typeid(
void* ) )
1012 if(
typeid( T9 ) !=
typeid(
void* ) )
1016 if(
typeid( T10 ) !=
typeid(
void* ) )
1048 : _val1( src._val1 )
1049 , _val2( src._val2 )
1050 , _val3( src._val3 )
1051 , _val4( src._val4 )
1052 , _val5( src._val5 )
1053 , _val6( src._val6 )
1054 , _val7( src._val7 )
1055 , _val8( src._val8 )
1056 , _val9( src._val9 )
1057 , _val10( src._val10 )
1076 const T2& val2 = T2(),
1077 const T3& val3 = T3(),
1078 const T4& val4 = T4(),
1079 const T5& val5 = T5(),
1080 const T6& val6 = T6(),
1081 const T7& val7 = T7(),
1082 const T8& val8 = T8(),
1083 const T9& val9 = T9(),
1084 const T10& val10 = T10() )
1107 case 10: rval &= ( _val10 == val._val10 );
1108 case 9: rval &= ( _val9 == val._val9 );
1109 case 8: rval &= ( _val8 == val._val8 );
1110 case 7: rval &= ( _val7 == val._val7 );
1111 case 6: rval &= ( _val6 == val._val6 );
1112 case 5: rval &= ( _val5 == val._val5 );
1113 case 4: rval &= ( _val4 == val._val4 );
1114 case 3: rval &= ( _val3 == val._val3 );
1115 case 2: rval &= ( _val2 == val._val2 );
1117 rval &= ( _val1 == val._val1 );
1128 return !( *
this == val );
1139 outstr <<
" " << _val2;
1141 outstr <<
" " << _val3;
1143 outstr <<
" " << _val4;
1145 outstr <<
" " << _val5;
1147 outstr <<
" " << _val6;
1149 outstr <<
" " << _val7;
1151 outstr <<
" " << _val8;
1153 outstr <<
" " << _val9;
1154 if( _numVals >= 10 )
1155 outstr <<
" " << _val10;
1205 if( _numVals >= 10 )
1213 GLS_EXPORT std::ostream&
operator<<( std::ostream& outstr,
const GlsMultiValBase& multiVal );
1214 GLS_EXPORT std::istream& operator>>( std::istream& instr, GlsMultiValBase& multiVal );
1226 std::string _string;
1247 : _string( str ? str :
"" )
1251 operator std::string()
const
1267 return str1._string == str2._string;
1273 return !( str1 == str2 );
1277 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropString& str )
1282 inline std::istream& operator>>( std::istream& instr, GlsPropString& str )
1285 disti::GetToEnd( instr, temp,
false );
1298 std::string _string;
1321 operator std::string()
const
1326 std::string& String()
1333 return str._string == _string;
1337 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropStringQuoted& str )
1343 inline std::istream& operator>>( std::istream& instr, GlsPropStringQuoted& str )
1350 if( instr.peek() !=
'\"' )
1352 disti::GetToEnd( instr, temp,
false );
1362 while( instr.good() )
1364 lastChar = currChar;
1365 currChar = instr.get();
1366 if( currChar != -1 )
1368 if( currChar ==
'\"' && lastChar !=
'\\' )
1370 temp += (char)currChar;
1381 void SpawnBrowser(
const char* url );
1391 GLS_EXPORT
bool CheckDistiLicense(
const char* licenseGroupName,
const char* feature,
const char* version,
bool quiet );
1425 T Clamp(
const T& val,
const T& min,
const T& max )
1427 return std::min( max, std::max( min, val ) );
1440 inline void Split(
const std::string& s,
const char delim, std::vector<std::string>& elems,
const std::size_t maxElems = 0 )
1442 std::istringstream ss( s );
1444 while( std::getline( ss, item, delim ) )
1446 elems.push_back( DISTI_RVAL_MOVE( item ) );
1447 if( elems.size() == maxElems )
1452 if( elems.size() == maxElems && ss.good() && maxElems > 0 )
1454 std::string remainder;
1455 std::getline( ss, remainder,
'\0' );
1456 elems.back() += delim + remainder;
1470 inline std::vector<std::string> Split(
const std::string& s,
const char delim,
const std::size_t maxElems = 0 )
1472 std::vector<std::string> elems;
1473 Split( s, delim, elems, maxElems );
1478 inline std::string MakeString(
const char*
const cStr )
1480 return ( cStr ? cStr :
"" );
1487 class IGlsStateManager;
1490 class TextureLoaderList;
1524 void operator=(
const GlsGlobals& ) DISTI_SPECIAL_MEM_FUN_DELETE;
1531 inline void _checkPointerSize()
1535 || ( DISTI_IS_BITNESS( 32 ) &&
sizeof(
void* ) == 4 ),
1536 "Pointer size is unexpected for this bitness" );
1537 DISTI_STATIC_ASSERT_STR( DISTI_IS_BITNESS( 64 ) ^ DISTI_IS_BITNESS( 32 ),
"Expected one bitness to be detected." );
1544 GLS_EXPORT
void glsPerspective(
double fovy,
double aspect,
double zNear,
double zFar );
1546 #ifdef MATRIX_TYPE_FLOAT
1547 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1548 const float modelMatrix[ 16 ],
1549 const float projMatrix[ 16 ],
1550 const int viewport[ 4 ],
1551 double* winx,
double* winy,
double* winz );
1553 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1554 const float modelMatrix[ 16 ],
1555 const float projMatrix[ 16 ],
1556 const int viewport[ 4 ],
1557 double* objx,
double* objy,
double* objz );
1559 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1560 const double modelMatrix[ 16 ],
1561 const double projMatrix[ 16 ],
1562 const int viewport[ 4 ],
1563 double* winx,
double* winy,
double* winz );
1565 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1566 const double modelMatrix[ 16 ],
1567 const double projMatrix[ 16 ],
1568 const int viewport[ 4 ],
1569 double* objx,
double* objy,
double* objz );
The DistiUnhideGlobalsDummyClass class.
const T & Min(const T &x, const T &y)
Definition: util.h:257
int EncodedStringMinLength()
GlsPropStringQuoted(const std::string &str)
Definition: util.h:1308
virtual void StreamOut(ostreamType &outstr) const
Definition: util.h:1135
#define DISTI_STATIC_ASSERT_STR(expr, msg)
Uses C++11's static_assert() because it is available on this platform.
Definition: gls_cpp_lang_support.h:384
std::string GetExtension(const std::string &filename)
GlsDefinedColorEnum
Definition: util.h:188
bool IS_NEGATIVE(const X &x)
Definition: util.h:133
const int INLINE_TEXTURE_LINE_LENGTH
Definition: util.h:115
std::string & String()
Definition: util.h:1259
std::string FileExtension(const std::string &filepath)
void GlsGetPerspective(GlsMatrixType &m, double fovy, double aspect, double zNear, double zFar)
GlsPropString(const char *str)
Definition: util.h:1246
unsigned char glsDefinedColors[GLS_COLOR_MAX][4]
bool IsNearZero(X x)
Definition: util.h:152
virtual bool operator!=(const GlsMultiVal &val) const
Definition: util.h:1126
Definition: dynamic_array.h:66
The disti::DynamicArray class. A templated array of objects capable of dynamically growing...
Class to contain current OpenGL view, projection and draw matrices.
Definition: util.h:296
std::string FileName(const std::string &filepath)
const char * GetBaseName(const char *path)
bool VeryCloseToZero(X x)
Definition: util.h:148
int EncodeString(char *dest, const char *src, const int dest_str_length)
The GlsMatrixAffine class.
void AppendTrailingSlash(char *s)
GlsFontMan & GetFontMan()
Gets the global font manager.
void RemoveDoubleSlashes(char *path, char slash= '/')
Definition: texture_loader.h:55
bool CheckDistiLicense(const char *licenseGroupName, const char *feature, const char *version, bool quiet)
std::string GetQualifiedInstanceName(const DisplayFrame *topFrame, const DisplayObject *obj)
Hold global objects so we can control order of destruction.
Definition: util.h:1493
std::string GetDefaultComponentClassName(const char *dllFileName)
bool BETWEEN(const X &x, const X1 &x1, const X2 &x2)
Definition: util.h:125
GlsMultiVal()
Definition: util.h:1037
char * PathToOS(const char *path)
bool IS_ZERO(X x)
Definition: util.h:156
Definition: gls_state_manager_interface.h:67
void RemoveSpaces(std::string &entry)
const T & Max(const T &x, const T &y)
Definition: util.h:268
virtual bool operator==(const GlsMultiVal &val) const
Definition: util.h:1100
GlsPropStringQuoted(const char *str)
Definition: util.h:1316
bool SilentMode() const
Definition: util.h:866
A file for all GL Studio files to include.
Mutex & GetImageListMutex()
Gets the mutex for protecting the image list.
int range_check(int num, double val,...)
std::ostream & operator<<(std::ostream &outstr, const AttributeName &name)
Defines the stream out operator.
bool GetObjectCoordinatesTransformSameView(DisplayObject *from, DisplayObject *to, GlsMatrixType *outTransform)
float AngularDistanceRad(float angle1, float angle2)
std::string Uppercase(const std::string &str)
void ChangeGlsDefinedColor(GlsDefinedColorEnum index, unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
Definition: util.h:217
Definition: scoped_ptr.h:53
GlsPropString()
Definition: util.h:1229
bool GetComponentClassNames(const char *dllFileName, DynamicArray< std::string > &nameList, const char *createClassTag="CreateComponent_")
VertexNoColor Vector
Definition: gls_font_base.h:66
DisplayFrame * GetTopLevelDisplayFrame(DisplayFrame *frame)
GlsMatrixAffine< T > FindCoordinateTransformation(const Vector &new_origin, const Vector &new_i, const Vector &new_j, const Vector &new_k)
Definition: util.h:346
bool FindNonColinearVertices(int arraySize, Vector array[], int &index1, int &index2, int &index3, bool isVectorArray=true)
bool GetObjectCoordinatesTransform(DisplayObject *from, DisplayObject *to, GlsMatrixType *outTransform)
int DecodeString(char *dest, const char *src, const int dest_str_length)
bool CalculateTexPointsFromTexCoords(DisplayObject *object)
TextureLoaderList & GetTextureLoaders()
Gets the global list of texture loaders.
Definition: gls_mutex.h:52
friend bool operator!=(const GlsPropString &str1, const GlsPropString &str2)
Definition: util.h:1271
The disti::Vertex class. A class for manipulating 3D vertices.
GlsMultiVal(const GlsMultiVal &src)
Definition: util.h:1047
void PushResourcePath(const char *resourcePath)
IGlsStateManager & GetStateManager()
Gets the global state manager instance.
void Translate(Type x, Type y, Type z)
Definition: gls_matrix_affine.h:198
bool FileExists(const char *filename)
int C_EncodeString(char *dest, const char *src, const int dest_str_length)
static GlsGlobals & Instance()
Singleton instance.
virtual void StreamIn(istreamType &instr)
Definition: util.h:1162
GlsMultiVal(const T1 &val1, const T2 &val2=T2(), const T3 &val3=T3(), const T4 &val4=T4(), const T5 &val5=T5(), const T6 &val6=T6(), const T7 &val7=T7(), const T8 &val8=T8(), const T9 &val9=T9(), const T10 &val10=T10())
Definition: util.h:1074
FILE * gls_fopen(const char *filename, const char *flags)
bool IsDirectory(const char *filename)
float AngularDistanceDeg(float angle1, float angle2)
std::string FilePath(const std::string &filepath)
Definition: gls_font_man.h:59
bool Equal(T1 x, T2 y, float precision=0.001f)
Definition: util.h:246
char * GetFileName(const char *name)
List_c & GetImageList()
Gets the image list.
bool NotColinear(const Vector &a, const Vector &b, const Vector &c)
const char * GetFilePath(const char *name)
std::string ResolveRuntimeResourcePath(const char *fileName)
void PushResourceFinder(std::string(*finder)(const std::string &))
A smart pointer with unique ownership – poor man's std::unique_ptr.
std::string ResolvePath(const char *path)
bool CloseToZero(const X x, const X threshold=X(1e-1))
Definition: util.h:141
friend bool operator==(const GlsPropString &str1, const GlsPropString &str2)
Definition: util.h:1265
Contains the DistiAssert macro.
Macros and helper code to determine what subset of C++11/14/17 is available.
void GlsGetOrtho(GlsMatrixType &m, double left, double right, double bottom, double top, double zNear, double zFar)
GlsPropString(const std::string &str)
Definition: util.h:1237
int Safe_fopen(const char *filename, char *flags, FILE **f)
unsigned char * GlsDefinedColor(GlsDefinedColorEnum index)
Definition: util.h:209
Definition: bmpimage.h:46
GlsPropStringQuoted()
Definition: util.h:1301
bool IS_POSITIVE(const X &x)
Definition: util.h:137