1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

View File

@ -1,15 +1,15 @@
// ------------------------------------------------------------------------------------------------
#include "Base/AABB.hpp"
#include "Base/Sphere.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("AABB"))
SQMOD_DECL_TYPENAME(Typename, _SC("AABB"))
// ------------------------------------------------------------------------------------------------
const AABB AABB::NIL = AABB(0, 0);
@ -19,13 +19,6 @@ const AABB AABB::MAX = AABB(HUGE_VALF, -HUGE_VALF);
// ------------------------------------------------------------------------------------------------
SQChar AABB::Delim = ',';
// ------------------------------------------------------------------------------------------------
AABB::AABB() noexcept
: min(HUGE_VALF), max(-HUGE_VALF)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(Value mins, Value maxs) noexcept
: min(mins), max(maxs)
@ -285,7 +278,7 @@ bool AABB::operator >= (const AABB & b) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::Cmp(const AABB & o) const
int32_t AABB::Cmp(const AABB & o) const
{
if (*this == o)
{
@ -302,9 +295,9 @@ Int32 AABB::Cmp(const AABB & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr AABB::ToString() const
String AABB::ToString() const
{
return ToStrF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
return fmt::format("{},{},{},{},{},{}", min.x, min.y, min.z, max.x, max.y, max.z);
}
// ------------------------------------------------------------------------------------------------
@ -531,7 +524,7 @@ AABB::Value AABB::Area() const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsVector3Inside(const Vector3 & point) const
int32_t AABB::IsVector3Inside(const Vector3 & point) const
{
return (point.x < min.x || point.x > max.x ||
point.y < min.y || point.y > max.y ||
@ -539,7 +532,7 @@ Int32 AABB::IsVector3Inside(const Vector3 & point) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsVector3InsideEx(Value x, Value y, Value z) const
int32_t AABB::IsVector3InsideEx(Value x, Value y, Value z) const
{
return (x < min.x || x > max.x ||
y < min.y || y > max.y ||
@ -547,7 +540,7 @@ Int32 AABB::IsVector3InsideEx(Value x, Value y, Value z) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInside(const AABB & box) const
int32_t AABB::IsAABBInside(const AABB & box) const
{
if (box.max.x < min.x || box.min.x > max.x ||
box.max.y < min.y || box.min.y > max.y ||
@ -568,7 +561,7 @@ Int32 AABB::IsAABBInside(const AABB & box) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
int32_t AABB::IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
{
if (xmax < min.x || xmin > max.x ||
ymax < min.y || ymin > max.y ||
@ -589,7 +582,7 @@ Int32 AABB::IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideFast(const AABB & box) const
int32_t AABB::IsAABBInsideFast(const AABB & box) const
{
if (box.max.x < min.x || box.min.x > max.x ||
box.max.y < min.y || box.min.y > max.y ||
@ -604,7 +597,7 @@ Int32 AABB::IsAABBInsideFast(const AABB & box) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
int32_t AABB::IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
{
if (xmax < min.x || xmin > max.x ||
ymax < min.y || ymin > max.y ||
@ -619,7 +612,7 @@ Int32 AABB::IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, V
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInside(const Sphere & sphere) const
int32_t AABB::IsSphereInside(const Sphere & sphere) const
{
Value dist_squared = 0, temp;
const Vector3 & center = sphere.pos;
@ -673,14 +666,14 @@ Int32 AABB::IsSphereInside(const Sphere & sphere) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideEx(Value x, Value y, Value z, Value r) const
int32_t AABB::IsSphereInsideEx(Value x, Value y, Value z, Value r) const
{
return IsSphereInside(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideFast(const Sphere & sphere) const
int32_t AABB::IsSphereInsideFast(const Sphere & sphere) const
{
Value dist_squared = 0, temp;
const Vector3& center = sphere.pos;
@ -729,36 +722,22 @@ Int32 AABB::IsSphereInsideFast(const Sphere & sphere) const
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const
int32_t AABB::IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const
{
return IsSphereInsideFast(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
LightObj AABB::Format(const String & spec, StackStrF & fmt) const
String AABB::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 6, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), min.x, min.y, min.z, max.x, max.y, max.z);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("min_x", min.x)
, fmt::arg("min_y", min.y)
, fmt::arg("min_z", min.z)
, fmt::arg("max_x", max.x)
, fmt::arg("max_y", max.y)
, fmt::arg("max_z", max.z)
);
}
// ------------------------------------------------------------------------------------------------
@ -865,39 +844,6 @@ void Register_AABB(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< AABB >)
.StaticFmtFunc(_SC("FromStr"), &AABB::Get)
.StaticFmtFunc(_SC("FromStrEx"), &AABB::GetEx)
// Operator Exposure
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opAddAssign"), &AABB::operator +=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opSubAssign"), &AABB::operator -=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opMulAssign"), &AABB::operator *=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opDivAssign"), &AABB::operator /=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opModAssign"), &AABB::operator %=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opAddAssignS"), &AABB::operator +=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opSubAssignS"), &AABB::operator -=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opMulAssignS"), &AABB::operator *=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opDivAssignS"), &AABB::operator /=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opModAssignS"), &AABB::operator %=)
.Func< AABB & (AABB::*)(void) >(_SC("opPreInc"), &AABB::operator ++)
.Func< AABB & (AABB::*)(void) >(_SC("opPreDec"), &AABB::operator --)
.Func< AABB (AABB::*)(int) >(_SC("opPostInc"), &AABB::operator ++)
.Func< AABB (AABB::*)(int) >(_SC("opPostDec"), &AABB::operator --)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opAdd"), &AABB::operator +)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opSub"), &AABB::operator -)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opMul"), &AABB::operator *)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opDiv"), &AABB::operator /)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opMod"), &AABB::operator %)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opAddS"), &AABB::operator +)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opSubS"), &AABB::operator -)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opMulS"), &AABB::operator *)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opDivS"), &AABB::operator /)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opModS"), &AABB::operator %)
.Func< AABB (AABB::*)(void) const >(_SC("opUnPlus"), &AABB::operator +)
.Func< AABB (AABB::*)(void) const >(_SC("opUnMinus"), &AABB::operator -)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opEqual"), &AABB::operator ==)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opNotEqual"), &AABB::operator !=)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opLessThan"), &AABB::operator <)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opGreaterThan"), &AABB::operator >)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opLessEqual"), &AABB::operator <=)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opGreaterEqual"), &AABB::operator >=)
);
}

View File

@ -3,6 +3,9 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector3.hpp"
// ------------------------------------------------------------------------------------------------
#include <cmath>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -31,12 +34,12 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* The minimum and maximum components of this type.
*/
Vector3 min, max;
Vector3 min{HUGE_VALF}, max{-HUGE_VALF};
/* --------------------------------------------------------------------------------------------
* Construct with zero size.
*/
AABB() noexcept;
AABB() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a an equally sized and perfectly shaped box from scalar values.
@ -254,12 +257,12 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const AABB & b) const;
SQMOD_NODISCARD int32_t Cmp(const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(AABB(s, s, s, s, s, s));
}
@ -267,7 +270,7 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
const auto v = static_cast< Value >(s);
return Cmp(AABB(v, v, v, v, v, v));
@ -276,7 +279,7 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
const auto v = static_cast< Value >(s);
return Cmp(AABB(v, v, v, v, v, v));
@ -285,7 +288,7 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
const auto v = static_cast< Value >(0);
return Cmp(AABB(v, v, v, v, v, v));
@ -294,7 +297,7 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
@ -379,97 +382,97 @@ struct AABB
/* --------------------------------------------------------------------------------------------
* Check if the box is empty. This means that there is no space between the min and max edge.
*/
bool Empty() const;
SQMOD_NODISCARD bool Empty() const;
/* --------------------------------------------------------------------------------------------
* Return true if this bounding box is defined via a previous call to Define() or Merge().
*/
bool Defined() const;
SQMOD_NODISCARD bool Defined() const;
/* --------------------------------------------------------------------------------------------
* Return center.
*/
Vector3 Center() const;
SQMOD_NODISCARD Vector3 Center() const;
/* --------------------------------------------------------------------------------------------
* Get size/extent of the box (maximal distance of two points in the box).
*/
Vector3 Size() const;
SQMOD_NODISCARD Vector3 Size() const;
/* --------------------------------------------------------------------------------------------
* Return half-size.
*/
Vector3 HalfSize() const;
SQMOD_NODISCARD Vector3 HalfSize() const;
/* --------------------------------------------------------------------------------------------
* Get radius of the bounding sphere.
*/
Value Radius() const;
SQMOD_NODISCARD Value Radius() const;
/* --------------------------------------------------------------------------------------------
* Get the volume enclosed by the box in cubed units.
*/
Value Volume() const;
SQMOD_NODISCARD Value Volume() const;
/* --------------------------------------------------------------------------------------------
* Get the surface area of the box in squared units.
*/
Value Area() const;
SQMOD_NODISCARD Value Area() const;
/* --------------------------------------------------------------------------------------------
* Test if a point is inside.
*/
Int32 IsVector3Inside(const Vector3 & point) const;
SQMOD_NODISCARD int32_t IsVector3Inside(const Vector3 & point) const;
/* --------------------------------------------------------------------------------------------
* Test if a point is inside.
*/
Int32 IsVector3InsideEx(Value x, Value y, Value z) const;
SQMOD_NODISCARD int32_t IsVector3InsideEx(Value x, Value y, Value z) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is inside, outside or intersects.
*/
Int32 IsAABBInside(const AABB & box) const;
SQMOD_NODISCARD int32_t IsAABBInside(const AABB & box) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is inside, outside or intersects.
*/
Int32 IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
SQMOD_NODISCARD int32_t IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is (partially) inside or outside.
*/
Int32 IsAABBInsideFast(const AABB & box) const;
SQMOD_NODISCARD int32_t IsAABBInsideFast(const AABB & box) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is (partially) inside or outside.
*/
Int32 IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
SQMOD_NODISCARD int32_t IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is inside, outside or intersects.
*/
Int32 IsSphereInside(const Sphere & sphere) const;
SQMOD_NODISCARD int32_t IsSphereInside(const Sphere & sphere) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is inside, outside or intersects.
*/
Int32 IsSphereInsideEx(Value x, Value y, Value z, Value r) const;
SQMOD_NODISCARD int32_t IsSphereInsideEx(Value x, Value y, Value z, Value r) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is (partially) inside or outside.
*/
Int32 IsSphereInsideFast(const Sphere & sphere) const;
SQMOD_NODISCARD int32_t IsSphereInsideFast(const Sphere & sphere) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is (partially) inside or outside.
*/
Int32 IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const;
SQMOD_NODISCARD int32_t IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.

View File

