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);
170std::string gGetFirstLineOfText(const std::string& text);
171
172bool gIsInt(const std::string& str);
173bool gIsFloat(const std::string& str);
174bool gIsNumber(const std::string& str);
175bool gIsString(const std::string& str);
176
177template <class T>
178std::string gToStr(const T& numValue) {
179 std::ostringstream out;
180 out << numValue;
181 return out.str();
182}
183
184template <class T>
185std::string gToHex(const T& numValue, int width = sizeof(T)*2) {
186 std::ostringstream stream;
187 stream << "0x"
188 << std::setfill ('0') << std::setw(width)
189 << std::hex << numValue;
190 return stream.str();
191}
192
193template <class T>
194std::string gToStr(const T& value, int width, char fill) {
195 std::ostringstream out;
196 out << std::fixed << std::setfill(fill) << std::setw(width) << value;
197 return out.str();
198}
199
200int gToInt(const std::string& intString);
201float gToFloat(const std::string& floatString);
202double gToDouble(const std::string& doubleString);
203std::string gWStrToStr(const std::wstring& s);
204
205template <typename T>
206int gSign(T val) {
207 return (T(0) < val) - (val < T(0));
208}
209
210std::string gCodepointToStr(unsigned int codepoint);
211
212std::string gEncodeBase64(unsigned char* data, int len);
213std::string gDecodeBase64(const std::string& encoded_string);
214
215bool gIsBase64(char c);
216
218void gOpenUrlInDefaultBrowser(std::string url);
219
221public:
222 gUTF8Iterator(const std::string & str);
223 utf8::iterator<std::string::const_iterator> begin() const;
224 utf8::iterator<std::string::const_iterator> end() const;
225 utf8::iterator<std::string::const_reverse_iterator> rbegin() const;
226 utf8::iterator<std::string::const_reverse_iterator> rend() const;
227
228private:
229 std::string src_valid;
230};
231
232class gLog {
233public:
234 enum {
239 };
240
242 gLog(const std::string& tag);
244
245 gLog& operator<<(std::ostream& (*func)(std::ostream&)){
246 func(logmessage);
247 return *this;
248 }
249
250 template <typename T>
251 gLog& operator<<(const T& value) {
252 logmessage << value;
253 return *this;
254 }
255
257 static bool isLoggingEnabled();
258
259 static std::string getLogLevelName(int logLevel);
260
261protected:
262 static bool isloggingenabled;
264 std::stringstream logmessage;
265 std::string logtag;
266 static std::string loglevelname[];
267
268private:
269 std::string logmsg;
270};
271
272class gLogi : public gLog {
273public:
274 gLogi(const std::string& tag = "");
275};
276
277class gLogd : public gLog {
278public:
279 gLogd(const std::string& tag = "");
280};
281
282class gLogw : public gLog {
283public:
284 gLogw(const std::string& tag = "");
285};
286
287class gLoge : public gLog {
288public:
289 gLoge(const std::string& tag = "");
290};
291
295
296template<typename T>
297T gClamp(T t, T min, T max) {
298 return t > max ? max : t < min ? min : t;
299}
300
338 const std::string& dialogTitle,
339 const std::string& defaultPathAndFile,
340 int filterNum,
341 const std::string* filterPatterns,
342 const std::string& filterDescription,
343 bool isMultipleSelectionAllowed = false);
344
370 const std::string& dialogTitle,
371 const std::string& defaultPathAndFile,
372 std::initializer_list<const char*> filterPatterns,
373 const std::string& filterDescription,
374 bool isMultipleSelectionAllowed = false);
375
377 const std::string& dialogTitle,
378 const std::string& defaultPathAndFile,
379 int filterNum,
380 const char** filterPatterns,
381 const std::string& filterDescription,
382 bool isMultipleSelectionAllowed = false);
383
385 const std::string& dialogTitle,
386 const std::string& defaultPathAndFile,
387 int filterNum,
388 const std::string* filterPatterns,
389 const std::string& filterDescription);
390
391
393 const std::string& dialogTitle,
394 const std::string& defaultPathAndFile,
395 std::initializer_list<const char*> filterPatterns,
396 const std::string& filterDescription);
397
399 const std::string& dialogTitle,
400 const std::string& defaultPathAndFile,
401 int filterNum,
402 const char** filterPatterns,
403 const std::string& filterDescription);
404
406 const std::string& dialogTitle,
407 const std::string& defaultPath);
408
409
420 const std::string& title,
421 const std::string& message,
422 const std::string& dialogue,
423 const std::string& iconType,
424 int defaultButton);
425
434std::string gShowInputBox(
435 const std::string& title,
436 const std::string& message,
437 const std::string& defaultInput);
438
448 const std::string& title,
449 const std::string& defaultHex,
450 unsigned char const defaultRGB[3]);
451
452bool gCheckCollision(int xLeft1, int yUp1, int xRight1, int yBottom1,
453 int xLeft2, int yUp2, int xRight2, int yBottom2);
454
455bool gCheckPixelPerfectCollision(gImage* image1, int x1, int y1, gImage* image2, int x2, int y2);
456
457std::string gFixPath(const std::string& path);
458
459class gUtils {
460public:
462 virtual ~gUtils();
463};
464
465#endif /* UTILS_GUTILS_H_ */
Definition gColor.h:17
Definition gImage.h:34
Definition gUtils.h:232
gLog(const std::string &tag)
static bool isloggingenabled
Definition gUtils.h:262
static void setLoggingEnabled(bool isLoggingEnabled)
std::string logtag
Definition gUtils.h:265
gLog & operator<<(const T &value)
Definition gUtils.h:251
static std::string getLogLevelName(int logLevel)
gLog & operator<<(std::ostream &(*func)(std::ostream &))
Definition gUtils.h:245
static std::string loglevelname[]
Definition gUtils.h:266
@ LOGLEVEL_DEBUG
Definition gUtils.h:236
@ LOGLEVEL_WARNING
Definition gUtils.h:237
@ LOGLEVEL_ERROR
Definition gUtils.h:238
@ LOGLEVEL_INFO
Definition gUtils.h:235
static bool isLoggingEnabled()
int loglevel
Definition gUtils.h:263
std::stringstream logmessage
Definition gUtils.h:264
Definition gUtils.h:277
gLogd(const std::string &tag="")
Definition gUtils.h:287
gLoge(const std::string &tag="")
Definition gUtils.h:272
gLogi(const std::string &tag="")
Definition gUtils.h:282
gLogw(const std::string &tag="")
unsigned int format
Definition gTexture.h:150
int width
Definition gTexture.h:153
int height
Definition gTexture.h:153
unsigned char * data
Definition gTexture.h:154
Definition gUtils.h:220
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:459
virtual ~gUtils()
std::string gToStr(const T &numValue)
Definition gUtils.h:178
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:185
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)
std::string gGetFirstLineOfText(const std::string &text)
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:206
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:297
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)