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

A 2D 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)
 Constructor that takes 2 components. More...
 
 Vector (const Vector &other)=default
 Default copy constructor. More...
 
template<typename U >
 Vector (const Vector< U, 2 > &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, 2 >:: { ... }  
 
T data [2]
 Generic representation. More...
 
T x
 First coordinate in the (x,y) representation. More...
 
T y
 Second coordinate in the (x,y) representation. More...
 
T u
 First coordinate in the (u,v) representation. More...
 
T v
 Second coordinate in the (u,v) representation. More...
 
T s
 First coordinate in the (s,t) representation. More...
 
T t
 Second coordinate in the (s,t) representation. More...
 
T width
 First coordinate in the size representation. More...
 
T height
 Second coordinate in the size representation. More...
 
T col
 First coordinate in the indices representation. More...
 
T row
 Second coordinate in the indices representation. More...
 

Detailed Description

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

A 2D vector.

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

Several common typedef are defined:

Usage example:

gf::Vector2f v1(16.5f, 24.0f);
v1.x = 18.2f;
gf::Vector2f v2 = v1 * 5.0f;
gf::Vector2f v3 = v1 + v2;
bool different = (v2 != v3);
size.width = 1920;
size.height = 1080;
gf::Vector2f texCoords;
texCoords.u = 0.0f;
texCoords.v = 0.3f;

Constructor & Destructor Documentation

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

Default constructor.

This constructor is defaulted so that this type is trivial.

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

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

Constructor that takes 2 components.

Parameters
xThe first component
yThe second component
template<typename T >
gf::Vector< T, 2 >::Vector ( const Vector< T, 2 > &  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, 2 >::Vector ( const Vector< U, 2 > &  other)
inline

Converting copy constructor.

Parameters
otherThe vector to copy from

Member Function Documentation

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

Iterator.to the first element.

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

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

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

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

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

First coordinate in the indices representation.

See also
row
template<typename T >
T gf::Vector< T, 2 >::data[2]

Generic representation.

template<typename T >
T gf::Vector< T, 2 >::height

Second coordinate in the size representation.

See also
width
template<typename T >
T gf::Vector< T, 2 >::row

Second coordinate in the indices representation.

See also
col
template<typename T >
T gf::Vector< T, 2 >::s

First coordinate in the (s,t) representation.

See also
t
template<typename T >
T gf::Vector< T, 2 >::t

Second coordinate in the (s,t) representation.

See also
s
template<typename T >
T gf::Vector< T, 2 >::u

First coordinate in the (u,v) representation.

See also
v
template<typename T >
T gf::Vector< T, 2 >::v

Second coordinate in the (u,v) representation.

See also
u
template<typename T >
T gf::Vector< T, 2 >::width

First coordinate in the size representation.

See also
height
template<typename T >
T gf::Vector< T, 2 >::x

First coordinate in the (x,y) representation.

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

Second coordinate in the (x,y) representation.

See also
x