@ -1,251 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
#include "sqrat/sqratUtil.h"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <stdexcept>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/
inline unsigned int NextPow2(unsigned int num)
{
--num;
num |= num >> 1u;
num |= num >> 2u;
num |= num >> 4u;
num |= num >> 8u;
num |= num >> 16u;
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Throw an memory exception.
*/
void ThrowMemExcept(const char * msg, ...)
{
// Exception messages should be concise
SQChar buffer[256];
// Variable arguments structure
va_list args;
// Get the specified arguments
va_start(args, msg);
// Run the specified format
int ret = std::vsnprintf(buffer, sizeof(buffer), msg, args);
// Check for formatting errors
if (ret < 0)
{
throw Sqrat::Exception(_SC("Unknown memory error")); // NOLINT(hicpp-exception-baseclass,cert-err60-cpp)
}
// Throw the actual exception
throw Sqrat::Exception(buffer); // NOLINT(hicpp-exception-baseclass,cert-err60-cpp)
}
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(const Buffer & o)
: m_Ptr(nullptr)
, m_Cap(o.m_Cap)
, m_Cur(o.m_Cur)
{
if (m_Cap)
{
Request(o.m_Cap);
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
}
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
// ------------------------------------------------------------------------------------------------
Buffer & Buffer::operator = (const Buffer & o) // NOLINT(cert-oop54-cpp)
{
if (m_Ptr != o.m_Ptr)
{
// Can we work in the current buffer?
if (m_Cap && o.m_Cap <= m_Cap)
{
// It's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Do we even have data to copy?
else if (!o.m_Cap)
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
else
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
// Request a larger buffer
Request(o.m_Cap);
// Now it's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Also copy the edit cursor
m_Cur = o.m_Cur;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Grow(SzType n)
{
// Backup the current memory
Buffer bkp(m_Ptr, m_Cap, m_Cur);
// Acquire a bigger buffer
Request(bkp.m_Cap + n);
// Copy the data from the old buffer
std::memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Copy the previous edit cursor
m_Cur = bkp.m_Cur;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Request(SzType n)
{
// NOTE: Function assumes (n > 0)
assert(n > 0);
// Round up the size to a power of two number
n = (n & (n - 1)) ? NextPow2(n) : n;
// Release previous memory if any
delete[] m_Ptr; // Implicitly handles null!
// Attempt to allocate memory
m_Ptr = new Value[n];
// If no errors occurred then we can set the size
m_Cap = n;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Release()
{
// Deallocate the memory
delete[] m_Ptr; // Implicitly handles null!
// Explicitly reset the buffer
m_Ptr = nullptr;
m_Cap = 0;
m_Cur = 0;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
{
// Do we have what to write?
if (!data || !size)
{
return 0;
}
// See if the buffer size must be adjusted
else if ((pos + size) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + size) - m_Cap + 32);
}
// Copy the data into the internal buffer
std::memcpy(m_Ptr + pos, data, size);
// Return the amount of data written to the buffer
return size;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Call the function that takes the variable argument list
const SzType ret = WriteF(pos, fmt, args);
// Finalize the variable argument list
va_end(args);
// Return the result
return ret;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
{
// Is the specified position within range?
if (pos >= m_Cap)
{
// Acquire a larger buffer
Grow(pos - m_Cap + 32);
}
// Backup the variable argument list
va_list args_cpy;
va_copy(args_cpy, args);
// Attempt to write to the current buffer
// (if empty, it should tell us the necessary size)
int ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + ret) - m_Cap + 32);
// Retry writing the requested information
ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
}
// Return the value 0 if data could not be written
if (ret < 0)
{
return 0;
}
// Return the number of written characters
return static_cast< SzType >(ret);
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
{
// Is there any string to write?
if (str && *str != '\0')
{
// Forward this to the regular write function
return Write(pos, str, std::strlen(str));
}
// Nothing to write
return 0;
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendF(const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Forward this to the regular write function
m_Cur += WriteF(m_Cur, fmt, args);
// Finalize the variable argument list
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendS(const char * str)
{
// Is there any string to write?
if (str)
{
m_Cur += Write(m_Cur, str, std::strlen(str));
}
}
} // Namespace:: SqMod

View File

@ -1,851 +0,0 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include <cmath>
#include <cassert>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
void ThrowMemExcept(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Reusable and re-scalable buffer memory for quick memory allocations.
*/
class Buffer
{
public:
// --------------------------------------------------------------------------------------------
typedef char Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Value & Reference; // A reference to the stored value type.
typedef const Value & ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Value * Pointer; // A pointer to the stored value type.
typedef const Value * ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef unsigned int SzType; // The type used to represent size in general.
// --------------------------------------------------------------------------------------------
static_assert(sizeof(Value) == 1, "Value type must be 1 byte");
private:
/* --------------------------------------------------------------------------------------------
* Construct and take ownership of the specified buffer.
*/
Buffer(Pointer & ptr, SzType & cap, SzType & cur)
: m_Ptr(ptr)
, m_Cap(cap)
, m_Cur(cur)
{
ptr = nullptr;
cap = 0;
cur = 0;
}
/* --------------------------------------------------------------------------------------------
* Round a size value.
*/
template < typename T > static inline SzType RoundSz(T n)
{
return static_cast< SzType >(floor(n));
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor. (null)
*/
Buffer()
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Explicit size constructor.
*/
explicit Buffer(SzType size)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
{
Request(size < 8 ? 8 : size);
}
/* --------------------------------------------------------------------------------------------
* Explicit size and cursor position constructor.
*/
Buffer(SzType size, SzType pos)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
{
Request(size < 8 ? 8 : size);
Move(pos);
}
/* --------------------------------------------------------------------------------------------
* Explicit size and buffer constructor.
*/
Buffer(ConstPtr data, SzType size)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
{
Request(size < 8 ? 8 : size);
m_Cur += Write(m_Cur, data, size);
}
/* --------------------------------------------------------------------------------------------
* Explicit size, data and cursor position constructor.
*/
Buffer(ConstPtr data, SzType size, SzType pos)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Cur(0)
{
Request(size < 8 ? 8 : size);
Write(m_Cur, data, size);
Move(pos);
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Buffer(const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Buffer(Buffer && o) noexcept
: m_Ptr(o.m_Ptr)
, m_Cap(o.m_Cap)
, m_Cur(o.m_Cur)
{
o.m_Ptr = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Buffer();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (Buffer && o) noexcept
{
if (m_Ptr != o.m_Ptr)
{
if (m_Ptr)
{
Release();
}
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
m_Cur = o.m_Cur;
o.m_Ptr = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Buffer & o) const
{
return (m_Cap == o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Buffer & o) const
{
return (m_Cap != o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Buffer & o) const
{
return (m_Cap < o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Buffer & o) const
{
return (m_Cap > o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Buffer & o) const
{
return (m_Cap <= o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Buffer & o) const
{
return (m_Cap >= o.m_Cap);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean.
*/
explicit operator bool () const // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
{
return (m_Ptr != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Negation operator.
*/
bool operator ! () const
{
return (!m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer.
*/
Pointer Data()
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer.
*/
ConstPtr Data() const
{
return m_Ptr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * Get()
{
return reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * Get() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
template < typename T = Value > T & At(SzType n)
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept("Element of size (%d) at index (%u) is out of buffer capacity (%u)",
sizeof(T), n, m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + n);
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
template < typename T = Value > const T & At(SzType n) const
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Make sure that the specified element is withing buffer range
else if (n > (m_Cap - sizeof(T)))
{
ThrowMemExcept("Element of size (%d) at index (%u) is out of buffer capacity (%u)",
sizeof(T), n, m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + n);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * Begin()
{
return reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * Begin() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > T * End()
{
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T = Value > const T * End() const
{
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the front of the buffer.
*/
template < typename T = Value > T & Front()
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the front of the buffer.
*/
template < typename T = Value > const T & Front() const
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the first element in the buffer.
*/
template < typename T = Value > T & Next()
{
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the first element in the buffer.
*/
template < typename T = Value > const T & Next() const
{
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the back of the buffer.
*/
template < typename T = Value > T & Back()
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cap - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the back of the buffer.
*/
template < typename T = Value > const T & Back() const
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cap - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the last element in the buffer.
*/
template < typename T = Value > T & Prev()
{
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cap - (sizeof(T) * 2)));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the last element in the buffer.
*/
template < typename T = Value > const T & Prev() const
{
// Make sure that the buffer can host at least two elements of this type
if (m_Cap < (sizeof(T) * 2))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host two elements of size (%u)",
m_Cap, sizeof(T));
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cap - (sizeof(T) * 2)));
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements ahead.
*/
template < typename T = Value > void Advance(SzType n)
{
// Do we need to scale the buffer?
if ((m_Cur + (n * sizeof(T))) > m_Cap)
{
Grow(m_Cur + (n * sizeof(T)));
}
// Advance to the specified position
m_Cur += (n * sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to the specified number of elements behind.
*/
template < typename T = Value > void Retreat(SzType n)
{
// Can we move that much backward?
if ((n * sizeof(T)) <= m_Cur)
{
m_Cur -= (n * sizeof(T));
}
// Just got to the beginning
else
{
m_Cur = 0;
}
}
/* --------------------------------------------------------------------------------------------
* Reposition the edit cursor to a fixed position within the buffer.
*/
template < typename T = Value > void Move(SzType n)
{
// Do we need to scale the buffer?
if ((n * sizeof(T)) > m_Cap)
{
Grow(n * sizeof(T));
}
// Move to the specified position
m_Cur = (n * sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Append a value to the current cursor location and advance the cursor.
*/
template < typename T = Value > void Push(T v)
{
// Do we need to scale the buffer?
if ((m_Cur + sizeof(T)) > m_Cap)
{
Grow(m_Cap + sizeof(T));
}
// Assign the specified value
*reinterpret_cast< T * >(m_Ptr + m_Cur) = v;
// Move to the next element
m_Cur += sizeof(T);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the cursor position.
*/
template < typename T = Value > T & Cursor()
{
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element at the cursor position.
*/
template < typename T = Value > const T & Cursor() const
{
// Make sure that at least one element of this type exists after the cursor
if ((m_Cur + sizeof(T)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur, m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the cursor position.
*/
template < typename T = Value > T & Before()
{
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept("Cannot read an element of size (%u) before the cursor at (%u)",
sizeof(T), m_Cur);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + (m_Cur - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element before the cursor position.
*/
template < typename T = Value > const T & Before() const
{
// The cursor must have at least one element of this type behind
if (m_Cur < sizeof(T))
{
ThrowMemExcept("Cannot read an element of size (%u) before the cursor at (%u)",
sizeof(T), m_Cur);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + (m_Cur - sizeof(T)));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the cursor position.
*/
template < typename T = Value > T & After()
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
return *reinterpret_cast< T * >(m_Ptr + m_Cur + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the element after the cursor position.
*/
template < typename T = Value > const T & After() const
{
// Make sure that the buffer can host at least one element of this type
if (m_Cap < sizeof(T))
{
ThrowMemExcept("Buffer capacity of (%u) is unable to host an element of size (%u)",
m_Cap, sizeof(T));
}
// There must be buffer left for at least two elements of this type after the cursor
else if ((m_Cur + (sizeof(T) * 2)) > m_Cap)
{
ThrowMemExcept("Element of size (%u) starting at (%u) exceeds buffer capacity (%u)",
sizeof(T), m_Cur + sizeof(T), m_Cap);
}
// Return the requested element
return *reinterpret_cast< const T * >(m_Ptr + m_Cur + sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve maximum elements it can hold for a certain type.
*/
template < typename T = Value > static SzType Max()
{
return static_cast< SzType >(0xFFFFFFFF / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in element count.
*/
template < typename T = Value > SzType Size() const
{
return static_cast< SzType >(m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in byte count.
*/
SzType Capacity() const
{
return m_Cap;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in byte count.
*/
template < typename T = Value > SzType CapacityAs() const
{
return static_cast< SzType >(m_Cap / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
SzType Position() const
{
return m_Cur;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the current position of the cursor in the buffer.
*/
template < typename T = Value > SzType PositionAs() const
{
return static_cast< SzType >(m_Cur / sizeof(T));
}
/* --------------------------------------------------------------------------------------------
* Retrieve the amount of unused buffer after the edit cursor.
*/
SzType Remaining() const
{
return m_Cap - m_Cur;
}
/* --------------------------------------------------------------------------------------------
* Grow the size of the internal buffer by the specified amount of bytes.
*/
void Grow(SzType n);
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
template < typename T = Value > Buffer Adjust(SzType n)
{
// Do we meet the minimum size?
if (n < 8)
{
n = 8; // Adjust to minimum size
}
// See if the requested capacity doesn't exceed the limit
if (n > Max< T >())
{
ThrowMemExcept("Requested buffer of (%u) elements exceeds the (%u) limit", n, Max< T >());
}
// Is there an existing buffer?
else if (n && !m_Cap)
{
Request(n * sizeof(T)); // Request the memory
}
// Should the size be increased?
else if (n > m_Cap)
{
// Backup the current memory
Buffer bkp(m_Ptr, m_Cap, m_Cur);
// Request the memory
Request(n * sizeof(T));
// Return the backup
return bkp;
}
// Return an empty buffer
return Buffer();
}
/* --------------------------------------------------------------------------------------------
* Release the managed memory.
*/
void Reset()
{
if (m_Ptr)
{
Release();
}
}
/* --------------------------------------------------------------------------------------------
* Release the managed memory.
*/
void ResetAll()
{
if (m_Ptr)
{
Release();
}
}
/* --------------------------------------------------------------------------------------------
* Swap the contents of two buffers.
*/
void Swap(Buffer & o)
{
Pointer p = m_Ptr;
SzType n = m_Cap;
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
o.m_Ptr = p;
o.m_Cap = n;
}
/* --------------------------------------------------------------------------------------------
* Write a portion of a buffer to the internal buffer.
*/
SzType Write(SzType pos, ConstPtr data, SzType size);
/* --------------------------------------------------------------------------------------------
* Write another buffer to the internal buffer.
*/
SzType Write(SzType pos, const Buffer & b)
{
return Write(pos, b.m_Ptr, b.m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Write a formatted string to the internal buffer.
*/
SzType WriteF(SzType pos, const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* Write a formatted string to the internal buffer.
*/
SzType WriteF(SzType pos, const char * fmt, va_list args);
/* --------------------------------------------------------------------------------------------
* Write a string to the internal buffer.
*/
SzType WriteS(SzType pos, const char * str);
/* --------------------------------------------------------------------------------------------
* Write a portion of a string to the internal buffer.
*/
SzType WriteS(SzType pos, const char * str, SzType size)
{
return Write(pos, str, size);
}
/* --------------------------------------------------------------------------------------------
* Append a portion of a buffer to the internal buffer.
*/
void Append(ConstPtr data, SzType size)
{
m_Cur += Write(m_Cur, data, size);
}
/* --------------------------------------------------------------------------------------------
* Append another buffer to the internal buffer.
*/
void Append(const Buffer & b)
{
m_Cur += Write(m_Cur, b.m_Ptr, b.m_Cur);
}
/* --------------------------------------------------------------------------------------------
* Append a formatted string to the internal buffer.
*/
void AppendF(const char * fmt, ...);
/* --------------------------------------------------------------------------------------------
* Append a formatted string to the internal buffer.
*/
void AppendF(const char * fmt, va_list args)
{
m_Cur += WriteF(m_Cur, fmt, args);
}
/* --------------------------------------------------------------------------------------------
* Append a string to the internal buffer.
*/
void AppendS(const char * str);
/* --------------------------------------------------------------------------------------------
* Append a portion of a string to the internal buffer.
*/
void AppendS(const char * str, SzType size)
{
m_Cur += Write(m_Cur, str, size);
}
protected:
/* --------------------------------------------------------------------------------------------
* Request the memory specified in the capacity.
*/
void Request(SzType n);
/* --------------------------------------------------------------------------------------------
* Release the managed memory buffer.
*/
void Release();
private:
// --------------------------------------------------------------------------------------------
Pointer m_Ptr; /* Pointer to the memory buffer. */
SzType m_Cap; /* The total size of the buffer. */
SzType m_Cur; /* The buffer edit cursor. */
};
} // Namespace:: SqMod

View File

@ -1,18 +1,15 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Circle.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Circle"))
SQMOD_DECL_TYPENAME(Typename, _SC("Circle"))
// ------------------------------------------------------------------------------------------------
const Circle Circle::NIL = Circle();
@ -22,13 +19,6 @@ const Circle Circle::MAX = Circle(std::numeric_limits< Circle::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Circle::Delim = ',';
// ------------------------------------------------------------------------------------------------
Circle::Circle() noexcept
: pos(0.0, 0.0), rad(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(Value rv) noexcept
: pos(0.0, 0.0), rad(rv)
@ -100,7 +90,7 @@ Circle & Circle::operator /= (const Circle & c)
Circle & Circle::operator %= (const Circle & c)
{
pos %= c.pos;
rad = std::fmod(rad, c.rad);
rad = fmodf(rad, c.rad);
return *this;
}
@ -136,7 +126,7 @@ Circle & Circle::operator /= (Value r)
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator %= (Value r)
{
rad = std::fmod(rad, r);
rad = fmodf(rad, r);
return *this;
}
@ -236,7 +226,7 @@ Circle Circle::operator / (const Circle & c) const
// ------------------------------------------------------------------------------------------------
Circle Circle::operator % (const Circle & c) const
{
return {pos % c.pos, std::fmod(rad, c.rad)};
return {pos % c.pos, fmodf(rad, c.rad)};
}
// ------------------------------------------------------------------------------------------------
@ -266,7 +256,7 @@ Circle Circle::operator / (Value r) const
// ------------------------------------------------------------------------------------------------
Circle Circle::operator % (Value r) const
{
return {std::fmod(rad, r)};
return {fmodf(rad, r)};
}
// ------------------------------------------------------------------------------------------------
@ -302,7 +292,7 @@ Circle Circle::operator % (const Vector2 & p) const
// ------------------------------------------------------------------------------------------------
Circle Circle::operator + () const
{
return {pos.Abs(), std::fabs(rad)};
return {pos.Abs(), fabsf(rad)};
}
// ------------------------------------------------------------------------------------------------
@ -348,7 +338,7 @@ bool Circle::operator >= (const Circle & c) const
}
// ------------------------------------------------------------------------------------------------
Int32 Circle::Cmp(const Circle & o) const
int32_t Circle::Cmp(const Circle & o) const
{
if (*this == o)
{
@ -365,9 +355,9 @@ Int32 Circle::Cmp(const Circle & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Circle::ToString() const
String Circle::ToString() const
{
return ToStrF("%f,%f,%f", pos.x, pos.y, rad);
return fmt::format("{},{},{}", pos.x, pos.y, rad);
}
// ------------------------------------------------------------------------------------------------
@ -465,7 +455,7 @@ void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin
// ------------------------------------------------------------------------------------------------
Circle Circle::Abs() const
{
return {pos.Abs(), std::fabs(rad)};
return {pos.Abs(), fabsf(rad)};
}
// ------------------------------------------------------------------------------------------------
@ -483,9 +473,9 @@ Array Circle::ToPointsArray(SQInteger num_segments) const
SQFloat theta = 2.0f * SQMOD_PI * static_cast< SQFloat >(i) / static_cast< SQFloat >(num_segments);
#endif // SQUSEDOUBLE
// Calculate the x component
SQFloat x = (rad * std::cos(theta)) + pos.x;
SQFloat x = (rad * cosf(theta)) + pos.x;
// Calculate the y component
SQFloat y = (rad * std::sin(theta)) + pos.y;
SQFloat y = (rad * sinf(theta)) + pos.y;
// Push the Vector2 instance on the stack
Var< Vector2 >::push(vm, Vector2{x, y});
// Insert the element on the stack into the array
@ -496,30 +486,13 @@ Array Circle::ToPointsArray(SQInteger num_segments) const
}
// ------------------------------------------------------------------------------------------------
LightObj Circle::Format(const String & spec, StackStrF & fmt) const
String Circle::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 3, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), pos.x, pos.y, rad);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", pos.x)
, fmt::arg("y", pos.y)
, fmt::arg("r", rad)
);
}
// ------------------------------------------------------------------------------------------------
@ -603,49 +576,6 @@ void Register_Circle(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Circle >)
.StaticFmtFunc(_SC("FromStr"), &Circle::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Circle::GetEx)
// Operator Exposure
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opAddAssign"), &Circle::operator +=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opSubAssign"), &Circle::operator -=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opMulAssign"), &Circle::operator *=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opDivAssign"), &Circle::operator /=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opModAssign"), &Circle::operator %=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opAddAssignR"), &Circle::operator +=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opSubAssignR"), &Circle::operator -=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opMulAssignR"), &Circle::operator *=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opDivAssignR"), &Circle::operator /=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opModAssignR"), &Circle::operator %=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opAddAssignP"), &Circle::operator +=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opSubAssignP"), &Circle::operator -=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opMulAssignP"), &Circle::operator *=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opDivAssignP"), &Circle::operator /=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opModAssignP"), &Circle::operator %=)
.Func< Circle & (Circle::*)(void) >(_SC("opPreInc"), &Circle::operator ++)
.Func< Circle & (Circle::*)(void) >(_SC("opPreDec"), &Circle::operator --)
.Func< Circle (Circle::*)(int) >(_SC("opPostInc"), &Circle::operator ++)
.Func< Circle (Circle::*)(int) >(_SC("opPostDec"), &Circle::operator --)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opAdd"), &Circle::operator +)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opSub"), &Circle::operator -)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opMul"), &Circle::operator *)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opDiv"), &Circle::operator /)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opMod"), &Circle::operator %)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opAddR"), &Circle::operator +)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opSubR"), &Circle::operator -)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opMulR"), &Circle::operator *)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opDivR"), &Circle::operator /)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opModR"), &Circle::operator %)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opAddP"), &Circle::operator +)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opSubP"), &Circle::operator -)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opMulP"), &Circle::operator *)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opDivP"), &Circle::operator /)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opModP"), &Circle::operator %)
.Func< Circle (Circle::*)(void) const >(_SC("opUnPlus"), &Circle::operator +)
.Func< Circle (Circle::*)(void) const >(_SC("opUnMinus"), &Circle::operator -)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opEqual"), &Circle::operator ==)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opNotEqual"), &Circle::operator !=)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opLessThan"), &Circle::operator <)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opGreaterThan"), &Circle::operator >)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opLessEqual"), &Circle::operator <=)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opGreaterEqual"), &Circle::operator >=)
);
}

View File

@ -1,7 +1,6 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Vector2.hpp"
// ------------------------------------------------------------------------------------------------
@ -32,13 +31,13 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* The position and radius components of this type.
*/
Vector2 pos;
Value rad;
Vector2 pos{};
Value rad{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Circle() noexcept;
Circle() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a circle at position 0,0 using the specified radius.
@ -303,12 +302,12 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Circle & c) const;
SQMOD_NODISCARD int32_t Cmp(const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
@ -316,7 +315,7 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
@ -324,7 +323,7 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
@ -332,7 +331,7 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Circle(static_cast< Value >(0)));
}
@ -340,7 +339,7 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
@ -409,17 +408,17 @@ struct Circle
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Circle Abs() const;
SQMOD_NODISCARD Circle Abs() const;
/* --------------------------------------------------------------------------------------------
* Transform this into an array of Vector2 points that form a circle.
*/
Array ToPointsArray(SQInteger num_segments) const;
SQMOD_NODISCARD Array ToPointsArray(SQInteger num_segments) const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.

View File

@ -1,19 +1,16 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Color3.hpp"
#include "Base/Color4.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Color3"))
SQMOD_DECL_TYPENAME(Typename, _SC("Color3"))
// ------------------------------------------------------------------------------------------------
const Color3 Color3::NIL = Color3();
@ -24,13 +21,6 @@ const Color3 Color3::MAX = Color3(std::numeric_limits< Color3::Value >::max());
SQChar Color3::Delim = ',';
bool Color3::UpperCaseHex = false;
// ------------------------------------------------------------------------------------------------
Color3::Color3() noexcept
: r(0), g(0), b(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(Value sv) noexcept
: r(sv), g(sv), b(sv)
@ -469,7 +459,7 @@ Color3::operator Color4 () const
}
// ------------------------------------------------------------------------------------------------
Int32 Color3::Cmp(const Color3 & o) const
int32_t Color3::Cmp(const Color3 & o) const
{
if (*this == o)
{
@ -486,9 +476,9 @@ Int32 Color3::Cmp(const Color3 & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Color3::ToString() const
String Color3::ToString() const
{
return ToStrF("%u,%u,%u", r, g, b);
return fmt::format("{},{},{}", r, g, b);
}
// ------------------------------------------------------------------------------------------------
@ -544,13 +534,13 @@ void Color3::SetName(StackStrF & name)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetRGB() const
uint32_t Color3::GetRGB() const
{
return Uint32(r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
return uint32_t(r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color3::SetRGB(Uint32 p)
void Color3::SetRGB(uint32_t p)
{
r = static_cast< Value >((p >> 16u) & 0xFFu);
g = static_cast< Value >((p >> 8u) & 0xFFu);
@ -558,13 +548,13 @@ void Color3::SetRGB(Uint32 p)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetRGBA() const
uint32_t Color3::GetRGBA() const
{
return Uint32(r << 24u | g << 16u | b << 8u | 0u); // NOLINT(hicpp-signed-bitwise)
return (r << 24u | g << 16u | b << 8u | 0u); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color3::SetRGBA(Uint32 p)
void Color3::SetRGBA(uint32_t p)
{
r = static_cast< Value >((p >> 24u) & 0xFFu);
g = static_cast< Value >((p >> 16u) & 0xFFu);
@ -572,13 +562,13 @@ void Color3::SetRGBA(Uint32 p)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetARGB() const
uint32_t Color3::GetARGB() const
{
return Uint32(0u << 24u | r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
return (0u << 24u | r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color3::SetARGB(Uint32 p)
void Color3::SetARGB(uint32_t p)
{
r = static_cast< Value >((p >> 16u) & 0xFFu);
g = static_cast< Value >((p >> 8u) & 0xFFu);
@ -660,30 +650,13 @@ LightObj Color3::ToHex4() const
}
// ------------------------------------------------------------------------------------------------
LightObj Color3::Format(const String & spec, StackStrF & fmt) const
String Color3::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 3, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), r, g, b);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("r", r)
, fmt::arg("g", g)
, fmt::arg("b", b)
);
}
// ------------------------------------------------------------------------------------------------
@ -697,8 +670,8 @@ const Color3 & Color3::GetEx(SQChar delim, StackStrF & str)
{
static Color3 col;
// The minimum and maximum values supported by the Color3 type
static constexpr Uint32 min = std::numeric_limits< Color3::Value >::min();
static constexpr Uint32 max = std::numeric_limits< Color3::Value >::max();
static constexpr uint32_t min = std::numeric_limits< Color3::Value >::min();
static constexpr uint32_t max = std::numeric_limits< Color3::Value >::max();
// Clear previous values, if any
col.Clear();
// Is the specified string empty?
@ -712,7 +685,7 @@ const Color3 & Color3::GetEx(SQChar delim, StackStrF & str)
fs[4] = delim;
fs[9] = delim;
// The sscanf function requires at least 32 bit integers
Uint32 r = 0, g = 0, b = 0;
uint32_t r = 0, g = 0, b = 0;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &r, &g, &b);
// Cast the extracted integers to the value used by the Color3 type
@ -798,60 +771,6 @@ void Register_Color3(HSQUIRRELVM vm)
.StaticFunc(_SC("SetUpperCaseHex"), &SqSetColor3UpperCaseHex)
.StaticFmtFunc(_SC("FromStr"), &Color3::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color3::GetEx)
// Operator Exposure
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opAddAssign"), &Color3::operator +=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opSubAssign"), &Color3::operator -=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opMulAssign"), &Color3::operator *=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opDivAssign"), &Color3::operator /=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opModAssign"), &Color3::operator %=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opAndAssign"), &Color3::operator &=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opOrAssign"), &Color3::operator |=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opXorAssign"), &Color3::operator ^=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opShlAssign"), &Color3::operator <<=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opShrAssign"), &Color3::operator >>=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opAddAssignS"), &Color3::operator +=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opSubAssignS"), &Color3::operator -=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opMulAssignS"), &Color3::operator *=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opDivAssignS"), &Color3::operator /=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opModAssignS"), &Color3::operator %=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opAndAssignS"), &Color3::operator &=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opOrAssignS"), &Color3::operator |=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opXorAssignS"), &Color3::operator ^=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opShlAssignS"), &Color3::operator <<=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opShrAssignS"), &Color3::operator >>=)
.Func< Color3 & (Color3::*)(void) >(_SC("opPreInc"), &Color3::operator ++)
.Func< Color3 & (Color3::*)(void) >(_SC("opPreDec"), &Color3::operator --)
.Func< Color3 (Color3::*)(int) >(_SC("opPostInc"), &Color3::operator ++)
.Func< Color3 (Color3::*)(int) >(_SC("opPostDec"), &Color3::operator --)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opAdd"), &Color3::operator +)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opSub"), &Color3::operator -)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opMul"), &Color3::operator *)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opDiv"), &Color3::operator /)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opMod"), &Color3::operator %)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opAnd"), &Color3::operator &)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opOr"), &Color3::operator |)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShl"), &Color3::operator ^)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShl"), &Color3::operator <<)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShr"), &Color3::operator >>)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opAddS"), &Color3::operator +)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opSubS"), &Color3::operator -)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opMulS"), &Color3::operator *)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opDivS"), &Color3::operator /)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opModS"), &Color3::operator %)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opAndS"), &Color3::operator &)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opOrS"), &Color3::operator |)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShlS"), &Color3::operator ^)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShlS"), &Color3::operator <<)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShrS"), &Color3::operator >>)
.Func< Color3 (Color3::*)(void) const >(_SC("opUnPlus"), &Color3::operator +)
.Func< Color3 (Color3::*)(void) const >(_SC("opUnMinus"), &Color3::operator -)
.Func< Color3 (Color3::*)(void) const >(_SC("opCom"), &Color3::operator ~)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opEqual"), &Color3::operator ==)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opNotEqual"), &Color3::operator !=)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opLessThan"), &Color3::operator <)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opGreaterThan"), &Color3::operator >)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opLessEqual"), &Color3::operator <=)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opGreaterEqual"), &Color3::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -36,12 +36,12 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/
Value r, g, b;
Value r{0}, g{0}, b{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Color3() noexcept;
Color3() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a color with all components with the same specified color.
@ -366,12 +366,12 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color3 & c) const;
SQMOD_NODISCARD int32_t Cmp(const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
@ -379,7 +379,7 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
@ -387,7 +387,7 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
@ -395,7 +395,7 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Color3(static_cast< Value >(0)));
}
@ -403,7 +403,7 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -443,32 +443,32 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
SQMOD_NODISCARD uint32_t GetRGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
void SetRGB(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
SQMOD_NODISCARD uint32_t GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
void SetRGBA(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
SQMOD_NODISCARD uint32_t GetARGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
void SetARGB(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
@ -506,17 +506,17 @@ struct Color3
/* --------------------------------------------------------------------------------------------
* Generate a 3 component hex color from the values in this instance.
*/
LightObj ToHex() const;
SQMOD_NODISCARD LightObj ToHex() const;
/* --------------------------------------------------------------------------------------------
* Generate a 4 component hex color from the values in this instance.
*/
LightObj ToHex4() const;
SQMOD_NODISCARD LightObj ToHex4() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.

View File

@ -1,19 +1,16 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Color4.hpp"
#include "Base/Color3.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Color4"))
SQMOD_DECL_TYPENAME(Typename, _SC("Color4"))
// ------------------------------------------------------------------------------------------------
const Color4 Color4::NIL = Color4();
@ -24,13 +21,6 @@ const Color4 Color4::MAX = Color4(std::numeric_limits< Color4::Value >::max());
SQChar Color4::Delim = ',';
bool Color4::UpperCaseHex = false;
// ------------------------------------------------------------------------------------------------
Color4::Color4() noexcept
: r(0), g(0), b(0), a(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value sv) noexcept
: r(sv), g(sv), b(sv), a(0)
@ -494,7 +484,7 @@ Color4::operator Color3 () const
}
// ------------------------------------------------------------------------------------------------
Int32 Color4::Cmp(const Color4 & o) const
int32_t Color4::Cmp(const Color4 & o) const
{
if (*this == o)
{
@ -511,9 +501,9 @@ Int32 Color4::Cmp(const Color4 & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Color4::ToString() const
String Color4::ToString() const
{
return ToStrF("%u,%u,%u,%u", r, g, b, a);
return fmt::format("{},{},{},{}", r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
@ -573,13 +563,13 @@ void Color4::SetName(StackStrF & name)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetRGB() const
uint32_t Color4::GetRGB() const
{
return Uint32(r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
return uint32_t(r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color4::SetRGB(Uint32 p)
void Color4::SetRGB(uint32_t p)
{
r = static_cast< Value >((p >> 16u) & 0xFFu);
g = static_cast< Value >((p >> 8u) & 0xFFu);
@ -587,13 +577,13 @@ void Color4::SetRGB(Uint32 p)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetRGBA() const
uint32_t Color4::GetRGBA() const
{
return Uint32(r << 24u | g << 16u | b << 8u | a); // NOLINT(hicpp-signed-bitwise)
return uint32_t(r << 24u | g << 16u | b << 8u | a); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color4::SetRGBA(Uint32 p)
void Color4::SetRGBA(uint32_t p)
{
r = static_cast< Value >((p >> 24u) & 0xFFu);
g = static_cast< Value >((p >> 16u) & 0xFFu);
@ -602,13 +592,13 @@ void Color4::SetRGBA(Uint32 p)
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetARGB() const
uint32_t Color4::GetARGB() const
{
return Uint32(a << 24u | r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
return uint32_t(a << 24u | r << 16u | g << 8u | b); // NOLINT(hicpp-signed-bitwise)
}
// ------------------------------------------------------------------------------------------------
void Color4::SetARGB(Uint32 p)
void Color4::SetARGB(uint32_t p)
{
a = static_cast< Value >((p >> 24u) & 0xFFu);
r = static_cast< Value >((p >> 16u) & 0xFFu);
@ -695,30 +685,14 @@ LightObj Color4::ToHex3() const
}
// ------------------------------------------------------------------------------------------------
LightObj Color4::Format(const String & spec, StackStrF & fmt) const
String Color4::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 4, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), r, g, b, a);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("r", r)
, fmt::arg("g", g)
, fmt::arg("b", b)
, fmt::arg("a", a)
);
}
// ------------------------------------------------------------------------------------------------
@ -732,8 +706,8 @@ const Color4 & Color4::GetEx(SQChar delim, StackStrF & str)
{
static Color4 col;
// The minimum and maximum values supported by the Color4 type
static constexpr Uint32 min = std::numeric_limits< Color4::Value >::min();
static constexpr Uint32 max = std::numeric_limits< Color4::Value >::max();
static constexpr uint32_t min = std::numeric_limits< Color4::Value >::min();
static constexpr uint32_t max = std::numeric_limits< Color4::Value >::max();
// Clear previous values, if any
col.Clear();
// Is the specified string empty?
@ -748,7 +722,7 @@ const Color4 & Color4::GetEx(SQChar delim, StackStrF & str)
fs[9] = delim;
fs[14] = delim;
// The sscanf function requires at least 32 bit integers
Uint32 r = 0, g = 0, b = 0, a = 0;
uint32_t r = 0, g = 0, b = 0, a = 0;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &r, &g, &b, &a);
// Cast the extracted integers to the value used by the Color4 type
@ -837,60 +811,6 @@ void Register_Color4(HSQUIRRELVM vm)
.StaticFunc(_SC("SetUpperCaseHex"), &SqSetColor4UpperCaseHex)
.StaticFmtFunc(_SC("FromStr"), &Color4::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color4::GetEx)
// Operator Exposure
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opAddAssign"), &Color4::operator +=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opSubAssign"), &Color4::operator -=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opMulAssign"), &Color4::operator *=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opDivAssign"), &Color4::operator /=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opModAssign"), &Color4::operator %=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opAndAssign"), &Color4::operator &=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opOrAssign"), &Color4::operator |=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opXorAssign"), &Color4::operator ^=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opShlAssign"), &Color4::operator <<=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opShrAssign"), &Color4::operator >>=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opAddAssignS"), &Color4::operator +=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opSubAssignS"), &Color4::operator -=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opMulAssignS"), &Color4::operator *=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opDivAssignS"), &Color4::operator /=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opModAssignS"), &Color4::operator %=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opAndAssignS"), &Color4::operator &=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opOrAssignS"), &Color4::operator |=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opXorAssignS"), &Color4::operator ^=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opShlAssignS"), &Color4::operator <<=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opShrAssignS"), &Color4::operator >>=)
.Func< Color4 & (Color4::*)(void) >(_SC("opPreInc"), &Color4::operator ++)
.Func< Color4 & (Color4::*)(void) >(_SC("opPreDec"), &Color4::operator --)
.Func< Color4 (Color4::*)(int) >(_SC("opPostInc"), &Color4::operator ++)
.Func< Color4 (Color4::*)(int) >(_SC("opPostDec"), &Color4::operator --)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opAdd"), &Color4::operator +)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opSub"), &Color4::operator -)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opMul"), &Color4::operator *)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opDiv"), &Color4::operator /)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opMod"), &Color4::operator %)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opAnd"), &Color4::operator &)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opOr"), &Color4::operator |)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShl"), &Color4::operator ^)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShl"), &Color4::operator <<)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShr"), &Color4::operator >>)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opAddS"), &Color4::operator +)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opSubS"), &Color4::operator -)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opMulS"), &Color4::operator *)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opDivS"), &Color4::operator /)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opModS"), &Color4::operator %)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opAndS"), &Color4::operator &)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opOrS"), &Color4::operator |)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShlS"), &Color4::operator ^)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShlS"), &Color4::operator <<)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShrS"), &Color4::operator >>)
.Func< Color4 (Color4::*)(void) const >(_SC("opUnPlus"), &Color4::operator +)
.Func< Color4 (Color4::*)(void) const >(_SC("opUnMinus"), &Color4::operator -)
.Func< Color4 (Color4::*)(void) const >(_SC("opCom"), &Color4::operator ~)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opEqual"), &Color4::operator ==)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opNotEqual"), &Color4::operator !=)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opLessThan"), &Color4::operator <)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opGreaterThan"), &Color4::operator >)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opLessEqual"), &Color4::operator <=)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opGreaterEqual"), &Color4::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -36,12 +36,12 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/
Value r, g, b, a;
Value r{0}, g{0}, b{0}, a{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Color4() noexcept;
Color4() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a color with all components with the same specified color.
@ -366,12 +366,12 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color4 & c) const;
SQMOD_NODISCARD int32_t Cmp(const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
@ -379,7 +379,7 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
@ -387,7 +387,7 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
@ -395,7 +395,7 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Color4(static_cast< Value >(0)));
}
@ -403,7 +403,7 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -443,32 +443,32 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
SQMOD_NODISCARD uint32_t GetRGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
void SetRGB(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
SQMOD_NODISCARD uint32_t GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
void SetRGBA(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
SQMOD_NODISCARD uint32_t GetARGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
void SetARGB(uint32_t p);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
@ -506,17 +506,17 @@ struct Color4
/* --------------------------------------------------------------------------------------------
* Generate a 4 component hex color from the values in this instance.
*/
LightObj ToHex() const;
SQMOD_NODISCARD LightObj ToHex() const;
/* --------------------------------------------------------------------------------------------
* Generate a 3 component hex color from the values in this instance.
*/
LightObj ToHex3() const;
SQMOD_NODISCARD LightObj ToHex3() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.

View File

@ -1,5 +1,8 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Core/Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -312,12 +315,12 @@ template < typename... Ts > struct SqDynArgImpl;
*/
template < > struct SqDynArgImpl< >
{
template < typename T > static Int32 Try(const T & /*val*/, HSQUIRRELVM vm)
template < typename T > static SQInteger Try(const T & /*val*/, HSQUIRRELVM vm)
{
const String tn1(SqTypeName(vm, 1));
const String tn2(SqTypeName(vm, 2));
return sq_throwerror(vm, ToStrF("Such operation is not possible between (%s) and (%s)",
tn1.c_str(), tn2.c_str()));
return sq_throwerrorf(vm, "Such operation is not possible between (%s) and (%s)",
tn1.c_str(), tn2.c_str());
}
};
@ -326,7 +329,7 @@ template < > struct SqDynArgImpl< >
*/
template < typename U, typename... Ts > struct SqDynArgImpl< U, Ts... >
{
template < typename F > static Int32 Try(F & fn, HSQUIRRELVM vm)
template < typename F > static SQInteger Try(F & fn, HSQUIRRELVM vm)
{
typedef typename std::decay< U >::type ArgType;
// Can the stack value be used with the current type?
@ -389,7 +392,7 @@ template < typename F, typename U, typename... Ts > SQInteger SqDynArgFwd(HSQUIR
{
return SqDynArgImpl< U, Ts... >::Try(fn, vm);
}
catch (const Sqrat::Exception & e)
catch (const std::exception & e)
{
return sq_throwerror(vm, e.what());
}
@ -397,6 +400,7 @@ template < typename F, typename U, typename... Ts > SQInteger SqDynArgFwd(HSQUIR
{
return sq_throwerror(vm, "Unknown error occurred during comparison");
}
SQ_UNREACHABLE
// We shouldn't really reach this point but something must be returned
return sq_throwerror(vm, "Operation encountered unknown behavior");
}

View File

@ -2,14 +2,11 @@
#include "Base/Quaternion.hpp"
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -17,7 +14,7 @@ namespace SqMod {
#define STOVAL(v) static_cast< Quaternion::Value >(v)
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Quaternion"))
SQMOD_DECL_TYPENAME(Typename, _SC("Quaternion"))
// ------------------------------------------------------------------------------------------------
const Quaternion Quaternion::NIL(0);
@ -28,13 +25,6 @@ const Quaternion Quaternion::IDENTITY(1.0, 0.0, 0.0, 0.0);
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion() noexcept
: x(0.0), y(0.0), z(0.0), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value sv) noexcept
: x(sv), y(sv), z(sv), w(sv)
@ -129,10 +119,10 @@ Quaternion & Quaternion::operator /= (const Quaternion & q)
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator %= (const Quaternion & q)
{
x = std::fmod(x, q.x);
y = std::fmod(y, q.y);
z = std::fmod(z, q.z);
w = std::fmod(w, q.w);
x = fmodf(x, q.x);
y = fmodf(y, q.y);
z = fmodf(z, q.z);
w = fmodf(w, q.w);
return *this;
}
@ -179,10 +169,10 @@ Quaternion & Quaternion::operator /= (Value s)
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
w = std::fmod(w, s);
x = fmodf(x, s);
y = fmodf(y, s);
z = fmodf(z, s);
w = fmodf(w, s);
return *this;
}
@ -279,19 +269,19 @@ Quaternion Quaternion::operator / (Value s) const
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator % (const Quaternion & q) const
{
return {std::fmod(x, q.x), std::fmod(y, q.y), std::fmod(z, q.z), std::fmod(w, q.w)};
return {fmodf(x, q.x), fmodf(y, q.y), fmodf(z, q.z), fmodf(w, q.w)};
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator % (Value s) const
{
return {std::fmod(x, s), std::fmod(y, s), std::fmod(z, s), std::fmod(w, s)};
return {fmodf(x, s), fmodf(y, s), fmodf(z, s), fmodf(w, s)};
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + () const
{
return {std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w)};
return {fabsf(x), fabsf(y), fabsf(z), fabsf(w)};
}
// ------------------------------------------------------------------------------------------------
@ -337,7 +327,7 @@ bool Quaternion::operator >= (const Quaternion & q) const
}
// ------------------------------------------------------------------------------------------------
Int32 Quaternion::Cmp(const Quaternion & o) const
int32_t Quaternion::Cmp(const Quaternion & o) const
{
if (*this == o)
{
@ -354,9 +344,9 @@ Int32 Quaternion::Cmp(const Quaternion & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Quaternion::ToString() const
String Quaternion::ToString() const
{
return ToStrF("%f,%f,%f,%f", x, y, z, w);
return fmt::format("{},{},{},{}", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -395,24 +385,24 @@ void Quaternion::SetVector3(const Vector3 & v)
// ------------------------------------------------------------------------------------------------
void Quaternion::SetVector3Ex(Value nx, Value ny, Value nz)
{
Float64 angle;
double angle;
angle = (nx * 0.5);
const Float64 sr = std::sin(angle);
const Float64 cr = std::cos(angle);
const double sr = sin(angle);
const double cr = cos(angle);
angle = (ny * 0.5);
const Float64 sp = std::sin(angle);
const Float64 cp = std::cos(angle);
const double sp = sin(angle);
const double cp = cos(angle);
angle = (nz * 0.5);
const Float64 sy = std::sin(angle);
const Float64 cy = std::cos(angle);
const double sy = sin(angle);
const double cy = cos(angle);
const Float64 cpcy = (cp * cy);
const Float64 spcy = (sp * cy);
const Float64 cpsy = (cp * sy);
const Float64 spsy = (sp * sy);
const double cpcy = (cp * cy);
const double spcy = (sp * cy);
const double cpsy = (cp * sy);
const double spsy = (sp * sy);
x = STOVAL(sr * cpcy - cr * spsy);
y = STOVAL(cr * spcy + sr * cpsy);
@ -477,13 +467,13 @@ void Quaternion::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Abs() const
{
return {std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w)};
return {fabsf(x), fabsf(y), fabsf(z), fabsf(w)};
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::IsNaN() const
{
return std::isnan(w) || std::isnan(x) || std::isnan(y) || std::isnan(z);
return isnanf(w) || isnanf(x) || isnanf(y) || isnanf(z);
}
// ------------------------------------------------------------------------------------------------
@ -511,7 +501,7 @@ void Quaternion::Normalize()
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsGt(len_squared, STOVAL(0.0)))
{
const Value inv_len = STOVAL(1.0) / std::sqrt(len_squared);
const Value inv_len = STOVAL(1.0) / sqrtf(len_squared);
x *= inv_len;
y *= inv_len;
@ -542,11 +532,11 @@ void Quaternion::FromAngleAxis(Value angle, const Vector3 & axis)
{
const Vector3 norm_axis = axis.Normalized();
angle *= SQMOD_DEGTORAD_2;
const Value sin_angle = std::sin(angle);
const Value sin_angle = sinf(angle);
x = norm_axis.x * sin_angle;
y = norm_axis.y * sin_angle;
z = norm_axis.z * sin_angle;
w = std::cos(angle);
w = cosf(angle);
}
// ------------------------------------------------------------------------------------------------
@ -559,7 +549,7 @@ void Quaternion::FromRotationTo(const Vector3 & start, const Vector3 & end)
if (EpsGt(d, STOVAL(-1.0)))
{
const Vector3 c = norm_start.CrossProduct(norm_end);
const Value s = std::sqrt((STOVAL(1.0) + d) * STOVAL(2.0));
const Value s = sqrtf((STOVAL(1.0) + d) * STOVAL(2.0));
const Value inv_s = STOVAL(1.0) / s;
x = c.x * inv_s;
@ -603,11 +593,11 @@ Quaternion Quaternion::Inverse() const
// ------------------------------------------------------------------------------------------------
Vector3 Quaternion::ToEuler() const
{
const Float64 sqw = (w * w);
const Float64 sqx = (x * x);
const Float64 sqy = (y * y);
const Float64 sqz = (z * z);
const Float64 test = 2.0 * ((y * w) - (x * z));
const double sqw = (w * w);
const double sqx = (x * x);
const double sqy = (y * y);
const double sqz = (z * z);
const double test = 2.0 * ((y * w) - (x * z));
if (EpsEq(test, 1.0))
{
@ -617,7 +607,7 @@ Vector3 Quaternion::ToEuler() const
// attitude = rotation about y-axis
STOVAL(SQMOD_PI64 / 2.0),
// heading = rotation about z-axis
STOVAL(-2.0 * std::atan2(x, w))
STOVAL(-2.0 * atan2f(x, w))
};
}
else if (EpsEq(test, -1.0))
@ -628,17 +618,17 @@ Vector3 Quaternion::ToEuler() const
// attitude = rotation about y-axis
STOVAL(SQMOD_PI64 / -2.0),
// heading = rotation about z-axis
STOVAL(2.0 * std::atan2(x, w))
STOVAL(2.0 * atan2f(x, w))
};
}
return {
// bank = rotation about x-axis
STOVAL(std::atan2(2.0 * ((y * z) + (x * w)), (-sqx - sqy + sqz + sqw))),
STOVAL(atan2(2.0 * ((y * z) + (x * w)), (-sqx - sqy + sqz + sqw))),
// attitude = rotation about y-axis
STOVAL(std::asin(Clamp(test, -1.0, 1.0))),
STOVAL(asin(Clamp(test, -1.0, 1.0))),
// heading = rotation about z-axis
STOVAL(std::atan2(2.0 * ((x * y) + (z * w)), (sqx - sqy - sqz + sqw)))
STOVAL(atan2(2.0 * ((x * y) + (z * w)), (sqx - sqy - sqz + sqw)))
};
}
@ -672,15 +662,15 @@ Quaternion Quaternion::Slerp(Quaternion quat, Value t) const
quat = -quat;
}
const Value angle = std::acos(cos_angle);
Value sin_angle = std::sin(angle);
const Value angle = acosf(cos_angle);
Value sin_angle = sinf(angle);
Value t1, t2;
if (sin_angle > STOVAL(0.001))
{
Value inv_sin_angle = STOVAL(1.0) / sin_angle;
t1 = std::sin((STOVAL(1.0) - t) * angle) * inv_sin_angle;
t2 = std::sin(t * angle) * inv_sin_angle;
t1 = sinf((STOVAL(1.0) - t) * angle) * inv_sin_angle;
t2 = sinf(t * angle) * inv_sin_angle;
}
else
{
@ -721,30 +711,14 @@ const Quaternion & Quaternion::Get(StackStrF & str)
}
// ------------------------------------------------------------------------------------------------
LightObj Quaternion::Format(const String & spec, StackStrF & fmt) const
String Quaternion::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 4, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), x, y, z, w);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", x)
, fmt::arg("y", y)
, fmt::arg("z", z)
, fmt::arg("w", w)
);
}
// ------------------------------------------------------------------------------------------------
@ -839,39 +813,6 @@ void Register_Quaternion(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Quaternion >)
.StaticFmtFunc(_SC("FromStr"), &Quaternion::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Quaternion::GetEx)
// Operator Exposure
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opAddAssign"), &Quaternion::operator +=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opSubAssign"), &Quaternion::operator -=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opMulAssign"), &Quaternion::operator *=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opDivAssign"), &Quaternion::operator /=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opModAssign"), &Quaternion::operator %=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opAddAssignS"), &Quaternion::operator +=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opSubAssignS"), &Quaternion::operator -=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opMulAssignS"), &Quaternion::operator *=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opDivAssignS"), &Quaternion::operator /=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opModAssignS"), &Quaternion::operator %=)
.Func< Quaternion & (Quaternion::*)(void) >(_SC("opPreInc"), &Quaternion::operator ++)
.Func< Quaternion & (Quaternion::*)(void) >(_SC("opPreDec"), &Quaternion::operator --)
.Func< Quaternion (Quaternion::*)(int) >(_SC("opPostInc"), &Quaternion::operator ++)
.Func< Quaternion (Quaternion::*)(int) >(_SC("opPostDec"), &Quaternion::operator --)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opAdd"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opSub"), &Quaternion::operator -)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opMul"), &Quaternion::operator *)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opDiv"), &Quaternion::operator /)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opMod"), &Quaternion::operator %)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opAddS"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opSubS"), &Quaternion::operator -)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opMulS"), &Quaternion::operator *)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opDivS"), &Quaternion::operator /)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opModS"), &Quaternion::operator %)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("opUnPlus"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("opUnMinus"), &Quaternion::operator -)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opEqual"), &Quaternion::operator ==)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opNotEqual"), &Quaternion::operator !=)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opLessThan"), &Quaternion::operator <)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opGreaterThan"), &Quaternion::operator >)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opLessEqual"), &Quaternion::operator <=)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opGreaterEqual"), &Quaternion::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -32,12 +32,12 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
Value x{0}, y{0}, z{0}, w{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Quaternion() noexcept;
Quaternion() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct from scalar value.
@ -257,12 +257,12 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Quaternion & q) const;
SQMOD_NODISCARD int32_t Cmp(const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
@ -270,7 +270,7 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
@ -278,7 +278,7 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
@ -286,7 +286,7 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Quaternion(static_cast< Value >(0)));
}
@ -294,7 +294,7 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -357,27 +357,27 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Quaternion Abs() const;
SQMOD_NODISCARD Quaternion Abs() const;
/* --------------------------------------------------------------------------------------------
* Return whether is NaN.
*/
bool IsNaN() const;
SQMOD_NODISCARD bool IsNaN() const;
/* --------------------------------------------------------------------------------------------
* Return squared length.
*/
Value LengthSquared() const;
SQMOD_NODISCARD Value LengthSquared() const;
/* --------------------------------------------------------------------------------------------
* Calculate dot product.
*/
Value DotProduct(const Quaternion & quat) const;
SQMOD_NODISCARD Value DotProduct(const Quaternion & quat) const;
/* --------------------------------------------------------------------------------------------
* Return conjugate.
*/
Quaternion Conjugate() const;
SQMOD_NODISCARD Quaternion Conjugate() const;
/* --------------------------------------------------------------------------------------------
* Normalize to unit length.
@ -387,7 +387,7 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Return normalized to unit length.
*/
Quaternion Normalized() const;
SQMOD_NODISCARD Quaternion Normalized() const;
/* --------------------------------------------------------------------------------------------
* Define from an angle (in degrees) and axis.
@ -402,47 +402,47 @@ struct Quaternion
/* --------------------------------------------------------------------------------------------
* Return inverse.
*/
Quaternion Inverse() const;
SQMOD_NODISCARD Quaternion Inverse() const;
/* --------------------------------------------------------------------------------------------
* Return Euler angles in degrees.
*/
Vector3 ToEuler() const;
SQMOD_NODISCARD Vector3 ToEuler() const;
/* --------------------------------------------------------------------------------------------
* Return yaw angle in degrees.
*/
Value YawAngle() const;
SQMOD_NODISCARD Value YawAngle() const;
/* --------------------------------------------------------------------------------------------
* Return pitch angle in degrees.
*/
Value PitchAngle() const;
SQMOD_NODISCARD Value PitchAngle() const;
/* --------------------------------------------------------------------------------------------
* Return roll angle in degrees.
*/
Value RollAngle() const;
SQMOD_NODISCARD Value RollAngle() const;
/* --------------------------------------------------------------------------------------------
* Spherical interpolation with another quaternion.
*/
Quaternion Slerp(Quaternion quat, Value t) const;
SQMOD_NODISCARD Quaternion Slerp(Quaternion quat, Value t) const;
/* --------------------------------------------------------------------------------------------
* Normalized linear interpolation with another quaternion.
*/
Quaternion Nlerp(const Quaternion & quat, Value t) const;
SQMOD_NODISCARD Quaternion Nlerp(const Quaternion & quat, Value t) const;
/* --------------------------------------------------------------------------------------------
* Normalized linear interpolation with another quaternion.
*/
Quaternion NlerpEx(const Quaternion & quat, Value t, bool shortest_path) const;
SQMOD_NODISCARD Quaternion NlerpEx(const Quaternion & quat, Value t, bool shortest_path) const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.

