SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SL.h
Go to the documentation of this file.
1 /**
2  * \file sl/SL.h
3  * \brief Platform detection, basic type definitions and utility macros
4  * \details SL.h is included (directly or indirectly) by every SL class and
5  * defines the vocabulary the rest of the framework is written in.
6  * It provides four things:
7  *
8  * - **Platform detection.** Exactly one of SL_OS_MACOS,
9  * SL_OS_MACIOS, SL_OS_WINDOWS, SL_OS_ANDROID, SL_OS_LINUX or
10  * SL_EMSCRIPTEN is defined from the compiler's own predefined
11  * macros; an unknown platform is a hard #error. Depending on the
12  * platform, SL_GLES, SL_GLES3 and SL_USE_DISCARD_STEREOMODES are
13  * derived from it, and the platform's system headers are included.
14  * - **Compiler detection.** One of SL_COMP_MSVC, SL_COMP_BORLANDC,
15  * SL_COMP_INTEL or SL_COMP_GNUC, together with the portable
16  * spellings SL_STDCALL and SL_DEPRECATED.
17  * - **Type aliases.** SLchar, SLint, SLfloat and friends mirror the
18  * OpenGL types so that vertex data can be handed to the GL without
19  * casting. Their vector forms are prefixed SLV (1D) and SLVV (2D).
20  * - **Macros.** Bit manipulation (SL_GETBIT, SL_SETBIT, SL_DELBIT,
21  * SL_TOGBIT) and logging and error reporting (SL_LOG,
22  * SL_LOG_DEBUG, SL_EXIT_MSG, SL_WARN_MSG), the latter forwarding
23  * to the Utils namespace.
24  *
25  * The macros are not listed individually in this documentation:
26  * Doxygen is configured with SKIP_FUNCTION_MACROS, and the platform
27  * defines live inside conditional branches that the documentation
28  * build does not take. Read this header itself for those.
29  * \date October 2015
30  * \authors Marcus Hudritsch
31  * \copyright http://opensource.org/licenses/GPL-3.0
32  * \remarks Please use clangformat to format the code. See more code style on
33  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
34  */
35 
36 #ifndef SL_H
37 #define SL_H
38 
39 //////////////////////////////////////////////////////////
40 // Preprocessor constant definitions used in the SLProject
41 //////////////////////////////////////////////////////////
42 
43 //-----------------------------------------------------------------------------
44 /* Determine one of the following operating systems:
45 SL_OS_MACOS :Apple Mac OSX
46 SL_OS_MACIOS :Apple iOS
47 SL_OS_WINDOWS :Microsoft desktop Windows XP, 7, 8, ...
48 SL_OS_ANDROID :Goggle Android
49 SL_OS_LINUX :Linux desktop OS
50 
51 With the OS definition the following constants are defined:
52 SL_GLES : Any version of OpenGL ES
53 SL_GLES3: Supports only OpenGL ES3
54 SL_USE_DISCARD_STEREOMODES: The discard stereo modes can be used (SLCamera)
55 */
56 
57 #ifdef __APPLE__
58 # include <TargetConditionals.h>
59 # if TARGET_OS_IOS
60 # define SL_OS_MACIOS
61 # define SL_GLES
62 # define SL_GLES3
63 # else
64 # define SL_OS_MACOS
65 # if defined(_DEBUG)
66 
67 # endif
68 # endif
69 #elif defined(ANDROID) || defined(ANDROID_NDK)
70 # define SL_OS_ANDROID
71 # define SL_GLES
72 # define SL_GLES3
73 #elif defined(_WIN32)
74 # define SL_OS_WINDOWS
75 # define SL_USE_DISCARD_STEREOMODES
76 # ifdef _DEBUG
77 # define _GLDEBUG
78 # endif
79 # define STDCALL __stdcall
80 #elif defined(linux) || defined(__linux) || defined(__linux__)
81 # define SL_OS_LINUX
82 # define SL_USE_DISCARD_STEREOMODES
83 # ifdef _DEBUG
84 # endif
85 #elif defined(__EMSCRIPTEN__)
86 # define SL_EMSCRIPTEN
87 # define SL_USE_DISCARD_STEREOMODES
88 # define SL_GLES
89 # define SL_GLES3
90 #else
91 # error "SL has not been ported to this OS"
92 #endif
93 
94 //-----------------------------------------------------------------------------
95 /* One of the following constants defines the GUI system. It is not set here
96 but as a compile definition by CMake; see modules/sl/CMakeLists.txt.
97 
98 SL_GUI_QT :Qt on OSX, Windows, Linux or Android
99 SL_GUI_OBJC :ObjectiveC on iOS
100 SL_GUI_GLFW :GLFW on OSX, Windows or Linux (the only one CMake sets today)
101 SL_GUI_JAVA :Java on Android (queried by SLGLOVRWorkaround.h)
102 */
103 
104 //-----------------------------------------------------------------------------
105 #if defined(SL_OS_MACIOS)
106 # include <chrono>
107 # include <functional>
108 # include <random>
109 # include <sys/time.h>
110 # include <thread>
111 # include <CoreServices/CoreServices.h> // for system info
112 # include <zlib.h>
113 #elif defined(SL_OS_MACOS)
114 # include <chrono>
115 # include <functional>
116 # include <random>
117 # include <ctime>
118 # include <thread>
119 //# include <CoreServices/CoreServices.h> // for system info
120 # include <sys/sysctl.h> // for system info
121 #elif defined(SL_OS_ANDROID)
122 # include <sys/time.h>
123 # include <sys/system_properties.h>
124 # include <chrono>
125 # include <functional>
126 # include <random>
127 # include <sstream>
128 # include <thread>
129 #elif defined(SL_OS_WINDOWS)
130 # include <chrono>
131 # include <functional>
132 # include <random>
133 # include <thread>
134 # include <windows.h>
135 #elif defined(SL_OS_LINUX)
136 # include <chrono>
137 # include <functional>
138 # include <random>
139 # include <sstream>
140 # include <sys/time.h>
141 # include <thread>
142 #elif defined(SL_EMSCRIPTEN)
143 # include <random>
144 #else
145 # error "SL has not been ported to this OS"
146 #endif
147 
148 #include <Utils.h>
149 
150 //-----------------------------------------------------------------------------
151 using std::string;
152 using std::vector;
153 
154 //-----------------------------------------------------------------------------
155 // Determine compiler
156 #if defined(__GNUC__)
157 # undef _MSC_VER
158 #endif
159 
160 #if defined(_MSC_VER)
161 # define SL_COMP_MSVC
162 # define SL_STDCALL __stdcall
163 # define SL_DEPRECATED __declspec(deprecated)
164 # define _CRT_SECURE_NO_DEPRECATE // visual 8 secure crt warning
165 #elif defined(__BORLANDC__)
166 # define SL_COMP_BORLANDC
167 # define SL_STDCALL Stdcall
168 # define SL_DEPRECATED // @todo Does this compiler support deprecated attributes
169 #elif defined(__INTEL_COMPILER)
170 # define SL_COMP_INTEL
171 # define SL_STDCALL Stdcall
172 # define SL_DEPRECATED // @todo does this compiler support deprecated attributes
173 #elif defined(__GNUC__)
174 # define SL_COMP_GNUC
175 # define SL_STDCALL
176 # define SL_DEPRECATED __attribute__((deprecated))
177 #else
178 # error "SL has not been ported to this compiler"
179 #endif
180 
181 //-----------------------------------------------------------------------------
182 //! Redefinition of standard types for platform independence
183 /*! The SL types mirror the OpenGL types of the same name so that vertex
184 attribute data can be passed to the GL without casting. */
185 typedef string SLstring; //!< analog to std::string
186 #ifndef SL_OS_ANDROID
187 typedef std::wstring SLwstring; //!< analog to std::wstring, not available on Android
188 #endif
189 typedef char SLchar; //!< analog to GLchar (char is signed [-128 ... 127]!)
190 typedef unsigned char SLuchar; //!< analog to GLuchar
191 typedef signed long SLlong; //!< analog to GLlong
192 typedef unsigned long SLulong; //!< analog to GLulong
193 typedef signed char SLbyte; //!< analog to GLbyte
194 typedef unsigned char SLubyte; //!< analog to GLubyte
195 typedef short SLshort; //!< analog to GLshort
196 typedef unsigned short SLushort; //!< analog to GLushort
197 typedef int SLint; //!< analog to GLint
198 typedef unsigned int SLuint; //!< analog to GLuint
199 typedef int SLsizei; //!< analog to GLsizei
200 typedef float SLfloat; //!< analog to GLfloat
201 typedef double SLdouble; //!< analog to GLdouble
202 typedef bool SLbool; //!< analog to GLbool
203 typedef unsigned int SLenum; //!< analog to GLenum
204 typedef unsigned int SLbitfield; //!< analog to GLbitfield
205 
206 // Fixed width integer types for data whose size must not vary by platform
207 typedef int8_t SLint8; //!< 8 bit signed integer
208 typedef uint8_t SLuint8; //!< 8 bit unsigned integer
209 typedef int16_t SLint16; //!< 16 bit signed integer
210 typedef uint16_t SLuint16; //!< 16 bit unsigned integer
211 typedef int32_t SLint32; //!< 32 bit signed integer
212 typedef uint32_t SLuint32; //!< 32 bit unsigned integer
213 typedef int64_t SLint64; //!< 64 bit signed integer
214 typedef uint64_t SLuint64; //!< 64 bit unsigned integer
215 
216 //! All 1D vectors begin with SLV*
217 typedef vector<SLbool> SLVbool;
218 typedef vector<SLbyte> SLVbyte;
219 typedef vector<SLubyte> SLVubyte;
220 typedef vector<SLchar> SLVchar;
221 typedef vector<SLuchar> SLVuchar;
222 typedef vector<SLshort> SLVshort;
223 typedef vector<SLushort> SLVushort;
224 typedef vector<SLint> SLVint;
225 typedef vector<SLuint> SLVuint;
226 typedef vector<SLlong> SLVlong;
227 typedef vector<SLulong> SLVulong;
228 typedef vector<SLfloat> SLVfloat;
229 typedef vector<SLstring> SLVstring;
230 typedef vector<size_t> SLVsize_t;
231 
232 //! All 2D vectors begin with SLVV*
233 typedef vector<vector<SLfloat>> SLVVfloat;
234 typedef vector<vector<SLuchar>> SLVVuchar;
235 typedef vector<vector<SLushort>> SLVVushort;
236 typedef vector<vector<SLuint>> SLVVuint;
237 typedef vector<vector<SLchar>> SLVVchar;
238 typedef vector<vector<SLshort>> SLVVshort;
239 typedef vector<vector<SLint>> SLVVint;
240 
241 //-----------------------------------------------------------------------------
242 //! Returns the memory in bytes reserved by a vector
243 /*! Note that this reports the *capacity*, not the size: it is the memory the
244 vector currently occupies, which is what the scene statistics report. */
245 template<class T>
246 inline SLuint
247 SL_sizeOfVector(const T& vector)
248 {
249  return (SLint)(vector.capacity() * sizeof(typename T::value_type));
250 }
251 //-----------------------------------------------------------------------------
252 //! Bit manipulation macros for ones that forget it always
253 /*! VAR is the bit field, VAL the bit mask to test or modify. The arguments are
254 not parenthesised, so pass plain variables rather than expressions.
255 SL_TOGBIT expands to a bare if/else and must not be used as the body of an
256 unbraced if, or it will bind to the wrong else. */
257 #define SL_GETBIT(VAR, VAL) VAR& VAL
258 #define SL_SETBIT(VAR, VAL) VAR |= VAL
259 #define SL_DELBIT(VAR, VAL) VAR &= ~VAL
260 #define SL_TOGBIT(VAR, VAL) \
261  if ((VAR) & (VAL)) \
262  (VAR) &= ~(VAL); \
263  else \
264  (VAR) |= VAL
265 //-----------------------------------------------------------------------------
266 //! Silences the unused-parameter warning in XCode
267 /*! \warning Defective and unused: the parameter is named r while the body
268 casts x, so any use fails to compile unless an unrelated x is in scope. It has
269 no callers anywhere in the project. Fix the argument name or delete it before
270 using it. */
271 #define UNUSED_PARAMETER(r) ((void)(x))
272 
273 //-----------------------------------------------------------------------------
274 //! Some debugging and error handling macros
275 /*! All four tag their output with "SLProject" and forward to the Utils
276 namespace. SL_LOG_DEBUG compiles to nothing outside debug builds. SL_EXIT_MSG
277 terminates the application; SL_WARN_MSG only reports. Both pass __LINE__ and
278 __FILE__ so the origin appears in the message. */
279 #define SL_LOG(...) Utils::log("SLProject", __VA_ARGS__)
280 #if _DEBUG
281 # define SL_LOG_DEBUG(...) Utils::log("SLProject", __VA_ARGS__)
282 #else
283 # define SL_LOG_DEBUG(...) \
284  { \
285  }
286 #endif
287 
288 #define SL_EXIT_MSG(message) Utils::exitMsg("SLProject", (message), __LINE__, __FILE__)
289 #define SL_WARN_MSG(message) Utils::warnMsg("SLProject", (message), __LINE__, __FILE__)
290 //-----------------------------------------------------------------------------
291 #endif
float SLfloat
analog to GLfloat
Definition: SL.h:200
unsigned int SLenum
analog to GLenum
Definition: SL.h:203
vector< vector< SLuint > > SLVVuint
Definition: SL.h:236
vector< SLlong > SLVlong
Definition: SL.h:226
vector< SLubyte > SLVubyte
Definition: SL.h:219
unsigned int SLbitfield
analog to GLbitfield
Definition: SL.h:204
uint64_t SLuint64
64 bit unsigned integer
Definition: SL.h:214
unsigned long SLulong
analog to GLulong
Definition: SL.h:192
vector< vector< SLchar > > SLVVchar
Definition: SL.h:237
vector< vector< SLshort > > SLVVshort
Definition: SL.h:238
unsigned int SLuint
analog to GLuint
Definition: SL.h:198
int8_t SLint8
8 bit signed integer
Definition: SL.h:207
char SLchar
analog to GLchar (char is signed [-128 ... 127]!)
Definition: SL.h:189
vector< size_t > SLVsize_t
Definition: SL.h:230
unsigned char SLuchar
analog to GLuchar
Definition: SL.h:190
double SLdouble
analog to GLdouble
Definition: SL.h:201
vector< SLuchar > SLVuchar
Definition: SL.h:221
vector< SLshort > SLVshort
Definition: SL.h:222
int64_t SLint64
64 bit signed integer
Definition: SL.h:213
vector< vector< SLushort > > SLVVushort
Definition: SL.h:235
int32_t SLint32
32 bit signed integer
Definition: SL.h:211
vector< SLint > SLVint
Definition: SL.h:224
bool SLbool
analog to GLbool
Definition: SL.h:202
unsigned short SLushort
analog to GLushort
Definition: SL.h:196
unsigned char SLubyte
analog to GLubyte
Definition: SL.h:194
vector< SLfloat > SLVfloat
Definition: SL.h:228
vector< SLstring > SLVstring
Definition: SL.h:229
uint16_t SLuint16
16 bit unsigned integer
Definition: SL.h:210
vector< SLbool > SLVbool
All 1D vectors begin with SLV*.
Definition: SL.h:217
signed long SLlong
analog to GLlong
Definition: SL.h:191
vector< vector< SLint > > SLVVint
Definition: SL.h:239
vector< vector< SLfloat > > SLVVfloat
All 2D vectors begin with SLVV*.
Definition: SL.h:233
short SLshort
analog to GLshort
Definition: SL.h:195
vector< SLulong > SLVulong
Definition: SL.h:227
signed char SLbyte
analog to GLbyte
Definition: SL.h:193
int16_t SLint16
16 bit signed integer
Definition: SL.h:209
vector< SLushort > SLVushort
Definition: SL.h:223
vector< SLbyte > SLVbyte
Definition: SL.h:218
uint8_t SLuint8
8 bit unsigned integer
Definition: SL.h:208
std::wstring SLwstring
analog to std::wstring, not available on Android
Definition: SL.h:187
int SLsizei
analog to GLsizei
Definition: SL.h:199
vector< SLchar > SLVchar
Definition: SL.h:220
SLuint SL_sizeOfVector(const T &vector)
Returns the memory in bytes reserved by a vector.
Definition: SL.h:247
vector< SLuint > SLVuint
Definition: SL.h:225
vector< vector< SLuchar > > SLVVuchar
Definition: SL.h:234
string SLstring
Redefinition of standard types for platform independence.
Definition: SL.h:185
uint32_t SLuint32
32 bit unsigned integer
Definition: SL.h:212
int SLint
analog to GLint
Definition: SL.h:197