42 #ifndef _GLS_RUNTIME_UTIL_H
43 #define _GLS_RUNTIME_UTIL_H
66 #if (defined(LINUX) || defined(SUNOS) || defined(SGI)) && !defined(ANDROID) && !defined(NOSOUND)
67 #include <audiofile.h>
71 #include <android/asset_manager.h>
76 #pragma warning (disable:4786)
79 const int DIALOG_MAX_DIRECTORY_LENGTH = 1024;
99 #define strcasecmp(x,y) _stricmp(x,y)
107 #define PARSER_CLEARSTRING_DELIMETER_START "#$STRING_START$#"
108 #define PARSER_CLEARSTRING_DELIMETER_END "#$STRING_END$#"
111 #define M_PI 3.14159265358979323846
116 #define DEG_TO_RAD (M_PI/180.0)
119 #define RAD_TO_DEG (180.0/M_PI)
121 #define HIT_TOLERANCE 5.0
125 #define BETWEEN(x,x1,x2) ((((x) >= (x1)) && ((x) <= (x2))) || \
126 (((x) >= (x2)) && ((x) <= (x1))))
129 #define IS_NEGATIVE(x) ((fabs(x) > 0.0001)?(x < 0.0):0)
132 #define IS_POSITIVE(x) ((fabs(x) > 0.0001)?(x > 0.0):0)
135 #define CloseToZero(x) (fabs((x)) < 0.1)
138 #define VeryCloseToZero(x) (fabs((x)) < 0.0001)
141 #define IS_ZERO(x) (fabs((x)) < 0.001)
145 #define MIN(A,B) ((A) < (B) ? (A) : (B))
150 #define MAX(A,B) ((A) > (B) ? (A) : (B))
159 GLS_EXPORT
bool gltIsExtSupported(
const char *extension);
165 # define GLSTUDIO_CLAMP_TO_EDGE ( OpenGLVersion() >= 1.2f ? 0x812F : GL_CLAMP)
167 # define GLSTUDIO_CLAMP_TO_EDGE GL_CLAMP_TO_EDGE
170 # define GLSTUDIO_CLAMP_TO_EDGE GL_CLAMP_TO_EDGE
203 if ((index <0) || (index >= GLS_COLOR_MAX))
204 return glsDefinedColors[0];
206 return glsDefinedColors[index];
217 if ((index >= 0) && (index < GLS_COLOR_MAX))
219 glsDefinedColors[index][0] = red;
220 glsDefinedColors[index][1] = green;
221 glsDefinedColors[index][2] = blue;
222 glsDefinedColors[index][3] = alpha;
238 template<
class T1,
class T2>
239 bool Equal(T1 x, T2 y,
float precision = 0.001f)
241 return (x - precision) <= y && y <= (x + precision);
250 const T&
Min(
const T& x,
const T& y)
252 return x < y ? x : y;
261 const T&
Max(
const T& x,
const T& y)
263 return x < y ? y : x;
271 template<
class T,
class Pred>
272 const T&
Min(
const T& x,
const T& y, Pred pr)
274 return pr(x, y) ? x : y;
282 template<
class T,
class Pred>
283 const T&
Max(
const T& x,
const T& y, Pred pr)
285 return pr(x, y) ? y : x;
324 template <
class T,
bool TypeIsSimple>
330 unsigned int size() {
return array.
Count(); }
332 void push(
const T& item)
334 array[array.
Count()] = item;
340 return array[array.
Count()-1];
345 if(array.
Count() > 0)
379 rotation_scaling(0,0) = new_i.x; rotation_scaling(0,1) = new_j.x; rotation_scaling(0,2) = new_k.x;
380 rotation_scaling(1,0) = new_i.y; rotation_scaling(1,1) = new_j.y; rotation_scaling(1,2) = new_k.y;
381 rotation_scaling(2,0) = new_i.z; rotation_scaling(2,1) = new_j.z; rotation_scaling(2,2) = new_k.z;
384 rotation_scaling.
Invert();
387 return rotation_scaling * transform;
394 GlsMatrixAffine<T> transform;
395 GlsMatrixAffine<T> rotation_scaling;
399 transform.Translate(-new_origin);
403 rotation_scaling(0,0) = new_i.x; rotation_scaling(0,1) = new_j.x; rotation_scaling(0,2) = new_k.x;
404 rotation_scaling(1,0) = new_i.y; rotation_scaling(1,1) = new_j.y; rotation_scaling(1,2) = new_k.y;
405 rotation_scaling(2,0) = new_i.z; rotation_scaling(2,1) = new_j.z; rotation_scaling(2,2) = new_k.z;
408 rotation_scaling.Invert();
411 result = rotation_scaling * transform;
478 int& index1,
int& index2,
int& index3,
479 bool isVectorArray =
true);
502 GLS_EXPORT
int EncodeString(
char* dest,
const char* src,
const int dest_str_length );
508 GLS_EXPORT std::string
EncodeString(
const std::string& src);
521 GLS_EXPORT
int C_EncodeString(
char *dest,
const char *src,
const int dest_str_length );
535 GLS_EXPORT
int DecodeString(
char *dest,
const char *src,
const int dest_str_length);
541 GLS_EXPORT std::string
DecodeString(
const std::string& src);
550 unsigned char *pixels;
553 GLS_EXPORT
char *MakeRelativePath(
const char *originalPath,
const char *relativePath);
554 GLS_EXPORT
void ConvertBackslashToSlash(
char *str);
555 GLS_EXPORT
void ConvertBackslashToSlash(std::string &str);
573 GLS_EXPORT
int range_check(
int num,
double val,...);
582 GLS_EXPORT
int Safe_fopen(
const char *filename,
char *flags,FILE **f);
584 GLS_EXPORT
int Safe_fopen(
const char *filename,
char *flags,std::fstream &outstr);
593 GLS_EXPORT
void EnableDirectAssetLoading(
bool enableDirectAssetLoading);
601 GLS_EXPORT
bool HasAssetExtension(
const char *filename);
609 GLS_EXPORT
bool AssetExists(
const char *filename);
616 GLS_EXPORT
void SetAssetManager(AAssetManager *assetManager);
619 #if defined(ANDROID) || ( defined(LINUX) && defined(GLES) )
625 GLS_EXPORT std::string
ResolvePath(
const char* filename_);
631 GLS_EXPORT
void SetResourcePath(
const char *resourcePath);
642 GLS_EXPORT FILE *
gls_fopen(
const char *filename,
const char *flags);
667 GLS_EXPORT std::string
ResolvePath(
const char *path);
670 inline std::string
ResolvePath(
const std::string& path )
681 GLS_EXPORT FILE *
gls_fopen(
const char *filename,
const char *flags);
684 #if !defined(NOSOUND)
685 #if (defined(LINUX) || defined(SUNOS) || defined(SGI)) && !defined(ANDROID)
686 GLS_EXPORT AFfilehandle gls_afOpenFile(
const char *path,
const char *mode,AFfilesetup setup);
697 GLS_EXPORT std::string GetAbsolutePathAndFileNameInDefaultResourceBundle(
const char* fileName);
710 GLS_EXPORT
bool FileExists(
const char *filename);
711 GLS_EXPORT
bool FileExists(
const std::string &filename);
716 GLS_EXPORT
bool IsDirectory(
const char* filename );
717 GLS_EXPORT
bool IsDirectory(
const std::string& filename );
722 GLS_EXPORT std::string
FileExtension(
const std::string& filepath);
727 GLS_EXPORT std::string
FileName(
const std::string& filepath);
733 GLS_EXPORT std::string
FilePath(
const std::string& filepath);
739 GLS_EXPORT
const char *
GetFilePath(
const char *name);
763 GLS_EXPORT
char *
PathToOS(
const char *path);
770 GLS_EXPORT
const std::string
GetExtension(
const std::string &filename);
799 GLS_EXPORT
bool ContainsNonBlank(
const std::string& val );
801 GLS_EXPORT
bool GetNoSpaces(FILE *f,
char *result,
int maxLen);
802 GLS_EXPORT
bool GetToEnd(std::istream &instr, std::string &result,
bool decode);
803 GLS_EXPORT
bool GetVertex(std::istream &instr, Vertex* vert,
bool getColor);
808 GLS_EXPORT
bool GetComponentClassNames(
const char* dllFileName, DynamicArray<std::string,false>& nameList,
const char* createClassTag =
"CreateComponent_" );
825 GLS_EXPORT
bool OpenFileDialog(Fl_Window *win,
char *filePath,
unsigned int filePathSize,
826 char *directory = NULL,
const char *filterStr = NULL,
827 const char *defaultExt = NULL,
const char *title = NULL,
828 bool multiSelect =
false,
bool createFile =
false,
829 bool fileMustExist =
false,
bool pathMustExist =
false,
830 bool noChangeDirectory =
false);
832 GLS_EXPORT
bool SaveFileDialog(Fl_Window *win,
char *filePath,
unsigned int filePathSize,
833 char *directory = NULL,
const char *filterStr = NULL,
834 const char *defaultExt = NULL,
const char *title = NULL,
835 bool createFile =
false,
836 bool fileMustExist =
false,
bool pathMustExist =
false,
837 bool noChangeDirectory =
false);
839 GLS_EXPORT
void CheckGLError(
void);
849 GLS_EXPORT std::string
Uppercase(
const std::string& str);
851 GLS_EXPORT std::string ReplaceEnvironmentVariables(
const char* originalPath);
862 GLS_EXPORT
void ReadCommandLine(
int argc,
char **argv );
863 GLS_EXPORT
void Usage();
868 bool SilentMode(
void) {
return _silentMode; }
870 static GLS_EXPORT glsCommandLine* Instance();
871 static glsCommandLine *_instance;
885 GLS_EXPORT InterfaceDescriptionClass(
const InterfaceDescriptionClass& source);
887 GLS_EXPORT ~InterfaceDescriptionClass();
889 GLS_EXPORT InterfaceDescriptionClass(
const char* code,
const char* usage,
const char* comment);
891 GLS_EXPORT
void operator=(
const InterfaceDescriptionClass& source);
892 GLS_EXPORT
void Code(
const char*);
893 GLS_EXPORT
const char* Code()
const;
894 GLS_EXPORT
void Usage(
const char*);
895 GLS_EXPORT
const char* Usage()
const;
896 GLS_EXPORT
void Comment(
const char*);
897 GLS_EXPORT
const char* Comment()
const;
909 typedef std::ostream ostreamType;
910 typedef std::istream istreamType;
912 virtual void StreamOut(ostreamType & outstr)
const = 0;
913 virtual void StreamIn(istreamType & instr) = 0;
948 if (
typeid(T2) !=
typeid(
void*)) _numVals++;
else return;
949 if (
typeid(T3) !=
typeid(
void*)) _numVals++;
else return;
950 if (
typeid(T4) !=
typeid(
void*)) _numVals++;
else return;
951 if (
typeid(T5) !=
typeid(
void*)) _numVals++;
else return;
952 if (
typeid(T6) !=
typeid(
void*)) _numVals++;
else return;
953 if (
typeid(T7) !=
typeid(
void*)) _numVals++;
else return;
954 if (
typeid(T8) !=
typeid(
void*)) _numVals++;
else return;
955 if (
typeid(T9) !=
typeid(
void*)) _numVals++;
else return;
956 if (
typeid(T10) !=
typeid(
void*)) _numVals++;
else return;
1023 const T2& val2 = T2(),
1024 const T3& val3 = T3(),
1025 const T4& val4 = T4(),
1026 const T5& val5 = T5(),
1027 const T6& val6 = T6(),
1028 const T7& val7 = T7(),
1029 const T8& val8 = T8(),
1030 const T9& val9 = T9(),
1031 const T10& val10 = T10() ) :
1054 case 10: rval &= (_val10 == val._val10);
1055 case 9: rval &= (_val9 == val._val9);
1056 case 8: rval &= (_val8 == val._val8);
1057 case 7: rval &= (_val7 == val._val7);
1058 case 6: rval &= (_val6 == val._val6);
1059 case 5: rval &= (_val5 == val._val5);
1060 case 4: rval &= (_val4 == val._val4);
1061 case 3: rval &= (_val3 == val._val3);
1062 case 2: rval &= (_val2 == val._val2);
1063 case 1: rval &= (_val1 == val._val1);
1074 return !( *
this == val );
1087 outstr <<
" " <<_val2;
1089 outstr <<
" " <<_val3;
1091 outstr <<
" " <<_val4;
1093 outstr <<
" " <<_val5;
1095 outstr <<
" " <<_val6;
1097 outstr <<
" " <<_val7;
1099 outstr <<
" " <<_val8;
1101 outstr <<
" " <<_val9;
1103 outstr <<
" " <<_val10;
1163 GLS_EXPORT std::ostream & operator<<(std::ostream & outstr,
const GlsMultiValBase & multiVal);
1164 GLS_EXPORT std::istream & operator>>(std::istream & instr, GlsMultiValBase & multiVal);
1176 std::string _string;
1202 operator std::string()
const
1219 return str1._string == str2._string;
1225 return !(str1 == str2 );
1229 inline std::ostream & operator<<(std::ostream & outstr,
const GlsPropString & str)
1234 inline std::istream & operator>>(std::istream & instr, GlsPropString & str)
1237 disti::GetToEnd(instr, temp,
false);
1250 std::string _string;
1273 operator std::string()
const
1277 std::string& String()
1283 return str._string == _string;
1286 inline std::ostream & operator<<(std::ostream & outstr,
const GlsPropStringQuoted & str)
1291 inline std::istream & operator>>(std::istream & instr, GlsPropStringQuoted & str)
1298 if (instr.peek() !=
'\"')
1300 disti::GetToEnd(instr, temp,
false);
1310 while (instr.good())
1312 lastChar = currChar;
1313 currChar = instr.get();
1316 if (currChar ==
'\"' && lastChar !=
'\\')
1318 temp += (char)currChar;
1329 void SpawnBrowser(
const char *url);
1339 GLS_EXPORT
bool CheckDistiLicense(
const char* licenseGroupName,
const char *feature,
const char *version,
bool quiet);
1373 T Clamp(
const T& val,
const T& min,
const T& max )
1375 return std::min( max, std::max( min, val ) );
1388 inline void Split(
const std::string& s,
const char delim, std::vector<std::string>& elems,
const std::size_t maxElems = 0 )
1390 std::istringstream ss( s );
1392 while( std::getline( ss, item, delim ) )
1394 elems.push_back( DISTI_RVAL_MOVE( item ) );
1395 if( elems.size() == maxElems )
1400 if( elems.size() == maxElems && ss.good() && maxElems > 0 )
1402 std::string remainder;
1403 std::getline( ss, remainder,
'\0' );
1404 elems.back() += delim + remainder;
1418 inline std::vector<std::string> Split(
const std::string& s,
const char delim,
const std::size_t maxElems = 0 )
1420 std::vector<std::string> elems;
1421 Split( s, delim, elems, maxElems );
1422 return DISTI_RVAL_MOVE( elems );
1426 inline std::string MakeString(
const char*
const cStr )
1428 return ( cStr ? cStr :
"" );
1435 GLS_EXPORT
void glsPerspective(
double fovy,
double aspect,
double zNear,
double zFar);
1438 #ifdef MATRIX_TYPE_FLOAT
1439 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1440 const float modelMatrix[16],
1441 const float projMatrix[16],
1442 const int viewport[4],
1443 double *winx,
double *winy,
double *winz);
1445 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1446 const float modelMatrix[16],
1447 const float projMatrix[16],
1448 const int viewport[4],
1449 double *objx,
double *objy,
double *objz);
1451 GLS_EXPORT
bool glsProject(
double objx,
double objy,
double objz,
1452 const double modelMatrix[16],
1453 const double projMatrix[16],
1454 const int viewport[4],
1455 double *winx,
double *winy,
double *winz);
1457 GLS_EXPORT
bool glsUnProject(
double winx,
double winy,
double winz,
1458 const double modelMatrix[16],
1459 const double projMatrix[16],
1460 const int viewport[4],
1461 double *objx,
double *objy,
double *objz);
The DistiUnhideGlobalsDummyClass class.
char * GetBaseName(char *basename)
const T & Min(const T &x, const T &y)
Definition: util.h:250
int EncodedStringMinLength()
GlsPropStringQuoted(const std::string &str)
Definition: util.h:1261
virtual void StreamOut(ostreamType &outstr) const
Definition: util.h:1083
GlsDefinedColorEnum
Definition: util.h:176
const int INLINE_TEXTURE_LINE_LENGTH
Definition: util.h:104
std::string & String()
Definition: util.h:1211
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:1198
unsigned char glsDefinedColors[GLS_COLOR_MAX][4]
virtual bool operator!=(const GlsMultiVal &val) const
Definition: util.h:1072
Definition: dynamic_array.h:63
const std::string GetExtension(const std::string &filename)
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:289
std::string FileName(const std::string &filepath)
int EncodeString(char *dest, const char *src, const int dest_str_length)
The GlsMatrixAffine class.
void AppendTrailingSlash(char *s)
void RemoveDoubleSlashes(char *path, char slash= '/')
bool CheckDistiLicense(const char *licenseGroupName, const char *feature, const char *version, bool quiet)
std::string GetQualifiedInstanceName(const DisplayFrame *topFrame, const DisplayObject *obj)
std::string GetDefaultComponentClassName(const char *dllFileName)
GlsMultiVal()
Definition: util.h:979
char * PathToOS(const char *path)
const T & Max(const T &x, const T &y)
Definition: util.h:261
virtual bool operator==(const GlsMultiVal &val) const
Definition: util.h:1047
GlsPropStringQuoted(const char *str)
Definition: util.h:1270
A file for all GL Studio files to include.
int range_check(int num, double val,...)
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:211
GlsPropString()
Definition: util.h:1179
VertexNoColor Vector
Definition: gls_font_base.h:68
GlsMatrixAffine< T > FindCoordinateTransformation(const Vector &new_origin, const Vector &new_i, const Vector &new_j, const Vector &new_k)
Definition: util.h:368
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:120
bool CalculateTexPointsFromTexCoords(DisplayObject *object)
friend bool operator!=(const GlsPropString &str1, const GlsPropString &str2)
Definition: util.h:1223
GlsMultiVal(const GlsMultiVal &src)
Definition: util.h:992
void PushResourcePath(const char *resourcePath)
void Translate(Type x, Type y, Type z)
Definition: gls_matrix_affine.h:197
bool FileExists(const char *filename)
int C_EncodeString(char *dest, const char *src, const int dest_str_length)
virtual void StreamIn(istreamType &instr)
Definition: util.h:1112
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:1021
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)
bool Equal(T1 x, T2 y, float precision=0.001f)
Definition: util.h:239
char * GetFileName(const char *name)
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 &))
std::string ResolvePath(const char *path)
friend bool operator==(const GlsPropString &str1, const GlsPropString &str2)
Definition: util.h:1217
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:1188
int Safe_fopen(const char *filename, char *flags, FILE **f)
unsigned char * GlsDefinedColor(GlsDefinedColorEnum index)
Definition: util.h:200
Definition: bmpimage.h:46
GlsPropStringQuoted()
Definition: util.h:1252