Gamedev Framework (gf) 1.2.0
A C++17 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...
 
constexpr Vector (ZeroType) noexcept
 Constructor that zero the vector out. More...
 
constexpr Vector (T val) noexcept
 Constructor that fills the vector with a value. More...
 
constexpr Vector (const T *array)
 Constructor that takes an array. More...
 
constexpr Vector (T first, T second) noexcept
 Constructor that takes 2 components. More...
 
 Vector (const Vector &other)=default
 Default copy constructor. More...
 
template<typename U >
 Vector (const Vector< U, 2 > &other) noexcept
 Converting copy constructor. More...
 
Vectoroperator= (const Vector &other)=default
 Default copy assignment. More...
 
constexpr T operator[] (std::size_t i) const
 Access to the \( i \)-th coordinate. More...
 
constexpr T & operator[] (std::size_t i)
 Access to the \( i \)-th coordinate. More...
 
T * begin ()
 Iterator.to the first element. More...
 
T * end ()
 Iterator to the element after the last one. More...
 
const T * begin () const
 Iterator.to the first element (const version). More...
 
const T * end () const
 Iterator on the element after the last one (const version). More...
 
const T * cbegin () const
 Iterator.on the first element (const version). More...
 
const T * cend () const
 Iterator on the element after the last one (const version). More...
 
constexpr void zero () noexcept
 Zero out the vector. More...
 

Public Attributes

union gf::Vector< T, 2 >:: { ... }  
 
union gf::Vector< T, 2 >:: { ... }  
 
x
 First coordinate in the (x,y) representation. More...
 
u
 First coordinate in the (u,v) representation. More...
 
s
 First coordinate in the (s,t) representation. More...
 
width
 First coordinate in the size representation. More...
 
col
 First coordinate in the indices representation. More...
 
y
 Second coordinate in the (x,y) representation. More...
 
v
 Second coordinate in the (u,v) representation. More...
 
t
 Second coordinate in the (s,t) representation. More...
 
height
 Second coordinate in the size representation. More...
 
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

◆ Vector() [1/7]

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

Default constructor.

This constructor is defaulted so that this type is trivial.

◆ Vector() [2/7]

template<typename T >
constexpr gf::Vector< T, 2 >::Vector ( ZeroType  )
inlineconstexprnoexcept

Constructor that zero the vector out.

◆ Vector() [3/7]

template<typename T >
constexpr gf::Vector< T, 2 >::Vector ( val)
inlineexplicitconstexprnoexcept

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

◆ Vector() [4/7]

template<typename T >
constexpr gf::Vector< T, 2 >::Vector ( const T *  array)
inlineexplicitconstexpr

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

◆ Vector() [5/7]

template<typename T >
constexpr gf::Vector< T, 2 >::Vector ( first,
second 
)
inlineconstexprnoexcept

Constructor that takes 2 components.

Parameters
firstThe first component
secondThe second component

◆ Vector() [6/7]

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

◆ Vector() [7/7]

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

Converting copy constructor.

Parameters
otherThe vector to copy from

Member Function Documentation

◆ 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=()

template<typename T >
Vector & gf::Vector< T, 2 >::operator= ( const Vector< T, 2 > &  other)
default

Default copy assignment.

This operator is defaulted so that this type is trivial.

◆ operator[]() [1/2]

template<typename T >
constexpr T & gf::Vector< T, 2 >::operator[] ( std::size_t  i)
inlineconstexpr

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

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

◆ operator[]() [2/2]

template<typename T >
constexpr T gf::Vector< T, 2 >::operator[] ( std::size_t  i) const
inlineconstexpr

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

◆ zero()

template<typename T >
constexpr void gf::Vector< T, 2 >::zero ( )
inlineconstexprnoexcept

Zero out the vector.

Member Data Documentation

◆ 

union gf::Vector< T, 2 > { ... } gf::Vector< T, 2 >::@67

An anonymous union to handle the first coordinate

◆ 

union gf::Vector< T, 2 > { ... } gf::Vector< T, 2 >::@69

An anonymous union to handle the second coordinate

◆ col

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

First coordinate in the indices representation.

See also
row

◆ height

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

Second coordinate in the size representation.

See also
width

◆ row

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

Second coordinate in the indices representation.

See also
col

◆ s

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

First coordinate in the (s,t) representation.

See also
t

◆ t

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

Second coordinate in the (s,t) representation.

See also
s

◆ u

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

First coordinate in the (u,v) representation.

See also
v

◆ v

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

Second coordinate in the (u,v) representation.

See also
u

◆ width

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

First coordinate in the size representation.

See also
height

◆ x

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

First coordinate in the (x,y) representation.

See also
y

◆ y

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

Second coordinate in the (x,y) representation.

See also
x