A collection of useful macros.
More...
Defines |
| #define | sgeLock(surface) |
| | Lock a surface.
|
| #define | sgeUnlock(surface) |
| | Unlock a surface.
|
| #define | sgeBailOut(format, args...) |
| | Hard exit.
|
| #define | sgeMalloc(target, type, amount) |
| | Allocate ram, fill with 0.
|
| #define | sgeMallocNoInit(target, type, amount) |
| | Allocate ram, uninitialized.
|
| #define | sgeRealloc(target, type, amount) |
| | Resize allocated ram.
|
| #define | sgeNew(target, type) sgeMalloc(target,type,1); |
| | Create one object (aka struct)
|
| #define | sgeFree(target) |
| | Free the object.
|
| #define | sgeRandom(from, range) (from + (int) ((float)(range+1) * (rand() / (RAND_MAX + (float)from)))) |
| | Get a random integer.
|
| #define | sgeRandomFloat(from, range) (float) ((float)from + ((float)(range+1) * (rand() / (RAND_MAX + (float)from)))) |
| | Get a random float.
|
| #define | sgeByteSwap16(val) (val) |
| | swap byteorder on big endian, otherwise do nothing
|
| #define | sgeByteSwap32(val) (val) |
| | swap byteorder on big endian, otherwise do nothing
|
| #define | sgeRound(val) round(val) |
| | round a integer value, if there is no c lib function available
|
| #define | MAX(x, y) ((x) > (y) ? (x) : (y)) |
| | define MAX if not already there
|
| #define | MIN(x, y) ((x) < (y) ? (x) : (y)) |
| | define MIN if not already there
|
| #define | MINMAX(value, lower, upper) MIN(MAX((value), (lower)), (upper)) |
| | keep a number in a certain range
|
Detailed Description
A collection of useful macros.
Macros for common tasks such as memory management and cross platform C incompatibilities.
Define Documentation
| #define MAX |
( |
|
x, |
|
|
|
y |
|
) |
| ((x) > (y) ? (x) : (y)) |
define MAX if not already there
- Parameters:
-
| x | a number |
| y | the max limit of the number |
- Returns:
- x if it is in range of y, otherwise y
Definition at line 349 of file sge.h.
| #define MIN |
( |
|
x, |
|
|
|
y |
|
) |
| ((x) < (y) ? (x) : (y)) |
define MIN if not already there
- Parameters:
-
| x | a number |
| y | the min limit of the number |
- Returns:
- x if it is in range of y, otherwise y
Definition at line 359 of file sge.h.
| #define MINMAX |
( |
|
value, |
|
|
|
lower, |
|
|
|
upper |
|
) |
| MIN(MAX((value), (lower)), (upper)) |
keep a number in a certain range
- Parameters:
-
| value | the number to keep in range |
| lower | the lower end of the range |
| upper | the upper end of the range |
- Returns:
- a number ensured to be in range of lower and upper borders
Definition at line 369 of file sge.h.
| #define sgeBailOut |
( |
|
format, |
|
|
|
args... |
|
) |
| |
Value:do {\
fprintf(stderr,(format),args); \
exit(-1); \
} while (0)
Hard exit.
- Parameters:
-
| format | printf format string |
| args | arguments for the format string |
Exit out of the application without any cleanup.
You should use this only if there is a unresolvable situation
Definition at line 204 of file sge.h.
| #define sgeByteSwap16 |
( |
|
val | ) |
(val) |
swap byteorder on big endian, otherwise do nothing
- Parameters:
-
- Returns:
- a 16bit number in a unique endianess
Use this for example if you write 16 or 32 bit numbers into a binary file to ensure it reads the same value on little and big endian
Definition at line 314 of file sge.h.
| #define sgeByteSwap32 |
( |
|
val | ) |
(val) |
swap byteorder on big endian, otherwise do nothing
- Parameters:
-
| val | a 32bit number (depending on prefix) |
- Returns:
- a 32bit number in a unique endianess
Use this for example if you write 16 or 32 bit numbers into a binary file to ensure it reads the same value on little and big endian
Definition at line 324 of file sge.h.
| #define sgeFree |
( |
|
target | ) |
|
Value:do {\
free(target);\
target=NULL;\
} while(0)
Free the object.
- Parameters:
-
| target | the pointer to the struct to free |
Definition at line 281 of file sge.h.
| #define sgeLock |
( |
|
surface | ) |
|
Value:do {\
if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);\
} while (0)
Lock a surface.
- Parameters:
-
Lock a surface for drawing operations.
You should always use this before operating on pixel level on a surface. On most platforms your code will work without locking surfaces, but there are platforms where you will experience crashes.
Definition at line 178 of file sge.h.
| #define sgeMalloc |
( |
|
target, |
|
|
|
type, |
|
|
|
amount |
|
) |
| |
Value:do {\
(target)=(type *)malloc((amount+1)*sizeof(type));\
if ((target)==NULL) {\
sgeBailOut("could not allocate %d bytes of ram\n", (int)((amount)*sizeof(type)));\
}\
memset(target,0,((amount)*sizeof(type)));\
} while (0)
Allocate ram, fill with 0.
- Parameters:
-
| target | the pointer to the struct to alloc |
| type | the struct type to alloc |
| amount | the number of sructs to alloc |
- Returns:
- a pointer to the allocated and zero filled ram
Allocates ram for a structure and fills it with zeros.
MyStruct *obj;
sgeMalloc(obj, MyStruct, 1);
Definition at line 223 of file sge.h.
| #define sgeMallocNoInit |
( |
|
target, |
|
|
|
type, |
|
|
|
amount |
|
) |
| |
Value:do {\
(target)=(type *)malloc((amount+1)*sizeof(type));\
if ((target)==NULL) {\
sgeBailOut("could not allocate %d bytes of ram\n", (int)((amount)*sizeof(type)));\
}\
} while (0)
Allocate ram, uninitialized.
- Parameters:
-
| target | the pointer to the struct to alloc |
| type | the struct type to alloc |
| amount | the number of sructs to alloc |
- Returns:
- a pointer to the allocated ram
Allocates ram for a structure
MyStruct *obj;
sgeMalloc(obj, MyStruct, 1);
Definition at line 245 of file sge.h.
| #define sgeNew |
( |
|
target, |
|
|
|
type |
|
) |
| sgeMalloc(target,type,1); |
Create one object (aka struct)
- Parameters:
-
| target | the pointer to the struct to alloc |
| type | the struct type to alloc |
- Returns:
- a pointer with allocated and zero filled ram
Definition at line 274 of file sge.h.
| #define sgeRandom |
( |
|
from, |
|
|
|
range |
|
) |
| (from + (int) ((float)(range+1) * (rand() / (RAND_MAX + (float)from)))) |
Get a random integer.
- Parameters:
-
| from | the base for the random |
| range | the range the random will be around from +/- |
- Returns:
- a random integer
Definition at line 293 of file sge.h.
| #define sgeRandomFloat |
( |
|
from, |
|
|
|
range |
|
) |
| (float) ((float)from + ((float)(range+1) * (rand() / (RAND_MAX + (float)from)))) |
Get a random float.
- Parameters:
-
| from | the base for the random |
| range | the range the random will be around from +/- |
- Returns:
- a random float
Definition at line 302 of file sge.h.
| #define sgeRealloc |
( |
|
target, |
|
|
|
type, |
|
|
|
amount |
|
) |
| |
Value:do {\
(target)=realloc((target),sizeof(type)*(amount+1));\
if (target==NULL) {\
sgeBailOut("could not allocate %d bytes of ram\n", (int)((amount)*sizeof(type)));\
}\
} while (0)
Resize allocated ram.
- Parameters:
-
| target | the pointer to the struct to alloc |
| type | the struct type to alloc |
| amount | the number of sructs to alloc |
- Returns:
- a pointer to the increased ram
Definition at line 260 of file sge.h.
| #define sgeRound |
( |
|
val | ) |
round(val) |
round a integer value, if there is no c lib function available
val a float value
- Returns:
- a int value rounded
Definition at line 339 of file sge.h.
| #define sgeUnlock |
( |
|
surface | ) |
|
Value:do {\
if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);\
} while (0)
Unlock a surface.
- Parameters:
-
Unlock a surface, must be called after processing a locked surface.
Definition at line 189 of file sge.h.