View File

@ -1,195 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Base/ScriptSrc.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <algorithm>
#include <stdexcept>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class to ensure the file handle is closed regardless of the situation.
*/
class FileHandle
{
public:
// --------------------------------------------------------------------------------------------
std::FILE * mFile; // Handle to the opened file.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
explicit FileHandle(CSStr path)
: mFile(std::fopen(path, "rb"))
{
if (!mFile)
{
STHROWF("Unable to open script source (%s)", path);
}
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
FileHandle(const FileHandle & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
FileHandle(FileHandle && o) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~FileHandle()
{
if (mFile)
{
std::fclose(mFile);
}
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
FileHandle & operator = (const FileHandle & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
FileHandle & operator = (FileHandle && o) = delete;
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed file handle.
*/
operator std::FILE * () // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
{
return mFile;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed file handle.
*/
operator std::FILE * () const // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
{
return mFile;
}
};
// ------------------------------------------------------------------------------------------------
void ScriptSrc::Process()
{
// Attempt to open the specified file
FileHandle fp(mPath.c_str());
// First 2 bytes of the file will tell if this is a compiled script
std::uint16_t tag;
// Go to the end of the file
std::fseek(fp, 0, SEEK_END);
// Calculate buffer size from beginning to current position
const LongI length = std::ftell(fp);
// Go back to the beginning
std::fseek(fp, 0, SEEK_SET);
// Read the first 2 bytes of the file and determine the file type
if ((length >= 2) && (std::fread(&tag, 1, 2, fp) != 2 || tag == SQ_BYTECODE_STREAM_TAG))
{
return; // Probably an empty file or compiled script
}
// Allocate enough space to hold the file data
mData.resize(static_cast< size_t >(length), 0);
// Go back to the beginning
std::fseek(fp, 0, SEEK_SET);
// Read the file contents into allocated data
std::fread(&mData[0], 1, static_cast< size_t >(length), fp);
// Where the last line ended
size_t line_start = 0, line_end = 0;
// Process the file data and locate new lines
for (String::const_iterator itr = mData.cbegin(); itr != mData.cend();)
{
// Is this a Unix style line ending?
if (*itr == '\n')
{
// Extract the line length
line_end = static_cast< size_t >(std::distance(mData.cbegin(), itr));
// Store the beginning of the line
mLine.emplace_back(line_start, line_end);
// Advance to the next line
line_start = line_end+1;
// The line end character was not included
++itr;
}
// Is this a Windows style line ending?
else if (*itr == '\r')
{
if (*(++itr) == '\n')
{
// Extract the line length
line_end = static_cast< size_t >(std::distance(mData.cbegin(), itr) - 1);
// Store the beginning of the line
mLine.emplace_back(line_start, line_end);
// Advance to the next line
line_start = line_end+2;
// The line end character was not included
++itr;
}
}
else
{
++itr;
}
}
// Should we add the last line as well?
if (mData.size() - line_start > 0)
{
mLine.emplace_back(line_start, mData.size());
}
// Specify that this script contains line information
mInfo = true;
}
// ------------------------------------------------------------------------------------------------
ScriptSrc::ScriptSrc(const String & path, bool delay, bool info)
: mExec()
, mPath(path)
, mData()
, mLine()
, mInfo(info)
, mDelay(delay)
{
// Is the specified path empty?
if (mPath.empty())
{
throw std::runtime_error("Invalid or empty script path");
}
// Should we load the file contents for debugging purposes?
else if (mInfo)
{
Process();
}
}
// ------------------------------------------------------------------------------------------------
String ScriptSrc::FetchLine(size_t line, bool trim) const
{
// Do we have such line?
if (line > mLine.size())
{
return String(); // Nope!
}
// Grab it's range in the file
Line::const_reference l = mLine.at(line);
// Grab the code from that line
String code = mData.substr(l.first, l.second - l.first);
// Trim whitespace from the beginning of the code code
if (trim)
{
code.erase(0, code.find_first_not_of(" \t\n\r\f\v"));
}
// Return the resulting string
return code;
}
} // Namespace:: SqMod

View File

@ -1,71 +0,0 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <vector>
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Core;
/* ------------------------------------------------------------------------------------------------
* Hold a information about loaded scripts as it's contents and executable code.
*/
class ScriptSrc
{
public:
// --------------------------------------------------------------------------------------------
typedef std::vector< std::pair< Uint32, Uint32 > > Line;
// --------------------------------------------------------------------------------------------
Script mExec; // Reference to the script object.
String mPath; // Path to the script file.
String mData; // The contents of the script file.
Line mLine; // List of lines of code in the data.
bool mInfo; // Whether this script contains line information.
bool mDelay; // Don't execute immediately after compilation.
/* --------------------------------------------------------------------------------------------
* Read file contents and calculate information about the lines of code.
*/
void Process();
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
ScriptSrc(const String & path, bool delay = false, bool info = false);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
ScriptSrc(const ScriptSrc & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
ScriptSrc(ScriptSrc && o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
ScriptSrc & operator = (const ScriptSrc & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
ScriptSrc & operator = (ScriptSrc && o) = default;
/* --------------------------------------------------------------------------------------------
* Fetches a line from the code. Can also triim whitespace at the beginning.
*/
String FetchLine(size_t line, bool trim = true) const;
};
} // Namespace:: SqMod

View File

@ -1,19 +1,9 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Shared.hpp"
#include "Base/Buffer.hpp"
#include "Base/Color3.hpp"
#include "Library/Numeric/Random.hpp"
#include "Core/Utility.hpp"
#include "Library/String.hpp"
// ------------------------------------------------------------------------------------------------
#include <cerrno>
#include <cstring>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <windows.h>
#endif // SQMOD_OS_WINDOWS
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -163,114 +153,6 @@ static const Color3 SQ_Color_List[] =
Color3(154, 205, 50)
};
// ------------------------------------------------------------------------------------------------
bool NameFilterCheck(CSStr filter, CSStr name)
{
// If only one of them is null then they don't match
if ((!filter && name) || (filter && !name))
{
return false;
}
// If they're both null or the filter is empty then there's nothing to check for
else if ((!filter && !name) || (*filter == '\0'))
{
return true;
}
SQChar ch = 0;
// Start comparing the strings
while (true)
{
// Grab the current character from filter
ch = *(filter++);
// See if the filter or name was completed
if (ch == '\0' || *name == '\0')
{
break; // They matched so far
}
// Are we supposed to perform a wild-card search?
else if (ch == '*')
{
// Grab the next character from filter
ch = *(filter++);
// Start comparing characters until the first match
while (*name != '\0')
{
if (*(name++) == ch)
{
break;
}
}
}
// See if the character matches doesn't have to match
else if (ch != '?' && *name != ch)
{
return false; // The character had to match and failed
}
else
{
++name;
}
}
// At this point the name satisfied the filter
return true;
}
// ------------------------------------------------------------------------------------------------
bool NameFilterCheckInsensitive(CSStr filter, CSStr name)
{
// If only one of them is null then they don't match
if ((!filter && name) || (filter && !name))
{
return false;
}
// If they're both null or the filter is empty then there's nothing to check for
else if ((!filter && !name) || (*filter == '\0'))
{
return true;
}
SQChar ch = 0;
// Start comparing the strings
while (true)
{
// Grab the current character from filter
ch = static_cast< SQChar >(std::tolower(*(filter++)));
// See if the filter or name was completed
if (ch == '\0' || *name == '\0')
{
break; // They matched so far
}
// Are we supposed to perform a wild-card search?
else if (ch == '*')
{
// Grab the next character from filter
ch = static_cast< SQChar >(std::tolower(*(filter++)));
// Start comparing characters until the first match
while (*name != '\0')
{
if (static_cast< SQChar >(std::tolower(*(name++))) == ch)
{
break;
}
}
}
// See if the character matches doesn't have to match
else if (ch != '?' && static_cast< SQChar >(std::tolower(*name)) != ch)
{
return false; // The character had to match and failed
}
else
{
++name;
}
}
// At this point the name satisfied the filter
return true;
}
// ------------------------------------------------------------------------------------------------
const Color3 & GetRandomColor()
{
@ -284,7 +166,7 @@ Color3 GetColor(StackStrF & name)
}
// ------------------------------------------------------------------------------------------------
Color3 GetColorStr(CSStr name)
Color3 GetColorStr(const SQChar * name)
{
// See if we actually have something to search for
if(!name || *name == '\0')
@ -292,7 +174,7 @@ Color3 GetColorStr(CSStr name)
return Color3::NIL; // Use default color
}
// Clone the string into an editable version
CSStr str = StrJustAlphaNum(name);
const SQChar * str = StrJustAlphaNum(name);
str = StrToLowercase(str);
// See if we still have a valid name after the cleanup
if(!str || *str == '\0')
@ -300,7 +182,7 @@ Color3 GetColorStr(CSStr name)
return Color3::NIL; // Use default color
}
// Calculate the name length
const Uint32 len = std::strlen(str);
const size_t len = std::strlen(str);
// Get the most significant characters used to identify a weapon
SQChar a = str[0], b = 0, c = 0, d = str[len-1];
// Look for deeper specifiers
@ -889,186 +771,42 @@ Color3 GetColorStr(CSStr name)
}
}
// ------------------------------------------------------------------------------------------------
bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String & spec)
{
// Is the specified string empty?
if (fmt.mLen <= 0)
{
return false; // Nothing to parse
}
// Backup current string size so we can revert back if anything
const size_t size = out.size();
// Number of processed arguments
Uint32 count = 0;
// Attempt to predict the required space
out.reserve(size + static_cast< size_t >(fmt.mLen) + arg * 2);
// Previously processed characters and the current one
SQChar p2, p1=0, c=0;
// Look for the value specifier in the specified string
for (SQInteger i = 0; i <= fmt.mLen; ++i) {
// Advance to the peaked character
p2 = p1, p1 = c, c = fmt.mPtr[i];
// The escape character is literal?
if (p1 == '\\' && p2 == '\\')
{
out.push_back('\\');
// Safeguard against a sequence of escape characters
p1 = 0;
}
// The marker is literal?
if (p1 == '\\' && c == '$')
{
out.push_back('$');
}
// Is this a marker?
else if (c == '$') {
// Did we run out of allowed arguments?
if (count++ < arg) {
out.append(spec); // Append the format specifier to the string
} else {
// Discard everything so far
out.resize(size);
// Signal failure
SqThrowF("Requested (%u) values but only (%u) available", count, arg);
}
} else if (c != '\\') {
// Append the current character to the string
out.push_back(c);
}
}
// Parsed successfully
return true;
}
// ------------------------------------------------------------------------------------------------
size_t PrintToStrF(String & out, CSStr str, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, str);
// Forward to the actual implementation
const size_t r = PrintToStrFv(out, str, args);
// Finalize the variable argument list
va_end(args);
// Return result
return r;
}
// ------------------------------------------------------------------------------------------------
size_t PrintToStrFv(String & out, CSStr str, va_list vl)
{
va_list args;
// Backup original size to revert back if necessary
const size_t size = out.size();
// The estimated buffer required
ssize_t len = 256;
begin:
// Do not modify the original va_list
va_copy(args, vl);
// Reserve the necessary space
out.resize(size + static_cast< size_t >(len), '\0');
// Attempt to generate the specified string
int res = std::vsnprintf(&out[0] + size, len, str, args);
// Do we need more space?
if (res >= len)
{
// Adjust to required size
len = res + 1;
// Try again
goto begin;
}
// Did the format failed?
else if (res < 0)
{
// Discard changes
out.resize(size);
}
else
{
// Discard extra characters
out.resize(size + static_cast< size_t >(res));
}
// Return the amount of written characters
return static_cast< size_t >(res);
}
// ------------------------------------------------------------------------------------------------
void SqThrowLastF(CSStr msg, ...)
{
// Acquire a moderately sized buffer
Buffer b(128);
// Prepare the arguments list
va_list args;
va_start (args, msg);
// Attempt to run the specified format
if (b.WriteF(0, msg, args) == 0)
{
b.At(0) = '\0'; // Make sure the string is null terminated
}
// Finalize the argument list
va_end(args);
#ifdef SQMOD_OS_WINDOWS
// Get the error message, if any.
const DWORD error_num = ::GetLastError();
// Was there an error recorded?
if(error_num == 0)
{
// Invoker is responsible for making sure this doesn't happen!
SqThrowF("%s [Unknown error]", b.Data());
}
// The resulted message buffer
LPSTR msg_buff = nullptr;
// Attempt to obtain the error message
const std::size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // NOLINT(hicpp-signed-bitwise)
nullptr, error_num, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // NOLINT(hicpp-signed-bitwise)
reinterpret_cast< LPSTR >(&msg_buff), 0, nullptr);
// Copy the message buffer before freeing it
std::string message(msg_buff, size);
//Free the message buffer
LocalFree(msg_buff);
// Now it's safe to throw the error
SqThrowF("%s [%s]", b.Data(), message.c_str());
#else
SqThrowF("%s [%s]", b.Data(), std::strerror(errno));
#endif // SQMOD_OS_WINDOWS
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqPackRGB(SQInteger r, SQInteger g, SQInteger b)
{
return static_cast< Int32 >(SQMOD_PACK_RGB(
ConvTo< Uint8 >::From(r), // NOLINT(hicpp-signed-bitwise)
ConvTo< Uint8 >::From(g),
ConvTo< Uint8 >::From(b)
return static_cast< int32_t >(SQMOD_PACK_RGB(
ConvTo< uint8_t >::From(r), // NOLINT(hicpp-signed-bitwise)
ConvTo< uint8_t >::From(g),
ConvTo< uint8_t >::From(b)
));
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqPackRGBA(SQInteger r, SQInteger g, SQInteger b, SQInteger a)
{
return static_cast< Int32 >(SQMOD_PACK_RGBA(
ConvTo< Uint8 >::From(r), // NOLINT(hicpp-signed-bitwise)
ConvTo< Uint8 >::From(g),
ConvTo< Uint8 >::From(b),
ConvTo< Uint8 >::From(a)
return static_cast< int32_t >(SQMOD_PACK_RGBA(
ConvTo< uint8_t >::From(r), // NOLINT(hicpp-signed-bitwise)
ConvTo< uint8_t >::From(g),
ConvTo< uint8_t >::From(b),
ConvTo< uint8_t >::From(a)
));
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqPackARGB(SQInteger r, SQInteger g, SQInteger b, SQInteger a)
{
return static_cast< Int32 >(SQMOD_PACK_ARGB(
ConvTo< Uint8 >::From(a), // NOLINT(hicpp-signed-bitwise)
ConvTo< Uint8 >::From(r),
ConvTo< Uint8 >::From(g),
ConvTo< Uint8 >::From(b)
return static_cast< int32_t >(SQMOD_PACK_ARGB(
ConvTo< uint8_t >::From(a), // NOLINT(hicpp-signed-bitwise)
ConvTo< uint8_t >::From(r),
ConvTo< uint8_t >::From(g),
ConvTo< uint8_t >::From(b)
));
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqNameFilterCheck(HSQUIRRELVM vm)
{
const Int32 top = sq_gettop(vm);
const int32_t top = sq_gettop(vm);
// Was the filter string specified?
if (top <= 1)
{
@ -1102,7 +840,7 @@ static SQInteger SqNameFilterCheck(HSQUIRRELVM vm)
// ------------------------------------------------------------------------------------------------
static SQInteger SqNameFilterCheckInsensitive(HSQUIRRELVM vm)
{
const Int32 top = sq_gettop(vm);
const int32_t top = sq_gettop(vm);
// Was the filter string specified?
if (top <= 1)
{

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -9,23 +9,23 @@ namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper constants used by the bas types.
*/
static constexpr Float32 SQMOD_PI = 3.14159265358979323846264338327950288f;
static constexpr Float64 SQMOD_PI64 = 3.1415926535897932384626433832795028841971693993751d;
static constexpr float SQMOD_PI = 3.14159265358979323846264338327950288f;
static constexpr double SQMOD_PI64 = 3.1415926535897932384626433832795028841971693993751;
static constexpr Float32 SQMOD_RECIPROCAL_PI = (1.0f / SQMOD_PI);
static constexpr Float64 SQMOD_RECIPROCAL_PI64 = 1.0 / SQMOD_PI64;
static constexpr float SQMOD_RECIPROCAL_PI = (1.0f / SQMOD_PI);
static constexpr double SQMOD_RECIPROCAL_PI64 = (1.0 / SQMOD_PI64);
static constexpr Float32 SQMOD_HALF_PI = (SQMOD_PI * 0.5f);
static constexpr Float32 SQMOD_HALF_PI64 = (SQMOD_PI64 * 0.5);
static constexpr float SQMOD_HALF_PI = (SQMOD_PI * 0.5f);
static constexpr double SQMOD_HALF_PI64 = (SQMOD_PI64 * 0.5);
static constexpr Float32 SQMOD_DEGTORAD = SQMOD_PI / 180.0f;
static constexpr Float64 SQMOD_DEGTORAD64 = SQMOD_PI64 / 180.0;
static constexpr float SQMOD_DEGTORAD = SQMOD_PI / 180.0f;
static constexpr double SQMOD_DEGTORAD64 = SQMOD_PI64 / 180.0;
static constexpr Float32 SQMOD_DEGTORAD_2 = SQMOD_PI / 360.0f; // M_DEGTORAD / 2.f
static constexpr Float64 SQMOD_DEGTORAD64_2 = SQMOD_PI64 / 360.0; // M_DEGTORAD / 2.f
static constexpr float SQMOD_DEGTORAD_2 = SQMOD_PI / 360.0f; // M_DEGTORAD / 2.f
static constexpr double SQMOD_DEGTORAD64_2 = SQMOD_PI64 / 360.0; // M_DEGTORAD / 2.f
static constexpr Float32 SQMOD_RADTODEG = 1.0f / SQMOD_DEGTORAD;
static constexpr Float64 SQMOD_RADTODEG64 = 1.0 / SQMOD_DEGTORAD64;
static constexpr float SQMOD_RADTODEG = 1.0f / SQMOD_DEGTORAD;
static constexpr double SQMOD_RADTODEG64 = 1.0 / SQMOD_DEGTORAD64;
/* ------------------------------------------------------------------------------------------------
* Intersection test result.
@ -37,152 +37,19 @@ enum Intersection
SQMODI_INSIDE,
};
/* ------------------------------------------------------------------------------------------------
* Helper used to reference and keep track of signal instances.
*/
typedef std::pair< Signal *, LightObj > SignalPair;
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary logging functions.
*/
extern void LogDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary logging functions.
*/
extern void LogSDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary logging functions.
*/
extern void LogDbgV(CCStr fmt, va_list vlist);
extern void LogUsrV(CCStr fmt, va_list vlist);
extern void LogScsV(CCStr fmt, va_list vlist);
extern void LogInfV(CCStr fmt, va_list vlist);
extern void LogWrnV(CCStr fmt, va_list vlist);
extern void LogErrV(CCStr fmt, va_list vlist);
extern void LogFtlV(CCStr fmt, va_list vlist);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary logging functions.
*/
extern void LogSDbgV(CCStr fmt, va_list vlist);
extern void LogSUsrV(CCStr fmt, va_list vlist);
extern void LogSScsV(CCStr fmt, va_list vlist);
extern void LogSInfV(CCStr fmt, va_list vlist);
extern void LogSWrnV(CCStr fmt, va_list vlist);
extern void LogSErrV(CCStr fmt, va_list vlist);
extern void LogSFtlV(CCStr fmt, va_list vlist);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary conditional logging functions.
*/
extern bool cLogDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary conditional logging functions.
*/
extern bool cLogSDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Initialize a signal instance into the specified pair.
*/
extern void InitSignalPair(SignalPair & sp, LightObj & et, const char * name);
/* ------------------------------------------------------------------------------------------------
* Reset/release the specified signal pair.
*/
extern void ResetSignalPair(SignalPair & sp, bool clear = true);
/* ------------------------------------------------------------------------------------------------
* A simple implementation of name filtering.
*/
bool NameFilterCheck(CSStr filter, CSStr name);
/* ------------------------------------------------------------------------------------------------
* A simple implementation of name filtering without case sensitivity.
*/
bool NameFilterCheckInsensitive(CSStr filter, CSStr name);
/* ------------------------------------------------------------------------------------------------
* Obtain a randomly chosen color from a list of known colors.
*/
const Color3 & GetRandomColor();
SQMOD_NODISCARD const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColorStr(CSStr name);
SQMOD_NODISCARD Color3 GetColorStr(const SQChar * name);
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColor(StackStrF & name);
/* ------------------------------------------------------------------------------------------------
* Throw the last system error as an exception.
*/
void SqThrowLastF(CSStr msg, ...);
/* ------------------------------------------------------------------------------------------------
* Retrieve the string delimiter of a base type.
*/
template < typename T > inline SQInteger SqGetDelimiter()
{
return T::Delim;
}
/* ------------------------------------------------------------------------------------------------
* Modify the string delimiter of a base type.
*/
template < typename T > inline void SqSetDelimiter(SQInteger c)
{
T::Delim = ConvTo< SQChar >::From(c);
}
/* ------------------------------------------------------------------------------------------------
* Used internally to build format strings for math types.
*/
bool BuildFormatString(String & out, StackStrF & fmt, Uint32 arg, const String & spec);
/* ------------------------------------------------------------------------------------------------
* Append a formatted string to a string container.
*/
size_t PrintToStrF(String & out, CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Append a formatted string to a string container.
*/
size_t PrintToStrFv(String & out, CSStr str, va_list vl);
SQMOD_NODISCARD Color3 GetColor(StackStrF & name);
} // Namespace:: SqMod

View File

@ -1,18 +1,15 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Sphere.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Sphere"))
SQMOD_DECL_TYPENAME(Typename, _SC("Sphere"))
// ------------------------------------------------------------------------------------------------
const Sphere Sphere::NIL = Sphere();
@ -22,13 +19,6 @@ const Sphere Sphere::MAX = Sphere(std::numeric_limits< Sphere::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Sphere::Delim = ',';
// ------------------------------------------------------------------------------------------------
Sphere::Sphere() noexcept
: pos(0.0), rad(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(Value rv) noexcept
: pos(0.0), rad(rv)
@ -100,7 +90,7 @@ Sphere & Sphere::operator /= (const Sphere & s)
Sphere & Sphere::operator %= (const Sphere & s)
{
pos %= s.pos;
rad = std::fmod(rad, s.rad);
rad = fmodf(rad, s.rad);
return *this;
}
@ -136,7 +126,7 @@ Sphere & Sphere::operator /= (Value r)
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator %= (Value r)
{
rad = std::fmod(rad, r);
rad = fmodf(rad, r);
return *this;
}
@ -236,7 +226,7 @@ Sphere Sphere::operator / (const Sphere & s) const
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator % (const Sphere & s) const
{
return {pos % s.pos, std::fmod(rad, s.rad)};
return {pos % s.pos, fmodf(rad, s.rad)};
}
// ------------------------------------------------------------------------------------------------
@ -266,7 +256,7 @@ Sphere Sphere::operator / (Value r) const
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator % (Value r) const
{
return {std::fmod(rad, r)};
return {fmodf(rad, r)};
}
// ------------------------------------------------------------------------------------------------
@ -302,7 +292,7 @@ Sphere Sphere::operator % (const Vector3 & p) const
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator + () const
{
return {pos.Abs(), std::fabs(rad)};
return {pos.Abs(), fabsf(rad)};
}
// ------------------------------------------------------------------------------------------------
@ -348,7 +338,7 @@ bool Sphere::operator >= (const Sphere & s) const
}
// ------------------------------------------------------------------------------------------------
Int32 Sphere::Cmp(const Sphere & o) const
int32_t Sphere::Cmp(const Sphere & o) const
{
if (*this == o)
{
@ -365,9 +355,9 @@ Int32 Sphere::Cmp(const Sphere & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Sphere::ToString() const
String Sphere::ToString() const
{
return ToStrF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
return fmt::format("{},{},{},{}", pos.x, pos.y, pos.z, rad);
}
// ------------------------------------------------------------------------------------------------
@ -465,34 +455,18 @@ void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin
// ------------------------------------------------------------------------------------------------
Sphere Sphere::Abs() const
{
return {pos.Abs(), std::fabs(rad)};
return {pos.Abs(), fabsf(rad)};
}
// ------------------------------------------------------------------------------------------------
LightObj Sphere::Format(const String & spec, StackStrF & fmt) const
String Sphere::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 4, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), pos.x, pos.y, pos.z, rad);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", pos.x)
, fmt::arg("y", pos.y)
, fmt::arg("z", pos.z)
, fmt::arg("r", rad)
);
}
// ------------------------------------------------------------------------------------------------
@ -574,49 +548,6 @@ void Register_Sphere(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Sphere >)
.StaticFmtFunc(_SC("FromStr"), &Sphere::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Sphere::GetEx)
// Operator Exposure
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opAddAssign"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opSubAssign"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opMulAssign"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opDivAssign"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opModAssign"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opAddAssignR"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opSubAssignR"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opMulAssignR"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opDivAssignR"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opModAssignR"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opAddAssignP"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opSubAssignP"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opMulAssignP"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opDivAssignP"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opModAssignP"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(void) >(_SC("opPreInc"), &Sphere::operator ++)
.Func< Sphere & (Sphere::*)(void) >(_SC("opPreDec"), &Sphere::operator --)
.Func< Sphere (Sphere::*)(int) >(_SC("opPostInc"), &Sphere::operator ++)
.Func< Sphere (Sphere::*)(int) >(_SC("opPostDec"), &Sphere::operator --)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opAdd"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opSub"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opMul"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opDiv"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opMod"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opAddR"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opSubR"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opMulR"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opDivR"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opModR"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opAddP"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opSubP"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opMulP"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opDivP"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opModP"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(void) const >(_SC("opUnPlus"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(void) const >(_SC("opUnMinus"), &Sphere::operator -)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opEqual"), &Sphere::operator ==)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opNotEqual"), &Sphere::operator !=)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opLessThan"), &Sphere::operator <)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opGreaterThan"), &Sphere::operator >)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opLessEqual"), &Sphere::operator <=)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opGreaterEqual"), &Sphere::operator >=)
);
}

View File

@ -1,7 +1,6 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Vector3.hpp"
// ------------------------------------------------------------------------------------------------
@ -32,13 +31,13 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* The position and radius components of this type.
*/
Vector3 pos;
Value rad;
Vector3 pos{};
Value rad{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Sphere() noexcept;
Sphere() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a sphere at position 0,0,0 using the specified radius.
@ -303,12 +302,12 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Sphere & s) const;
SQMOD_NODISCARD int32_t Cmp(const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
@ -316,7 +315,7 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
@ -324,7 +323,7 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
@ -332,7 +331,7 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Sphere(static_cast< Value >(0)));
}
@ -340,7 +339,7 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
@ -408,12 +407,12 @@ struct Sphere
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Sphere Abs() const;
SQMOD_NODISCARD Sphere Abs() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.

View File

@ -1,823 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
#include "Base/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <ctime>
#include <cfloat>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <windows.h>
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
#include <sqstdstring.h>
#include "Library/Numeric/LongInt.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
PluginFuncs* _Func = nullptr; //NOLINT(bugprone-reserved-identifier)
PluginCallbacks* _Clbk = nullptr; //NOLINT(bugprone-reserved-identifier)
PluginInfo* _Info = nullptr; //NOLINT(bugprone-reserved-identifier)
/* ------------------------------------------------------------------------------------------------
* Common buffers to reduce memory allocations. To be immediately copied upon return!
*/
static SQChar g_Buffer[4096];
static SQChar g_NumBuf[1024];
// ------------------------------------------------------------------------------------------------
SStr GetTempBuff()
{
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTempBuffSize()
{
return sizeof(g_Buffer);
}
/* --------------------------------------------------------------------------------------------
* Raw console message output.
*/
static inline void OutputMessageImpl(CCStr msg, va_list args)
{
#ifdef SQMOD_OS_WINDOWS
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY); // NOLINT(hicpp-signed-bitwise)
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("\033[21;32m[SQMOD]\033[0m");
std::vprintf(msg, args);
std::puts("");
#endif // SQMOD_OS_WINDOWS
}
/* --------------------------------------------------------------------------------------------
* Raw console error output.
*/
static inline void OutputErrorImpl(CCStr msg, va_list args)
{
#ifdef SQMOD_OS_WINDOWS
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY); // NOLINT(hicpp-signed-bitwise)
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY); // NOLINT(hicpp-signed-bitwise)
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("\033[21;91m[SQMOD]\033[0m");
std::vprintf(msg, args);
std::puts("");
#endif // SQMOD_OS_WINDOWS
}
// --------------------------------------------------------------------------------------------
void OutputDebug(CCStr msg, ...)
{
#ifdef _DEBUG
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
#else
SQMOD_UNUSED_VAR(msg);
#endif
}
// --------------------------------------------------------------------------------------------
void OutputMessage(CCStr msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// --------------------------------------------------------------------------------------------
void OutputError(CCStr msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputErrorImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void SqThrowF(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
// Write a generic message at least
std::strcpy(g_Buffer, "Unknown error has occurred");
}
// Finalize the argument list
va_end(args);
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer); // NOLINT(hicpp-exception-baseclass,cert-err60-cpp)
}
// ------------------------------------------------------------------------------------------------
CSStr FmtStr(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
STHROWF("Failed to run the specified string format");
}
// Finalize the argument list
va_end(args);
// Return the data from the buffer
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CSStr str, ...)
{
// Prepare the arguments list
va_list args;
va_start(args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
g_Buffer[0] = '\0'; // Make sure the string is null terminated
}
// Finalize the argument list
va_end(args);
// Return the resulted string
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStringF(CSStr str, ...)
{
// Acquire a moderately sized buffer
Buffer b(128);
// Prepare the arguments list
va_list args;
va_start (args, str);
// Attempt to run the specified format
if (b.WriteF(0, str, args) == 0)
{
b.At(0) = '\0'; // Make sure the string is null terminated
}
// Finalize the argument list
va_end(args);
// Return the resulted string
return b.Get< SQChar >();
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqThrowErrorF(HSQUIRRELVM vm, CCStr str, ...)
{
// Prepare the arguments list
va_list args;
va_start(args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
return sq_throwerror(vm, _SC("Formatting error occurred while throwing a script error"));
}
// Finalize the argument list
va_end(args);
// Throw the resulted string
return sq_throwerror(vm, g_Buffer);
}
// ------------------------------------------------------------------------------------------------
bool SToB(CSStr str)
{
// Temporary buffer to store the lowercase string
SQChar buffer[8];
// The currently processed character
unsigned i = 0;
// Convert only the necessary characters to lowercase
while (i < 7 && *str != '\0')
{
buffer[i++] = static_cast< SQChar >(std::tolower(*(str++)));
}
// Add the null terminator
buffer[i] = '\0';
// Compare the lowercase string and return the result
return std::strcmp(buffer, "true") == 0 || std::strcmp(buffer, "yes") == 0 ||
std::strcmp(buffer, "on") == 0 || std::strcmp(buffer, "1") == 0;
}
// ------------------------------------------------------------------------------------------------
StackStrF & DummyStackStrF()
{
static StackStrF s;
s.Release();
return s;
}
// ------------------------------------------------------------------------------------------------
Object & NullObject()
{
static Object o;
o.Release();
return o;
}
// ------------------------------------------------------------------------------------------------
LightObj & NullLightObj()
{
static LightObj o;
o.Release();
return o;
}
// ------------------------------------------------------------------------------------------------
Table & NullTable()
{
static Table t;
t.Release();
return t;
}
// ------------------------------------------------------------------------------------------------
Array & NullArray()
{
static Array a;
a.Release();
return a;
}
// ------------------------------------------------------------------------------------------------
Function & NullFunction()
{
static Function f;
f.Release();
return f;
}
// ------------------------------------------------------------------------------------------------
String & NullString()
{
static String s;
s.resize(0);
return s;
}
// ------------------------------------------------------------------------------------------------
String & StringRef(const SQChar * str)
{
static String s;
s.assign(str);
return s;
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int8 >::ToStr(Int8 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int8 ConvNum< Int8 >::FromStr(CSStr s)
{
return ConvTo< Int8 >::From(std::strtol(s, nullptr, 10));
}
Int8 ConvNum< Int8 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int8 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint8 >::ToStr(Uint8 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint8 ConvNum< Uint8 >::FromStr(CSStr s)
{
return ConvTo< Uint8 >::From(std::strtoul(s, nullptr, 10));
}
Uint8 ConvNum< Uint8 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint8 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int16 >::ToStr(Int16 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int16 ConvNum< Int16 >::FromStr(CSStr s)
{
return ConvTo< Int16 >::From(std::strtol(s, nullptr, 10));
}
Int16 ConvNum< Int16 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int16 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint16 >::ToStr(Uint16 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint16 ConvNum< Uint16 >::FromStr(CSStr s)
{
return ConvTo< Uint16 >::From(std::strtoul(s, nullptr, 10));
}
Uint16 ConvNum< Uint16 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint16 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int32 >::ToStr(Int32 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%d", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int32 ConvNum< Int32 >::FromStr(CSStr s)
{
return ConvTo< Int32 >::From(std::strtol(s, nullptr, 10));
}
Int32 ConvNum< Int32 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Int32 >::From(std::strtol(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint32 >::ToStr(Uint32 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%u", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint32 ConvNum< Uint32 >::FromStr(CSStr s)
{
return ConvTo< Uint32 >::From(std::strtoul(s, nullptr, 10));
}
Uint32 ConvNum< Uint32 >::FromStr(CSStr s, Int32 base)
{
return ConvTo< Uint32 >::From(std::strtoul(s, nullptr, base));
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Int64 >::ToStr(Int64 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%lld", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Int64 ConvNum< Int64 >::FromStr(CSStr s)
{
return std::strtoll(s, nullptr, 10);
}
Int64 ConvNum< Int64 >::FromStr(CSStr s, Int32 base)
{
return std::strtoll(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Uint64 >::ToStr(Uint64 v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%llu", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Uint64 ConvNum< Uint64 >::FromStr(CSStr s)
{
return std::strtoull(s, nullptr, 10);
}
Uint64 ConvNum< Uint64 >::FromStr(CSStr s, Int32 base)
{
return std::strtoull(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< LongI >::ToStr(LongI v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%ld", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
LongI ConvNum< LongI >::FromStr(CSStr s)
{
return std::strtol(s, nullptr, 10);
}
LongI ConvNum< LongI >::FromStr(CSStr s, Int32 base)
{
return std::strtol(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Ulong >::ToStr(Ulong v)
{
// Write the numeric value to the buffer
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%lu", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the beginning of the buffer
return g_NumBuf;
}
Ulong ConvNum< Ulong >::FromStr(CSStr s)
{
return std::strtoul(s, nullptr, 10);
}
Ulong ConvNum< Ulong >::FromStr(CSStr s, Int32 base)
{
return std::strtoul(s, nullptr, base);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Float32 >::ToStr(Float32 v)
{
// Attempt to convert the value to a string
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%f", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the data from the buffer
return g_NumBuf;
}
Float32 ConvNum< Float32 >::FromStr(CSStr s)
{
return std::strtof(s, nullptr);
}
Float32 ConvNum< Float32 >::FromStr(CSStr s, Int32 /*base*/)
{
return std::strtof(s, nullptr);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< Float64 >::ToStr(Float64 v)
{
// Attempt to convert the value to a string
if (std::snprintf(g_NumBuf, sizeof(g_NumBuf), "%f", v) < 0)
{
g_NumBuf[0] = '\0';
}
// Return the data from the buffer
return g_NumBuf;
}
Float64 ConvNum< Float64 >::FromStr(CSStr s)
{
return std::strtod(s, nullptr);
}
Float64 ConvNum< Float64 >::FromStr(CSStr s, Int32 /*base*/)
{
return std::strtod(s, nullptr);
}
// ------------------------------------------------------------------------------------------------
CSStr ConvNum< bool >::ToStr(bool v)
{
if (v)
{
g_NumBuf[0] = 't';
g_NumBuf[1] = 'r';
g_NumBuf[2] = 'u';
g_NumBuf[3] = 'e';
g_NumBuf[4] = '\0';
}
else
{
g_NumBuf[0] = 'f';
g_NumBuf[1] = 'a';
g_NumBuf[2] = 'l';
g_NumBuf[3] = 's';
g_NumBuf[4] = 'e';
g_NumBuf[5] = '\0';
}
return g_NumBuf;
}
bool ConvNum< bool >::FromStr(CSStr s)
{
return std::strcmp(s, "true") == 0;
}
bool ConvNum< bool >::FromStr(CSStr s, Int32 /*base*/)
{
return std::strcmp(s, "true") == 0;
}
// ------------------------------------------------------------------------------------------------
CSStr SqTypeName(SQObjectType type)
{
switch (type)
{
case OT_NULL: return _SC("null");
case OT_INTEGER: return _SC("integer");
case OT_FLOAT: return _SC("float");
case OT_BOOL: return _SC("bool");
case OT_STRING: return _SC("string");
case OT_TABLE: return _SC("table");
case OT_ARRAY: return _SC("array");
case OT_USERDATA: return _SC("userdata");
case OT_CLOSURE: return _SC("closure");
case OT_NATIVECLOSURE: return _SC("nativeclosure");
case OT_GENERATOR: return _SC("generator");
case OT_USERPOINTER: return _SC("userpointer");
case OT_THREAD: return _SC("thread");
case OT_FUNCPROTO: return _SC("funcproto");
case OT_CLASS: return _SC("class");
case OT_INSTANCE: return _SC("instance");
case OT_WEAKREF: return _SC("weakref");
case OT_OUTER: return _SC("outer");
default: return _SC("unknown");
}
}
// ------------------------------------------------------------------------------------------------
String SqTypeName(HSQUIRRELVM vm, SQInteger idx)
{
// Remember the current stack size
const StackGuard sg(vm);
// Attempt to retrieve the type name of the specified value
if (SQ_FAILED(sq_typeof(vm, idx)))
{
return _SC("unknown");
}
// Attempt to convert the obtained value to a string
StackStrF val(vm, -1);
// Did the conversion failed?
if (SQ_FAILED(val.Proc(false)))
{
return _SC("unknown");
}
// Return the obtained string value
return String(val.mPtr, static_cast< size_t >(val.mLen));
}
// ------------------------------------------------------------------------------------------------
Object BufferToStrObj(const Buffer & b)
{
// Obtain the initial stack size
const StackGuard sg(SqVM());
// Push the string onto the stack
sq_pushstring(SqVM(), b.Data(), b.Position());
// Obtain the object from the stack and return it
return Var< Object >(SqVM(), -1).value;
}
// --------------------------------------------------------------------------------------------
Object BufferToStrObj(const Buffer & b, Uint32 size)
{
// Perform a range check on the specified buffer
if (size > b.Capacity())
{
STHROWF("The specified buffer size is out of range: %u >= %u", size, b.Capacity());
}
// Obtain the initial stack size
const StackGuard sg(SqVM());
// Push the string onto the stack
sq_pushstring(SqVM(), b.Data(), size);
// Obtain the object from the stack and return it
return Var< Object >(SqVM(), -1).value;
}
// ------------------------------------------------------------------------------------------------
SQInteger PopStackInteger(HSQUIRRELVM vm, SQInteger idx)
{
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return val;
}
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return ConvTo< SQInteger >::From(val);
}
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return static_cast< SQInteger >(val);
}
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
return ConvTo< SQInteger >::From(std::strtoll(val, nullptr, 10));
} else break;
}
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return sq_getsize(vm, idx);
}
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return ConvTo< SQInteger >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return ConvTo< SQInteger >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fall back
return sq_getsize(vm, idx);
}
default: break;
}
// Default to 0
return 0;
}
// ------------------------------------------------------------------------------------------------
SQFloat PopStackFloat(HSQUIRRELVM vm, SQInteger idx)
{
// Identify which type must be extracted
switch (sq_gettype(vm, idx))
{
case OT_FLOAT:
{
SQFloat val;
sq_getfloat(vm, idx, &val);
return val;
}
case OT_INTEGER:
{
SQInteger val;
sq_getinteger(vm, idx, &val);
return ConvTo< SQFloat >::From(val);
}
case OT_BOOL:
{
SQBool val;
sq_getbool(vm, idx, &val);
return ConvTo< SQFloat >::From(val);
}
case OT_STRING:
{
CSStr val = nullptr;
// Attempt to retrieve and convert the string
if (SQ_SUCCEEDED(sq_getstring(vm, idx, &val)) && val != nullptr && *val != '\0')
{
#ifdef SQUSEDOUBLE
return std::strtod(val, nullptr);
#else
return std::strtof(val, nullptr);
#endif // SQUSEDOUBLE
} else break;
}
case OT_ARRAY:
case OT_TABLE:
case OT_CLASS:
case OT_USERDATA:
{
return ConvTo< SQFloat >::From(sq_getsize(vm, idx));
}
case OT_INSTANCE:
{
// Attempt to treat the value as a signed long instance
try
{
return ConvTo< SQFloat >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to treat the value as a unsigned long instance
try
{
return ConvTo< SQFloat >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
}
catch (...)
{
// Just ignore it...
}
// Attempt to get the size of the instance as a fall back
return ConvTo< SQFloat >::From(sq_getsize(vm, idx));
}
default: break;
}
// Default to 0
return 0.0;
}
} // Namespace:: SqMod

File diff suppressed because it is too large Load Diff

View File

@ -1,194 +0,0 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <type_traits>
/* ------------------------------------------------------------------------------------------------
* Hybrid associative container combining the performance of a vector and usefulness of a map container.
*/
template < class Key, class T, class Pred = std::equal_to< Key > > struct VecMap
{
// --------------------------------------------------------------------------------------------
using key_type = Key;
using mapped_type = T;
using value_type = std::pair< Key, T >;
using storage_type = std::vector< value_type >;
using key_equal = Pred;
using pointer = typename storage_type::pointer;
using const_pointer = typename storage_type::const_pointer;
using reference = typename storage_type::reference;
using const_reference = typename storage_type::const_reference;
using size_type = typename storage_type::size_type;
using difference_type = typename storage_type::difference_type;
using iterator = typename storage_type::iterator;
using const_iterator = typename storage_type::const_iterator;
using insert_return_type = std::pair< iterator, pointer >;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
VecMap() noexcept(noexcept(storage_type())) = default;
/* --------------------------------------------------------------------------------------------
* Copy constructor. Does exactly what you expect it to do.
*/
VecMap(const VecMap & o)
: m_Storage(o.m_Storage)
{
}
/* --------------------------------------------------------------------------------------------
* Move constructor. Does exactly what you expect it to do.
*/
VecMap(VecMap && o) noexcept
: m_Storage(std::forward< storage_type >(o.m_Storage))
{
}
/* --------------------------------------------------------------------------------------------
* Sub-script operator.
*/
mapped_type & operator [] (const key_type & key)
{
for (auto & e : m_Storage)
{
if (e.first == key) return e.second;
}
m_Storage.emplace_back(key, mapped_type{});
return m_Storage.back().second;
}
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning. See: std::vector::begin()
*/
iterator begin() noexcept { return m_Storage.begin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning (const). See: std::vector::[c]begin()
*/
const_iterator begin() const noexcept { return m_Storage.begin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning (const). See: std::vector::cbegin()
*/
const_iterator cbegin() const noexcept { return m_Storage.cbegin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning. See: std::vector::end()
*/
iterator end() noexcept { return m_Storage.end(); }
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning (const). See: std::vector::[c]end()
*/
const_iterator end() const noexcept { return m_Storage.end(); }
/* --------------------------------------------------------------------------------------------
* Retrieve an iterator to the beginning (const). See: std::vector::cend()
*/
const_iterator cend() const noexcept { return m_Storage.cend(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning. See: std::vector::rbegin()
*/
iterator rbegin() noexcept { return m_Storage.rbegin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning (const). See: std::vector::[c]rbegin()
*/
const_iterator rbegin() const noexcept { return m_Storage.rbegin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning (const). See: std::vector::crbegin()
*/
const_iterator crbegin() const noexcept { return m_Storage.crbegin(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning. See: std::vector::rend()
*/
iterator rend() noexcept { return m_Storage.rend(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning (const). See: std::vector::[c]rend()
*/
const_iterator rend() const noexcept { return m_Storage.rend(); }
/* --------------------------------------------------------------------------------------------
* Retrieve a reverse iterator to the beginning (const). See: std::vector::crend()
*/
const_iterator crend() const noexcept { return m_Storage.crend(); }
/* --------------------------------------------------------------------------------------------
* Check if elements are stored in the container.
*/
bool empty() const noexcept { return m_Storage.empty(); }
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements stored in the container.
*/
size_type size() const noexcept { return m_Storage.size(); }
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements that can be stored in the container.
*/
size_type max_size() const noexcept { return m_Storage.max_size(); }
/* --------------------------------------------------------------------------------------------
* Reserve space for a specific amount of elements.
*/
void reserve(size_type n) { m_Storage.reserve(n); }
/* --------------------------------------------------------------------------------------------
* Retrieve the number of elements that can be held in currently allocated storage.
*/
size_type capacity() const noexcept { return m_Storage.capacity(); }
/* --------------------------------------------------------------------------------------------
* Reduce memory usage by freeing unused memory.
*/
void conform() { m_Storage.shrink_to_fit(); }
/* --------------------------------------------------------------------------------------------
* Discard all stored elements.
*/
void clear() noexcept { m_Storage.clear(); }
/* --------------------------------------------------------------------------------------------
* Locate a an element with a specific key and obtain an iterator to it's location.
*/
iterator find(const key_type & key) noexcept
{
return std::find_if(m_Storage.begin(), m_Storage.end(),
[&](reference e) -> bool { return e.first == key; });
}
/* --------------------------------------------------------------------------------------------
* Locate a an element with a specific key and obtain an iterator to it's location.
*/
const_iterator find(const key_type & key) const noexcept
{
return std::find_if(m_Storage.cbegin(), m_Storage.cend(),
[&](const_reference e) -> bool { return e.first == key; });
}
/* --------------------------------------------------------------------------------------------
* Check if an element with a specific key exists in the container.
*/
bool exists(const key_type & key) const noexcept { return find(key) != m_Storage.cend(); }
/* --------------------------------------------------------------------------------------------
* Append a new element to the end of the container.
*/
template< class... Args > mapped_type & emplace_back( Args&&... args )
{
m_Storage.emplace_back(std::forward< Args >(args)...);
return m_Storage.back().second;
}
/* --------------------------------------------------------------------------------------------
* Remove the last element of the container.
*/
void pop_back() { m_Storage.pop_back(); }
/* --------------------------------------------------------------------------------------------
* Removes specified element from the container. Returns true if found and removed, false otherwise.
*/
bool erase(const key_type & key)
{
auto itr = find(key);
if (itr != m_Storage.end())
{
m_Storage.erase(itr);
return true;
}
return false;
}
/* --------------------------------------------------------------------------------------------
* Removes specified element from the container. Returns iterator to the next element.
*/
iterator erase(iterator pos) { return m_Storage.erase(pos); }
/* --------------------------------------------------------------------------------------------
* Removes specified element from the container. Returns iterator to the next element.
*/
iterator erase(const_iterator pos) { return m_Storage.erase(pos); }
private:
/* --------------------------------------------------------------------------------------------
* Internal container used to store elements.
*/
storage_type m_Storage;
};

View File

@ -1,19 +1,16 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector2.hpp"
#include "Base/Vector2i.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector2"))
SQMOD_DECL_TYPENAME(Typename, _SC("Vector2"))
// ------------------------------------------------------------------------------------------------
const Vector2 Vector2::NIL = Vector2(0);
@ -23,13 +20,6 @@ const Vector2 Vector2::MAX = Vector2(std::numeric_limits< Vector2::Value >::max(
// ------------------------------------------------------------------------------------------------
SQChar Vector2::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector2::Vector2() noexcept
: x(0.0), y(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2(Value sv) noexcept
: x(sv), y(sv)
@ -95,8 +85,8 @@ Vector2 & Vector2::operator /= (const Vector2 & v)
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator %= (const Vector2 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
x = fmodf(x, v.x);
y = fmodf(y, v.y);
return *this;
}
@ -135,8 +125,8 @@ Vector2 & Vector2::operator /= (Value s)
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
x = fmodf(x, s);
y = fmodf(y, s);
return *this;
}
@ -201,7 +191,7 @@ Vector2 Vector2::operator / (const Vector2 & v) const
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator % (const Vector2 & v) const
{
return {std::fmod(x, v.x), std::fmod(y, v.y)};
return {fmodf(x, v.x), fmodf(y, v.y)};
}
// ------------------------------------------------------------------------------------------------
@ -231,13 +221,13 @@ Vector2 Vector2::operator / (Value s) const
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator % (Value s) const
{
return {std::fmod(x, s), std::fmod(y, s)};
return {fmodf(x, s), fmodf(y, s)};
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator + () const
{
return {std::fabs(x), std::fabs(y)};
return {fabsf(x), fabsf(y)};
}
// ------------------------------------------------------------------------------------------------
@ -283,7 +273,7 @@ bool Vector2::operator >= (const Vector2 & v) const
}
// ------------------------------------------------------------------------------------------------
Int32 Vector2::Cmp(const Vector2 & o) const
int32_t Vector2::Cmp(const Vector2 & o) const
{
if (*this == o)
{
@ -300,9 +290,9 @@ Int32 Vector2::Cmp(const Vector2 & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Vector2::ToString() const
String Vector2::ToString() const
{
return ToStrF("%f,%f", x, y);
return fmt::format("{},{}", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -373,34 +363,16 @@ void Vector2::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::Abs() const
{
return {std::fabs(x), std::fabs(y)};
return {fabsf(x), fabsf(y)};
}
// ------------------------------------------------------------------------------------------------
LightObj Vector2::Format(const String & spec, StackStrF & fmt) const
String Vector2::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 2, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), x, y);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", x)
, fmt::arg("y", y)
);
}
// ------------------------------------------------------------------------------------------------
@ -476,39 +448,6 @@ void Register_Vector2(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector2 >)
.StaticFmtFunc(_SC("FromStr"), &Vector2::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector2::GetEx)
// Operator Exposure
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opAddAssign"), &Vector2::operator +=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opSubAssign"), &Vector2::operator -=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opMulAssign"), &Vector2::operator *=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opDivAssign"), &Vector2::operator /=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opModAssign"), &Vector2::operator %=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opAddAssignS"), &Vector2::operator +=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opSubAssignS"), &Vector2::operator -=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opMulAssignS"), &Vector2::operator *=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opDivAssignS"), &Vector2::operator /=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opModAssignS"), &Vector2::operator %=)
.Func< Vector2 & (Vector2::*)(void) >(_SC("opPreInc"), &Vector2::operator ++)
.Func< Vector2 & (Vector2::*)(void) >(_SC("opPreDec"), &Vector2::operator --)
.Func< Vector2 (Vector2::*)(int) >(_SC("opPostInc"), &Vector2::operator ++)
.Func< Vector2 (Vector2::*)(int) >(_SC("opPostDec"), &Vector2::operator --)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opAdd"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opSub"), &Vector2::operator -)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opMul"), &Vector2::operator *)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opDiv"), &Vector2::operator /)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opMod"), &Vector2::operator %)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opAddS"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opSubS"), &Vector2::operator -)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opMulS"), &Vector2::operator *)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opDivS"), &Vector2::operator /)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opModS"), &Vector2::operator %)
.Func< Vector2 (Vector2::*)(void) const >(_SC("opUnPlus"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(void) const >(_SC("opUnMinus"), &Vector2::operator -)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opEqual"), &Vector2::operator ==)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opNotEqual"), &Vector2::operator !=)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opLessThan"), &Vector2::operator <)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opGreaterThan"), &Vector2::operator >)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opLessEqual"), &Vector2::operator <=)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opGreaterEqual"), &Vector2::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -31,12 +31,12 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* The x and y components of this type.
*/
Value x, y;
Value x{0}, y{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector2() noexcept;
Vector2() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
@ -246,12 +246,12 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2 & v) const;
SQMOD_NODISCARD int32_t Cmp(const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
@ -259,7 +259,7 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
@ -267,7 +267,7 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
@ -275,7 +275,7 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Vector2(static_cast< Value >(0)));
}
@ -283,7 +283,7 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -336,12 +336,12 @@ struct Vector2
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector2 Abs() const;
SQMOD_NODISCARD Vector2 Abs() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.

View File

@ -1,19 +1,16 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector2i.hpp"
#include "Base/Vector2.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector2i"))
SQMOD_DECL_TYPENAME(Typename, _SC("Vector2i"))
// ------------------------------------------------------------------------------------------------
const Vector2i Vector2i::NIL = Vector2i(0);
@ -23,13 +20,6 @@ const Vector2i Vector2i::MAX = Vector2i(std::numeric_limits< Vector2i::Value >::
// ------------------------------------------------------------------------------------------------
SQChar Vector2i::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i() noexcept
: x(0), y(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i(Value sv) noexcept
: x(sv), y(sv)
@ -429,7 +419,7 @@ bool Vector2i::operator >= (const Vector2i & v) const
}
// ------------------------------------------------------------------------------------------------
Int32 Vector2i::Cmp(const Vector2i & o) const
int32_t Vector2i::Cmp(const Vector2i & o) const
{
if (*this == o)
{
@ -446,9 +436,9 @@ Int32 Vector2i::Cmp(const Vector2i & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Vector2i::ToString() const
String Vector2i::ToString() const
{
return ToStrF("%d,%d", x, y);
return fmt::format("{},{}", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -523,30 +513,12 @@ Vector2i Vector2i::Abs() const
}
// ------------------------------------------------------------------------------------------------
LightObj Vector2i::Format(const String & spec, StackStrF & fmt) const
String Vector2i::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 2, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), x, y);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", x)
, fmt::arg("y", y)
);
}
// ------------------------------------------------------------------------------------------------
@ -622,60 +594,6 @@ void Register_Vector2i(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector2i >)
.StaticFmtFunc(_SC("FromStr"), &Vector2i::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector2i::GetEx)
// Operator Exposure
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opAddAssign"), &Vector2i::operator +=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opSubAssign"), &Vector2i::operator -=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opMulAssign"), &Vector2i::operator *=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opDivAssign"), &Vector2i::operator /=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opModAssign"), &Vector2i::operator %=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opAndAssign"), &Vector2i::operator &=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opOrAssign"), &Vector2i::operator |=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opXorAssign"), &Vector2i::operator ^=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opShlAssign"), &Vector2i::operator <<=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opShrAssign"), &Vector2i::operator >>=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opAddAssignS"), &Vector2i::operator +=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opSubAssignS"), &Vector2i::operator -=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opMulAssignS"), &Vector2i::operator *=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opDivAssignS"), &Vector2i::operator /=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opModAssignS"), &Vector2i::operator %=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opAndAssignS"), &Vector2i::operator &=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opOrAssignS"), &Vector2i::operator |=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opXorAssignS"), &Vector2i::operator ^=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opShlAssignS"), &Vector2i::operator <<=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opShrAssignS"), &Vector2i::operator >>=)
.Func< Vector2i & (Vector2i::*)(void) >(_SC("opPreInc"), &Vector2i::operator ++)
.Func< Vector2i & (Vector2i::*)(void) >(_SC("opPreDec"), &Vector2i::operator --)
.Func< Vector2i (Vector2i::*)(int) >(_SC("opPostInc"), &Vector2i::operator ++)
.Func< Vector2i (Vector2i::*)(int) >(_SC("opPostDec"), &Vector2i::operator --)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opAdd"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opSub"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opMul"), &Vector2i::operator *)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opDiv"), &Vector2i::operator /)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opMod"), &Vector2i::operator %)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opAnd"), &Vector2i::operator &)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opOr"), &Vector2i::operator |)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShl"), &Vector2i::operator ^)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShl"), &Vector2i::operator <<)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShr"), &Vector2i::operator >>)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opAddS"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opSubS"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opMulS"), &Vector2i::operator *)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opDivS"), &Vector2i::operator /)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opModS"), &Vector2i::operator %)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opAndS"), &Vector2i::operator &)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opOrS"), &Vector2i::operator |)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShlS"), &Vector2i::operator ^)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShlS"), &Vector2i::operator <<)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShrS"), &Vector2i::operator >>)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opUnPlus"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opUnMinus"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opCom"), &Vector2i::operator ~)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opEqual"), &Vector2i::operator ==)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opNotEqual"), &Vector2i::operator !=)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opLessThan"), &Vector2i::operator <)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opGreaterThan"), &Vector2i::operator >)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opLessEqual"), &Vector2i::operator <=)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opGreaterEqual"), &Vector2i::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -31,12 +31,12 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* The x and y components of this type.
*/
Value x, y;
Value x{0}, y{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector2i() noexcept;
Vector2i() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
@ -351,12 +351,12 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2i & v) const;
SQMOD_NODISCARD int32_t Cmp(const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
@ -364,7 +364,7 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
@ -372,7 +372,7 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
@ -380,7 +380,7 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Vector2i(static_cast< Value >(0)));
}
@ -388,7 +388,7 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -441,12 +441,12 @@ struct Vector2i
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector2i Abs() const;
SQMOD_NODISCARD Vector2i Abs() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.

