Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | Public Attributes | List of all members
gf::Vector< T, 4 > Struct Template Reference

A 4D vector. More...

#include <gf/Vector.h>

Public Member Functions

 Vector ()=default
 Default constructor. More...
 
 Vector (T val)
 Constructor that fills the vector with a value. More...
 
 Vector (T *array)
 Constructor that takes an array. More...
 
constexpr Vector (T x, T y, T z, T w)
 Constructor that takes 4 components. More...
 
 Vector (const Vector &other)=default
 Default copy constructor. More...
 
template<typename U >
 Vector (const Vector< U, 4 > &other)
 Converting copy constructor. More...
 
T operator[] (std::size_t i) const
 Access to the \( i \)-th coordinate. More...
 
Toperator[] (std::size_t i)
 Access to the \( i \)-th coordinate. More...
 
Tbegin (void)
 Iterator.to the first element. More...
 
Tend (void)
 Iterator to the element after the last one. More...
 
T const * begin (void) const
 Iterator.to the first element (const version). More...
 
T const * end (void) const
 Iterator on the element after the last one (const version). More...
 
T const * cbegin (void) const
 Iterator.on the first element (const version). More...
 
T const * cend (void) const
 Iterator on the element after the last one (const version). More...
 

Public Attributes

union gf::Vector< T, 4 >:: { ... }  
 
T data [4]
 Generic representation. More...
 
T x
 First coordinate in the (x,y,z,w) representation. More...
 
T y
 Second coordinate in the (x,y,z,w) representation. More...
 
T z
 Third coordinate in the (x,y,z,w) representation. More...
 
T w
 Fourth coordinate in the (x,y,z,w) representation. More...
 
T r
 First coordinate in the (r,g,b,a) representation. More...
 
T g
 Second coordinate in the (r,g,b,a) representation. More...
 
T b
 Third coordinate in the (r,g,b,a) representation. More...
 
T a
 Fourth coordinate in the (r,g,b,a) representation. More...
 
Vector< T, 2 > xy
 Swizzle to get the first two coordinates as a 2D vector. More...
 
Vector< T, 3 > xyz
 Swizzle to get the first three coordinates as a 3D vector. More...
 
Vector< T, 3 > rgb
 Swizzle to get the first three coordinates as a RGB color. More...
 

Detailed Description

template<typename T>
struct gf::Vector< T, 4 >

A 4D vector.

This specialization of gf::Vector handles the 4-dimension spaces. It can be accessed with various representations:

Several common typedef are defined:

For colors, some additional typedef are defined:

Usage example:

gf::Vector4f v1(98.0f, 75.0f, 23.0f);
v1.x = 57.0f;
gf::Vector4f v2 = v1 * 5.0f;
gf::Vector4f v3 = v1 + v2;
gf::Color4u opaqueGreen(0x00, 0xFF, 0x00, 0xFF);

Constructor & Destructor Documentation

template<typename T >
gf::Vector< T, 4 >::Vector ( )
default

Default constructor.

This constructor is defaulted so that this type is trivial.

template<typename T >
gf::Vector< T, 4 >::Vector ( T  val)
inlineexplicit

Constructor that fills the vector with a value.

This constructor takes a value and fills the entire vector with this value. Care must be taken when calling this constructor:

gf::Vector<int, 4> vecOK(42); // OK, vector is filled with 42
gf::Vector<int, 4> vecKO{42}; // KO, vector is initialized with a 42 in the first coordinate
Parameters
valThe value to fill the vector with
template<typename T >
gf::Vector< T, 4 >::Vector ( T array)
inlineexplicit

Constructor that takes an array.

This constructor can ease conversion from other math libraries. The array must contain enough data for 4 dimensions.

float array[4] = { 1.0f, -1.0f, 0.5f, 0.0f };
Parameters
arrayAn array with the values of the vector
template<typename T >
constexpr gf::Vector< T, 4 >::Vector ( T  x,
T  y,
T  z,
T  w 
)
inline

