1 | // |
---|

2 | // Lol Engine |
---|

3 | // |
---|

4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|

5 | // This program is free software; you can redistribute it and/or |
---|

6 | // modify it under the terms of the Do What The Fuck You Want To |
---|

7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|

8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|

9 | // |
---|

10 | |
---|

11 | // |
---|

12 | // The Matrix classes |
---|

13 | // ------------------ |
---|

14 | // |
---|

15 | |
---|

16 | #if !defined __DH_MATRIX_H__ |
---|

17 | #define __DH_MATRIX_H__ |
---|

18 | |
---|

19 | #define OPERATORS(elems) \ |
---|

20 | inline Vec##elems<T> operator+(Vec##elems<T> const &op) const \ |
---|

21 | { \ |
---|

22 | Vec##elems<T> ret; \ |
---|

23 | for (int n = 0; n < elems; n++) \ |
---|

24 | ret[n] = (*this)[n] + op[n]; \ |
---|

25 | return ret; \ |
---|

26 | } \ |
---|

27 | \ |
---|

28 | inline Vec##elems<T> operator-(Vec##elems<T> const &op) const \ |
---|

29 | { \ |
---|

30 | Vec##elems<T> ret; \ |
---|

31 | for (int n = 0; n < elems; n++) \ |
---|

32 | ret[n] = (*this)[n] - op[n]; \ |
---|

33 | return ret; \ |
---|

34 | } |
---|

35 | |
---|

36 | template <typename T> struct Vec2 |
---|

37 | { |
---|

38 | Vec2() { x = y = 0; } |
---|

39 | Vec2(T _x, T _y) { x = _x; y = _y; } |
---|

40 | T& operator[](int n) { return *(&x + n); } |
---|

41 | T const& operator[](int n) const { return *(&x + n); } |
---|

42 | |
---|

43 | OPERATORS(2) |
---|

44 | |
---|

45 | union { T x; T a; T i; }; |
---|

46 | union { T y; T b; T j; }; |
---|

47 | }; |
---|

48 | |
---|

49 | typedef Vec2<float> Float2; |
---|

50 | typedef Vec2<int> Int2; |
---|

51 | |
---|

52 | template <typename T> struct Vec3 |
---|

53 | { |
---|

54 | Vec3() { x = y = z = 0; } |
---|

55 | Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } |
---|

56 | T& operator[](int n) { return *(&x + n); } |
---|

57 | |
---|

58 | OPERATORS(3) |
---|

59 | |
---|

60 | union { T x; T a; T i; }; |
---|

61 | union { T y; T b; T j; }; |
---|

62 | union { T z; T c; T k; }; |
---|

63 | }; |
---|

64 | |
---|

65 | typedef Vec3<float> Float3; |
---|

66 | typedef Vec3<int> Int3; |
---|

67 | |
---|

68 | #endif // __DH_MATRIX_H__ |
---|

69 | |
---|