View File

@ -2,14 +2,11 @@
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
#include "Base/Quaternion.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -17,7 +14,7 @@ namespace SqMod {
#define STOVAL(v) static_cast< Vector3::Value >(v)
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector3"))
SQMOD_DECL_TYPENAME(Typename, _SC("Vector3"))
// ------------------------------------------------------------------------------------------------
const Vector3 Vector3::NIL(STOVAL(0.0));
@ -34,13 +31,6 @@ const Vector3 Vector3::ONE(STOVAL(1.0), STOVAL(1.0), STOVAL(1.0));
// ------------------------------------------------------------------------------------------------
SQChar Vector3::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector3::Vector3() noexcept
: x(0.0), y(0.0), z(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3(Value sv) noexcept
: x(sv), y(sv), z(sv)
@ -121,9 +111,9 @@ Vector3 & Vector3::operator /= (const Vector3 & v)
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator %= (const Vector3 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
z = std::fmod(z, v.z);
x = fmodf(x, v.x);
y = fmodf(y, v.y);
z = fmodf(z, v.z);
return *this;
}
@ -166,9 +156,9 @@ Vector3 & Vector3::operator /= (Value s)
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
x = fmodf(x, s);
y = fmodf(y, s);
z = fmodf(z, s);
return *this;
}
@ -237,7 +227,7 @@ Vector3 Vector3::operator / (const Vector3 & v) const
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator % (const Vector3 & v) const
{
return {std::fmod(x, v.x), std::fmod(y, v.y), std::fmod(z, v.z)};
return {fmodf(x, v.x), fmodf(y, v.y), fmodf(z, v.z)};
}
// ------------------------------------------------------------------------------------------------
@ -267,13 +257,13 @@ Vector3 Vector3::operator / (Value s) const
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator % (Value s) const
{
return {std::fmod(x, s), std::fmod(y, s), std::fmod(z, s)};
return {fmodf(x, s), fmodf(y, s), fmodf(z, s)};
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator + () const
{
return {std::fabs(x), std::fabs(y), std::fabs(z)};
return {fabsf(x), fabsf(y), fabsf(z)};
}
// ------------------------------------------------------------------------------------------------
@ -319,7 +309,7 @@ bool Vector3::operator >= (const Vector3 & v) const
}
// ------------------------------------------------------------------------------------------------
Int32 Vector3::Cmp(const Vector3 & o) const
int32_t Vector3::Cmp(const Vector3 & o) const
{
if (*this == o)
{
@ -336,9 +326,9 @@ Int32 Vector3::Cmp(const Vector3 & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Vector3::ToString() const
String Vector3::ToString() const
{
return ToStrF("%f,%f,%f", x, y, z);
return fmt::format("{},{},{}", x, y, z);
}
// ------------------------------------------------------------------------------------------------
@ -393,17 +383,17 @@ void Vector3::SetQuaternionEx(Value qx, Value qy, Value qz, Value qw)
// Quick conversion to Euler angles to give tilt to user
const Value sqx = (qx * qx), sqy = (qy * qy), sqz = (qz * qz), sqw = (qw * qw);
y = std::asin(STOVAL(2.0) * ((qw * qy) - (qx * qz)));
y = asinf(STOVAL(2.0) * ((qw * qy) - (qx * qz)));
if (EpsGt((SQMOD_PI * STOVAL(0.5)) - std::abs(y), STOVAL(1e-10)))
{
z = std::atan2(STOVAL(2.0) * ((qx * qy) + (qw * qz)), sqx - sqy - sqz + sqw);
x = std::atan2(STOVAL(2.0) * ((qw * qx) + (qy * qz)), sqw - sqx - sqy + sqz);
z = atan2f(STOVAL(2.0) * ((qx * qy) + (qw * qz)), sqx - sqy - sqz + sqw);
x = atan2f(STOVAL(2.0) * ((qw * qx) + (qy * qz)), sqw - sqx - sqy + sqz);
}
else
{
// Compute heading from local 'down' vector
z = std::atan2((STOVAL(2.0) * qy * qz) - (STOVAL(2.0) * qx * qw),
z = atan2f((STOVAL(2.0) * qy * qz) - (STOVAL(2.0) * qx * qw),
(STOVAL(2.0) * qx * qz) + (STOVAL(2.0) * qy * qw));
x = STOVAL(0.0);
@ -458,19 +448,19 @@ void Vector3::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmi
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Abs() const
{
return {std::fabs(x), std::fabs(y), std::fabs(z)};
return {fabsf(x), fabsf(y), fabsf(z)};
}
// ------------------------------------------------------------------------------------------------
bool Vector3::IsNaN() const
{
return std::isnan(x) || std::isnan(y) || std::isnan(z);
return isnanf(x) || isnanf(y) || isnanf(z);
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetLength() const
{
return std::sqrt((x * x) + (y * y) + (z * z));
return sqrtf((x * x) + (y * y) + (z * z));
}
// ------------------------------------------------------------------------------------------------
@ -492,7 +482,7 @@ void Vector3::SetLengthSquared(Value length)
{
Normalize();
// Assign the specified length
*this *= std::sqrt(length);
*this *= sqrtf(length);
}
// ------------------------------------------------------------------------------------------------
@ -502,7 +492,7 @@ Vector3 Vector3::Normalized() const
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsLt(len_squared, STOVAL(0.0)))
{
return (*this * (STOVAL(1.0) / std::sqrt(len_squared)));
return (*this * (STOVAL(1.0) / sqrtf(len_squared)));
}
else
{
@ -517,7 +507,7 @@ void Vector3::Normalize()
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsGt(len_squared, STOVAL(0.0)))
{
const Value inv_len = STOVAL(1.0) / std::sqrt(len_squared);
const Value inv_len = STOVAL(1.0) / sqrtf(len_squared);
x *= inv_len;
y *= inv_len;
z *= inv_len;
@ -545,19 +535,19 @@ Vector3 Vector3::CrossProduct(const Vector3 & vec) const
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::Angle(const Vector3 & vec) const
{
return std::acos(DotProduct(vec) / (GetLength() * vec.GetLength()));
return acosf(DotProduct(vec) / (GetLength() * vec.GetLength()));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetDistanceTo(const Vector3 & vec) const
{
return std::sqrt(std::pow(x - vec.x, 2) + std::pow(y - vec.y, 2) + std::pow(z - vec.z, 2));
return sqrtf(powf(x - vec.x, 2) + powf(y - vec.y, 2) + powf(z - vec.z, 2));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetSquaredDistanceTo(const Vector3 & vec) const
{
return (std::pow(x - vec.x, 2) + std::pow(y - vec.y, 2) + std::pow(z - vec.z, 2));
return (powf(x - vec.x, 2) + powf(y - vec.y, 2) + powf(z - vec.z, 2));
}
// ------------------------------------------------------------------------------------------------
@ -570,15 +560,15 @@ bool Vector3::IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const
// ------------------------------------------------------------------------------------------------
void Vector3::Interpolate(const Vector3 & a, const Vector3 & b, Value d)
{
x = STOVAL(static_cast< Float64 >(b.x) + ((a.x - b.x) * d));
y = STOVAL(static_cast< Float64 >(b.y) + ((a.y - b.y) * d));
z = STOVAL(static_cast< Float64 >(b.z) + ((a.z - b.z) * d));
x = STOVAL(static_cast< double >(b.x) + ((a.x - b.x) * d));
y = STOVAL(static_cast< double >(b.y) + ((a.y - b.y) * d));
z = STOVAL(static_cast< double >(b.z) + ((a.z - b.z) * d));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Interpolated(const Vector3 & vec, Value d) const
{
const Float64 inv = 1.0 - d;
const double inv = 1.0 - d;
return {
STOVAL((vec.x * inv) + (x * d)),
STOVAL((vec.y * inv) + (y * d)),
@ -590,15 +580,15 @@ Vector3 Vector3::Interpolated(const Vector3 & vec, Value d) const
Vector3 Vector3::Rotated(const Vector3 & axis, Value angle) const
{
const Vector3 o(axis * axis.DotProduct(*this));
return (o + ((*this - o) * std::cos(angle)) + (axis.CrossProduct(*this) * std::sin(angle)));
return (o + ((*this - o) * cosf(angle)) + (axis.CrossProduct(*this) * sinf(angle)));
}
// ------------------------------------------------------------------------------------------------
void Vector3::CenterRotateXZBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
const Value cs = cosf(degrees);
const Value sn = sinf(degrees);
x -= center.x;
z -= center.z;
x = static_cast< Value >((x * cs) - (z * sn)) + center.x;
@ -609,8 +599,8 @@ void Vector3::CenterRotateXZBy(Value degrees, const Vector3 & center)
void Vector3::CenterRotateXYBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
const Value cs = cosf(degrees);
const Value sn = sinf(degrees);
x -= center.x;
y -= center.y;
x = static_cast< Value >((x * cs) - (y * sn)) + center.x;
@ -621,8 +611,8 @@ void Vector3::CenterRotateXYBy(Value degrees, const Vector3 & center)
void Vector3::CenterRotateYZBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
const Value cs = cosf(degrees);
const Value sn = sinf(degrees);
z -= center.z;
y -= center.y;
y = static_cast< Value >((y * cs) - (z * sn)) + center.z;
@ -630,30 +620,13 @@ void Vector3::CenterRotateYZBy(Value degrees, const Vector3 & center)
}
// ------------------------------------------------------------------------------------------------
LightObj Vector3::Format(const String & spec, StackStrF & fmt) const
String Vector3::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 3, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), x, y, z);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", x)
, fmt::arg("y", y)
, fmt::arg("z", z)
);
}
// ------------------------------------------------------------------------------------------------
@ -767,39 +740,6 @@ void Register_Vector3(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector3 >)
.StaticFmtFunc(_SC("FromStr"), &Vector3::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector3::GetEx)
// Operator Exposure
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opAddAssign"), &Vector3::operator +=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opSubAssign"), &Vector3::operator -=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opMulAssign"), &Vector3::operator *=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opDivAssign"), &Vector3::operator /=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opModAssign"), &Vector3::operator %=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opAddAssignS"), &Vector3::operator +=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opSubAssignS"), &Vector3::operator -=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opMulAssignS"), &Vector3::operator *=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opDivAssignS"), &Vector3::operator /=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opModAssignS"), &Vector3::operator %=)
.Func< Vector3 & (Vector3::*)(void) >(_SC("opPreInc"), &Vector3::operator ++)
.Func< Vector3 & (Vector3::*)(void) >(_SC("opPreDec"), &Vector3::operator --)
.Func< Vector3 (Vector3::*)(int) >(_SC("opPostInc"), &Vector3::operator ++)
.Func< Vector3 (Vector3::*)(int) >(_SC("opPostDec"), &Vector3::operator --)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opAdd"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opSub"), &Vector3::operator -)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opMul"), &Vector3::operator *)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opDiv"), &Vector3::operator /)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opMod"), &Vector3::operator %)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opAddS"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opSubS"), &Vector3::operator -)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opMulS"), &Vector3::operator *)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opDivS"), &Vector3::operator /)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opModS"), &Vector3::operator %)
.Func< Vector3 (Vector3::*)(void) const >(_SC("opUnPlus"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(void) const >(_SC("opUnMinus"), &Vector3::operator -)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opEqual"), &Vector3::operator ==)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opNotEqual"), &Vector3::operator !=)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opLessThan"), &Vector3::operator <)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opGreaterThan"), &Vector3::operator >)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opLessEqual"), &Vector3::operator <=)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opGreaterEqual"), &Vector3::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -38,12 +38,12 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* The x, y and z components of this type.
*/
Value x, y, z;
Value x{0}, y{0}, z{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector3() noexcept;
Vector3() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
@ -258,12 +258,12 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector3 & v) const;
SQMOD_NODISCARD int32_t Cmp(const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
@ -271,7 +271,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
@ -279,7 +279,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
@ -287,7 +287,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(static_cast< Value >(0));
}
@ -295,7 +295,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -363,17 +363,17 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector3 Abs() const;
SQMOD_NODISCARD Vector3 Abs() const;
/* --------------------------------------------------------------------------------------------
* Return whether is NaN.
*/
bool IsNaN() const;
SQMOD_NODISCARD bool IsNaN() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the length.
*/
Value GetLength() const;
SQMOD_NODISCARD Value GetLength() const;
/* --------------------------------------------------------------------------------------------
* Assign the length.
@ -383,7 +383,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Return the squared length.
*/
Value GetLengthSquared() const;
SQMOD_NODISCARD Value GetLengthSquared() const;
/* --------------------------------------------------------------------------------------------
* Assign the squared length.
@ -393,7 +393,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Return normalized to unit length.
*/
Vector3 Normalized() const;
SQMOD_NODISCARD Vector3 Normalized() const;
/* --------------------------------------------------------------------------------------------
* Normalize to unit length.
@ -403,37 +403,37 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Calculate dot product.
*/
Value DotProduct(const Vector3 & vec) const;
SQMOD_NODISCARD Value DotProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Calculate absolute dot product.
*/
Value AbsDotProduct(const Vector3 & vec) const;
SQMOD_NODISCARD Value AbsDotProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Calculate cross product.
*/
Vector3 CrossProduct(const Vector3 & vec) const;
SQMOD_NODISCARD Vector3 CrossProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Returns the angle between this vector and another vector in degrees.
*/
Value Angle(const Vector3 & vec) const;
SQMOD_NODISCARD Value Angle(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Return the distance between this vector and another vector.
*/
Value GetDistanceTo(const Vector3 & vec) const;
SQMOD_NODISCARD Value GetDistanceTo(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Return the squared distance between this vector and another vector.
*/
Value GetSquaredDistanceTo(const Vector3 & vec) const;
SQMOD_NODISCARD Value GetSquaredDistanceTo(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Linear interpolation with another vector.
*/
bool IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const;
SQMOD_NODISCARD bool IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const;
/* --------------------------------------------------------------------------------------------
* Sets this vector to the linearly interpolated vector between a and b.
@ -443,12 +443,12 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Sets this vector to the linearly interpolated vector between a and b.
*/
Vector3 Interpolated(const Vector3 & vec, Value d) const;
SQMOD_NODISCARD Vector3 Interpolated(const Vector3 & vec, Value d) const;
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Y axis and the specified center.
*/
Vector3 Rotated(const Vector3 & axis, Value angle) const;
SQMOD_NODISCARD Vector3 Rotated(const Vector3 & axis, Value angle) const;
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Y axis and the specified center.
@ -492,7 +492,7 @@ struct Vector3
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.

View File

@ -2,19 +2,16 @@
#include "Base/Vector4.hpp"
#include "Base/Vector3.hpp"
#include "Base/Quaternion.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Base/Buffer.hpp"
#include "Core/Buffer.hpp"
#include "Core/Utility.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector4"))
SQMOD_DECL_TYPENAME(Typename, _SC("Vector4"))
// ------------------------------------------------------------------------------------------------
const Vector4 Vector4::NIL = Vector4(0);
@ -24,13 +21,6 @@ const Vector4 Vector4::MAX = Vector4(std::numeric_limits< Vector4::Value >::max(
// ------------------------------------------------------------------------------------------------
SQChar Vector4::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector4::Vector4() noexcept
: x(0.0), y(0.0), z(0.0), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(Value sv) noexcept
: x(sv), y(sv), z(sv), w(sv)
@ -125,10 +115,10 @@ Vector4 & Vector4::operator /= (const Vector4 & v)
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator %= (const Vector4 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
z = std::fmod(z, v.z);
w = std::fmod(w, v.w);
x = fmodf(x, v.x);
y = fmodf(y, v.y);
z = fmodf(z, v.z);
w = fmodf(w, v.w);
return *this;
}
@ -174,10 +164,10 @@ Vector4 & Vector4::operator /= (Value s)
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
w = std::fmod(w, s);
x = fmodf(x, s);
y = fmodf(y, s);
z = fmodf(z, s);
w = fmodf(w, s);
return *this;
}
@ -250,7 +240,7 @@ Vector4 Vector4::operator / (const Vector4 & v) const
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator % (const Vector4 & v) const
{
return {std::fmod(x, v.x), std::fmod(y, v.y), std::fmod(z, v.z), std::fmod(w, v.w)};
return {fmodf(x, v.x), fmodf(y, v.y), fmodf(z, v.z), fmodf(w, v.w)};
}
// ------------------------------------------------------------------------------------------------
@ -280,13 +270,13 @@ Vector4 Vector4::operator / (Value s) const
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator % (Value s) const
{
return {std::fmod(x, s), std::fmod(y, s), std::fmod(z, s), std::fmod(w, s)};
return {fmodf(x, s), fmodf(y, s), fmodf(z, s), fmodf(w, s)};
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator + () const
{
return {std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w)};
return {fabsf(x), fabsf(y), fabsf(z), fabsf(w)};
}
// ------------------------------------------------------------------------------------------------
@ -332,7 +322,7 @@ bool Vector4::operator >= (const Vector4 & v) const
}
// ------------------------------------------------------------------------------------------------
Int32 Vector4::Cmp(const Vector4 & o) const
int32_t Vector4::Cmp(const Vector4 & o) const
{
if (*this == o)
{
@ -349,9 +339,9 @@ Int32 Vector4::Cmp(const Vector4 & o) const
}
// ------------------------------------------------------------------------------------------------
CSStr Vector4::ToString() const
String Vector4::ToString() const
{
return ToStrF("%f,%f,%f,%f", x, y, z, w);
return fmt::format("{},{},{},{}", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -462,7 +452,7 @@ void Vector4::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmi
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::Abs() const
{
return {std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w)};
return {fabsf(x), fabsf(y), fabsf(z), fabsf(w)};
}
// ------------------------------------------------------------------------------------------------
@ -472,30 +462,14 @@ const Vector4 & Vector4::Get(StackStrF & str)
}
// ------------------------------------------------------------------------------------------------
LightObj Vector4::Format(const String & spec, StackStrF & fmt) const
String Vector4::Format(StackStrF & str) const
{
String out;
// Attempt to build the format string
if (!BuildFormatString(out, fmt, 4, spec))
{
return LightObj{}; // Default to null
}
// Empty string is unacceptable
else if (out.empty())
{
STHROWF("Unable to build a valid format string.");
}
// Grab a temporary buffer
Buffer buff(out.size());
// Generate the string
Buffer::SzType n = buff.WriteF(0, out.c_str(), x, y, z, w);
// Did the format failed?
if (!n && !out.empty())
{
STHROWF("Format failed. Please check format specifier and parameter count.");
}
// Return the resulted string
return LightObj{buff.Begin< SQChar >(), static_cast< SQInteger >(n)};
return fmt::format(str.ToStr()
, fmt::arg("x", x)
, fmt::arg("y", y)
, fmt::arg("z", z)
, fmt::arg("w", w)
);
}
// ------------------------------------------------------------------------------------------------
@ -575,39 +549,6 @@ void Register_Vector4(HSQUIRRELVM vm)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector4 >)
.StaticFmtFunc(_SC("FromStr"), &Vector4::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector4::GetEx)
// Operator Exposure
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opAddAssign"), &Vector4::operator +=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opSubAssign"), &Vector4::operator -=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opMulAssign"), &Vector4::operator *=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opDivAssign"), &Vector4::operator /=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opModAssign"), &Vector4::operator %=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opAddAssignS"), &Vector4::operator +=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opSubAssignS"), &Vector4::operator -=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opMulAssignS"), &Vector4::operator *=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opDivAssignS"), &Vector4::operator /=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opModAssignS"), &Vector4::operator %=)
.Func< Vector4 & (Vector4::*)(void) >(_SC("opPreInc"), &Vector4::operator ++)
.Func< Vector4 & (Vector4::*)(void) >(_SC("opPreDec"), &Vector4::operator --)
.Func< Vector4 (Vector4::*)(int) >(_SC("opPostInc"), &Vector4::operator ++)
.Func< Vector4 (Vector4::*)(int) >(_SC("opPostDec"), &Vector4::operator --)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opAdd"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opSub"), &Vector4::operator -)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opMul"), &Vector4::operator *)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opDiv"), &Vector4::operator /)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opMod"), &Vector4::operator %)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opAddS"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opSubS"), &Vector4::operator -)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opMulS"), &Vector4::operator *)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opDivS"), &Vector4::operator /)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opModS"), &Vector4::operator %)
.Func< Vector4 (Vector4::*)(void) const >(_SC("opUnPlus"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(void) const >(_SC("opUnMinus"), &Vector4::operator -)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opEqual"), &Vector4::operator ==)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opNotEqual"), &Vector4::operator !=)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opLessThan"), &Vector4::operator <)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opGreaterThan"), &Vector4::operator >)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opLessEqual"), &Vector4::operator <=)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opGreaterEqual"), &Vector4::operator >=)
);
}

View File

@ -1,7 +1,7 @@
#pragma once
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -31,12 +31,12 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
Value x{0}, y{0}, z{0}, w{0};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector4() noexcept;
Vector4() noexcept = default;
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
@ -256,12 +256,12 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector4 & v) const;
SQMOD_NODISCARD int32_t Cmp(const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
SQMOD_NODISCARD int32_t Cmp(SQFloat s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
@ -269,7 +269,7 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
SQMOD_NODISCARD int32_t Cmp(SQInteger s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
@ -277,7 +277,7 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
SQMOD_NODISCARD int32_t Cmp(bool s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
@ -285,7 +285,7 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
SQMOD_NODISCARD int32_t Cmp(std::nullptr_t) const
{
return Cmp(Vector4(static_cast< Value >(0)));
}
@ -293,7 +293,7 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
@ -361,12 +361,12 @@ struct Vector4
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector4 Abs() const;
SQMOD_NODISCARD Vector4 Abs() const;
/* --------------------------------------------------------------------------------------------
* Generate a formatted string with the values from this instance.
*/
LightObj Format(const String & spec, StackStrF & fmt) const;
SQMOD_NODISCARD String Format(StackStrF & str) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.