# source:trunk/src/matrix.h@237Tweet

Last change on this file since 237 was 237, checked in by sam, 11 years ago

Implement simple + and - operators for Int2, Float2 etc.

• Property svn:keywords set to `Id`
File size: 1.6 KB
Line
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
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
36template <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
49typedef Vec2<float> Float2;
50typedef Vec2<int> Int2;
51
52template <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
65typedef Vec3<float> Float3;
66typedef Vec3<int> Int3;
67
68#endif // __DH_MATRIX_H__
69
Note: See TracBrowser for help on using the repository browser.