From b3fc0aa9ed4b583704035e4b25c9896967a6e36c Mon Sep 17 00:00:00 2001 From: Javidx9 <25419386+OneLoneCoder@users.noreply.github.com> Date: Sat, 14 Sep 2024 23:26:31 +0100 Subject: [PATCH] How to really frustrate Moros --- extensions/olcPGEX_SplashScreen.h | 4 +- olcPixelGameEngine.h | 416 +++++++++++++++++++++++------- 2 files changed, 324 insertions(+), 96 deletions(-) diff --git a/extensions/olcPGEX_SplashScreen.h b/extensions/olcPGEX_SplashScreen.h index 5217b9fa..044ac603 100644 --- a/extensions/olcPGEX_SplashScreen.h +++ b/extensions/olcPGEX_SplashScreen.h @@ -165,7 +165,7 @@ namespace olc for (int y = 0; y < spr.Sprite()->height; y++) for (int x = 0; x < spr.Sprite()->width; x++) vBoom[y * spr.Sprite()->width + x] = std::make_pair( - vPosition + olc::vf2d(x, y), + vPosition + olc::vf2d(float(x), float(y)), olc::vf2d( (float(rand()) / float(RAND_MAX)) * 10.0f - 5.0f, (float(rand()) / float(RAND_MAX)) * 10.0f - 5.0f) @@ -207,7 +207,7 @@ namespace olc bComplete = true; } - pge->DrawPartialDecal(vScale * vBoom[y * spr.Sprite()->width + x].first * 2.0f, spr.Decal(), olc::vf2d(x, y), { 1, 1 }, vScale * 2.0f, olc::PixelF(1.0f, 1.0f, 1.0f, std::min(1.0f, std::max(4.0f - fParticleTime, 0.0f)))); + pge->DrawPartialDecal(vScale * vBoom[y * spr.Sprite()->width + x].first * 2.0f, spr.Decal(), olc::vf2d(float(x), float(y)), { 1, 1 }, vScale * 2.0f, olc::PixelF(1.0f, 1.0f, 1.0f, std::min(1.0f, std::max(4.0f - fParticleTime, 0.0f)))); } olc::vi2d vSize = pge->GetTextSizeProp("Copyright OneLoneCoder.com 2024"); diff --git a/olcPixelGameEngine.h b/olcPixelGameEngine.h index f355bf88..f050a4f8 100644 --- a/olcPixelGameEngine.h +++ b/olcPixelGameEngine.h @@ -3,7 +3,7 @@ olcPixelGameEngine.h +-------------------------------------------------------------+ - | OneLoneCoder Pixel Game Engine v2.27 | + | OneLoneCoder Pixel Game Engine v2.28 | | "What do you need? Pixels... Lots of Pixels..." - javidx9 | +-------------------------------------------------------------+ @@ -338,6 +338,7 @@ +Updated olcUTIL_Animate2D.h +Updated olcUTIL_SplashScreen.h +File Resolution for PGEtinker.com + 2.28: Brought olc::v_2d inline with other sources !! Apple Platforms will not see these updates immediately - Sorry, I dont have a mac to test... !! !! Volunteers willing to help appreciated, though PRs are manually integrated with credit !! @@ -417,7 +418,7 @@ int main() #include #pragma endregion -#define PGE_VER 227 +#define PGE_VER 228 // O------------------------------------------------------------------------------O // | COMPILER CONFIGURATION ODDITIES | @@ -585,6 +586,319 @@ int main() #endif #pragma endregion + + +#if !defined(OLC_VECTOR2D_DEFINED) + namespace olc + { + /* + A complete 2D geometric vector structure, with a variety + of useful utility functions and operator overloads + */ + template + struct v_2d + { + static_assert(std::is_arithmetic::value, "olc::v_2d must be numeric"); + + // x-axis component + T x = 0; + // y-axis component + T y = 0; + + // Default constructor + inline constexpr v_2d() = default; + + // Specific constructor + inline constexpr v_2d(T _x, T _y) : x(_x), y(_y) + {} + + // Copy constructor + inline constexpr v_2d(const v_2d& v) = default; + + // Assignment operator + inline constexpr v_2d& operator=(const v_2d& v) = default; + + + // Returns rectangular area of vector + inline constexpr auto area() const + { + return x * y; + } + + // Returns magnitude of vector + inline auto mag() const + { + return std::sqrt(x * x + y * y); + } + + // Returns magnitude squared of vector (useful for fast comparisons) + inline constexpr T mag2() const + { + return x * x + y * y; + } + + // Returns normalised version of vector + inline v_2d norm() const + { + auto r = 1 / mag(); + return v_2d(x * r, y * r); + } + + // Returns vector at 90 degrees to this one + inline constexpr v_2d perp() const + { + return v_2d(-y, x); + } + + // Rounds both components down + inline constexpr v_2d floor() const + { + return v_2d(std::floor(x), std::floor(y)); + } + + // Rounds both components up + inline constexpr v_2d ceil() const + { + return v_2d(std::ceil(x), std::ceil(y)); + } + + // Returns 'element-wise' max of this and another vector + inline constexpr v_2d max(const v_2d& v) const + { + return v_2d(std::max(x, v.x), std::max(y, v.y)); + } + + // Returns 'element-wise' min of this and another vector + inline constexpr v_2d min(const v_2d& v) const + { + return v_2d(std::min(x, v.x), std::min(y, v.y)); + } + + // Calculates scalar dot product between this and another vector + inline constexpr auto dot(const v_2d& rhs) const + { + return this->x * rhs.x + this->y * rhs.y; + } + + // Calculates 'scalar' cross product between this and another vector (useful for winding orders) + inline constexpr auto cross(const v_2d& rhs) const + { + return this->x * rhs.y - this->y * rhs.x; + } + + // Treat this as polar coordinate (R, Theta), return cartesian equivalent (X, Y) + inline constexpr v_2d cart() const + { + return v_2d(std::cos(y) * x, std::sin(y) * x); + } + + // Treat this as cartesian coordinate (X, Y), return polar equivalent (R, Theta) + inline constexpr v_2d polar() const + { + return v_2d(mag(), std::atan2(y, x)); + } + + // Clamp the components of this vector in between the 'element-wise' minimum and maximum of 2 other vectors + inline constexpr v_2d clamp(const v_2d& v1, const v_2d& v2) const + { + return this->max(v1).min(v2); + } + + // Linearly interpolate between this vector, and another vector, given normalised parameter 't' + inline constexpr v_2d lerp(const v_2d& v1, const double t) const + { + return (*this) * (T(1.0 - t)) + (v1 * T(t)); + } + + // Compare if this vector is numerically equal to another + inline constexpr bool operator == (const v_2d& rhs) const + { + return (this->x == rhs.x && this->y == rhs.y); + } + + // Compare if this vector is not numerically equal to another + inline constexpr bool operator != (const v_2d& rhs) const + { + return (this->x != rhs.x || this->y != rhs.y); + } + + // Return this vector as a std::string, of the form "(x,y)" + inline std::string str() const + { + return std::string("(") + std::to_string(this->x) + "," + std::to_string(this->y) + ")"; + } + + // Assuming this vector is incident, given a normal, return the reflection + inline constexpr v_2d reflect(const v_2d& n) const + { + return (*this) - 2.0 * (this->dot(n) * n); + } + + // Allow 'casting' from other v_2d types + template + inline constexpr operator v_2d() const + { + return { static_cast(this->x), static_cast(this->y) }; + } + }; + + // Multiplication operator overloads between vectors and scalars, and vectors and vectors + template + inline constexpr auto operator * (const TL& lhs, const v_2d& rhs) + { + return v_2d(lhs * rhs.x, lhs * rhs.y); + } + + template + inline constexpr auto operator * (const v_2d& lhs, const TR& rhs) + { + return v_2d(lhs.x * rhs, lhs.y * rhs); + } + + template + inline constexpr auto operator * (const v_2d& lhs, const v_2d& rhs) + { + return v_2d(lhs.x * rhs.x, lhs.y * rhs.y); + } + + template + inline constexpr auto operator *= (v_2d& lhs, const TR& rhs) + { + lhs = lhs * rhs; + return lhs; + } + + // Division operator overloads between vectors and scalars, and vectors and vectors + template + inline constexpr auto operator / (const TL& lhs, const v_2d& rhs) + { + return v_2d(lhs / rhs.x, lhs / rhs.y); + } + + template + inline constexpr auto operator / (const v_2d& lhs, const TR& rhs) + { + return v_2d(lhs.x / rhs, lhs.y / rhs); + } + + template + inline constexpr auto operator / (const v_2d& lhs, const v_2d& rhs) + { + return v_2d(lhs.x / rhs.x, lhs.y / rhs.y); + } + + template + inline constexpr auto operator /= (v_2d& lhs, const TR& rhs) + { + lhs = lhs / rhs; + return lhs; + } + + // Unary Addition operator (pointless but i like the platinum trophies) + template + inline constexpr auto operator + (const v_2d& lhs) + { + return v_2d(+lhs.x, +lhs.y); + } + + // Addition operator overloads between vectors and scalars, and vectors and vectors + template + inline constexpr auto operator + (const TL& lhs, const v_2d& rhs) + { + return v_2d(lhs + rhs.x, lhs + rhs.y); + } + + template + inline constexpr auto operator + (const v_2d& lhs, const TR& rhs) + { + return v_2d(lhs.x + rhs, lhs.y + rhs); + } + + template + inline constexpr auto operator + (const v_2d& lhs, const v_2d& rhs) + { + return v_2d(lhs.x + rhs.x, lhs.y + rhs.y); + } + + template + inline constexpr auto operator += (v_2d& lhs, const TR& rhs) + { + lhs = lhs + rhs; + return lhs; + } + + template + inline constexpr auto operator += (v_2d& lhs, const v_2d& rhs) + { + lhs = lhs + rhs; + return lhs; + } + + // Unary negation operator overoad for inverting a vector + template + inline constexpr auto operator - (const v_2d& lhs) + { + return v_2d(-lhs.x, -lhs.y); + } + + // Subtraction operator overloads between vectors and scalars, and vectors and vectors + template + inline constexpr auto operator - (const TL& lhs, const v_2d& rhs) + { + return v_2d(lhs - rhs.x, lhs - rhs.y); + } + + template + inline constexpr auto operator - (const v_2d& lhs, const TR& rhs) + { + return v_2d(lhs.x - rhs, lhs.y - rhs); + } + + template + inline constexpr auto operator - (const v_2d& lhs, const v_2d& rhs) + { + return v_2d(lhs.x - rhs.x, lhs.y - rhs.y); + } + + template + inline constexpr auto operator -= (v_2d& lhs, const TR& rhs) + { + lhs = lhs - rhs; + return lhs; + } + + // Greater/Less-Than Operator overloads - mathematically useless, but handy for "sorted" container storage + template + inline constexpr bool operator < (const v_2d& lhs, const v_2d& rhs) + { + return (lhs.y < rhs.y) || (lhs.y == rhs.y && lhs.x < rhs.x); + } + + template + inline constexpr bool operator > (const v_2d& lhs, const v_2d& rhs) + { + return (lhs.y > rhs.y) || (lhs.y == rhs.y && lhs.x > rhs.x); + } + + // Allow olc::v_2d to play nicely with std::cout + template + inline constexpr std::ostream& operator << (std::ostream& os, const v_2d& rhs) + { + os << rhs.str(); + return os; + } + + // Convenient types ready-to-go + typedef v_2d vi2d; + typedef v_2d vu2d; + typedef v_2d vf2d; + typedef v_2d vd2d; + } +#define OLC_VECTOR2D_DEFINED 1 +#endif + + + // O------------------------------------------------------------------------------O // | olcPixelGameEngine INTERFACE DECLARATION | // O------------------------------------------------------------------------------O @@ -597,7 +911,7 @@ namespace olc // Pixel Game Engine Advanced Configuration constexpr uint8_t nMouseButtons = 5; constexpr uint8_t nDefaultAlpha = 0xFF; - constexpr uint32_t nDefaultPixel = (nDefaultAlpha << 24); + constexpr uint32_t nDefaultPixel = uint32_t(nDefaultAlpha << 24); constexpr uint8_t nTabSizeInSpaces = 4; constexpr size_t OLC_MAX_VERTS = 128; enum rcode { FAIL = 0, OK = 1, NO_FILE = -1 }; @@ -688,92 +1002,6 @@ namespace olc }; - - - // O------------------------------------------------------------------------------O - // | olc::vX2d - A generic 2D vector type | - // O------------------------------------------------------------------------------O -#if !defined(OLC_VECTOR2D_DEFINED) - template - struct v2d_generic - { - T x = 0; - T y = 0; - v2d_generic() : x(0), y(0) {} - v2d_generic(T _x, T _y) : x(_x), y(_y) {} - v2d_generic(const v2d_generic& v) : x(v.x), y(v.y) {} - v2d_generic& operator=(const v2d_generic& v) = default; - T mag() const { return T(std::sqrt(x * x + y * y)); } - T mag2() const { return x * x + y * y; } - v2d_generic norm() const { T r = 1 / mag(); return v2d_generic(x * r, y * r); } - v2d_generic perp() const { return v2d_generic(-y, x); } - v2d_generic floor() const { return v2d_generic(std::floor(x), std::floor(y)); } - v2d_generic ceil() const { return v2d_generic(std::ceil(x), std::ceil(y)); } - v2d_generic max(const v2d_generic& v) const { return v2d_generic(std::max(x, v.x), std::max(y, v.y)); } - v2d_generic min(const v2d_generic& v) const { return v2d_generic(std::min(x, v.x), std::min(y, v.y)); } - v2d_generic cart() { return { std::cos(y) * x, std::sin(y) * x }; } - v2d_generic polar() { return { mag(), std::atan2(y, x) }; } - v2d_generic clamp(const v2d_generic& v1, const v2d_generic& v2) const { return this->max(v1).min(v2); } - v2d_generic lerp(const v2d_generic& v1, const double t) { return this->operator*(T(1.0 - t)) + (v1 * T(t)); } - T dot(const v2d_generic& rhs) const { return this->x * rhs.x + this->y * rhs.y; } - T cross(const v2d_generic& rhs) const { return this->x * rhs.y - this->y * rhs.x; } - v2d_generic operator + (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y); } - v2d_generic operator - (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y); } - v2d_generic operator * (const T& rhs) const { return v2d_generic(this->x * rhs, this->y * rhs); } - v2d_generic operator * (const v2d_generic& rhs) const { return v2d_generic(this->x * rhs.x, this->y * rhs.y); } - v2d_generic operator / (const T& rhs) const { return v2d_generic(this->x / rhs, this->y / rhs); } - v2d_generic operator / (const v2d_generic& rhs) const { return v2d_generic(this->x / rhs.x, this->y / rhs.y); } - v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this; } - v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this; } - v2d_generic& operator *= (const T& rhs) { this->x *= rhs; this->y *= rhs; return *this; } - v2d_generic& operator /= (const T& rhs) { this->x /= rhs; this->y /= rhs; return *this; } - v2d_generic& operator *= (const v2d_generic& rhs) { this->x *= rhs.x; this->y *= rhs.y; return *this; } - v2d_generic& operator /= (const v2d_generic& rhs) { this->x /= rhs.x; this->y /= rhs.y; return *this; } - v2d_generic operator + () const { return { +x, +y }; } - v2d_generic operator - () const { return { -x, -y }; } - bool operator == (const v2d_generic& rhs) const { return (this->x == rhs.x && this->y == rhs.y); } - bool operator != (const v2d_generic& rhs) const { return (this->x != rhs.x || this->y != rhs.y); } - const std::string str() const { return std::string("(") + std::to_string(this->x) + "," + std::to_string(this->y) + ")"; } - friend std::ostream& operator << (std::ostream& os, const v2d_generic& rhs) { os << rhs.str(); return os; } - operator v2d_generic() const { return { static_cast(this->x), static_cast(this->y) }; } - operator v2d_generic() const { return { static_cast(this->x), static_cast(this->y) }; } - operator v2d_generic() const { return { static_cast(this->x), static_cast(this->y) }; } - }; - - // Note: joshinils has some good suggestions here, but they are complicated to implement at this moment, - // however they will appear in a future version of PGE - template inline v2d_generic operator * (const float& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs * (float)rhs.x), (T)(lhs * (float)rhs.y)); } - template inline v2d_generic operator * (const double& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs * (double)rhs.x), (T)(lhs * (double)rhs.y)); } - template inline v2d_generic operator * (const int& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs * (int)rhs.x), (T)(lhs * (int)rhs.y)); } - template inline v2d_generic operator / (const float& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs / (float)rhs.x), (T)(lhs / (float)rhs.y)); } - template inline v2d_generic operator / (const double& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs / (double)rhs.x), (T)(lhs / (double)rhs.y)); } - template inline v2d_generic operator / (const int& lhs, const v2d_generic& rhs) - { return v2d_generic((T)(lhs / (int)rhs.x), (T)(lhs / (int)rhs.y)); } - - // To stop dandistine crying... - template inline bool operator < (const v2d_generic& lhs, const v2d_generic& rhs) - { return lhs.y < rhs.y || (lhs.y == rhs.y && lhs.x < rhs.x); } - template inline bool operator > (const v2d_generic& lhs, const v2d_generic& rhs) - { return lhs.y > rhs.y || (lhs.y == rhs.y && lhs.x > rhs.x); } - - typedef v2d_generic vi2d; - typedef v2d_generic vu2d; - typedef v2d_generic vf2d; - typedef v2d_generic vd2d; - -#define OLC_VECTOR2D_DEFINED 1 -#endif - - - - - - // O------------------------------------------------------------------------------O // | olc::ResourcePack - A virtual scrambled filesystem to pack your assets into | // O------------------------------------------------------------------------------O @@ -2780,7 +3008,7 @@ namespace olc if (structure == olc::DecalStructure::LIST) { - for (int tri = 0; tri < vPoints.size() / 3; tri++) + for (size_t tri = 0; tri < vPoints.size() / 3; tri++) { std::vector vP = { vPoints[tri * 3 + 0], vPoints[tri * 3 + 1], vPoints[tri * 3 + 2] }; std::vector vT = { vTex[tri * 3 + 0], vTex[tri * 3 + 1], vTex[tri * 3 + 2] }; @@ -2792,7 +3020,7 @@ namespace olc if (structure == olc::DecalStructure::STRIP) { - for (int tri = 2; tri < vPoints.size(); tri++) + for (size_t tri = 2; tri < vPoints.size(); tri++) { std::vector vP = { vPoints[tri - 2], vPoints[tri-1], vPoints[tri] }; std::vector vT = { vTex[tri - 2], vTex[tri - 1], vTex[tri] }; @@ -2804,7 +3032,7 @@ namespace olc if (structure == olc::DecalStructure::FAN) { - for (int tri = 2; tri < vPoints.size(); tri++) + for (size_t tri = 2; tri < vPoints.size(); tri++) { std::vector vP = { vPoints[0], vPoints[tri - 1], vPoints[tri] }; std::vector vT = { vTex[0], vTex[tri - 1], vTex[tri] }; @@ -3688,7 +3916,7 @@ namespace olc if (vConsoleCursor.y >= vConsoleSize.y) { vConsoleCursor.y = vConsoleSize.y - 1; - for (size_t i = 1; i < vConsoleSize.y; i++) + for (int i = 1; i < vConsoleSize.y; i++) sConsoleLines[i - 1] = sConsoleLines[i]; sConsoleLines[vConsoleCursor.y].clear(); } @@ -3766,7 +3994,7 @@ namespace olc sTextEntryString.erase(nTextEntryCursor-1, 1); nTextEntryCursor = std::max(0, nTextEntryCursor - 1); } - if (GetKey(olc::Key::DEL).bPressed && nTextEntryCursor < sTextEntryString.size()) + if (GetKey(olc::Key::DEL).bPressed && size_t(nTextEntryCursor) < sTextEntryString.size()) sTextEntryString.erase(nTextEntryCursor, 1); if (GetKey(olc::Key::UP).bPressed)