1 /**
2 Grimoire
3 Copyright (c) 2017 Enalye
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising
7 from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute
11 it freely, subject to the following restrictions:
12 
13 	1. The origin of this software must not be misrepresented;
14 	   you must not claim that you wrote the original software.
15 	   If you use this software in a product, an acknowledgment
16 	   in the product documentation would be appreciated but
17 	   is not required.
18 
19 	2. Altered source versions must be plainly marked as such,
20 	   and must not be misrepresented as being the original software.
21 
22 	3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 module core.vec4;
26 
27 import derelict.sdl2.sdl;
28 
29 import core.vec2;
30 
31 struct Vec4(T) {
32 	static assert(__traits(isArithmetic, T));
33 
34 	static if(__traits(isUnsigned, T)) {
35 		enum one = Vec4!T(1u, 1u, 1u, 1u);
36 		enum zero = Vec4!T(0u, 0u, 0u, 0u);
37 	}
38 	else {
39 		static if(__traits(isFloating, T)) {
40 			enum one = Vec4!T(1f, 1f, 1f, 1f);
41 			enum half = Vec4!T(.5f, .5f, .5f, .5f);
42 			enum zero = Vec4!T(0f, 0f, 0f, 0f);
43 		}
44 		else {
45 			enum one = Vec4!T(1, 1, 1, 1);
46 			enum zero = Vec4!T(0, 0, 0, 0);
47 		}
48 	}
49 
50 	T x, y, z, w;
51 
52 	@property {
53 		Vec2!T xy() const { return Vec2!T(x, y); }
54 		Vec2!T xy(Vec2!T v) {
55 			x = v.x;
56 			y = v.y;
57 			return v;
58 		}
59 
60 		Vec2!T zw() const { return Vec2!T(z, w); }
61 		Vec2!T zw(Vec2!T v) {
62 			z = v.x;
63 			w = v.y;
64 			return v;
65 		}
66 	}
67 
68 	this(T nx, T ny, T nz, T nw) {
69 		x = nx;
70 		y = ny;
71 		z = nz;
72 		w = nw;
73 	}
74 
75 	this(Vec2!T nxy, Vec2!T nzw) {
76 		x = nxy.x;
77 		y = nxy.y;
78 		z = nzw.x;
79 		w = nzw.y;
80 	}
81 
82 	void set(T nx, T ny, T nz, T nw) {
83 		x = nx;
84 		y = ny;
85 		z = nz;
86 		w = nw;
87 	}
88 
89 	void set(Vec2!T nxy, Vec2!T nzw) {
90 		x = nxy.x;
91 		y = nxy.y;
92 		z = nzw.x;
93 		w = nzw.y;
94 	}
95 
96 	bool opEquals(const Vec4!T v) const {
97 		return (x == v.x) && (y == v.y) && (z == v.z) && (w == v.w);
98 	}
99 
100 	Vec4!T opUnary(string op)() const {
101 		return mixin("Vec4!T(" ~ op ~ " x, " ~ op ~ " y, " ~ op ~ " z, " ~ op ~ " w)");
102 	}
103 
104 	Vec4!T opBinary(string op)(const Vec4!T v) const {
105 		return mixin("Vec4!T(x " ~ op ~ " v.x, y " ~ op ~ " v.y, z " ~ op ~ " v.z, w " ~ op ~ " v.w)");
106 	}
107 
108 	Vec4!T opBinary(string op)(T s) const {
109 		return mixin("Vec4!T(x " ~ op ~ " s, y " ~ op ~ " s, z " ~ op ~ " s, w " ~ op ~ " s)");
110 	}
111 
112 	Vec4!T opBinaryRight(string op)(T s) const {
113 		return mixin("Vec4!T(s " ~ op ~ " x, s " ~ op ~ " y, s " ~ op ~ " z, s " ~ op ~ "w)");
114 	}
115 
116 	Vec4!T opOpAssign(string op)(Vec4!T v) {
117 		mixin("x = x" ~ op ~ "v.x;y = y" ~ op ~ "v.y;z = z" ~ op ~ "v.z;w = w" ~ op ~ "v.w;");
118 		return this;
119 	}
120 
121 	Vec4!T opOpAssign(string op)(T s) {
122 		mixin("x = x" ~ op ~ "s;y = y" ~ op ~ "s;z = z" ~ op ~ "s;w = w" ~ op ~ "s;");
123 		return this;
124 	}
125 
126 	Vec4!U opCast(V: Vec4!U, U)() const {
127 		return V(cast(U)x, cast(U)y, cast(U)z, cast(U)w);
128 	}
129 
130 	static if(__traits(isIntegral, T)) {
131 		SDL_Rect toSdlRect() const {
132 			SDL_Rect sdlRect = { x, y, z, w };
133 			return sdlRect;
134 		}
135 	}
136 }
137 
138 alias Vec4f = Vec4!(float);
139 alias Vec4i = Vec4!(int);
140 alias Vec4u = Vec4!(uint);