From 65906bc4de308c5841154a3d25368ade2eb38947 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 14 Dec 2024 14:46:30 +0100 Subject: add std::formatter for Vector2 --- src/crepe/api/Vector2.h | 7 +++++++ src/crepe/api/Vector2.hpp | 6 ++++++ src/example/demo.cpp | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index bf9d124..f4ba2b2 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace crepe { //! 2D vector @@ -94,4 +96,9 @@ struct Vector2 { } // namespace crepe +template +struct std::formatter> : std::formatter { + format_context::iterator format(crepe::Vector2 vec, format_context & ctx) const; +}; + #include "Vector2.hpp" diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index ff53cb0..75d875a 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -164,3 +164,9 @@ Vector2 Vector2::perpendicular() const { } } // namespace crepe + +template +std::format_context::iterator std::formatter>::format(crepe::Vector2 vec, format_context & ctx) const { + return formatter::format(std::format("{{{}, {}}}", vec.x, vec.y), ctx); +} + diff --git a/src/example/demo.cpp b/src/example/demo.cpp index 1aadc54..c0c8062 100644 --- a/src/example/demo.cpp +++ b/src/example/demo.cpp @@ -12,7 +12,7 @@ using namespace std; class PlayerController : public Script { void update() { Rigidbody & body = get_component(); - Log::logf("linear_velocity = {{{}, {}}}", body.data.linear_velocity.x, body.data.linear_velocity.y); + logf("linear_velocity = {}", body.data.linear_velocity); if (get_key_state(Keycode::SPACE)) { body.add_force_linear({ 0, -1 }); -- cgit v1.2.3