Gamedev Framework (gf)  0.6.0 A C++11 framework for 2D games
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 ()
Iterator.to the first element. More...

Tend ()
Iterator to the element after the last one. More...

const Tbegin () const
Iterator.to the first element (const version). More...

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

const Tcbegin () const
Iterator.on the first element (const version). More...

const Tcend () 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:

• the generic representation with the data member
• the (x,y) representation, used for generic coordinates in the 2D space
• the (u,v) representation, used for texture coordinates (see UV mapping)
• the (s,t) representation, used for texture coordinates
• the size representation with a width member and a height member, used to represent a 2-dimensional size
• the indices representation with a col member and a row member, used to access a 2-dimensional array (gf::Array2D)

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;

◆ Vector() [1/6]

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

Default constructor.

This constructor is defaulted so that this type is trivial.

◆ Vector() [2/6]

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
 val The value to fill the vector with

◆ Vector() [3/6]

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
 array An array with the values of the vector

◆ Vector() [4/6]

template<typename T >
 constexpr gf::Vector< T, 2 >::Vector ( T x, T y )
inline

Constructor that takes 2 components.

Parameters
 x The first component y The second component

◆ Vector() [5/6]

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
 other The vector to copy from

◆ Vector() [6/6]

template<typename T >
template<typename U >
 gf::Vector< T, 2 >::Vector ( const Vector< U, 2 > & other )
inline

Converting copy constructor.

Parameters
 other The vector to copy from

◆ begin() [1/2]

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

Iterator.to the first element.

Returns
A pointer to the first element.

◆ begin() [2/2]

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

Iterator.to the first element (const version).

Returns
A pointer on the first const element.

◆ cbegin()

template<typename T >
 const T* gf::Vector< T, 2 >::cbegin ( ) const
inline

Iterator.on the first element (const version).

Returns
A pointer on the first const element.

◆ cend()

template<typename T >
 const T* gf::Vector< T, 2 >::cend ( ) 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.

◆ end() [1/2]

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

Iterator to the element after the last one.

Returns
An invalid pointer that is the adress after the last element.

◆ end() [2/2]

template<typename T >
 const T* gf::Vector< T, 2 >::end ( ) 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.

◆ operator[]() [1/2]

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
 i the coordinate number
Returns
The $$i$$-th coordinate of the vector

◆ operator[]() [2/2]

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
 i the coordinate number
Returns
The $$i$$-th coordinate of the vector

◆ @23

 union { ... }

An anonymous union to handle the various representations

◆ col

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

First coordinate in the indices representation.

row

◆ data

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

Generic representation.

◆ height

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

Second coordinate in the size representation.

width

◆ row

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

Second coordinate in the indices representation.

col

◆ s

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

First coordinate in the (s,t) representation.

t

◆ t

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

Second coordinate in the (s,t) representation.

s

◆ u

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

First coordinate in the (u,v) representation.

v

◆ v

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

Second coordinate in the (u,v) representation.

u

◆ width

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

First coordinate in the size representation.

height

◆ x

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

First coordinate in the (x,y) representation.

Second coordinate in the (x,y) representation.