Ckef01

Default vector type through 'template'

May 23rd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. utils.h
  2.  
  3. #ifndef GFX_UTILS_H
  4. #define GFX_UTILS_H
  5.  
  6. // Concatenation
  7. #define CAT_BAD(x,y) x ## y
  8. #define CAT(x,y) CAT_BAD(x,y)
  9.  
  10. // Template naming
  11. #define NAME_BAD(x,y) x ## _ ## y
  12. #define NAME(x,y) NAME_BAD(x,y)
  13.  
  14. #endif // GFX_UTILS_H
  15.  
  16.  
  17.  
  18. vec.h
  19.  
  20. #ifndef GFX_MATH_VEC_H
  21. #define GFX_MATH_VEC_H
  22.  
  23. #include "Groufix/utils.h"
  24.  
  25. // One worded datatypes
  26. typedef unsigned char uchar;
  27. typedef unsigned short ushort;
  28. typedef unsigned int uint;
  29.  
  30. #endif // GFX_MATH_VEC_H
  31.  
  32. /////////////////////////////////////////////////
  33. // Load all default sizes
  34. /////////////////////////////////////////////////
  35. #if !defined(VEC_SIZE)
  36.  
  37.     // Default 2D vector
  38.     #define VEC_SIZE 2
  39.     #include "Groufix/Math/vec.h"
  40.     #undef VEC_SIZE
  41.  
  42.     // Default 3D vector
  43.     #define VEC_SIZE 3
  44.     #include "Groufix/Math/vec.h"
  45.     #undef VEC_SIZE
  46.  
  47.     // Default 4D vector
  48.     #define VEC_SIZE 4
  49.     #include "Groufix/Math/vec.h"
  50.     #undef VEC_SIZE
  51.  
  52. /////////////////////////////////////////////////
  53. // Load all default datatypes
  54. /////////////////////////////////////////////////
  55. #elif !defined(VEC_TYPE)
  56.  
  57.     #define VEC_TYPE char
  58.     #include "Groufix/Math/vec.h"
  59.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), c);
  60.     #undef VEC_TYPE
  61.  
  62.     #define VEC_TYPE uchar
  63.     #include "Groufix/Math/vec.h"
  64.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), uc);
  65.     #undef VEC_TYPE
  66.  
  67.     #define VEC_TYPE short
  68.     #include "Groufix/Math/vec.h"
  69.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), s);
  70.     #undef VEC_TYPE
  71.  
  72.     #define VEC_TYPE ushort
  73.     #include "Groufix/Math/vec.h"
  74.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), us);
  75.     #undef VEC_TYPE
  76.  
  77.     #define VEC_TYPE int
  78.     #include "Groufix/Math/vec.h"
  79.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), i);
  80.     #undef VEC_TYPE
  81.  
  82.     #define VEC_TYPE uint
  83.     #include "Groufix/Math/vec.h"
  84.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), ui);
  85.     #undef VEC_TYPE
  86.  
  87.     #define VEC_TYPE float
  88.     #include "Groufix/Math/vec.h"
  89.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), f);
  90.     #undef VEC_TYPE
  91.  
  92.     #define VEC_TYPE double
  93.     #include "Groufix/Math/vec.h"
  94.     typedef VEC_NAME CAT(CAT(vec, VEC_SIZE), d);
  95.     #undef VEC_TYPE
  96.  
  97. #else
  98.  
  99. // Name & Function
  100. #undef VEC_NAME
  101. #undef VEC_FUNC
  102. #define VEC_NAME NAME(CAT(vec, VEC_SIZE), VEC_TYPE)
  103. #define VEC_FUNC(postfix) NAME(VEC_NAME, postfix)
  104.  
  105. typedef VEC_TYPE VEC_NAME[VEC_SIZE];
  106.  
  107. #endif
Advertisement
Add Comment
Please, Sign In to add comment