nomlib
Loading...
Searching...
No Matches
types.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_STDINT_TYPES_HPP
30#define NOMLIB_STDINT_TYPES_HPP
31
32#include <cstddef> // std::size_t
33#include <limits> // min && max types
34
35#include "nomlib/platforms.hpp"
36
37/*
38 TODO: This should be replaced by an actual CMake script -- think:
39 compile-time check for the necessary feature support for C++11 style
40 headers support.
41
42 Look into why GCC doesn't like the inclusion of <cstdint> -- otherwise
43 known as stdint.h. MSVCPP2013 & llvm-clang are fine with it).
44*/
45
46// IMPORTANT(JEFF): These declarations, too, will integrate into our (CMake)
47// platform detection routines some day.
48#if defined(__GNUC__)
49 #include <cstdint>
50#elif defined(__llvm__)
51 #include <sys/types.h>
52#endif
53
54// FIXME: The following declaration is necessary in order to avoid a very
55// nasty compiling conflict that can happen under Windows anytime the
56// windef.h header file is included (commonly from windows.h), due to min and
57// max macros being declared there. This is why macros are evil.
58//
59// http://support.microsoft.com/kb/143208
60// http://stackoverflow.com/questions/5004858/stdmin-gives-error
61#if defined( NOM_PLATFORM_WINDOWS )
62 #undef min
63 #undef max
64#endif
65
66// Borrowed from /usr/include/MacTypes.h && /usr/include/objc/objc.h:
67#ifndef NULL
68 #if defined( NOM_COMPILER_FEATURE_NULLPTR )
69 #define NULL nullptr
70 #else
71 #define NULL 0 //__DARWIN_NULL
72 #endif
73#endif // ! NULL
74
75// Portable fixed-size data types derive from stdint.h
76namespace nom {
77
79typedef int8_t int8;
80
82typedef uint8_t uint8;
83
85typedef int16_t int16;
86
88typedef uint16_t uint16;
89
91typedef int32_t int32;
92
94typedef uint32_t uint32;
95
97typedef float real32;
98
100typedef double real64;
101
106#if defined( NOM_COMPILER_MSVCPP ) && defined( NOM_PLATFORM_ARCH_X86 )
107 typedef signed __int64 int64;
108 typedef unsigned __int64 uint64;
109#else // Blindly assume a 64-bit architecture
110 typedef int64_t int64; //typedef signed long long int int64;
111 typedef uint64_t uint64; //typedef unsigned long long int uint64;
112#endif
113
114// Additional integer type definitions
115
117typedef unsigned char uchar;
118
121typedef signed int sint;
122
124typedef unsigned int uint;
125
126typedef std::size_t size_type;
127
128typedef intptr_t* int_ptr;
129typedef uintptr_t* uint_ptr;
130
133typedef sint* sint_ptr;
134
135typedef int32_t* int32_ptr;
136typedef uint32_t* uint32_ptr;
137
138typedef void* void_ptr;
139
140typedef unsigned long ulong;
141
142// Numerical min, max values for commonly used data types
143//
144// http://en.cppreference.com/w/cpp/types/numeric_limits
145
146const char NOM_CHAR_MIN = std::numeric_limits<char>::lowest();
147const char NOM_CHAR_MAX = std::numeric_limits<char>::max();
148const uchar NOM_UCHAR_MIN = std::numeric_limits<uchar>::lowest();
149const uchar NOM_UCHAR_MAX = std::numeric_limits<uchar>::max();
150
151const int8 NOM_INT8_MIN = std::numeric_limits<int8>::lowest();
152const int8 NOM_INT8_MAX = std::numeric_limits<int8>::max();
153const uint8 NOM_UINT8_MIN = std::numeric_limits<uint8>::lowest();
154const uint8 NOM_UINT8_MAX = std::numeric_limits<uint8>::max();
155
156const int16 NOM_INT16_MIN = std::numeric_limits<int16>::lowest();
157const int16 NOM_INT16_MAX = std::numeric_limits<int16>::max();
158const uint16 NOM_UINT16_MIN = std::numeric_limits<uint16>::lowest();
159const uint16 NOM_UINT16_MAX = std::numeric_limits<uint16>::max();
160
161const int NOM_INT_MIN = std::numeric_limits<int>::lowest();
162const int NOM_INT_MAX = std::numeric_limits<int>::max();
163const uint NOM_UINT_MIN = std::numeric_limits<uint>::lowest();
164const uint NOM_UINT_MAX = std::numeric_limits<uint>::max();
165
166const int32 NOM_INT32_MIN = std::numeric_limits<int32>::lowest();
167const int32 NOM_INT32_MAX = std::numeric_limits<int32>::max();
168const uint32 NOM_UINT32_MIN = std::numeric_limits<uint32>::lowest();
169const uint32 NOM_UINT32_MAX = std::numeric_limits<uint32>::max();
170
171const int64 NOM_INT64_MIN = std::numeric_limits<int64>::lowest();
172const int64 NOM_INT64_MAX = std::numeric_limits<int64>::max();
173const uint64 NOM_UINT64_MIN = std::numeric_limits<uint64>::lowest();
174const uint64 NOM_UINT64_MAX = std::numeric_limits<uint64>::max();
175
176const size_type NOM_SIZE_TYPE_MIN = std::numeric_limits<size_type>::lowest();
177const size_type NOM_SIZE_TYPE_MAX = std::numeric_limits<size_type>::max();
178
179const real32 NOM_REAL32_MIN = std::numeric_limits<real32>::lowest();
180const real32 NOM_REAL32_MAX = std::numeric_limits<real32>::max();
181
182const real64 NOM_REAL64_MIN = std::numeric_limits<real64>::lowest();
183const real64 NOM_REAL64_MAX = std::numeric_limits<real64>::max();
184
186static const int npos = -1;
187
191static const int DEFAULT_FONT_SIZE = 12;
192
198enum Alignment: uint32
199{
200 NONE = 0x0,
201 X_LEFT = 0x01,
202 X_CENTER = 0x02,
203 X_RIGHT = 0x4,
204
206 X_MASK = ( X_LEFT | X_CENTER | X_RIGHT ),
207
208 Y_TOP = 0x10,
209 Y_CENTER = 0x20,
210 Y_BOTTOM = 0x40,
211
213 Y_MASK = ( Y_TOP | Y_CENTER | Y_BOTTOM )
214};
215
217enum Anchor: uint32
218{
219 None = NONE, // 0
220 Left = X_LEFT, // 1
221 Center = X_CENTER, // 2
222 Right = X_RIGHT, // 4
223
224 TopLeft = Y_TOP | X_LEFT, // Hex: 0x11, Dec: 17
225 TopCenter = Y_TOP | X_CENTER, // Hex: 0x12, Dec: 18
226 TopRight = Y_TOP | X_RIGHT, // Hex: 0x14, Dec: 20
227
228 MiddleLeft = Y_CENTER | X_LEFT, // Hex: 0x21, Dec: 33
229 MiddleCenter = Y_CENTER | X_CENTER, // Hex: 0x22, Dec: 34
230 MiddleRight = Y_CENTER | X_RIGHT, // Hex: 0x24, Dec: 36
231
232 BottomLeft = Y_BOTTOM | X_LEFT, // Hex: 0x41, Dec: 65
233 BottomCenter = Y_BOTTOM | X_CENTER, // Hex: 0x42, Dec: 66
234 BottomRight = Y_BOTTOM | X_RIGHT // Hex: 0x44, Dec: 68
235};
236
237static_assert(sizeof(nom::uint8) == 1, "nom::uint8");
238static_assert(sizeof(nom::int8) == 1, "nom::int8");
239
240const nom::size_type NOM_BYTE = sizeof(uint8); // 1024;
241
242inline
243nom::size_type kilobyte(nom::size_type bytes)
244{
245 nom::size_type result = (bytes * NOM_BYTE);
246
247 return result;
248}
249
250inline
251nom::size_type megabyte(nom::size_type bytes)
252{
253 nom::size_type result = ( kilobyte(bytes) * NOM_BYTE);
254
255 return result;
256}
257
258inline
259nom::size_type gigabyte(nom::size_type bytes)
260{
261 nom::size_type result = ( megabyte(bytes) * NOM_BYTE );
262
263 return result;
264}
265
266inline
267nom::size_type terabyte(nom::size_type bytes)
268{
269 nom::size_type result = ( gigabyte(bytes) * NOM_BYTE );
270
271 return result;
272}
273
274} // namespace nom
275
277static_assert ( sizeof ( nom::uint16 ) == 2, "nom::uint16" );
278static_assert ( sizeof ( nom::int16 ) == 2, "nom::int16" );
279
280static_assert ( sizeof ( nom::uint32 ) == 4, "nom::uint32" );
281static_assert ( sizeof ( nom::int32 ) == 4, "nom::int32" );
282
283static_assert ( sizeof ( nom::uint64 ) == 8, "nom::uint64" );
284static_assert ( sizeof ( nom::int64 ) == 8, "nom::int64" );
285
286static_assert ( sizeof ( nom::real32 ) == 4, "nom::real32" );
287static_assert ( sizeof ( nom::real64 ) == 8, "nom::real64" );
288
289static_assert ( sizeof ( nom::uchar ) == 1, "nom::uchar" );
290
291// Blindly assumes we are on either a 64-bit or 32-bit platform.
292// TODO: Relocate this def to run-time (cmake gen)
293#if defined(NOM_PLATFORM_ARCH_X86_64)
294 static_assert( sizeof ( nom::ulong ) == 8, "nom::ulong" );
295 static_assert( sizeof ( nom::size_type ) == 8, "nom::size_type" );
296#else // #elif defined( NOM_PLATFORM_ARCH_X86_86 )
297 static_assert( sizeof ( nom::ulong ) == 4, "nom::ulong" );
298 static_assert( sizeof ( nom::size_type ) == 4, "nom::size_type" );
299#endif
300
301// Blindly assumes we are on either a 64-bit or 32-bit platform.
302#if defined(NOM_PLATFORM_ARCH_X86_64)
303 static_assert( sizeof(nom::int_ptr) == 8, "nom::int_ptr" );
304 static_assert( sizeof(nom::uint_ptr) == 8, "nom::uint_ptr" );
305 static_assert( sizeof ( nom::int32_ptr ) == ( sizeof(long) ), "nom::int32_ptr" );
306 static_assert( sizeof ( nom::uint32_ptr ) == ( sizeof(nom::ulong) ), "nom::uint32_ptr" );
307#else // #elif defined(NOM_PLATFORM_ARCH_X86)
308 static_assert( sizeof(nom::int_ptr) == 4, "nom::int_ptr" );
309 static_assert( sizeof(nom::uint_ptr) == 4, "nom::uint_ptr" );
310 static_assert( sizeof( nom::int32_ptr ) == ( sizeof( long ) ), "nom::int32_ptr" );
311 static_assert( sizeof( nom::uint32_ptr ) == ( sizeof( nom::ulong ) ), "nom::uint32_ptr" );
312#endif
313
315
316const nom::sint NOM_EXIT_FAILURE = 1; // EXIT_FAILURE from cstdlib headers
317const nom::sint NOM_EXIT_SUCCESS = 0; // EXIT_SUCCESS from cstdlib headers
318
319//#if defined(HAVE_SDL2)
320const nom::sint SDL_SUCCESS = 0; // Non-error return value for SDL2 API
321//#endif
322
323#define NOM_KILOBYTES(bytes) kilobyte(bytes)
324#define NOM_MEGABYTES(bytes) megabyte(bytes)
325#define NOM_GIGABYTES(bytes) gigabyte(bytes)
326#define NOM_TERABYTES(bytes) terabytes(bytes)
327
328// Configuration variables for the engine
329
330#define NOM_EVENT_QUEUE_STATISTICS "NOM_EVENT_QUEUE_STATISTICS"
331
332#endif // include guard defined