GlistEngine
Loading...
Searching...
No Matches
gUtils.h
Go to the documentation of this file.
1/*
2 * gUtils.h
3 *
4 * Created on: 5 Ara 2020
5 * Author: Noyan
6 */
7
8#pragma once
9
10#ifndef UTILS_GUTILS_H_
11#define UTILS_GUTILS_H_
12
13#include <string>
14#include <sstream>
15#include <ostream>
16#include <iostream>
17#include <iomanip>
18#include "utf8.h"
19#include <stdlib.h>
20#include <wchar.h>
21#include <vector>
22#include <memory>
23#include "gConstants.h"
24#include "gKeyCode.h"
25#include "gGUIEvents.h"
26#include "tinyfiledialogs.h"
27#include "gColor.h"
28#include <cstdint>
29#include <deque>
30
31/*
32#ifndef LINUX
33 #define LINUX __linux__
34#endif
35*/
36#ifdef _WIN64
37 //define something for Windows (64-bit)
38 #ifndef WIN64
39 #define WIN64 _WIN32
40 #endif
41#elif _WIN32
42 //define something for Windows (32-bit)
43 #ifndef WIN32
44 #define WIN32 _WIN32
45 #endif
46#elif __APPLE__
47 #include "TargetConditionals.h"
48 #if TARGET_IPHONE_SIMULATOR
49 // define something for simulator
50 #elif TARGET_OS_IPHONE
51 // define something for iphone
52 #else
53 #ifndef TARGET_OS_OSX
54 #define TARGET_OS_OSX 1
55 #endif
56 #ifndef APPLE
57 #define APPLE __APPLE__
58 #endif
59 // define something for OSX
60 #endif
61#elif __ANDROID__
62 // Android
63#elif __linux
64 // Linux
65 #ifndef LINUX
66 #define LINUX __linux__
67 #endif
68#elif __unix // all unices not caught above
69 // Unix
70#elif __posix
71 // POSIX
72#endif
73
74#if ANDROID || TARGET_OS_IPHONE || TARGET_OS_SIMULATOR || EMSCRIPTEN
75#ifndef GLIST_OPENGLES
76#define GLIST_OPENGLES 1
77#endif
78#endif
79
80#if EMSCRIPTEN
81#ifndef GLIST_WEB
82#define GLIST_WEB 1
83#endif
84#endif
85#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
86#ifndef GLIST_IOS
87#define GLIST_IOS 1
88#endif
89#endif
90#if ANDROID
91#ifndef GLIST_ANDROID
92#define GLIST_ANDROID 1
93#endif
94#endif
95
96
97#ifndef PI
98 #define PI 3.14159265358979323846
99#endif
100
101class gImage;
109
110float gRadToDeg(float radians);
111float gDegToRad(float degrees);
112
114float gRandom(float max);
115float gRandomf();
116
118unsigned int gGetUnixTime();
119
120/*
121 * Returns YYYY
122 */
124
125/*
126 * Returns M, starting from 1
127 */
129
130/*
131 * Returns D, starting from 1
132 */
134
139
140
141#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Available Ram Size
142uint64_t gGetAvailableRamSize();
143#endif
144
145#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Current Ram Size Used by Glist Engine
146uint64_t gGetRamSizeUsedbyGE();
147#endif
148
149#if defined(WIN32) || defined(LINUX) || defined(APPLE) //Total Ram Size
150uint64_t gGetTotalRamSize();
151#endif
152
153/*
154 * Rotates The Pixel Data upside down. Hence rotates flips the image upside down
155 */
156void gFlipImageDataVertically(unsigned char* pixelData, int width, int height, int numChannels);
157
159std::string gGetTimestampString(const std::string& format);
160void gStringReplace(std::string& input, const std::string& searchStr, const std::string& replaceStr);
161std::locale gGetLocale(const std::string & locale);
162std::string gToLower(const std::string& src, const std::string & locale = "");
163std::string gToUpper(const std::string& src, const std::string & locale = "");
164std::vector<std::string> gSplitString(const std::string& textToSplit, const std::string& delimiter);
165std::deque<std::string> gSplitStringToDeque(const std::string& textToSplit, const std::string& delimiter);
166std::string gReplaceAll(const std::string& source, const std::string& from, const std::string& to);
167bool gIsValidFilename(std::string fileName);
168
169std::string gGetFirstLineOfTextFile(std::string fullFilepath);
170
171bool gIsInt(const std::string& str);
172bool gIsFloat(const std::string& str);
173bool gIsNumber(const std::string& str);
174bool gIsString(const std::string& str);
175
176template <class T>
177std::string gToStr(const T& numValue) {
178 std::ostringstream out;
179 out << numValue;
180 return out.str();
181}
182
183template <class T>
184std::string gToHex(const T& numValue, int width = sizeof(T)*2) {
185 std::ostringstream stream;
186 stream << "0x"
187 << std::setfill ('0') << std::setw(width)
188 << std::hex << numValue;
189 return stream.str();
190}
191
192template <class T>
193std::string gToStr(const T& value, int width, char fill) {
194 std::ostringstream out;
195 out << std::fixed << std::setfill(fill) << std::setw(width) << value;
196 return out.str();
197}
198
199int gToInt(const std::string& intString);
200float gToFloat(const std::string& floatString);
201double gToDouble(const std::string& doubleString);
202std::string gWStrToStr(const std::wstring& s);
203
204template <typename T>
205int gSign(T val) {
206 return (T(0) < val) - (val < T(0));
207}
208
209std::string gCodepointToStr(unsigned int codepoint);
210
211std::string gEncodeBase64(unsigned char* data, int len);
212std::string gDecodeBase64(const std::string& encoded_string);
213
214bool gIsBase64(char c);
215
217void gOpenUrlInDefaultBrowser(std::string url);
218
220public:
221 gUTF8Iterator(const std::string & str);
222 utf8::iterator<std::string::const_iterator> begin() const;
223 utf8::iterator<std::string::const_iterator> end() const;
224 utf8::iterator<std::string::const_reverse_iterator> rbegin() const;
225 utf8::iterator<std::string::const_reverse_iterator> rend() const;
226
227private:
228 std::string src_valid;
229};
230
231class gLog {
232public:
233 enum {
238 };
239
241 gLog(const std::string& tag);
243
244 gLog& operator<<(std::ostream& (*func)(std::ostream&)){
245 func(logmessage);
246 return *this;
247 }
248
249 template <typename T>
250 gLog& operator<<(const T& value) {
251 logmessage << value;
252 return *this;
253 }
254
256 static bool isLoggingEnabled();
257
258 static std::string getLogLevelName(int logLevel);
259
260protected:
261 static bool isloggingenabled;
263 std::stringstream logmessage;
264 std::string logtag;
265 static std::string loglevelname[];
266
267private:
268 std::string logmsg;
269};
270
271class gLogi : public gLog {
272public:
273 gLogi(const std::string& tag = "");
274};
275
276class gLogd : public gLog {
277public:
278 gLogd(const std::string& tag = "");
279};
280
281class gLogw : public gLog {
282public:
283 gLogw(const std::string& tag = "");
284};
285
286class gLoge : public gLog {
287public:
288 gLoge(const std::string& tag = "");
289};
290
294
295template<typename T>
296T gClamp(T t, T min, T max) {
297 return t > max ? max : t < min ? min : t;
298}
299
337 const std::string& dialogTitle,
338 const std::string& defaultPathAndFile,
339 int filterNum,
340 const std::string* filterPatterns,
341 const std::string& filterDescription,
342 bool isMultipleSelectionAllowed = false);
343
369 const std::string& dialogTitle,
370 const std::string& defaultPathAndFile,
371 std::initializer_list<const char*> filterPatterns,
372 const std::string& filterDescription,
373 bool isMultipleSelectionAllowed = false);
374
376 const std::string& dialogTitle,
377 const std::string& defaultPathAndFile,
378 int filterNum,
379 const char** filterPatterns,
380 const std::string& filterDescription,
381 bool isMultipleSelectionAllowed = false);
382
384 const std::string& dialogTitle,
385 const std::string& defaultPathAndFile,
386 int filterNum,
387 const std::string* filterPatterns,
388 const std::string& filterDescription);
389
390
392 const std::string& dialogTitle,
393 const std::string& defaultPathAndFile,
394 std::initializer_list<const char*> filterPatterns,
395 const std::string& filterDescription);
396
398 const std::string& dialogTitle,
399 const std::string& defaultPathAndFile,
400 int filterNum,
401 const char** filterPatterns,
402 const std::string& filterDescription);
403
405 const std::string& dialogTitle,
406 const std::string& defaultPath);
407
408
419 const std::string& title,
420 const std::string& message,
421 const std::string& dialogue,
422 const std::string& iconType,
423 int defaultButton);
424
433std::string gShowInputBox(
434 const std::string& title,
435 const std::string& message,
436 const std::string& defaultInput);
437
447 const std::string& title,
448 const std::string& defaultHex,
449 unsigned char const defaultRGB[3]);
450
451bool gCheckCollision(int xLeft1, int yUp1, int xRight1, int yBottom1,
452 int xLeft2, int yUp2, int xRight2, int yBottom2);
453
454bool gCheckPixelPerfectCollision(gImage* image1, int x1, int y1, gImage* image2, int x2, int y2);
455
456std::string gFixPath(const std::string& path);
457
458class gUtils {
459public:
461 virtual ~gUtils();
462};
463
464#endif /* UTILS_GUTILS_H_ */
Definition gColor.h:17
Definition gImage.h:34
Definition gUtils.h:231
gLog(const std::string &tag)
static bool isloggingenabled
Definition gUtils.h:261
static void setLoggingEnabled(bool isLoggingEnabled)
std::string logtag
Definition gUtils.h:264
gLog & operator<<(const T &value)
Definition gUtils.h:250
static std::string getLogLevelName(int logLevel)
gLog & operator<<(std::ostream &(*func)(std::ostream &))
Definition gUtils.h:244
static std::string loglevelname[]
Definition gUtils.h:265
static bool isLoggingEnabled()
@ LOGLEVEL_DEBUG
Definition gUtils.h:235
@ LOGLEVEL_WARNING
Definition gUtils.h:236
@ LOGLEVEL_ERROR
Definition gUtils.h:237
@ LOGLEVEL_INFO
Definition gUtils.h:234
int loglevel
Definition gUtils.h:262
std::stringstream logmessage
Definition gUtils.h:263
Definition gUtils.h:276
gLogd(const std::string &tag="")
Definition gUtils.h:286
gLoge(const std::string &tag="")
Definition gUtils.h:271
gLogi(const std::string &tag="")
Definition gUtils.h:281
gLogw(const std::string &tag="")
unsigned int format
Definition gTexture.h:147
int width
Definition gTexture.h:150
int height
Definition gTexture.h:150
unsigned char * data
Definition gTexture.h:151
Definition gUtils.h:219
utf8::iterator< std::string::const_iterator > begin() const
gUTF8Iterator(const std::string &str)
utf8::iterator< std::string::const_reverse_iterator > rbegin() const
utf8::iterator< std::string::const_iterator > end() const
utf8::iterator< std::string::const_reverse_iterator > rend() const
Definition gUtils.h:458
virtual ~gUtils()
std::string gToStr(const T &numValue)
Definition gUtils.h:177
bool gCheckCollision(int xLeft1, int yUp1, int xRight1, int yBottom1, int xLeft2, int yUp2, int xRight2, int yBottom2)
bool gIsOnline()
int gGetMinutes()
int gGetYear()
int gDefaultMonitorHeight()
std::string gToLower(const std::string &src, const std::string &locale="")
std::string gShowOpenFileDialog(const std::string &dialogTitle, const std::string &defaultPathAndFile, int filterNum, const std::string *filterPatterns, const std::string &filterDescription, bool isMultipleSelectionAllowed=false)
int gShowMessageBox(const std::string &title, const std::string &message, const std::string &dialogue, const std::string &iconType, int defaultButton)
int gDefaultUnitWidth()
bool gIsValidFilename(std::string fileName)
bool gIsString(const std::string &str)
std::string gToHex(const T &numValue, int width=sizeof(T) *2)
Definition gUtils.h:184
void gFlipImageDataVertically(unsigned char *pixelData, int width, int height, int numChannels)
std::string gShowSelectFolderDialog(const std::string &dialogTitle, const std::string &defaultPath)
int gGetHours()
float gRandomf()
uint64_t gGetSystemTimeMillis()
bool gIsBase64(char c)
int gToInt(const std::string &intString)
int gGetMonth()
std::string gCodepointToStr(unsigned int codepoint)
bool gIsLoggingEnabled()
std::string gGetFirstLineOfTextFile(std::string fullFilepath)
double gToDouble(const std::string &doubleString)
std::vector< std::string > gSplitString(const std::string &textToSplit, const std::string &delimiter)
std::string gReplaceAll(const std::string &source, const std::string &from, const std::string &to)
gColor gShowColorChooser(const std::string &title, const std::string &defaultHex, unsigned char const defaultRGB[3])
int gGetSeconds()
std::string gToUpper(const std::string &src, const std::string &locale="")
std::string gShowInputBox(const std::string &title, const std::string &message, const std::string &defaultInput)
std::string gWStrToStr(const std::wstring &s)
void gOpenUrlInDefaultBrowser(std::string url)
void gEnableLogging()
std::string gDecodeBase64(const std::string &encoded_string)
bool gCheckPixelPerfectCollision(gImage *image1, int x1, int y1, gImage *image2, int x2, int y2)
float gRadToDeg(float radians)
std::string gFixPath(const std::string &path)
int gGetWeekday()
int gDefaultHeight()
int gDefaultUnitHeight()
int gSign(T val)
Definition gUtils.h:205
std::locale gGetLocale(const std::string &locale)
unsigned int gGetUnixTime()
bool gIsNumber(const std::string &str)
T gClamp(T t, T min, T max)
Definition gUtils.h:296
float gRandom(float max)
int gDefaultMonitorWidth()
float gToFloat(const std::string &floatString)
void gDisableLogging()
std::string gEncodeBase64(unsigned char *data, int len)
std::string gShowSaveFileDialog(const std::string &dialogTitle, const std::string &defaultPathAndFile, int filterNum, const std::string *filterPatterns, const std::string &filterDescription)
int gDefaultScreenScaling()
void gSeedRandom()
int gGetDay()
bool gIsFloat(const std::string &str)
std::deque< std::string > gSplitStringToDeque(const std::string &textToSplit, const std::string &delimiter)
float gDegToRad(float degrees)
std::string gGetTimestampString()
int gDefaultWidth()
void gStringReplace(std::string &input, const std::string &searchStr, const std::string &replaceStr)
bool gIsInt(const std::string &str)