Macros.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // C RunTime Header Files
  2. #ifndef GWEN_MACROS_H
  3. #define GWEN_MACROS_H
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #ifndef __APPLE__
  7. #include <malloc.h>
  8. #endif //__APPLE__
  9. #include <memory.h>
  10. #include <algorithm>
  11. // Not tied to platform api.
  12. #define GwenUtil_Min( a, b ) ( ( (a) < (b) ) ? (a) : (b) )
  13. #define GwenUtil_Max( a, b ) ( ( (a) > (b) ) ? (a) : (b) )
  14. #define GwenUtil_VSWPrintFSafeSized( _DstBuf_ARRAY_, _Format, _ArgList ) GwenUtil_VSWPrintFSafe( _DstBuf_ARRAY_, sizeof( _DstBuf_ARRAY_ ) / sizeof( wchar_t ), _Format, _ArgList )
  15. #ifdef _WIN32
  16. #ifndef NOMINMAX
  17. #define NOMINMAX
  18. #endif
  19. #include <windows.h>
  20. #define GwenUtil_VSNPrintFSafe( _DstBuf, _DstSize, _MaxCount, _Format, _ArgList ) vsnprintf_s( _DstBuf, _DstSize, _MaxCount, _Format, _ArgList )
  21. #define GwenUtil_VSWPrintFSafe( _DstBuf, _SizeInWords, _Format, _ArgList ) vswprintf_s( _DstBuf, _SizeInWords, _Format, _ArgList )
  22. #define GwenUtil_OutputDebugCharString( lpOutputString ) OutputDebugStringA( lpOutputString )
  23. #define GwenUtil_OutputDebugWideString( lpOutputString ) OutputDebugStringW( lpOutputString )
  24. #define GwenUtil_WideStringToFloat( _Str ) _wtof( _Str )
  25. #elif defined(__APPLE__)
  26. #include <CoreFoundation/CoreFoundation.h>
  27. #define GwenUtil_VSNPrintFSafe( _DstBuf, _DstSize, _MaxCount, _Format, _ArgList ) vsnprintf( _DstBuf, _DstSize, _Format, _ArgList )
  28. #define GwenUtil_VSWPrintFSafe( _DstBuf, _SizeInWords, _Format, _ArgList ) vswprintf( _DstBuf, _SizeInWords, _Format, _ArgList )
  29. #define GwenUtil_OutputDebugCharString( lpOutputString ) //printf( lpOutputString )
  30. #define GwenUtil_OutputDebugWideString( lpOutputString ) //wprintf( lpOutputString )
  31. #define GwenUtil_WideStringToFloat( _Str ) wcstof(_Str, NULL)
  32. #elif defined(__linux__)
  33. #define GwenUtil_VSNPrintFSafe( _DstBuf, _DstSize, _MaxCount, _Format, _ArgList ) vsnprintf( _DstBuf, _DstSize, _Format, _ArgList )
  34. #define GwenUtil_VSWPrintFSafe( _DstBuf, _SizeInWords, _Format, _ArgList ) vswprintf( _DstBuf, _SizeInWords, _Format, _ArgList )
  35. #define GwenUtil_OutputDebugCharString( lpOutputString ) //printf( lpOutputString )
  36. #define GwenUtil_OutputDebugWideString( lpOutputString ) //wprintf( lpOutputString )
  37. #define GwenUtil_WideStringToFloat( _Str ) wcstof(_Str, NULL)
  38. #else
  39. #error MUST_IMPLEMENT_PLATFORM
  40. #endif
  41. namespace Gwen
  42. {
  43. template <typename T1, typename T2, typename T3 >
  44. T1 Clamp( T1 current, T2 vmin, T3 vmax )
  45. {
  46. if ( current > vmax ) return (T1)vmax;
  47. if ( current < vmin ) return (T1)vmin;
  48. return current;
  49. }
  50. template <typename T, typename T2>
  51. inline T Approach( T fCurrent, T fTarget, T2 fDelta )
  52. {
  53. if ( fCurrent < fTarget )
  54. {
  55. fCurrent += fDelta;
  56. if ( fCurrent > fTarget ) return fTarget;
  57. }
  58. else if ( fCurrent > fTarget )
  59. {
  60. fCurrent -= fDelta;
  61. if ( fCurrent < fTarget ) return fTarget;
  62. }
  63. return fCurrent;
  64. }
  65. }
  66. #endif