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;
465 int& index1,
int& index2,
int& index3,
466 bool isVectorArray =
true );
489 GLS_EXPORT
int EncodeString(
char* dest,
const char* src,
const int dest_str_length );
495 GLS_EXPORT std::string
EncodeString(
const std::string& src );
508 GLS_EXPORT
int C_EncodeString(
char* dest,
const char* src,
const int dest_str_length );
522 GLS_EXPORT
int DecodeString(
char* dest,
const char* src,
const int dest_str_length );
528 GLS_EXPORT std::string
DecodeString(
const std::string& src );
536 unsigned char* pixels;
539 GLS_EXPORT
char* MakeRelativePath(
const char* originalPath,
const char* relativePath );
540 GLS_EXPORT
void ConvertBackslashToSlash(
char* str );
541 GLS_EXPORT
void ConvertBackslashToSlash( std::string& str );
558 GLS_EXPORT
int range_check(
int num,
double val, ... );
567 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, FILE** f );
569 GLS_EXPORT
int Safe_fopen(
const char* filename,
char* flags, std::fstream& outstr );
578 GLS_EXPORT
void EnableDirectAssetLoading(
bool enableDirectAssetLoading );
586 GLS_EXPORT
bool HasAssetExtension(
const char* filename );
594 GLS_EXPORT
bool AssetExists(
const char* filename );
601 GLS_EXPORT
void SetAssetManager( AAssetManager* assetManager );
604 #if defined( ANDROID ) || defined( __APPLE__ ) || ( defined( LINUX ) && defined( GLES ) ) || defined( QNX ) || defined( INTEGRITY )
610 GLS_EXPORT std::string
ResolvePath(
const char* filename_ );
616 GLS_EXPORT
void SetResourcePath(
const char* resourcePath );
627 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
652 GLS_EXPORT std::string
ResolvePath(
const char* path );
655 inline std::string
ResolvePath(
const std::string& path )
666 GLS_EXPORT FILE*
gls_fopen(
const char* filename,
const char* flags );
676 GLS_EXPORT std::string GetAbsolutePathAndFileNameInDefaultResourceBundle(
const char* fileName );
689 GLS_EXPORT
bool FileExists(
const char* filename );
690 GLS_EXPORT
bool FileExists(
const std::string& filename );
695 GLS_EXPORT
bool IsDirectory(
const char* filename );
696 GLS_EXPORT
bool IsDirectory(
const std::string& filename );
701 GLS_EXPORT std::string
FileExtension(
const std::string& filepath );
706 GLS_EXPORT std::string
FileName(
const std::string& filepath );
712 GLS_EXPORT std::string
FilePath(
const std::string& filepath );
720 GLS_EXPORT
const char*
GetFilePath(
const char* name );
746 GLS_EXPORT
char*
PathToOS(
const char* path );
753 GLS_EXPORT std::string
GetExtension(
const std::string& filename );
759 GLS_EXPORT
const char*
GetBaseName(
const char* path );
793 GLS_EXPORT
bool ContainsNonBlank(
const std::string& val );
795 GLS_EXPORT
bool GetNoSpaces( FILE* f,
char* result,
int maxLen );
796 GLS_EXPORT
bool GetToEnd( std::istream& instr, std::string& result,
bool decode );
797 GLS_EXPORT
bool GetVertex( std::istream& instr, Vertex* vert,
bool getColor );
802 GLS_EXPORT
bool GetComponentClassNames(
const char* dllFileName, DynamicArray<std::string>& nameList,
const char* createClassTag =
"CreateComponent_" );
818 #if !defined( GLES ) || defined( GLES_ANGLE )
819 GLS_EXPORT
bool OpenFileDialog( Fl_Window* win,
char* filePath,
unsigned int filePathSize,
820 char* directory = NULL,
const char* filterStr = NULL,
821 const char* defaultExt = NULL,
const char* title = NULL,
822 bool multiSelect =
false,
bool createFile =
false,
823 bool fileMustExist =
false,
bool pathMustExist =
false,
824 bool noChangeDirectory =
false );
826 GLS_EXPORT
bool SaveFileDialog( 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 createFile =
false,
830 bool fileMustExist =
false,
bool pathMustExist =
false,
831 bool noChangeDirectory =
false );
833 GLS_EXPORT
void CheckGLError(
void );
843 GLS_EXPORT std::string
Uppercase(
const std::string& str );
845 GLS_EXPORT std::string ReplaceEnvironmentVariables(
const char* originalPath );
852 GLS_EXPORT
void ReadCommandLine(
int argc,
char** argv );
853 GLS_EXPORT
void Usage();
861 static GLS_EXPORT glsCommandLine* Instance();
877 GLS_EXPORT InterfaceDescriptionClass(
const char* code,
const char* usage,
const char* comment );
879 GLS_EXPORT ~InterfaceDescriptionClass();
880 GLS_EXPORT InterfaceDescriptionClass(
const InterfaceDescriptionClass& source );
881 GLS_EXPORT InterfaceDescriptionClass& operator=(
const InterfaceDescriptionClass& source );
883 GLS_EXPORT
void Code(
const char* );
884 GLS_EXPORT
void Usage(
const char* );
885 GLS_EXPORT
void Comment(
const char* );
887 const char* Code()
const {
return _code; }
888 const char* Usage()
const {
return _usage; }
889 const char* Comment()
const {
return _comment; }
891 #if defined( DISTI_HAS_RVAL_REFS )
892 InterfaceDescriptionClass( InterfaceDescriptionClass&& source )
900 InterfaceDescriptionClass& operator=( InterfaceDescriptionClass&& source )
902 if(
this != &source )
915 void StealFrom( InterfaceDescriptionClass& source )
918 _code = source._code;
919 _usage = source._usage;
920 _comment = source._comment;
924 source._usage = NULL;
925 source._comment = NULL;
939 typedef std::ostream ostreamType;
940 typedef std::istream istreamType;
942 virtual void StreamOut( ostreamType& outstr )
const = 0;
943 virtual void StreamIn( istreamType& instr ) = 0;
977 if(
typeid( T2 ) !=
typeid(
void* ) )
981 if(
typeid( T3 ) !=
typeid(
void* ) )
985 if(
typeid( T4 ) !=
typeid(
void* ) )
989 if(
typeid( T5 ) !=
typeid(
void* ) )
993 if(
typeid( T6 ) !=
typeid(
void* ) )
997 if(
typeid( T7 ) !=
typeid(
void* ) )
1001 if(
typeid( T8 ) !=
typeid(
void* ) )
1005 if(
typeid( T9 ) !=
typeid(
void* ) )
1009 if(
typeid( T10 ) !=
typeid(
void* ) )
1041 : _val1( src._val1 )
1042 , _val2( src._val2 )
1043 , _val3( src._val3 )
1044 , _val4( src._val4 )
1045 , _val5( src._val5 )
1046 , _val6( src._val6 )
1047 , _val7( src._val7 )
1048 , _val8( src._val8 )
1049 , _val9( src._val9 )
1050 , _val10( src._val10 )
1069 const T2& val2 = T2(),
1070 const T3& val3 = T3(),
1071 const T4& val4 = T4(),
1072 const T5& val5 = T5(),
1073 const T6& val6 = T6(),
1074 const T7& val7 = T7(),
1075 const T8& val8 = T8(),
1076 const T9& val9 = T9(),
1077 const T10& val10 = T10() )
1100 case 10: rval &= ( _val10 == val._val10 );
1101 case 9: rval &= ( _val9 == val._val9 );
1102 case 8: rval &= ( _val8 == val._val8 );
1103 case 7: rval &= ( _val7 == val._val7 );
1104 case 6: rval &= ( _val6 == val._val6 );
1105 case 5: rval &= ( _val5 == val._val5 );
1106 case 4: rval &= ( _val4 == val._val4 );
1107 case 3: rval &= ( _val3 == val._val3 );
1108 case 2: rval &= ( _val2 == val._val2 );
1110 rval &= ( _val1 == val._val1 );
1121 return !( *
this == val );
1132 outstr <<
" " << _val2;
1134 outstr <<
" " << _val3;
1136 outstr <<
" " << _val4;
1138 outstr <<
" " << _val5;
1140 outstr <<
" " << _val6;
1142 outstr <<
" " << _val7;
1144 outstr <<
" " << _val8;
1146 outstr <<
" " << _val9;
1147 if( _numVals >= 10 )
1148 outstr <<
" " << _val10;
1198 if( _numVals >= 10 )
1206 GLS_EXPORT std::ostream&
operator<<( std::ostream& outstr,
const GlsMultiValBase& multiVal );
1207 GLS_EXPORT std::istream& operator>>( std::istream& instr, GlsMultiValBase& multiVal );
1219 std::string _string;
1240 : _string( str ? str :
"" )
1244 operator std::string()
const
1260 return str1._string == str2._string;
1266 return !( str1 == str2 );
1270 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropString& str )
1275 inline std::istream& operator>>( std::istream& instr, GlsPropString& str )
1278 disti::GetToEnd( instr, temp,
false );
1291 std::string _string;
1314 operator std::string()
const
1319 std::string& String()
1326 return str._string == _string;
1330 inline std::ostream&
operator<<( std::ostream& outstr,
const GlsPropStringQuoted& str )
1336 inline std::istream& operator>>( std::istream& instr, GlsPropStringQuoted& str )
1343 if( instr.peek() !=
'\"' )
1345 disti::GetToEnd( instr, temp,
false );
1355 while( instr.good() )
1357 lastChar = currChar;
1358 currChar = instr.get();
1359 if( currChar != -1 )
1361 if( currChar ==
'\"' && lastChar !=
'\\' )
1363 temp += (char)currChar;
1374 void SpawnBrowser(
const char* url );
1384 GLS_EXPORT
bool CheckDistiLicense(
const char* licenseGroupName,
const char* feature,
const char* version,
bool quiet );
1418 T Clamp(
const T& val,
const T& min,
const T& max )
1420 return std::min( max, std::max( min, val ) );
1433 inline void Split(
const std::string& s,
const char delim, std::vector<std::string>& elems,
const std::size_t maxElems = 0 )
1435 std::istringstream ss( s );
1437 while( std::getline( ss, item, delim ) )
1439 elems.push_back( DISTI_RVAL_MOVE( item ) );
1440 if( elems.size() == maxElems )
1445 if( elems.size() == maxElems && ss.good() && maxElems > 0 )
1447 std::string remainder;
1448 std::getline( ss, remainder,
'\0' );
1449 elems.back() += delim + remainder;
1463 inline std::vector<std::string> Split(
const std::string& s,
const char delim,
const std::size_t maxElems = 0 )
1465 std::vector<std::string> elems;
1466 Split( s, delim, elems, maxElems );
1471 inline std::string MakeString(
const char*
const cStr )
1473 return ( cStr ? cStr :
"" );
1480 class IGlsStateManager;
1483 class TextureLoaderList;
1517 void operator=(
const GlsGlobals& ) DISTI_SPECIAL_MEM_FUN_DELETE;
1524 inline void _checkPointerSize()
1528 || ( DISTI_IS_BITNESS( 32 ) &&
sizeof(
void* ) == 4 ),
1529 "Pointer size is unexpected for this bitness" );
1530 DISTI_STATIC_ASSERT_STR( DISTI_IS_BITNESS( 64 ) ^ DISTI_IS_BITNESS( 32 ),
"Expected one bitness to be detected." );
1537 GLS_EXPORT
void glsPerspective(
double fovy,
double aspect,
double zNear,
double zFar );
1539 #ifdef MATRIX_TYPE_FLOAT
1540 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1541 const float modelMatrix[ 16 ],
1542 const float projMatrix[ 16 ],
1543 const int viewport[ 4 ],
1544 double* winx,
double* winy,
double* winz );
1546 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1547 const float modelMatrix[ 16 ],
1548 const float projMatrix[ 16 ],
1549 const int viewport[ 4 ],
1550 double* objx,
double* objy,
double* objz );
1552 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1553 const double modelMatrix[ 16 ],
1554 const double projMatrix[ 16 ],
1555 const int viewport[ 4 ],
1556 double* winx,
double* winy,
double* winz );
1558 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1559 const double modelMatrix[ 16 ],
1560 const double projMatrix[ 16 ],
1561 const int viewport[ 4 ],
1562 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:1301
virtual void StreamOut(ostreamType &outstr) const
Definition: util.h:1128
#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:1252
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:1239
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:1119
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:1486
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:1030
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:1093
GlsPropStringQuoted(const char *str)
Definition: util.h:1309
bool SilentMode() const
Definition: util.h:859
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:1222
bool GetComponentClassNames(const char *dllFileName, DynamicArray< std::string > &nameList, const char *createClassTag="CreateComponent_")
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: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:1264
The disti::Vertex class. A class for manipulating 3D vertices.
GlsMultiVal(const GlsMultiVal &src)
Definition: util.h:1040
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:1155
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:1067
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:1258
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:1230
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:1294
bool IS_POSITIVE(const X &x)
Definition: util.h:137