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, 3 > Struct Template Reference

A 3D 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)
 Constructor that takes 3 components. More...
 
constexpr Vector (Vector< T, 2 > xy, T z)
 Constructor that takes a 2D vector and a z component. More...
 
 Vector (const Vector &other)=default
 Default copy constructor. More...
 
template<typename U >
 Vector (const Vector< U, 3 > &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, 3 >:: { ... }  
 
T data [3]
 Generic representation. More...
 
T x
 First coordinate in the (x,y,z) representation. More...
 
T y
 Second coordinate in the (x,y,z) representation. More...
 
T z
 Third coordinate in the (x,y,z) representation. More...
 
T r
 First coordinate in the (r,g,b) representation. More...
 
T g
 Second coordinate in the (r,g,b) representation. More...
 
T b
 Third coordinate in the (r,g,b) representation. More...
 
Vector< T, 2 > xy
 Swizzle to get the first two coordinates as a 2D vector. More...
 

Detailed Description

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

A 3D vector.

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

Several common typedef are defined:

For colors, some additional typedef are defined:

Usage example:

gf::Vector3f v1(58.0f, 96.0f, 63.0f);
v1.x = 94.0f;
gf::Vector3f v2 = v1 * 5.0f;
gf::Vector3f v3 = v1 + v2;
gf::Color3u green(0x00, 0xFF, 0x00);

Constructor & Destructor Documentation

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

Default constructor.

This constructor is defaulted so that this type is trivial.

template<typename T >
gf::Vector< T, 3 >::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, 3> vecOK(42); // OK, vector is filled with 42
gf::Vector<int, 3> 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, 3 >::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 3 dimensions.

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

Constructor that takes 3 components.

Parameters
xThe first component
yThe second component
zThe third component
template<typename T >
constexpr gf::Vector< T, 3 >::Vector ( Vector< T, 2 >  xy,
T  z 
)
inline

Constructor that takes a 2D vector and a z component.

Parameters
xyThe first 2 component, x and y
zThe z component
template<typename T >
gf::Vector< T, 3 >::Vector ( const Vector< T, 3 > &  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, 3 >::Vector ( const Vector< U, 3 > &  other)
inline

Converting copy constructor.

Parameters
otherThe vector to copy from

Member Function Documentation

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

Iterator.to the first element.

Returns
A pointer to the first element.
template<typename T >
T const* gf::Vector< T, 3 >::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, 3 >::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, 3 >::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, 3 >::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, 3 >::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, 3 >::operator[] ( std::size_t  i) const
inline

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

gf::Vector<int, 3> vec = { 1, 3, 5 };
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, 3 >::operator[] ( std::size_t  i)
inline

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

vec[0] = vec[1] = vec[2] = 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, 3 >::b

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

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

Generic representation.

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

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

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

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

See also
g, b
template<typename T >
T gf::Vector< T, 3 >::x

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

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

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

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

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

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

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

See also
x, y