JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Vec2.hpp
Go to the documentation of this file.
1// Copyright 2018-2024 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include <boost/qvm_lite.hpp>
8#include <iosfwd>
9
10#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
11#include <format>
12#endif
13
14namespace jngl {
15
36class Vec2 {
37public:
40
42 Vec2(double x, double y);
43
45 double x = 0;
46
48 double y = 0;
49
51 bool isNull() const;
52
54 void rotate(float angle);
55
57 template <class Archive> void serialize(Archive& ar, const unsigned int) {
58 ar(x, y);
59 }
60};
61
64
65} // namespace jngl
66
67#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
68template <> struct std::formatter<jngl::Vec2> {
69 constexpr static auto parse(std::format_parse_context& ctx) {
70 return ctx.begin();
71 }
72 auto format(const jngl::Vec2& v, auto& ctx) const {
73 return std::format_to(ctx.out(), "[x={}, y={}]", v.x, v.y);
74 }
75};
76#endif
77
78namespace boost::qvm {
79template <> struct vec_traits<jngl::Vec2> {
80 static int const dim = 2;
81 using scalar_type = double;
82
83 template <int I> static scalar_type& write_element(jngl::Vec2& v) {
84 return (&v.x)[I];
85 }
86 template <int I> static scalar_type read_element(const jngl::Vec2& v) {
87 return (&v.x)[I];
88 }
89
90 static scalar_type& write_element_idx(int i, jngl::Vec2& v) {
91 return (&v.x)[i];
92 }
93 static scalar_type read_element_idx(int i, jngl::Vec2 const& v) {
94 return (&v.x)[i];
95 }
96};
97} // namespace boost::qvm
98
99namespace jngl {
100using boost::qvm::operator+=;
101using boost::qvm::operator*=;
102using boost::qvm::operator/=;
103using boost::qvm::operator-=;
104using boost::qvm::operator==;
105using boost::qvm::operator!=;
106using boost::qvm::operator+;
107using boost::qvm::operator-;
108using boost::qvm::operator/;
109using boost::qvm::operator*;
110} // namespace jngl
Two-dimensional vector.
Definition Vec2.hpp:36
Vec2()
Null vector.
void rotate(float angle)
rotates the vector clock-wise by angle (radian)
double y
y component
Definition Vec2.hpp:48
bool isNull() const
returns true if both x and y are exactly 0
Vec2(double x, double y)
Creates a vector {x, y}.
void serialize(Archive &ar, const unsigned int)
Helper function to use with cereal or Boost.Serialization.
Definition Vec2.hpp:57
double x
x component
Definition Vec2.hpp:45
T format(T... args)
T format_to(T... args)
JNGL's main namespace.
std::ostream & operator<<(std::ostream &, const Vec2 &)
Prints the vector like this: [x=…, y=…].