From a68c3c96de4d5ee4bb7273a1a9b2da8a10f7360f Mon Sep 17 00:00:00 2001 From: Daniel Bratsberg Date: Sun, 3 Sep 2023 21:16:12 +0200 Subject: [PATCH 1/3] Adding ToString() func to Color, Vector2, Vector3, and Vector4 classes --- include/Color.hpp | 8 ++++++++ include/Vector2.hpp | 9 +++++++++ include/Vector3.hpp | 9 +++++++++ include/Vector4.hpp | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/include/Color.hpp b/include/Color.hpp index 6b84b2f5..0881c7f5 100644 --- a/include/Color.hpp +++ b/include/Color.hpp @@ -1,6 +1,7 @@ #ifndef RAYLIB_CPP_INCLUDE_COLOR_HPP_ #define RAYLIB_CPP_INCLUDE_COLOR_HPP_ +#include #include #include "./raylib.hpp" @@ -209,6 +210,13 @@ class Color : public ::Color { return ::ColorAlphaBlend(dst, *this, tint); } + inline std::string ToString() const + { + std::ostringstream oss; + oss << "Color(" << r << ", " << g << ", " << b << ", " << a << ")"; + return oss.str(); + } + inline static Color LightGray() { return LIGHTGRAY; } inline static Color Gray() { return GRAY; } inline static Color DarkGray() { return DARKGRAY; } diff --git a/include/Vector2.hpp b/include/Vector2.hpp index ca3d3fc7..34046152 100644 --- a/include/Vector2.hpp +++ b/include/Vector2.hpp @@ -5,6 +5,8 @@ #include #endif +#include + #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -422,6 +424,13 @@ class Vector2 : public ::Vector2 { return ::CheckCollisionPointLine(*this, p1, p2, threshold); } + inline std::string ToString() const + { + std::ostringstream oss; + oss << "Vector2(" << x << ", " << y << ")"; + return oss.str(); + } + private: void set(const ::Vector2& vec) { x = vec.x; diff --git a/include/Vector3.hpp b/include/Vector3.hpp index 04e9fdb0..c28f6813 100644 --- a/include/Vector3.hpp +++ b/include/Vector3.hpp @@ -5,6 +5,8 @@ #include #endif +#include + #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -329,6 +331,13 @@ class Vector3 : public ::Vector3 { return CheckCollisionSpheres(*this, radius1, center2, radius2); } + inline std::string ToString() const + { + std::ostringstream oss; + oss << "Vector3(" << x << ", " << y << ", " << z << ")"; + return oss.str(); + } + private: void set(const ::Vector3& vec) { x = vec.x; diff --git a/include/Vector4.hpp b/include/Vector4.hpp index 69013930..556c80cf 100644 --- a/include/Vector4.hpp +++ b/include/Vector4.hpp @@ -6,6 +6,8 @@ #include #endif +#include + #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -151,6 +153,13 @@ class Vector4 : public ::Vector4 { return ColorFromNormalized(); } + inline std::string ToString() const + { + std::ostringstream oss; + oss << "Vector4(" << x << ", " << y << ", " << z << ", " << w << ")"; + return oss.str(); + } + private: void set(const ::Vector4& vec4) { x = vec4.x; From f2a5238ed93b3f9a669c7ef987e8eef05dbf5000 Mon Sep 17 00:00:00 2001 From: Daniel Bratsberg Date: Sun, 3 Sep 2023 21:23:46 +0200 Subject: [PATCH 2/3] Adding std::string casts to Color, Vector2, Vector3, and Vector4 --- include/Color.hpp | 17 ++++++++++------- include/Vector2.hpp | 17 ++++++++++------- include/Vector3.hpp | 17 ++++++++++------- include/Vector4.hpp | 17 ++++++++++------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/include/Color.hpp b/include/Color.hpp index 0881c7f5..60c17713 100644 --- a/include/Color.hpp +++ b/include/Color.hpp @@ -66,6 +66,16 @@ class Color : public ::Color { return ::ColorToInt(*this); } + inline std::string ToString() const { + std::ostringstream oss; + oss << "Color(" << r << ", " << g << ", " << b << ", " << a << ")"; + return oss.str(); + } + + inline operator std::string() const { + return ToString(); + } + /** * Returns color with alpha applied, alpha goes from 0.0f to 1.0f */ @@ -210,13 +220,6 @@ class Color : public ::Color { return ::ColorAlphaBlend(dst, *this, tint); } - inline std::string ToString() const - { - std::ostringstream oss; - oss << "Color(" << r << ", " << g << ", " << b << ", " << a << ")"; - return oss.str(); - } - inline static Color LightGray() { return LIGHTGRAY; } inline static Color Gray() { return GRAY; } inline static Color DarkGray() { return DARKGRAY; } diff --git a/include/Vector2.hpp b/include/Vector2.hpp index 34046152..2e680e90 100644 --- a/include/Vector2.hpp +++ b/include/Vector2.hpp @@ -49,6 +49,16 @@ class Vector2 : public ::Vector2 { return !(*this == other); } + inline std::string ToString() const { + std::ostringstream oss; + oss << "Vector2(" << x << ", " << y << ")"; + return oss.str(); + } + + inline operator std::string() const { + return ToString(); + } + #ifndef RAYLIB_CPP_NO_MATH /** * Add two vectors (v1 + v2) @@ -424,13 +434,6 @@ class Vector2 : public ::Vector2 { return ::CheckCollisionPointLine(*this, p1, p2, threshold); } - inline std::string ToString() const - { - std::ostringstream oss; - oss << "Vector2(" << x << ", " << y << ")"; - return oss.str(); - } - private: void set(const ::Vector2& vec) { x = vec.x; diff --git a/include/Vector3.hpp b/include/Vector3.hpp index c28f6813..ecff7851 100644 --- a/include/Vector3.hpp +++ b/include/Vector3.hpp @@ -47,6 +47,16 @@ class Vector3 : public ::Vector3 { return !(*this == other); } + inline std::string ToString() const { + std::ostringstream oss; + oss << "Vector3(" << x << ", " << y << ", " << z << ")"; + return oss.str(); + } + + inline operator std::string() const { + return ToString(); + } + #ifndef RAYLIB_CPP_NO_MATH /** * Add two vectors @@ -331,13 +341,6 @@ class Vector3 : public ::Vector3 { return CheckCollisionSpheres(*this, radius1, center2, radius2); } - inline std::string ToString() const - { - std::ostringstream oss; - oss << "Vector3(" << x << ", " << y << ", " << z << ")"; - return oss.str(); - } - private: void set(const ::Vector3& vec) { x = vec.x; diff --git a/include/Vector4.hpp b/include/Vector4.hpp index 556c80cf..f3591968 100644 --- a/include/Vector4.hpp +++ b/include/Vector4.hpp @@ -60,6 +60,16 @@ class Vector4 : public ::Vector4 { return {x, y, z, w}; } + inline std::string ToString() const { + std::ostringstream oss; + oss << "Vector4(" << x << ", " << y << ", " << z << ", " << w << ")"; + return oss.str(); + } + + inline operator std::string() const { + return ToString(); + } + #ifndef RAYLIB_CPP_NO_MATH inline Vector4 Multiply(const ::Vector4& vector4) const { return QuaternionMultiply(*this, vector4); @@ -153,13 +163,6 @@ class Vector4 : public ::Vector4 { return ColorFromNormalized(); } - inline std::string ToString() const - { - std::ostringstream oss; - oss << "Vector4(" << x << ", " << y << ", " << z << ", " << w << ")"; - return oss.str(); - } - private: void set(const ::Vector4& vec4) { x = vec4.x; From bb5ae7820ff55683e986b0d22f2c16dc3f5481b1 Mon Sep 17 00:00:00 2001 From: Daniel Bratsberg Date: Sun, 3 Sep 2023 21:45:19 +0200 Subject: [PATCH 3/3] Changing to use TextFormat() instead of ostringstream for ToString() func --- include/Color.hpp | 5 +---- include/Vector2.hpp | 6 +----- include/Vector3.hpp | 6 +----- include/Vector4.hpp | 6 +----- 4 files changed, 4 insertions(+), 19 deletions(-) diff --git a/include/Color.hpp b/include/Color.hpp index 60c17713..783a7b6b 100644 --- a/include/Color.hpp +++ b/include/Color.hpp @@ -1,7 +1,6 @@ #ifndef RAYLIB_CPP_INCLUDE_COLOR_HPP_ #define RAYLIB_CPP_INCLUDE_COLOR_HPP_ -#include #include #include "./raylib.hpp" @@ -67,9 +66,7 @@ class Color : public ::Color { } inline std::string ToString() const { - std::ostringstream oss; - oss << "Color(" << r << ", " << g << ", " << b << ", " << a << ")"; - return oss.str(); + return TextFormat("Color(%d, %d, %d, %d)", r, g, b, a); } inline operator std::string() const { diff --git a/include/Vector2.hpp b/include/Vector2.hpp index 2e680e90..00f59dee 100644 --- a/include/Vector2.hpp +++ b/include/Vector2.hpp @@ -5,8 +5,6 @@ #include #endif -#include - #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -50,9 +48,7 @@ class Vector2 : public ::Vector2 { } inline std::string ToString() const { - std::ostringstream oss; - oss << "Vector2(" << x << ", " << y << ")"; - return oss.str(); + return TextFormat("Vector2(%f, %f)", x, y); } inline operator std::string() const { diff --git a/include/Vector3.hpp b/include/Vector3.hpp index ecff7851..b5e52be9 100644 --- a/include/Vector3.hpp +++ b/include/Vector3.hpp @@ -5,8 +5,6 @@ #include #endif -#include - #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -48,9 +46,7 @@ class Vector3 : public ::Vector3 { } inline std::string ToString() const { - std::ostringstream oss; - oss << "Vector3(" << x << ", " << y << ", " << z << ")"; - return oss.str(); + return TextFormat("Vector3(%f, %f, %f)", x, y, z); } inline operator std::string() const { diff --git a/include/Vector4.hpp b/include/Vector4.hpp index f3591968..a87e807f 100644 --- a/include/Vector4.hpp +++ b/include/Vector4.hpp @@ -6,8 +6,6 @@ #include #endif -#include - #include "./raylib.hpp" #include "./raymath.hpp" #include "./raylib-cpp-utils.hpp" @@ -61,9 +59,7 @@ class Vector4 : public ::Vector4 { } inline std::string ToString() const { - std::ostringstream oss; - oss << "Vector4(" << x << ", " << y << ", " << z << ", " << w << ")"; - return oss.str(); + return TextFormat("Vector4(%f, %f, %f, %f)", x, y, z, w); } inline operator std::string() const {