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__) && defined(__clang__)
51 #include <sys/types.h>
52 #include <cstdint>
53#elif defined(_MSC_VER) // Microsoft Visual C++
54 // STUB
55#endif
56
57// FIXME: The following declaration is necessary in order to avoid a very
58// nasty compiling conflict that can happen under Windows anytime the
59// windef.h header file is included (commonly from windows.h), due to min and
60// max macros being declared there. This is why macros are evil.
61//
62// http://support.microsoft.com/kb/143208
63// http://stackoverflow.com/questions/5004858/stdmin-gives-error
64#if defined( NOM_PLATFORM_WINDOWS )
65 #undef min
66 #undef max
67#endif
68
69// Borrowed from /usr/include/MacTypes.h && /usr/include/objc/objc.h:
70#ifndef NULL
71 #if defined( NOM_COMPILER_FEATURE_NULLPTR )
72 #define NULL nullptr
73 #else
74 #define NULL 0 //__DARWIN_NULL
75 #endif
76#endif // ! NULL
77
78// Portable fixed-size data types derive from stdint.h
79namespace nom {
80
82typedef int8_t int8;
83
85typedef uint8_t uint8;
86
88typedef int16_t int16;
89
91typedef uint16_t uint16;
92
94typedef int32_t int32;
95
97typedef uint32_t uint32;
98
100typedef float real32;
101
103typedef double real64;
104
109#if defined( NOM_COMPILER_MSVCPP ) && defined( NOM_PLATFORM_ARCH_X86 )
110 typedef signed __int64 int64;
111 typedef unsigned __int64 uint64;
112#else // Blindly assume a 64-bit architecture
113 typedef int64_t int64; //typedef signed long long int int64;
114 typedef uint64_t uint64; //typedef unsigned long long int uint64;
115#endif
116
117// Additional integer type definitions
118
120typedef unsigned char uchar;
121
124typedef signed int sint;
125
127typedef unsigned int uint;
128
129typedef std::size_t size_type;
130
131typedef intptr_t* int_ptr;
132typedef uintptr_t* uint_ptr;
133
136typedef sint* sint_ptr;
137
138typedef int32_t* int32_ptr;
139typedef uint32_t* uint32_ptr;
140
141typedef void* void_ptr;
142
143typedef unsigned long ulong;
144
145// Numerical min, max values for commonly used data types
146//
147// http://en.cppreference.com/w/cpp/types/numeric_limits
148
149const char NOM_CHAR_MIN = std::numeric_limits<char>::lowest();
150const char NOM_CHAR_MAX = std::numeric_limits<char>::max();
151const uchar NOM_UCHAR_MIN = std::numeric_limits<uchar>::lowest();
152const uchar NOM_UCHAR_MAX = std::numeric_limits<uchar>::max();
153
154const int8 NOM_INT8_MIN = std::numeric_limits<int8>::lowest();
155const int8 NOM_INT8_MAX = std::numeric_limits<int8>::max();
156const uint8 NOM_UINT8_MIN = std::numeric_limits<uint8>::lowest();
157const uint8 NOM_UINT8_MAX = std::numeric_limits<uint8>::max();
158
159const int16 NOM_INT16_MIN = std::numeric_limits<int16>::lowest();
160const int16 NOM_INT16_MAX = std::numeric_limits<int16>::max();
161const uint16 NOM_UINT16_MIN = std::numeric_limits<uint16>::lowest();
162const uint16 NOM_UINT16_MAX = std::numeric_limits<uint16>::max();
163
164const int NOM_INT_MIN = std::numeric_limits<int>::lowest();
165const int NOM_INT_MAX = std::numeric_limits<int>::max();
166const uint NOM_UINT_MIN = std::numeric_limits<uint>::lowest();
167const uint NOM_UINT_MAX = std::numeric_limits<uint>::max();
168
169const int32 NOM_INT32_MIN = std::numeric_limits<int32>::lowest();
170const int32 NOM_INT32_MAX = std::numeric_limits<int32>::max();
171const uint32 NOM_UINT32_MIN = std::numeric_limits<uint32>::lowest();
172const uint32 NOM_UINT32_MAX = std::numeric_limits<uint32>::max();
173
174const int64 NOM_INT64_MIN = std::numeric_limits<int64>::lowest();
175const int64 NOM_INT64_MAX = std::numeric_limits<int64>::max();
176const uint64 NOM_UINT64_MIN = std::numeric_limits<uint64>::lowest();
177const uint64 NOM_UINT64_MAX = std::numeric_limits<uint64>::max();
178
179const size_type NOM_SIZE_TYPE_MIN = std::numeric_limits<size_type>::lowest();
180const size_type NOM_SIZE_TYPE_MAX = std::numeric_limits<size_type>::max();
181
182const real32 NOM_REAL32_MIN = std::numeric_limits<real32>::lowest();
183const real32 NOM_REAL32_MAX = std::numeric_limits<real32>::max();
184
185const real64 NOM_REAL64_MIN = std::numeric_limits<real64>::lowest();
186const real64 NOM_REAL64_MAX = std::numeric_limits<real64>::max();
187
189static const int npos = -1;
190
194static const int DEFAULT_FONT_SIZE = 12;
195
201enum Alignment: uint32
202{
203 NONE = 0x0,
204 X_LEFT = 0x01,
205 X_CENTER = 0x02,
206 X_RIGHT = 0x4,
207
209 X_MASK = ( X_LEFT | X_CENTER | X_RIGHT ),
210
211 Y_TOP = 0x10,
212 Y_CENTER = 0x20,
213 Y_BOTTOM = 0x40,
214
216 Y_MASK = ( Y_TOP | Y_CENTER | Y_BOTTOM )
217};
218
220enum Anchor: uint32
221{
222 None = NONE, // 0
223 Left = X_LEFT, // 1
224 Center = X_CENTER, // 2
225 Right = X_RIGHT, // 4
226
227 TopLeft = Y_TOP | X_LEFT, // Hex: 0x11, Dec: 17
228 TopCenter = Y_TOP | X_CENTER, // Hex: 0x12, Dec: 18
229 TopRight = Y_TOP | X_RIGHT, // Hex: 0x14, Dec: 20
230
231 MiddleLeft = Y_CENTER | X_LEFT, // Hex: 0x21, Dec: 33
232 MiddleCenter = Y_CENTER | X_CENTER, // Hex: 0x22, Dec: 34
233 MiddleRight = Y_CENTER | X_RIGHT, // Hex: 0x24, Dec: 36
234
235 BottomLeft = Y_BOTTOM | X_LEFT, // Hex: 0x41, Dec: 65
236 BottomCenter = Y_BOTTOM | X_CENTER, // Hex: 0x42, Dec: 66
237 BottomRight = Y_BOTTOM | X_RIGHT // Hex: 0x44, Dec: 68
238};
239
240static_assert(sizeof(nom::uint8) == 1, "nom::uint8");
241static_assert(sizeof(nom::int8) == 1, "nom::int8");
242
243static const nom::size_type NOM_BYTE = sizeof(uint8); // 1024;
244
245inline
246nom::size_type kilobyte(nom::size_type bytes)
247{
248 nom::size_type result = (bytes * NOM_BYTE);
249
250 return result;
251}
252
253inline
254nom::size_type megabyte(nom::size_type bytes)
255{
256 nom::size_type result = ( kilobyte(bytes) * NOM_BYTE);
257
258 return result;
259}
260
261inline
262nom::size_type gigabyte(nom::size_type bytes)
263{
264 nom::size_type result = ( megabyte(bytes) * NOM_BYTE );
265
266 return result;
267}
268
269inline
270nom::size_type terabyte(nom::size_type bytes)
271{
272 nom::size_type result = ( gigabyte(bytes) * NOM_BYTE );
273
274 return result;
275}
276
277} // namespace nom
278
280static_assert ( sizeof ( nom::uint16 ) == 2, "nom::uint16" );
281static_assert ( sizeof ( nom::int16 ) == 2, "nom::int16" );
282
283static_assert ( sizeof ( nom::uint32 ) == 4, "nom::uint32" );
284static_assert ( sizeof ( nom::int32 ) == 4, "nom::int32" );
285
286static_assert ( sizeof ( nom::uint64 ) == 8, "nom::uint64" );
287static_assert ( sizeof ( nom::int64 ) == 8, "nom::int64" );
288
289static_assert ( sizeof ( nom::real32 ) == 4, "nom::real32" );
290static_assert ( sizeof ( nom::real64 ) == 8, "nom::real64" );
291
292static_assert ( sizeof ( nom::uchar ) == 1, "nom::uchar" );
293
294// TODO: Relocate this def to run-time (cmake gen)
295#if defined(NOM_PLATFORM_ARCH_X86_64)
296 #if defined(__WIN32__) || defined(_WIN32)
297 static_assert(sizeof(nom::ulong) == 4, "nom::ulong");
298 #else
299 static_assert(sizeof(nom::ulong) == 8, "nom::ulong");
300 #endif // __WIN32__
301 static_assert( sizeof ( nom::size_type ) == 8, "nom::size_type" );
302 static_assert( sizeof(nom::int_ptr) == 8, "nom::int_ptr" );
303 static_assert( sizeof(nom::uint_ptr) == 8, "nom::uint_ptr" );
304 static_assert( sizeof ( nom::int32_ptr ) == (8), "nom::int32_ptr" );
305 static_assert( sizeof ( nom::uint32_ptr ) == (8), "nom::uint32_ptr" );
306#else // #elif defined(NOM_PLATFORM_ARCH_X86)
307 static_assert( sizeof(nom::int_ptr) == 4, "nom::int_ptr" );
308 static_assert( sizeof(nom::uint_ptr) == 4, "nom::uint_ptr" );
309 static_assert( sizeof( nom::int32_ptr ) == ( sizeof( long ) ), "nom::int32_ptr" );
310 static_assert( sizeof( nom::uint32_ptr ) == ( sizeof( nom::ulong ) ), "nom::uint32_ptr" );
311#endif
312
314
315const nom::sint NOM_EXIT_FAILURE = 1; // EXIT_FAILURE from cstdlib headers
316const nom::sint NOM_EXIT_SUCCESS = 0; // EXIT_SUCCESS from cstdlib headers
317
318//#if defined(HAVE_SDL2)
319const nom::sint SDL_SUCCESS = 0; // Non-error return value for SDL2 API
320//#endif
321
322#define NOM_KILOBYTES(bytes) kilobyte(bytes)
323#define NOM_MEGABYTES(bytes) megabyte(bytes)
324#define NOM_GIGABYTES(bytes) gigabyte(bytes)
325#define NOM_TERABYTES(bytes) terabytes(bytes)
326
327// Configuration variables for the engine
328
329#define NOM_EVENT_QUEUE_STATISTICS "NOM_EVENT_QUEUE_STATISTICS"
330
331#endif // include guard defined