Constructor that takes 4 components.

Parameters
xThe first component
yThe second component
zThe third component
wThe fourth component
template<typename T >
gf::Vector< T, 4 >::Vector ( const Vector< T, 4 > &  other)
default

Default copy constructor.

This constructor is defaulted so that this type is trivial.

Parameters
otherThe vector to copy from
template<typename T >
template<typename U >
gf::Vector< T, 4 >::Vector ( const Vector< U, 4 > &  other)
inline

Converting copy constructor.

Parameters
otherThe vector to copy from

Member Function Documentation

template<typename T >
T* gf::Vector< T, 4 >::begin ( void  )
inline

Iterator.to the first element.

Returns
A pointer to the first element.
template<typename T >
T const* gf::Vector< T, 4 >::begin ( void  ) const
inline

Iterator.to the first element (const version).

Returns
A pointer on the first const element.
template<typename T >
T const* gf::Vector< T, 4 >::cbegin ( void  ) const
inline

Iterator.on the first element (const version).

Returns
A pointer on the first const element.
template<typename T >
T const* gf::Vector< T, 4 >::cend ( void  ) const
inline

Iterator on the element after the last one (const version).

Returns
An invalid pointer that is the adress after the last const element.
template<typename T >
T* gf::Vector< T, 4 >::end ( void  )
inline

Iterator to the element after the last one.

Returns
An invalid pointer that is the adress after the last element.
template<typename T >
T const* gf::Vector< T, 4 >::end ( void  ) const
inline

Iterator on the element after the last one (const version).

Returns
An invalid pointer that is the adress after the last const element.
template<typename T >
T gf::Vector< T, 4 >::operator[] ( std::size_t  i) const
inline

Access to the \( i \)-th coordinate.

gf::Vector<int, 4> vec = { 1, 3, 5, 7 };
std::printf("%i", vec[1]); // prints 3
Parameters
ithe coordinate number
Returns
The \( i \)-th coordinate of the vector
template<typename T >
T& gf::Vector< T, 4 >::operator[] ( std::size_t  i)
inline

Access to the \( i \)-th coordinate.

vec[0] = vec[1] = vec[2] = vec[3] = vec[4] = 0;
Parameters
ithe coordinate number
Returns
The \( i \)-th coordinate of the vector

Member Data Documentation

union { ... }

An anonymous union to handle the various representations

template<typename T >
T gf::Vector< T, 4 >::a

Fourth coordinate in the (r,g,b,a) representation.

See also
r, g, b
template<typename T >
T gf::Vector< T, 4 >::b

Third coordinate in the (r,g,b,a) representation.

See also
r, g, a
template<typename T >
T gf::Vector< T, 4 >::data[4]

Generic representation.

template<typename T >
T gf::Vector< T, 4 >::g

Second coordinate in the (r,g,b,a) representation.

See also
r, b, a
template<typename T >
T gf::Vector< T, 4 >::r

First coordinate in the (r,g,b,a) representation.

See also
g, b, a
template<typename T >
Vector<T, 3> gf::Vector< T, 4 >::rgb

Swizzle to get the first three coordinates as a RGB color.

template<typename T >
T gf::Vector< T, 4 >::w

Fourth coordinate in the (x,y,z,w) representation.

See also
x, y, z
template<typename T >
T gf::Vector< T, 4 >::x

First coordinate in the (x,y,z,w) representation.

See also
y, z, w
template<typename T >
Vector<T, 2> gf::Vector< T, 4 >::xy

Swizzle to get the first two coordinates as a 2D vector.

template<typename T >
Vector<T, 3> gf::Vector< T, 4 >::xyz

Swizzle to get the first three coordinates as a 3D vector.

template<typename T >
T gf::Vector< T, 4 >::y

Second coordinate in the (x,y,z,w) representation.

See also
x, z, w
template<typename T >
T gf::Vector< T, 4 >::z

Third coordinate in the (x,y,z,w) representation.

See also
x, y, w