aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSvenson Han Göthel <[email protected]>2024-07-14 15:12:02 +0200
committerSvenson Han Göthel <[email protected]>2024-07-14 15:12:02 +0200
commitb7c9fa953f71f85cdb8b6c67ca5ebe3d20fdc7e1 (patch)
tree71b4d32d79635501d15470cfcfeb16aebc36e91a
parent8539c25b0985d65d7c8428ef660041838dbbb641 (diff)
unit: Move literals to namspace pixel::literals (cleanup); Use C++20 numbers PI in high resolutionv0.9.3
-rw-r--r--examples/solarsystem.cpp1
-rw-r--r--examples/spacewars.cpp15
-rw-r--r--include/pixel/pixel.hpp5
-rw-r--r--include/pixel/unit.hpp74
4 files changed, 52 insertions, 43 deletions
diff --git a/examples/solarsystem.cpp b/examples/solarsystem.cpp
index c472043..681be68 100644
--- a/examples/solarsystem.cpp
+++ b/examples/solarsystem.cpp
@@ -31,6 +31,7 @@
#include <vector>
using namespace pixel;
+using namespace pixel::literals;
using namespace pixel::f2;
bool draw_all_orbits = false;
diff --git a/examples/spacewars.cpp b/examples/spacewars.cpp
index 51ece24..0f72b99 100644
--- a/examples/spacewars.cpp
+++ b/examples/spacewars.cpp
@@ -31,6 +31,8 @@
#include <algorithm>
#include <random>
+using namespace pixel::literals;
+
static pixel::input_event_t event;
constexpr static const int player_id_1 = 1;
constexpr static const int player_id_2 = 2;
@@ -358,7 +360,7 @@ class peng_t {
pixel::f2::vec_t g = sun->gravity_env(m_peng.p_center);
m_velo += g * dt;
m_peng.move( m_velo * dt );
- m_peng.rotate(pixel::adeg_to_rad(180.0f) * dt);
+ m_peng.rotate(std::numbers::pi_v<float> * dt); // 180 degrees (180_deg)
m_fuse = std::max(0.0f, m_fuse - dt);
return !sun->hit( m_peng.p_center ) &&
!hits_fragment() ;
@@ -596,7 +598,6 @@ class spaceship_t : public pixel::f2::linestrip_t {
typedef std::shared_ptr<spaceship_t> spaceship_ref_t;
std::vector<spaceship_ref_t> spaceship;
-
/**
* Unrotated:
*
@@ -608,7 +609,7 @@ std::vector<spaceship_ref_t> spaceship;
*/
spaceship_ref_t make_spaceship1(idscore_t* owner, const pixel::f2::point_t& m, const float h=spaceship_t::height) noexcept
{
- spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, pixel::adeg_to_rad(90.0f));
+ spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, 90_deg);
const float width = 4.0f/5.0f * h;;
@@ -650,7 +651,7 @@ spaceship_ref_t make_spaceship1(idscore_t* owner, const pixel::f2::point_t& m, c
*/
spaceship_ref_t make_spaceship2(idscore_t* owner, const pixel::f2::point_t& m, const float h=spaceship_t::height) noexcept
{
- spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, pixel::adeg_to_rad(90.0f));
+ spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, 90_deg);
const float w = 4.0f / 5.0f * h;
const float w_s = w / 4.0f;
@@ -711,7 +712,7 @@ spaceship_ref_t make_spaceship2(idscore_t* owner, const pixel::f2::point_t& m, c
*/
spaceship_ref_t make_spaceship3(idscore_t* owner, const pixel::f2::point_t& m, const float h=spaceship_t::height) noexcept
{
- spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, pixel::adeg_to_rad(90.0f));
+ spaceship_ref_t lf = std::make_shared<spaceship_t>(owner, m, 90_deg);
const float w = 4.0f / 5.0f * h;
pixel::f2::point_t p = {m.x - w / 2, m.y - h / 2};
@@ -758,8 +759,8 @@ void reset_asteroids(int count) {
const float height = height_h + height_h*next_rnd();
const float angle = pixel::adeg_to_rad(next_rnd() * 360.0f);
const float velocity = 10.0f + next_rnd() * 10.0f; // m/s
- const float rot_velocity = ( pixel::adeg_to_rad(15.0f) +
- next_rnd() * pixel::adeg_to_rad(15.0f) )
+ const float rot_velocity = ( 15_deg +
+ next_rnd() * 15_deg )
* ( i%2 == 0 ? 1.0f : -1.0f ); // angle/s
const float jitter = 1.0f / ( 4.0f + 4.0f * next_rnd() );
pixel::f2::point_t p0(pixel::cart_coord.min_x()+(int)(pixel::cart_coord.width()*next_rnd()),
diff --git a/include/pixel/pixel.hpp b/include/pixel/pixel.hpp
index 78aa41b..45d529e 100644
--- a/include/pixel/pixel.hpp
+++ b/include/pixel/pixel.hpp
@@ -39,6 +39,7 @@
#include <iostream>
#include <cctype>
+#include <pixel/unit.hpp>
#include <pixel/version.hpp>
#if defined(__EMSCRIPTEN__)
@@ -113,12 +114,12 @@ namespace pixel {
/** Converts arc-degree to radians */
inline constexpr float adeg_to_rad(const float arc_degree) noexcept {
- return arc_degree * (float)M_PI / 180.0f;
+ return arc_degree * std::numbers::pi_v<float> / 180.0f;
}
/** Converts radians to arc-degree */
inline constexpr float rad_to_adeg(const float rad) noexcept {
- return rad * 180.0f / (float)M_PI;
+ return rad * 180.0f / std::numbers::pi_v<float>;
}
enum class orientation_t {
diff --git a/include/pixel/unit.hpp b/include/pixel/unit.hpp
index 3331a47..e1a74c4 100644
--- a/include/pixel/unit.hpp
+++ b/include/pixel/unit.hpp
@@ -8,7 +8,8 @@
#ifndef INCLUDE_PIXEL_UNIT_HPP_
#define INCLUDE_PIXEL_UNIT_HPP_
-#include <cmath>
+#include <numbers>
+
namespace pixel {
/** Time in fractions of seconds. */
@@ -24,40 +25,45 @@ namespace pixel {
/** Angle in fraction of radians. */
typedef float si_angle_t;
- constexpr si_time_t operator ""_year(unsigned long long int __v) { return (si_time_t)__v*365.25*24*3600.0; }
- constexpr si_time_t operator ""_month(unsigned long long int __v) { return (si_time_t)__v*30*24*3600.0; }
- constexpr si_time_t operator ""_week(unsigned long long int __v) { return (si_time_t)__v*7.0*24.0*3600.0; }
- constexpr si_time_t operator ""_day(unsigned long long int __v) { return (si_time_t)__v*24.0*3600.0; }
+ namespace literals {
+ constexpr si_time_t operator ""_year(unsigned long long int __v) { return (si_time_t)__v*365.25f*24.0f*3600.0f; }
+ constexpr si_time_t operator ""_month(unsigned long long int __v) { return (si_time_t)__v*30.0f*24.0f*3600.0f; }
+ constexpr si_time_t operator ""_week(unsigned long long int __v) { return (si_time_t)__v*7.0f*24.0f*3600.0f; }
+ constexpr si_time_t operator ""_day(unsigned long long int __v) { return (si_time_t)__v*24.0f*3600.0f; }
+
+ constexpr si_time_t operator ""_h(unsigned long long int __v) { return (si_time_t)__v*3600.0f; }
+ constexpr si_time_t operator ""_min(unsigned long long int __v) { return (si_time_t)__v*60.0f; }
+ constexpr si_time_t operator ""_s(unsigned long long int __v) { return (si_time_t)__v; }
+ constexpr si_time_t operator ""_ms(unsigned long long int __v) { return (si_time_t)__v/1000.0f; }
- constexpr si_time_t operator ""_h(unsigned long long int __v) { return (si_time_t)__v*3600.0; }
- constexpr si_time_t operator ""_min(unsigned long long int __v) { return (si_time_t)__v*60.0; }
- constexpr si_time_t operator ""_s(unsigned long long int __v) { return (si_time_t)__v; }
- constexpr si_time_t operator ""_ms(unsigned long long int __v) { return (si_time_t)__v/1000.0; }
-
- constexpr si_length_t operator ""_km(unsigned long long int __v) { return (si_length_t)__v*1000.0; }
- constexpr si_length_t operator ""_m(unsigned long long int __v) { return (si_length_t)__v; }
- constexpr si_length_t operator ""_dm(unsigned long long int __v) { return (si_length_t)__v/10.0; }
- constexpr si_length_t operator ""_cm(unsigned long long int __v) { return (si_length_t)__v/100.0; }
- constexpr si_length_t operator ""_mm(unsigned long long int __v) { return (si_length_t)__v/1000.0; }
-
- constexpr si_mass_t operator ""_t(unsigned long long int __v) { return (si_mass_t)__v * 1000.0; }
- constexpr si_mass_t operator ""_kg(unsigned long long int __v) { return (si_mass_t)__v; }
- constexpr si_mass_t operator ""_g(unsigned long long int __v) { return (si_mass_t)__v/1000.0; }
- constexpr si_mass_t operator ""_mg(unsigned long long int __v) { return (si_mass_t)__v/1000000.0; }
-
- constexpr si_velo_t operator ""_km_s(unsigned long long int __v) { return (si_velo_t)__v * 1000.0; }
- constexpr si_velo_t operator ""_m_s(unsigned long long int __v) { return (si_velo_t)__v; }
- constexpr si_velo_t operator ""_km_h(unsigned long long int __v) { return (si_velo_t)__v / 3.6; }
- constexpr si_velo_t operator ""_m_h(unsigned long long int __v) { return (si_velo_t)__v / 3600.0; }
-
- constexpr si_accel_t operator ""_km_s2(unsigned long long int __v) { return (si_accel_t)__v * 1000; }
- constexpr si_accel_t operator ""_m_s2(unsigned long long int __v) { return (si_accel_t)__v; }
- constexpr si_accel_t operator ""_dm_s2(unsigned long long int __v) { return (si_accel_t)__v / 10; }
- constexpr si_accel_t operator ""_cm_s2(unsigned long long int __v) { return (si_accel_t)__v / 100; }
- constexpr si_accel_t operator ""_mm_s2(unsigned long long int __v) { return (si_accel_t)__v / 1000; }
-
- constexpr si_angle_t operator ""_rad(unsigned long long int __v) { return (si_angle_t)__v; }
- constexpr si_angle_t operator ""_adeg(unsigned long long int __v) { return (si_angle_t)__v / 180 * M_PI; }
+ constexpr si_length_t operator ""_km(unsigned long long int __v) { return (si_length_t)__v*1000.0f; }
+ constexpr si_length_t operator ""_m(unsigned long long int __v) { return (si_length_t)__v; }
+ constexpr si_length_t operator ""_dm(unsigned long long int __v) { return (si_length_t)__v/10.0f; }
+ constexpr si_length_t operator ""_cm(unsigned long long int __v) { return (si_length_t)__v/100.0f; }
+ constexpr si_length_t operator ""_mm(unsigned long long int __v) { return (si_length_t)__v/1000.0f; }
+
+ constexpr si_mass_t operator ""_t(unsigned long long int __v) { return (si_mass_t)__v * 1000.0f; }
+ constexpr si_mass_t operator ""_kg(unsigned long long int __v) { return (si_mass_t)__v; }
+ constexpr si_mass_t operator ""_g(unsigned long long int __v) { return (si_mass_t)__v/1000.0f; }
+ constexpr si_mass_t operator ""_mg(unsigned long long int __v) { return (si_mass_t)__v/1000000.0f; }
+
+ constexpr si_velo_t operator ""_km_s(unsigned long long int __v) { return (si_velo_t)__v * 1000.0f; }
+ constexpr si_velo_t operator ""_m_s(unsigned long long int __v) { return (si_velo_t)__v; }
+ constexpr si_velo_t operator ""_km_h(unsigned long long int __v) { return (si_velo_t)__v / 3.6f; }
+ constexpr si_velo_t operator ""_m_h(unsigned long long int __v) { return (si_velo_t)__v / 3600.0; }
+
+ constexpr si_accel_t operator ""_km_s2(unsigned long long int __v) { return (si_accel_t)__v * 1000.0f; }
+ constexpr si_accel_t operator ""_m_s2(unsigned long long int __v) { return (si_accel_t)__v; }
+ constexpr si_accel_t operator ""_dm_s2(unsigned long long int __v) { return (si_accel_t)__v / 10.0f; }
+ constexpr si_accel_t operator ""_cm_s2(unsigned long long int __v) { return (si_accel_t)__v / 100.0f; }
+ constexpr si_accel_t operator ""_mm_s2(unsigned long long int __v) { return (si_accel_t)__v / 1000.0f; }
+
+ constexpr si_angle_t operator ""_rad(unsigned long long int __v) { return (si_angle_t)__v; }
+ constexpr si_angle_t operator ""_deg(unsigned long long int __v) { return (si_angle_t)((long double)__v / 180.0 * std::numbers::pi_v<long double>); }
+
+ constexpr si_angle_t operator ""_rad(long double __v) { return (si_angle_t)__v; }
+ constexpr si_angle_t operator ""_deg(long double __v) { return (si_angle_t)(__v / 180.0 * std::numbers::pi_v<long double>); }
+ }
}
#endif /* INCLUDE_PIXEL_UNIT_HPP_ */