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

A 4D 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, T third, T fourth) noexcept
 Constructor that takes 4 components. More...
 
 Vector (const Vector &other)=default
 Default copy constructor. More...
 
template<typename U >
 Vector (const Vector< U, 4 > &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...
 
constexpr Vector< T, 2 > xy () const
 Swizzle to get the first two coordinates as a 2D vector. More...
 
constexpr Vector< T, 3 > xyz () const
 Swizzle to get the first three coordinates as a 3D vector. More...
 
constexpr Vector< T, 3 > rgb () const
 Swizzle to get the first three coordinates as a RGB color. More...
 

Public Attributes

union gf::Vector< T, 4 >:: { ... }  
 
union gf::Vector< T, 4 >:: { ... }  
 
union gf::Vector< T, 4 >:: { ... }  
 
union gf::Vector< T, 4 >:: { ... }  
 
x
 First coordinate in the (x,y,z,w) representation. More...
 
r
 First coordinate in the (r,g,b,a) representation. More...
 
y
 Second coordinate in the (x,y,z,w) representation. More...
 
g
 Second coordinate in the (r,g,b,a) representation. More...
 
z
 Third coordinate in the (x,y,z,w) representation. More...
 
b
 Third coordinate in the (r,g,b,a) representation. More...
 
w
 Fourth coordinate in the (x,y,z,w) representation. More...
 
a
 Fourth coordinate in the (r,g,b,a) representation. 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);
A 4D vector.
Definition: Vector.h:852

Constructor & Destructor Documentation

◆ Vector() [1/7]

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

Default constructor.

This constructor is defaulted so that this type is trivial.

◆ Vector() [2/7]

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

Constructor that zero the vector out.

◆ Vector() [3/7]

template<typename T >
constexpr gf::Vector< T, 4 >::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, 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

◆ Vector() [4/7]

template<typename T >
constexpr gf::Vector< T, 4 >::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 4 dimensions.

float array[4] = { 1.0f, -1.0f, 0.5f, 0.0f };
Parameters
arrayAn array with the values of the vector

◆ Vector() [5/7]

template<typename T >
constexpr gf::Vector< T, 4 >::Vector ( first,
second,
third,
fourth 
)
inlineconstexprnoexcept

Constructor that takes 4 components.

Parameters
firstThe first component
secondThe second component
thirdThe third component
fourthThe fourth component

◆ Vector() [6/7]

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

◆ Vector() [7/7]

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

Converting copy constructor.

Parameters
otherThe vector to copy from

Member Function Documentation

◆ begin() [1/2]

template<typename T >
T * gf::Vector< T, 4 >::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, 4 >::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, 4 >::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, 4 >::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, 4 >::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, 4 >::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, 4 >::operator= ( const Vector< T, 4 > &  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, 4 >::operator[] ( std::size_t  i)
inlineconstexpr

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

◆ operator[]() [2/2]

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

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

◆ rgb()

template<typename T >
constexpr Vector< T, 3 > gf::Vector< T, 4 >::rgb ( ) const
inlineconstexpr

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

◆ xy()

template<typename T >
constexpr Vector< T, 2 > gf::Vector< T, 4 >::xy ( ) const
inlineconstexpr

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

◆ xyz()

template<typename T >
constexpr Vector< T, 3 > gf::Vector< T, 4 >::xyz ( ) const
inlineconstexpr

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

◆ zero()

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

Zero out the vector.

Member Data Documentation

◆ 

union gf::Vector< T, 4 > { ... } gf::Vector< T, 4 >::@77

An anonymous union to handle the first coordinate

◆ 

union gf::Vector< T, 4 > { ... } gf::Vector< T, 4 >::@79

An anonymous union to handle the second coordinate

◆ 

union gf::Vector< T, 4 > { ... } gf::Vector< T, 4 >::@81

An anonymous union to handle the third coordinate

◆ 

union gf::Vector< T, 4 > { ... } gf::Vector< T, 4 >::@83

An anonymous union to handle the fourth coordinate

◆ a

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

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

See also
r, g, b

◆ b

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

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

See also
r, g, a

◆ g

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

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

See also
r, b, a

◆ r

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

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

See also
g, b, a

◆ w

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

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

See also
x, y, z

◆ x

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

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

See also
y, z, w

◆ y

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

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

See also
x, z, w

◆ z

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

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

See also
x, y, w