42 #ifndef INCLUDED_GLS_RUNTIME_UTIL_H
43 #define INCLUDED_GLS_RUNTIME_UTIL_H
75 #if defined( LINUX ) || defined( __VXWORKS__ ) || defined( __APPLE__ ) || defined( QNX )
80 # include <android/asset_manager.h>
83 #if defined( _MSC_VER )
85 # pragma warning( disable : 4786 )
88 const int DIALOG_MAX_DIRECTORY_LENGTH = 1024;
93 #if defined( GLS_DEBUG ) || !defined( NDEBUG )
94 # define GLS_DEBUG_CODE( x ) x
96 # define GLS_DEBUG_CODE( x )
114 inline int strcasecmp(
const char*
const x,
const char*
const y )
116 return _stricmp( x, y );
124 const char*
const PARSER_CLEARSTRING_DELIMETER_START =
"#$STRING_START$#";
125 const char*
const PARSER_CLEARSTRING_DELIMETER_END =
"#$STRING_END$#";
127 const double HIT_TOLERANCE = 5.0;
130 template<
class X,
class X1,
class X2>
131 bool BETWEEN(
const X& x,
const X1& x1,
const X2& x2 )
133 return ( x >= x1 && x <= x2 )
134 || ( x >= x2 && x <= x1 );
139 bool IS_NEGATIVE(
const X& x ) {
return std::fabs( x ) > 0.0001 ? ( x < 0.0 ) : 0; }
143 bool IS_POSITIVE(
const X& x ) {
return std::fabs( x ) > 0.0001 ? ( x > 0.0 ) : 0; }
149 return std::fabs( x ) < threshold;
167 # define MIN( A, B ) ( ( A ) < ( B ) ? ( A ) : ( B ) )
173 # define MAX( A, B ) ( ( A ) > ( B ) ? ( A ) : ( B ) )
182 GLS_EXPORT
bool gltIsExtSupported(
const char* extension );
187 # define GLSTUDIO_CLAMP_TO_EDGE ( OpenGLVersion() >= 1.2f ? 0x812F : GL_CLAMP )
189 # define GLSTUDIO_CLAMP_TO_EDGE GL_CLAMP_TO_EDGE
217 if( ( index < 0 ) || ( index >= GLS_COLOR_MAX ) )
218 return glsDefinedColors[ 0 ];
220 return glsDefinedColors[ index ];
227 unsigned char alpha )
229 if( ( index >= 0 ) && ( index < GLS_COLOR_MAX ) )
231 glsDefinedColors[ index ][ 0 ] = red;
232 glsDefinedColors[ index ][ 1 ] = green;
233 glsDefinedColors[ index ][ 2 ] = blue;
234 glsDefinedColors[ index ][ 3 ] = alpha;
250 template<
class T1,
class T2>
251 bool Equal( T1 x, T2 y,
float precision = 0.001f )
253 return ( x - precision ) <= y && y <= ( x + precision );
262 const T&
Min(
const T& x,
const T& y )
264 return x < y ? x : y;
273 const T&
Max(
const T& x,
const T& y )
275 return x < y ? y : x;
283 template<
class T,
class Pred>
284 const T&
Min(
const T& x,
const T& y, Pred pr )
286 return pr( x, y ) ? x : y;
294 template<
class T,
class Pred>
295 const T&
Max(
const T& x,
const T& y, Pred pr )
297 return pr( x, y ) ? y : x;
336 template<
class T,
bool TypeIsSimple>
343 unsigned int size() {
return array.
Count(); }
345 void push(
const T& item )
347 array[ array.
Count() ] = item;
353 return array[ array.
Count() - 1 ];
358 if( array.
Count() > 0 )
392 rotation_scaling( 0, 0 ) = new_i.x;
393 rotation_scaling( 0, 1 ) = new_j.x;
394 rotation_scaling( 0, 2 ) = new_k.x;
395 rotation_scaling( 1, 0 ) = new_i.y;
396 rotation_scaling( 1, 1 ) = new_j.y;
397 rotation_scaling( 1, 2 ) = new_k.y;
398 rotation_scaling( 2, 0 ) = new_i.z;
399 rotation_scaling( 2, 1 ) = new_j.z;
400 rotation_scaling( 2, 2 ) = new_k.z;
403 rotation_scaling.
Invert();
406 return rotation_scaling * transform;
412 GlsMatrixAffine<T> transform;
413 GlsMatrixAffine<T> rotation_scaling;
417 transform.Translate( -new_origin );
421 rotation_scaling( 0, 0 ) = new_i.x;
422 rotation_scaling( 0, 1 ) = new_j.x;
423 rotation_scaling( 0, 2 ) = new_k.x;
424 rotation_scaling( 1, 0 ) = new_i.y;
425 rotation_scaling( 1, 1 ) = new_j.y;
426 rotation_scaling( 1, 2 ) = new_k.y;
427 rotation_scaling( 2, 0 ) = new_i.z;
428 rotation_scaling( 2, 1 ) = new_j.z;
429 rotation_scaling( 2, 2 ) = new_k.z;
432 rotation_scaling.Invert();
435 result = rotation_scaling * transform;
500 int& index1,
int& index2,
int& index3,
501 bool isVectorArray =
true );
524 GLS_EXPORT
int EncodeString(
char* dest,
const char* src,
const int dest_str_length );
530 GLS_EXPORT std::string
EncodeString(
const std::string& src );
543 GLS_EXPORT
int C_EncodeString(
char* dest,
const char* src,
const int dest_str_length );
557 GLS_EXPORT
int DecodeString(
char* dest,
const char* src,
const int dest_str_length );
563 GLS_EXPORT std::string
DecodeString(
const std::string& src );
571 unsigned char* pixels;
574 GLS_EXPORT
char* MakeRelativePath(
const char* originalPath,
const char* relativePath );
575 GLS_EXPORT
void ConvertBackslashToSlash(
char* str );
576 GLS_EXPORT
void ConvertBackslashToSlash( std::string& str );
593 GLS_EXPORT
int range_check(
int num,
double val, ... );
602 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, FILE** f );
604 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, std::fstream& outstr );
613 GLS_EXPORT
void EnableDirectAssetLoading(
bool enableDirectAssetLoading );
621 GLS_EXPORT
bool HasAssetExtension(
const char* filename );
629 GLS_EXPORT
bool AssetExists(
const char* filename );
636 GLS_EXPORT
void SetAssetManager( AAssetManager* assetManager );
639 #if defined( ANDROID ) || defined( __APPLE__ ) || ( defined( LINUX ) && defined( GLES ) ) || defined( QNX ) || defined( INTEGRITY )
645 GLS_EXPORT std::string
ResolvePath(
const char* filename_ );
651 GLS_EXPORT
void SetResourcePath(
const char* resourcePath );
662 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
687 GLS_EXPORT std::string
ResolvePath(
const char* path );
690 inline std::string
ResolvePath(
const std::string& path )
701 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
711 GLS_EXPORT std::string GetAbsolutePathAndFileNameInDefaultResourceBundle(
const char* fileName );
724 GLS_EXPORT
bool FileExists(
const char* filename );
725 GLS_EXPORT
bool FileExists(
const std::string& filename );
730 GLS_EXPORT
bool IsDirectory(
const char* filename );
731 GLS_EXPORT
bool IsDirectory(
const std::string& filename );
736 GLS_EXPORT std::string
FileExtension(
const std::string& filepath );
741 GLS_EXPORT std::string
FileName(
const std::string& filepath );
747 GLS_EXPORT std::string
FilePath(
const std::string& filepath );
755 GLS_EXPORT
const char*
GetFilePath(
const char* name );
781 GLS_EXPORT
char*
PathToOS(
const char* path );
788 GLS_EXPORT std::string
GetExtension(
const std::string& filename );
794 GLS_EXPORT
const char*
GetBaseName(
const char* path );
828 GLS_EXPORT
bool ContainsNonBlank(
const std::string& val );
830 GLS_EXPORT
bool GetNoSpaces( FILE* f,
char* result,
int maxLen );
831 GLS_EXPORT
bool GetToEnd( std::istream& instr, std::string& result,
bool decode );
832 GLS_EXPORT
bool GetVertex( std::istream& instr, Vertex* vert,
bool getColor );
837 GLS_EXPORT
bool GetComponentClassNames(
const char* dllFileName, DynamicArray<std::string, false>& nameList,
const char* createClassTag =
"CreateComponent_" );
853 #if !defined( GLES ) || defined( GLES_ANGLE )
854 GLS_EXPORT
bool OpenFileDialog( Fl_Window* win,
char* filePath,
unsigned int filePathSize,
855 char* directory = NULL,
const char* filterStr = NULL,
856 const char* defaultExt = NULL,
const char* title = NULL,
857 bool multiSelect =
false,
bool createFile =
false,
858 bool fileMustExist =
false,
bool pathMustExist =
false,
859 bool noChangeDirectory =
false );
861 GLS_EXPORT
bool SaveFileDialog( Fl_Window* win,
char* filePath,
unsigned int filePathSize,
862 char* directory = NULL,
const char* filterStr = NULL,
863 const char* defaultExt = NULL,
const char* title = NULL,
864 bool createFile =
false,
865 bool fileMustExist =
false,
bool pathMustExist =
false,
866 bool noChangeDirectory =
false );
868 GLS_EXPORT
void CheckGLError(
void );
878 GLS_EXPORT std::string
Uppercase(
const std::string& str );
880 GLS_EXPORT std::string ReplaceEnvironmentVariables(
const char* originalPath );
889 GLS_EXPORT
void ReadCommandLine(
int argc,
char** argv );
890 GLS_EXPORT
void Usage();
895 bool SilentMode(
void ) {
return _silentMode; }
897 static GLS_EXPORT glsCommandLine* Instance();
898 static glsCommandLine* _instance;
912 GLS_EXPORT InterfaceDescriptionClass(
const InterfaceDescriptionClass& source );
914 GLS_EXPORT ~InterfaceDescriptionClass();
916 GLS_EXPORT InterfaceDescriptionClass(
const char* code,
const char* usage,
const char* comment );
918 GLS_EXPORT
void operator=(
const InterfaceDescriptionClass& source );
919 GLS_EXPORT
void Code(
const char* );
920 GLS_EXPORT
const char* Code()
const;
921 GLS_EXPORT
void Usage(
const char* );
922 GLS_EXPORT
const char* Usage()
const;
923 GLS_EXPORT
void Comment(
const char* );
924 GLS_EXPORT
const char* Comment()
const;
936 typedef std::ostream ostreamType;
937 typedef std::istream istreamType;
939 virtual void StreamOut( ostreamType& outstr )
const = 0;
940 virtual void StreamIn( istreamType& instr ) = 0;
974 if(
typeid( T2 ) !=
typeid(
void* ) )
978 if(
typeid( T3 ) !=
typeid(
void* ) )
982 if(
typeid( T4 ) !=
typeid(
void* ) )
986 if(
typeid( T5 ) !=
typeid(
void* ) )
990 if(
typeid( T6 ) !=
typeid(
void* ) )
994 if(
typeid( T7 ) !=
typeid(
void* ) )
998 if(
typeid( T8 ) !=
typeid(
void* ) )
1002 if(
typeid( T9 ) !=
typeid(
void* ) )
1006 if(
typeid( T10 ) !=
typeid(
void* ) )
1038 : _val1( src._val1 )
1039 , _val2( src._val2 )
1040 , _val3( src._val3 )
1041 , _val4( src._val4 )
1042 , _val5( src._val5 )
1043 , _val6( src._val6 )
1044 , _val7( src._val7 )
1045 , _val8( src._val8 )
1046 , _val9( src._val9 )
1047 , _val10( src._val10 )
1066 const T2& val2 = T2(),
1067 const T3& val3 = T3(),
1068 const T4& val4 = T4(),
1069 const T5& val5 = T5(),
1070 const T6& val6 = T6(),
1071 const T7& val7 = T7(),
1072 const T8& val8 = T8(),
1073 const T9& val9 = T9(),
1074 const T10& val10 = T10() )
1097 case 10: rval &= ( _val10 == val._val10 );
1098 case 9: rval &= ( _val9 == val._val9 );
1099 case 8: rval &= ( _val8 == val._val8 );
1100 case 7: rval &= ( _val7 == val._val7 );
1101 case 6: rval &= ( _val6 == val._val6 );
1102 case 5: rval &= ( _val5 == val._val5 );
1103 case 4: rval &= ( _val4 == val._val4 );
1104 case 3: rval &= ( _val3 == val._val3 );
1105 case 2: rval &= ( _val2 == val._val2 );
1107 rval &= ( _val1 == val._val1 );
1118 return !( *
this == val );
1129 outstr <<
" " << _val2;
1131 outstr <<
" " << _val3;
1133 outstr <<
" " << _val4;
1135 outstr <<
" " << _val5;
1137 outstr <<
" " << _val6;
1139 outstr <<
" " << _val7;
1141 outstr <<
" " << _val8;
1143 outstr <<
" " << _val9;
1144 if( _numVals >= 10 )
1145 outstr <<
" " << _val10;
1195 if( _numVals >= 10 )
1203 GLS_EXPORT std::ostream&
operator<<( std::ostream& outstr,
const GlsMultiValBase& multiVal );
1204 GLS_EXPORT std::istream& operator>>( std::istream& instr, GlsMultiValBase& multiVal );
1216 std::string _string;
1237 : _string( str ? str :
"" )
1241 operator std::string()
const
1257 return str1._string == str2._string;
1263 return !( str1 == str2 );
1267 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropString& str )
1272 inline std::istream& operator>>( std::istream& instr, GlsPropString& str )
1275 disti::GetToEnd( instr, temp,
false );
1288 std::string _string;
1311 operator std::string()
const
1316 std::string& String()
1323 return str._string == _string;
1327 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropStringQuoted& str )
1333 inline std::istream& operator>>( std::istream& instr, GlsPropStringQuoted& str )
1340 if( instr.peek() !=
'\"' )
1342 disti::GetToEnd( instr, temp,
false );
1352 while( instr.good() )
1354 lastChar = currChar;
1355 currChar = instr.get();
1356 if( currChar != -1 )
1358 if( currChar ==
'\"' && lastChar !=
'\\' )
1360 temp += (char)currChar;
1371 void SpawnBrowser(
const char* url );
1381 GLS_EXPORT
bool CheckDistiLicense(
const char* licenseGroupName,
const char* feature,
const char* version,
bool quiet );
1415 T Clamp(
const T& val,
const T& min,
const T& max )
1417 return std::min( max, std::max( min, val ) );
1430 inline void Split(
const std::string& s,
const char delim, std::vector<std::string>& elems,
const std::size_t maxElems = 0 )
1432 std::istringstream ss( s );
1434 while( std::getline( ss, item, delim ) )
1436 elems.push_back( DISTI_RVAL_MOVE( item ) );
1437 if( elems.size() == maxElems )
1442 if( elems.size() == maxElems && ss.good() && maxElems > 0 )
1444 std::string remainder;
1445 std::getline( ss, remainder,
'\0' );
1446 elems.back() += delim + remainder;
1460 inline std::vector<std::string> Split(
const std::string& s,
const char delim,
const std::size_t maxElems = 0 )
1462 std::vector<std::string> elems;
1463 Split( s, delim, elems, maxElems );
1468 inline std::string MakeString(
const char*
const cStr )
1470 return ( cStr ? cStr :
"" );
1477 class IGlsStateManager;
1480 class TextureLoaderList;
1514 void operator=(
const GlsGlobals& ) DISTI_SPECIAL_MEM_FUN_DELETE;
1520 GLS_EXPORT
void glsPerspective(
double fovy,
double aspect,
double zNear,
double zFar );
1522 #ifdef MATRIX_TYPE_FLOAT
1523 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1524 const float modelMatrix[ 16 ],
1525 const float projMatrix[ 16 ],
1526 const int viewport[ 4 ],
1527 double* winx,
double* winy,
double* winz );
1529 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1530 const float modelMatrix[ 16 ],
1531 const float projMatrix[ 16 ],
1532 const int viewport[ 4 ],
1533 double* objx,
double* objy,
double* objz );
1535 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1536 const double modelMatrix[ 16 ],
1537 const double projMatrix[ 16 ],
1538 const int viewport[ 4 ],
1539 double* winx,
double* winy,
double* winz );
1541 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1542 const double modelMatrix[ 16 ],
1543 const double projMatrix[ 16 ],
1544 const int viewport[ 4 ],
1545 double* objx,
double* objy,
double* objz );
The DistiUnhideGlobalsDummyClass class.
const T & Min(const T &x, const T &y)
Definition: util.h:262
int EncodedStringMinLength()
GlsPropStringQuoted(const std::string &str)
Definition: util.h:1298
virtual void StreamOut(ostreamType &outstr) const
Definition: util.h:1125
std::string GetExtension(const std::string &filename)
GlsDefinedColorEnum
Definition: util.h:194
bool IS_NEGATIVE(const X &x)
Definition: util.h:139
const int INLINE_TEXTURE_LINE_LENGTH
Definition: util.h:121
std::string & String()
Definition: util.h:1249
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:1236
unsigned char glsDefinedColors[GLS_COLOR_MAX][4]
bool IsNearZero(X x)
Definition: util.h:158
virtual bool operator!=(const GlsMultiVal &val) const
Definition: util.h:1116
Definition: dynamic_array.h:62
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:301
std::string FileName(const std::string &filepath)
const char * GetBaseName(const char *path)
bool VeryCloseToZero(X x)
Definition: util.h:154
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:1483
std::string GetDefaultComponentClassName(const char *dllFileName)
bool BETWEEN(const X &x, const X1 &x1, const X2 &x2)
Definition: util.h:131
GlsMultiVal()
Definition: util.h:1027
char * PathToOS(const char *path)
bool IS_ZERO(X x)
Definition: util.h:162
Definition: gls_state_manager_interface.h:67
void RemoveSpaces(std::string &entry)
const T & Max(const T &x, const T &y)
Definition: util.h:273
virtual bool operator==(const GlsMultiVal &val) const
Definition: util.h:1090
GlsPropStringQuoted(const char *str)
Definition: util.h:1306
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:223
Definition: scoped_ptr.h:53
GlsPropString()
Definition: util.h:1219
VertexNoColor Vector
Definition: gls_font_base.h:66
GlsMatrixAffine< T > FindCoordinateTransformation(const Vector &new_origin, const Vector &new_i, const Vector &new_j, const Vector &new_k)
Definition: util.h:381
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 GetComponentClassNames(const char *dllFileName, DynamicArray< std::string, false > &nameList, const char *createClassTag="CreateComponent_")
void Count(const unsigned int count)
Definition: dynamic_array.h:115
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:1261
The disti::Vertex class. A class for manipulating 3D vertices.
GlsMultiVal(const GlsMultiVal &src)
Definition: util.h:1037
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:196
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:1152
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:1064
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:251
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:147
friend bool operator==(const GlsPropString &str1, const GlsPropString &str2)
Definition: util.h:1255
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:1227
int Safe_fopen(const char *filename, char *flags, FILE **f)
unsigned char * GlsDefinedColor(GlsDefinedColorEnum index)
Definition: util.h:215
Definition: bmpimage.h:46
GlsPropStringQuoted()
Definition: util.h:1291
bool IS_POSITIVE(const X &x)
Definition: util.h:143