G43riko
10/28/2016 - 8:27 AM

CPP Utils

#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))

template<typename T>
int indexOf(T[] array, uint sizeOfArray, const T& toFind){ //25.5.2017
  for(int i=0 ; i<sizeOfArray ; i++){
    if(array[i] == toFind){
      return i;
    }
  }
  return -1;
}


#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x)) //22.5.2017

#define SWAP(t, x, y) do { \ // 22.5.2017
	t temp = x; \
	x = y; \
	y = temp; \
} while (0)

#define SET_IF_IS_NULL(el, val) \
  if(el == nullptr){            \
      el = val;                 \
  }

#define CHECK_AND_CLEAR(el) \
  if(el){                   \
      el -> cleanUp();      \
      delete el;            \
      el = nullptr;         \
  }
  
#define SINI (a) (int)sin(num)
#define SINF(a) (float)sin(a)
#define COSI(a) (int)cos(num)
#define COSF(a) (float)cos(a)

#define ABS(x) (x < 0 ? -x : x)

//macro ako náhrada pre new
//použitie: GNEW(Vector3f, 2, 3, 4)
#define GNEW(X, ...) (new (malloc(sizeof(X))) X(__VA_ARGS__))

//Equal
#define EQ(a, b) !(a < b || a > b)

#define IS_NULL(x) (x == nullptr)
#define IS_NOT_NULL(x) (x != nullptr)

//Not Equal
#define NEQ(a, b) !EQ(a, b)

//Equal Zero
#define NZ(a) EQ(a, 0)

//Not Equal Zero
#define NEZ(a) !NZ(a)

/*******************************************MATH******************************************************/
#define MATH_PI 3.1415926535897932384626433832795
#define TO_RADIANS(x) (float)((x) * MATH_PI / 180.0f)
#define TO_DEGREES(x) (float)((x) * 180.0f / MATH_PI)

#ifndef MAX
  #define MIN (a, b) a < b ? a : b
#endif

#ifndef MAX
  #define MAX (a, b) a > b ? a : b
#endif

#ifndef CLAMP
  #define CLAMP(a, b, c) (a < b ? b : a > c ? c : a);
#endif

//Linerar Interpolation
#define LERP(a, b, c) (a + c * (b - a));