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

Migrated the host module to C++ exceptions as well.

Also enabled the latest C++ revision in the project.
Replaced the Random library with the one provided by C++11.
Implemented a simple AES256 encryption class.
Various other fixes and improvements.
This commit is contained in:
Sandu Liviu Catalin
2016-03-10 05:57:13 +02:00
parent 3162221e7f
commit 70e5f0ba21
124 changed files with 14873 additions and 14062 deletions

View File

@ -14,6 +14,14 @@ const AABB AABB::MAX = AABB(Vector3::MIN, Vector3::MAX);
// ------------------------------------------------------------------------------------------------
SQChar AABB::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger AABB::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("AABB");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
AABB::AABB()
: min(-1.0), max(1.0)
@ -49,27 +57,6 @@ AABB::AABB(const Vector3 & vmin, const Vector3 & vmax)
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(const AABB & b)
: min(b.min), max(b.max)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::~AABB()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator = (const AABB & b)
{
min = b.min;
max = b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator = (Value s)
{
@ -304,7 +291,7 @@ Int32 AABB::Cmp(const AABB & o) const
// ------------------------------------------------------------------------------------------------
CSStr AABB::ToString() const
{
return ToStringF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
return ToStrF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
}
// ------------------------------------------------------------------------------------------------
@ -392,6 +379,7 @@ void Register_AABB(HSQUIRRELVM vm)
.Prop(_SC("abs"), &AABB::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &AABB::ToString)
.SquirrelFunc(_SC("_typename"), &AABB::Typename)
.Func(_SC("_cmp"), &AABB::Cmp)
/* Metamethods */
.Func<AABB (AABB::*)(const AABB &) const>(_SC("_add"), &AABB::operator +)

View File

@ -8,304 +8,328 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent an axis aligned bounding box in three-dimensional space.
*/
struct AABB
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const AABB NIL;
static const AABB MIN;
static const AABB MAX;
static const AABB MIN;
static const AABB MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The minimum and maximum components of this type.
*/
Vector3 min, max;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
AABB();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an equally sized and perfectly shaped box from a scalar value.
*/
AABB(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an equally sized but imperfectly shaped box from individual components of a
* three-dimensional point.
*/
AABB(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an unequally sized and imperfectly shaped box from individual components of two
* three-dimensional points.
*/
AABB(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a an unequally sized and imperfectly shaped box from two three-dimensional
* vectors representing two three-dimensional points.
*/
AABB(const Vector3 & vmin, const Vector3 & vmax);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
AABB(const AABB & o);
AABB(const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~AABB();
AABB(AABB && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
AABB & operator = (const AABB & o);
~AABB() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
AABB & operator = (const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
AABB & operator = (AABB && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
AABB & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Three-dimensional vector assignment operator.
*/
AABB & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment operator threated as a three-dimensional vector.
*/
AABB & operator = (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
AABB & operator += (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
AABB & operator -= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
AABB & operator *= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
AABB & operator /= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
AABB & operator %= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
AABB & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
AABB & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
AABB & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
AABB & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
AABB & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
AABB & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
AABB & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
AABB operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
AABB operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
AABB operator + (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
AABB operator - (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
AABB operator * (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
AABB operator / (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
AABB operator % (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
AABB operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
AABB operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
AABB operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
AABB operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
AABB operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
AABB operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
AABB operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set an equally sized and perfectly shaped box from a scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized but imperfectly shaped box from individual components of a
* three-dimensional point.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from individual components of two
* three-dimensional points.
*/
void Set(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set the same box from another instance of this type.
*/
void Set(const AABB & b);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized and imperfectly shaped box from a single three-dimensional vector
* representing a single three-dimensional point.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from two three-dimensional vectors
* representing two three-dimensional points.
*/
void Set(const Vector3 & nmin, const Vector3 & nmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set an equally sized and imperfectly shaped box from a single four-dimensional vector
* representing a single three-dimensional point.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set an unequally sized and imperfectly shaped box from two four-dimensional vectors
* representing two three-dimensional points.
*/
void Set(const Vector4 & nmin, const Vector4 & nmax);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -314,7 +338,7 @@ static const AABB MAX;
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
AABB Abs() const;
};

View File

@ -1,10 +1,10 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
#include "Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <exception>
#include <stdexcept>
@ -14,7 +14,7 @@ namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/
static inline unsigned int NPow2(unsigned int num)
inline unsigned int NextPow2(unsigned int num)
{
--num;
num |= num >> 1;
@ -40,9 +40,7 @@ void ThrowMemExcept(const char * msg, ...)
int ret = vsnprintf(buffer, sizeof(buffer), msg, args);
// Check for formatting errors
if (ret < 0)
{
throw std::runtime_error("Unknown memory error");
}
// Throw the actual exception
throw std::runtime_error(buffer);
}
@ -53,12 +51,10 @@ void ThrowMemExcept(const char * msg, ...)
static Buffer::Pointer AllocMem(Buffer::SzType size)
{
// Attempt to allocate memory directly
Buffer::Pointer ptr = (Buffer::Pointer)malloc(size);
Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(malloc(size));
// Validate the allocated memory
if (!ptr)
{
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
// Return the allocated memory
return ptr;
}
@ -75,36 +71,36 @@ class MemCat
public:
// --------------------------------------------------------------------------------------------
typedef Buffer::Value Value;
typedef Buffer::Value Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Buffer::Reference Reference;
typedef Buffer::ConstRef ConstRef;
typedef Buffer::Reference Reference; // A reference to the stored value type.
typedef Buffer::ConstRef ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::Pointer Pointer;
typedef Buffer::ConstPtr ConstPtr;
typedef Buffer::Pointer Pointer; // A pointer to the stored value type.
typedef Buffer::ConstPtr ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::SzType SzType;
typedef Buffer::SzType SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
*
* Structure used to store a memory chunk in the linked list.
*/
struct Node
{
// ----------------------------------------------------------------------------------------
SzType mCap;
Pointer mPtr;
Node* mNext;
SzType mCap; /* The size of the memory chunk. */
Pointer mPtr; /* Pointer to the memory chunk. */
Node* mNext; /* The next node in the list. */
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Node(Node * next)
: mCap(0), mPtr(NULL), mNext(next)
: mCap(0), mPtr(nullptr), mNext(next)
{
/* ... */
}
@ -120,7 +116,7 @@ private:
* Default constructor.
*/
MemCat()
: m_Head(NULL)
: m_Head(nullptr)
{
/* ... */
}
@ -130,20 +126,18 @@ private:
*/
~MemCat()
{
for (Node * node = m_Head, * next = NULL; node; node = next)
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
m_Head = NULL;
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
@ -151,20 +145,18 @@ private:
*/
void Clear()
{
for (Node * node = m_Head, * next = NULL; node; node = next)
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
Push(node);
}
// Explicitly set the head node to null
m_Head = NULL;
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
@ -174,21 +166,17 @@ private:
{
// NOTE: Function assumes (size > 0)
// Find a buffer large enough to satisfy the requested size
for (Node * node = m_Head, * prev = NULL; node; prev = node, node = node->mNext)
for (Node * node = m_Head, * prev = nullptr; node; prev = node, node = node->mNext)
{
// Is this buffer large enough?
if (node->mCap >= size)
{
// Was there a previous node?
if (prev)
{
prev->mNext = node->mNext;
}
// Probably this was the head
else
{
m_Head = node->mNext;
}
// Assign the memory
ptr = node->mPtr;
// Assign the size
@ -200,10 +188,11 @@ private:
}
}
// Round up the size to a power of two number
size = (size & (size - 1)) ? NPow2(size) : size;
size = (size & (size - 1)) ? NextPow2(size) : size;
// Allocate the memory directly
ptr = AllocMem(size);
// See if the memory could be allocated
// (shouldn't reach this point if allocation failed)
if (!ptr)
{
// Revert the size
@ -219,9 +208,7 @@ private:
void Drop(Pointer & ptr, SzType & size)
{
if (!ptr)
{
ThrowMemExcept("Cannot store invalid memory buffer");
}
// Request a node instance
Node * node = Pull();
// Assign the specified memory
@ -245,9 +232,7 @@ private:
s_Nodes = new Node(s_Nodes);
// Validate the head node
if (!s_Nodes)
{
ThrowMemExcept("Unable to allocate memory nodes");
}
}
}
@ -256,10 +241,9 @@ private:
*/
static Node * Pull()
{
// Are there any nodes available?
if (!s_Nodes)
{
Make();
}
Make(); // Make some!
// Grab the head node
Node * node = s_Nodes;
// Promote the next node as the head
@ -275,9 +259,7 @@ private:
{
// See if the node is even valid
if (!node)
{
ThrowMemExcept("Attempting to push invalid node");
}
// Demote the current head node
node->mNext = s_Nodes;
// Promote as the head node
@ -286,7 +268,7 @@ private:
};
// ------------------------------------------------------------------------------------------------
MemCat::Node * MemCat::s_Nodes = NULL;
MemCat::Node * MemCat::s_Nodes = nullptr;
/* ------------------------------------------------------------------------------------------------
* Lightweight memory allocator to reduce the overhead of small allocations.
@ -294,8 +276,8 @@ MemCat::Node * MemCat::s_Nodes = NULL;
class Memory
{
// --------------------------------------------------------------------------------------------
friend class Buffer;
friend class MemRef;
friend class Buffer; // Allow the buffer type to access the memory categories.
friend class MemRef; // Allow the memory manager reference to create new instances.
private:
@ -316,7 +298,7 @@ private:
*/
~Memory()
{
for (MemCat::Node * node = MemCat::s_Nodes, * next = NULL; node; node = next)
for (MemCat::Node * node = MemCat::s_Nodes, * next = nullptr; node; node = next)
{
// Save the next node
next = node->mNext;
@ -324,13 +306,15 @@ private:
delete node;
}
// Explicitly set the head node to null
MemCat::s_Nodes = NULL;
MemCat::s_Nodes = nullptr;
}
private:
// --------------------------------------------------------------------------------------------
MemCat m_Small, m_Medium, m_Large;
MemCat m_Small; // Small memory allocations of <= 1024 bytes.
MemCat m_Medium; // Medium memory allocations of <= 4096 bytes.
MemCat m_Large; // Large memory allocations of <= 4096 bytes.
};
// ------------------------------------------------------------------------------------------------
@ -340,9 +324,7 @@ MemRef MemRef::s_Mem;
void MemRef::Grab()
{
if (m_Ptr)
{
++(*m_Ref);
}
}
// ------------------------------------------------------------------------------------------------
@ -352,8 +334,8 @@ void MemRef::Drop()
{
delete m_Ptr;
delete m_Ref;
m_Ptr = NULL;
m_Ref = NULL;
m_Ptr = nullptr;
m_Ref = nullptr;
}
}
@ -369,13 +351,9 @@ const MemRef & MemRef::Get()
return s_Mem;
}
// ------------------------------------------------------------------------------------------------
Buffer::Pointer Buffer::s_Ptr = NULL;
Buffer::SzType Buffer::s_Cap = 0;
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(const Buffer & o)
: m_Ptr(NULL)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(o.m_Mem)
{
@ -389,10 +367,9 @@ Buffer::Buffer(const Buffer & o)
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
}
// ------------------------------------------------------------------------------------------------
@ -400,24 +377,25 @@ Buffer & Buffer::operator = (const Buffer & o)
{
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
memcpy(m_Ptr, o.m_Ptr, m_Cap);
}
// Do we even have data to copy?
else if (!o.m_Cap)
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
}
else
{
// Do we have a buffer?
if (m_Ptr)
{
Release();
}
Release(); // Release it!
// Request a larger buffer
Request(o.m_Cap);
// Now it's safe to copy the data
memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
}
@ -433,67 +411,51 @@ void Buffer::Request(SzType n)
if (!m_Mem)
{
// Round up the size to a power of two number
n = (n & (n - 1)) ? NPow2(n) : n;
n = (n & (n - 1)) ? NextPow2(n) : n;
// Allocate the memory directly
m_Ptr = AllocMem(n);
}
// Find out in which category does this buffer reside
else if (n <= 1024)
{
m_Mem->m_Small.Grab(m_Ptr, n);
}
else if (n <= 4096)
{
m_Mem->m_Medium.Grab(m_Ptr, n);
}
else
{
m_Mem->m_Large.Grab(m_Ptr, n);
}
// If no errors occured then we can set the size
m_Cap= n;
// If no errors occurred then we can set the size
m_Cap = n;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Release()
{
// TODO: Implement a limit on how much memory can actually be pooled.
// Is there a memory manager available?
if (!m_Mem)
{
// Deallocate the memory directly
free(m_Ptr);
}
free(m_Ptr); // Deallocate the memory directly
// Find out to which category does this buffer belong
else if (m_Cap <= 1024)
{
m_Mem->m_Small.Drop(m_Ptr, m_Cap);
}
else if (m_Cap <= 4096)
{
m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
}
else
{
m_Mem->m_Large.Drop(m_Ptr, m_Cap);
}
// Explicitly reset the buffer
m_Ptr = NULL;
m_Ptr = nullptr;
m_Cap = 0;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
{
// Make sure the pos is not out of bounds
// Make sure the position is not out of bounds
if (pos > m_Cap || !data || !size)
{
return 0;
}
// See if the buffer size must be adjusted
else if ((pos + size) >= m_Cap)
{
// Backup current data
Buffer bkp = Adjust< Value >(pos + size);
// Allocate a larger memory chunk and backup old data
Buffer bkp(Adjust< Value >(NextPow2(pos + size)));
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
}
@ -506,48 +468,44 @@ Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
{
// Make sure the pos is not out of bounds
if (pos > m_Cap)
{
return 0;
}
// Initialize the arguments list
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Initial attempt to write to the current buffer
// (if empty, it should tell us the necessary size)
int ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Backup current data
Buffer bkp = Adjust< Value >(pos + ret);
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Argument list was modified during the initial format
va_end(args);
va_start(args, fmt);
// Resume writting the requested information
ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
}
// Finalize the arguments list
// Call the function that takes the variable argument list
SzType ret = WriteF(pos, fmt, args);
// Finalize the variable argument list
va_end(args);
// Return the size of the written data in bytes
return (ret < 0) ? 0 : (SzType)ret;
// Return the result
return ret;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
{
// Make sure the pos is not out of bounds
// Make sure the position is not out of bounds
if (pos > m_Cap)
{
return 0;
}
// Backup the variable argument list
va_list args_cpy;
va_copy(args_cpy, args);
// Attempt to write to the current buffer
int ret = vsnprintf(m_Ptr + pos, m_Cap - pos, fmt, args);
// Return the size of the written data in bytes
return (ret < 0) ? 0 : (SzType)ret;
// (if empty, it should tell us the necessary size)
int ret = vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Allocate a larger memory chunk and backup old data
Buffer bkp(Adjust< Value >(NextPow2(pos + ret)));
// Copy data back from the old buffer
memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Retry writing the requested information
ret = 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);
}
} // Namespace:: SQMod
} // Namespace:: SqMod

View File

@ -2,7 +2,10 @@
#define _BASE_BUFFER_HPP_
// ------------------------------------------------------------------------------------------------
#include <assert.h>
#include <cassert>
// ------------------------------------------------------------------------------------------------
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -64,6 +67,17 @@ public:
Grab();
}
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
MemRef(MemRef && o)
: m_Ptr(o.m_Ptr), m_Ref(o.m_Ref)
{
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
@ -87,6 +101,22 @@ public:
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
MemRef & operator = (MemRef && o)
{
if (m_Ptr != o.m_Ptr)
{
Drop();
m_Ptr = o.m_Ptr;
m_Ref = o.m_Ref;
o.m_Ptr = nullptr;
o.m_Ref = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two memory managers.
*/
@ -116,7 +146,7 @@ public:
*/
Memory * operator -> () const
{
assert(m_Ptr != NULL);
assert(m_Ptr);
return m_Ptr;
}
@ -125,7 +155,7 @@ public:
*/
Memory & operator * () const
{
assert(m_Ptr != NULL);
assert(m_Ptr);
return *m_Ptr;
}
};
@ -134,43 +164,58 @@ public:
void ThrowMemExcept(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Reusable buffer memory for quick allocations.
* 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 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 & 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 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. */
typedef unsigned int SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
* Construct and take ownership of the specified buffer.
*/
Buffer(Pointer & ptr, SzType & cap, const MemRef & mem)
: m_Ptr(ptr)
, m_Cap(cap)
, m_Mem(mem)
{
ptr = nullptr;
cap = 0;
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null). Not null of a previous buffer was marked as movable.
*/
Buffer()
: m_Ptr(s_Ptr)
, m_Cap(s_Cap)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(MemRef::Get())
{
s_Ptr = NULL;
s_Cap = 0;
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Explicit size constructor.
*/
Buffer(SzType n)
: m_Ptr(NULL)
: m_Ptr(nullptr)
, m_Cap(0)
, m_Mem(MemRef::Get())
{
@ -182,6 +227,15 @@ public:
*/
Buffer(const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Buffer(Buffer && o)
: m_Ptr(o.m_Ptr), m_Cap(o.m_Cap), m_Mem(o.m_Mem)
{
o.m_Ptr = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
@ -192,6 +246,23 @@ public:
*/
Buffer & operator = (const Buffer & o);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Buffer & operator = (Buffer && o)
{
if (m_Ptr != o.m_Ptr)
{
if (m_Ptr)
Release();
m_Ptr = o.m_Ptr;
m_Cap = o.m_Cap;
m_Mem = o.m_Mem;
o.m_Ptr = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
@ -251,7 +322,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * Get()
template < typename T = Value> T * Get()
{
return reinterpret_cast< T * >(m_Ptr);
}
@ -259,7 +330,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * Get() const
template < typename T = Value> const T * Get() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
@ -267,7 +338,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the a certain element.
*/
template < typename T > T & At(SzType n)
template < typename T = Value> T & At(SzType n)
{
assert(n < m_Cap);
return reinterpret_cast< T * >(m_Ptr)[n];
@ -276,7 +347,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the a certain element.
*/
template < typename T > const T & At(SzType n) const
template < typename T = Value> const T & At(SzType n) const
{
assert(n < m_Cap);
return reinterpret_cast< const T * >(m_Ptr)[n];
@ -285,7 +356,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * Begin()
template < typename T = Value> T * Begin()
{
return reinterpret_cast< T * >(m_Ptr);
}
@ -293,7 +364,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * Begin() const
template < typename T = Value> const T * Begin() const
{
return reinterpret_cast< const T * >(m_Ptr);
}
@ -301,7 +372,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > T * End()
template < typename T = Value> T * End()
{
return reinterpret_cast< T * >(m_Ptr) + (m_Cap / sizeof(T));
}
@ -309,7 +380,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the internal buffer casted as a different type.
*/
template < typename T > const T * End() const
template < typename T = Value> const T * End() const
{
return reinterpret_cast< const T * >(m_Ptr) + (m_Cap / sizeof(T));
}
@ -333,7 +404,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve maximum elements it can hold for a certain type.
*/
template < typename T > static SzType Max()
template < typename T = Value> static SzType Max()
{
return (0xFFFFFFFF / sizeof(T));
}
@ -341,7 +412,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the current buffer capacity in element count.
*/
template < typename T > SzType Size() const
template < typename T = Value> SzType Size() const
{
return (m_Cap / sizeof(T));
}
@ -357,32 +428,28 @@ public:
/* --------------------------------------------------------------------------------------------
* Makes sure there is enough capacity to hold the specified element count.
*/
template < typename T > Buffer Adjust(SzType n)
template < typename T = Value> Buffer Adjust(SzType n)
{
// Do we meet the minimum size?
if (n < 8)
{
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 the memory
Request(n * sizeof(T));
}
Request(n * sizeof(T)); // Request the memory
// Should the size be increased?
else if (n > m_Cap)
{
// Backup the current memory
Move();
Buffer bkp(m_Ptr, m_Cap, m_Mem);
// Request the memory
Request(n * sizeof(T));
// Return the backup
return std::move(bkp);
}
// Return an empty buffer or the backup (if any)
// Return an empty buffer
return Buffer();
}
@ -392,9 +459,7 @@ public:
void Reset()
{
if (m_Ptr)
{
Release();
}
}
/* --------------------------------------------------------------------------------------------
@ -437,17 +502,6 @@ protected:
*/
void Release();
/* --------------------------------------------------------------------------------------------
* Moves the internal buffer to the global members to be taken over by the next instance.
*/
void Move()
{
s_Ptr = m_Ptr;
s_Cap = m_Cap;
m_Ptr = NULL;
m_Cap = 0;
}
private:
// --------------------------------------------------------------------------------------------
@ -456,10 +510,6 @@ private:
// --------------------------------------------------------------------------------------------
MemRef m_Mem;
// --------------------------------------------------------------------------------------------
static Pointer s_Ptr; /* Pointer to a moved memory buffer. */
static SzType s_Cap; /* The total size of the moved buffer. */
};
} // Namespace:: SqMod

View File

@ -14,6 +14,14 @@ const Circle Circle::MAX = Circle(NumLimit< Circle::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Circle::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Circle::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Circle");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Circle::Circle()
: pos(0.0, 0.0), rad(0.0)
@ -42,27 +50,6 @@ Circle::Circle(Value xv, Value yv, Value rv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(const Circle & o)
: pos(o.pos), rad(o.rad)
{
}
// ------------------------------------------------------------------------------------------------
Circle::~Circle()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (const Circle & o)
{
pos = o.pos;
rad = o.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (Value r)
{
@ -341,7 +328,7 @@ Int32 Circle::Cmp(const Circle & o) const
// ------------------------------------------------------------------------------------------------
CSStr Circle::ToString() const
{
return ToStringF("%f,%f,%f", pos.x, pos.y, rad);
return ToStrF("%f,%f,%f", pos.x, pos.y, rad);
}
// ------------------------------------------------------------------------------------------------
@ -395,35 +382,28 @@ void Circle::Generate()
void Circle::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
SqThrowF("max value is lower than min value");
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax);
}
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax)
{
if (EpsLt(rmax, rmin))
{
SqThrow("max value is lower than min value");
}
else
{
pos.Generate(xmin, xmax, ymin, ymax);
rad = GetRandomFloat32(rmin, rmax);
}
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(rmax, rmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
@ -452,6 +432,7 @@ void Register_Circle(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Circle::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Circle::ToString)
.SquirrelFunc(_SC("_typename"), &Circle::Typename)
.Func(_SC("_cmp"), &Circle::Cmp)
/* Metamethods */
.Func<Circle (Circle::*)(const Circle &) const>(_SC("_add"), &Circle::operator +)

View File

@ -9,355 +9,370 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional circle.
*/
struct Circle
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Circle NIL;
static const Circle MIN;
static const Circle MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The position and radius components of this type.
*/
Vector2 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Circle();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at position 0,0 using the specified radius.
*/
Circle(Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at the specified position using the specified radius.
*/
Circle(const Vector2 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a circle at the specified position using the specified radius.
*/
Circle(Value xv, Value yv, Value rv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Circle(const Circle & o);
Circle(const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Circle();
Circle(Circle && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Circle & operator = (const Circle & o);
~Circle() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Circle & operator = (const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Circle & operator = (Circle && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Circle & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position assignment operator.
*/
Circle & operator = (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Circle & operator += (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Circle & operator -= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Circle & operator *= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Circle & operator /= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Circle & operator %= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition assignment operator.
*/
Circle & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction assignment operator.
*/
Circle & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication assignment operator.
*/
Circle & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius division assignment operator.
*/
Circle & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo assignment operator.
*/
Circle & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position addition assignment operator.
*/
Circle & operator += (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction assignment operator.
*/
Circle & operator -= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication assignment operator.
*/
Circle & operator *= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position division assignment operator.
*/
Circle & operator /= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo assignment operator.
*/
Circle & operator %= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Circle & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Circle & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Circle operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Circle operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Circle operator + (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Circle operator - (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Circle operator * (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Circle operator / (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Circle operator % (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition operator.
*/
Circle operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction operator.
*/
Circle operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication operator.
*/
Circle operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius division operator.
*/
Circle operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo operator.
*/
Circle operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position addition operator.
*/
Circle operator + (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction operator.
*/
Circle operator - (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication operator.
*/
Circle operator * (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position division operator.
*/
Circle operator / (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo operator.
*/
Circle operator % (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Circle operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Circle operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void Set(Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the circle from another instance of this type.
*/
void Set(const Circle & nc);
/* --------------------------------------------------------------------------------------------
* ...
* Set the position from the specified position.
*/
void Set(const Vector2 & np);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(const Vector2 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(Value nx, Value ny, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned circle.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized or positioned circle within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -365,7 +380,7 @@ struct Circle
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Circle Abs() const;
};

View File

@ -15,6 +15,14 @@ const Color3 Color3::MAX = Color3(NumLimit< Color3::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Color3::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Color3::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Color3");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Color3::Color3()
: r(0), g(0), b(0)
@ -36,28 +44,6 @@ Color3::Color3(Value rv, Value gv, Value bv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(const Color3 & o)
: r(o.r), g(o.g), b(o.b)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::~Color3()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (const Color3 & o)
{
r = o.r;
g = o.g;
b = o.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (Value s)
{
@ -449,7 +435,7 @@ Int32 Color3::Cmp(const Color3 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Color3::ToString() const
{
return ToStringF("%u,%u,%u", r, g, b);
return ToStrF("%u,%u,%u", r, g, b);
}
// ------------------------------------------------------------------------------------------------
@ -502,9 +488,9 @@ Uint32 Color3::GetRGB() const
void Color3::SetRGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -515,9 +501,9 @@ Uint32 Color3::GetRGBA() const
void Color3::SetRGBA(Uint32 p)
{
r = Value((p >> 24) & 0xFF);
g = Value((p >> 16) & 0xFF);
b = Value((p >> 8) & 0xFF);
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -528,9 +514,9 @@ Uint32 Color3::GetARGB() const
void Color3::SetARGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -544,29 +530,21 @@ void Color3::Generate()
void Color3::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
}
void Color3::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
@ -578,9 +556,9 @@ void Color3::Random()
// ------------------------------------------------------------------------------------------------
void Color3::Inverse()
{
r = Value(~r);
g = Value(~g);
b = Value(~b);
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
}
// ================================================================================================
@ -606,6 +584,7 @@ void Register_Color3(HSQUIRRELVM vm)
.Prop(_SC("str"), &Color3::SetCol)
/* Core Metamethods */
.Func(_SC("_tostring"), &Color3::ToString)
.SquirrelFunc(_SC("_typename"), &Color3::Typename)
.Func(_SC("_cmp"), &Color3::Cmp)
/* Metamethods */
.Func<Color3 (Color3::*)(const Color3 &) const>(_SC("_add"), &Color3::operator +)

View File

@ -8,434 +8,449 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent an opaque RGB color.
*/
struct Color3
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color3 NIL;
static const Color3 MIN;
static const Color3 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The red, green and blue components of this type.
*/
Value r, g, b;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Color3();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a color with all components with the same specified color.
*/
Color3(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green and blue colors.
*/
Color3(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Color3(const Color3 & o);
Color3(const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Color3();
Color3(Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Color3 & operator = (const Color3 & o);
~Color3() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Color3 & operator = (const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Color3 & operator = (Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Named color assignment operator.
*/
Color3 & operator = (CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Transparent color assignment operator.
*/
Color3 & operator = (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Color3 & operator += (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Color3 & operator -= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Color3 & operator *= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Color3 & operator /= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Color3 & operator %= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Color3 & operator &= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Color3 & operator |= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Color3 & operator ^= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Color3 & operator <<= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Color3 & operator >>= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Color3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Color3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Color3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Color3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Color3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Color3 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Color3 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Color3 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Color3 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Color3 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Color3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Color3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Color3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Color3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Color3 operator + (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Color3 operator - (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Color3 operator * (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Color3 operator / (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Color3 operator % (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Color3 operator & (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Color3 operator | (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Color3 operator ^ (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Color3 operator << (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Color3 operator >> (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Color3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Color3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Color3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Color3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Color3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Color3 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Color3 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Color3 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Color3 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Color3 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Color3 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Color3 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Color3 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Implicit conversion to transparent color.
*/
operator Color4 () const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an opaque color.
*/
void Set(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr str, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values from the identified color.
*/
void SetCol(CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -443,12 +458,12 @@ struct Color3
}
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* ...
* Inverse the color.
*/
void Inverse();
};

View File

@ -15,6 +15,14 @@ const Color4 Color4::MAX = Color4(NumLimit< Color4::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Color4::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Color4::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Color4");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Color4::Color4()
: r(0), g(0), b(0), a(0)
@ -24,7 +32,7 @@ Color4::Color4()
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value sv)
: r(sv), g(sv), b(sv), a(sv)
: r(sv), g(sv), b(sv), a(0)
{
/* ... */
}
@ -43,29 +51,6 @@ Color4::Color4(Value rv, Value gv, Value bv, Value av)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(const Color4 & o)
: r(o.r), g(o.g), b(o.b), a(o.a)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::~Color4()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (const Color4 & o)
{
r = o.r;
g = o.g;
b = o.b;
a = o.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (Value s)
{
@ -482,7 +467,7 @@ Int32 Color4::Cmp(const Color4 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Color4::ToString() const
{
return ToStringF("%u,%u,%u,%u", r, g, b, a);
return ToStrF("%u,%u,%u,%u", r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
@ -546,9 +531,9 @@ Uint32 Color4::GetRGB() const
void Color4::SetRGB(Uint32 p)
{
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -559,10 +544,10 @@ Uint32 Color4::GetRGBA() const
void Color4::SetRGBA(Uint32 p)
{
r = Value((p >> 24) & 0xFF);
g = Value((p >> 16) & 0xFF);
b = Value((p >> 8) & 0xFF);
a = Value((p) & 0xFF);
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
a = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -573,10 +558,10 @@ Uint32 Color4::GetARGB() const
void Color4::SetARGB(Uint32 p)
{
a = Value((p >> 24) & 0xFF);
r = Value((p >> 16) & 0xFF);
g = Value((p >> 8) & 0xFF);
b = Value((p) & 0xFF);
a = static_cast< Value >((p >> 24) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
@ -591,31 +576,23 @@ void Color4::Generate()
void Color4::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
a = GetRandomUint8(min, max);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
a = GetRandomUint8(min, max);
}
void Color4::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin || amax < amin)
{
SqThrow("max value is lower than min value");
}
else
{
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
a = GetRandomUint8(bmin, bmax);
}
SqThrowF("max value is lower than min value");
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
a = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
@ -627,10 +604,10 @@ void Color4::Random()
// ------------------------------------------------------------------------------------------------
void Color4::Inverse()
{
r = Value(~r);
g = Value(~g);
b = Value(~b);
a = Value(~a);
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
a = static_cast< Value >(~a);
}
// ================================================================================================
@ -658,6 +635,7 @@ void Register_Color4(HSQUIRRELVM vm)
.Prop(_SC("str"), &Color4::SetCol)
/* Core Metamethods */
.Func(_SC("_tostring"), &Color4::ToString)
.SquirrelFunc(_SC("_typename"), &Color4::Typename)
.Func(_SC("_cmp"), &Color4::Cmp)
/* Metamethods */
.Func<Color4 (Color4::*)(const Color4 &) const>(_SC("_add"), &Color4::operator +)

View File

@ -8,444 +8,459 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a transparent RGBA color.
*/
struct Color4
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color4 NIL;
static const Color4 MIN;
static const Color4 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The red, green and blue components of this type.
*/
Value r, g, b, a;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Color4();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a color with all components with the same specified color.
*/
Color4(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green and blue colors.
*/
Color4(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct with individually specified red, green, blue and alpha colors.
*/
Color4(Value rv, Value gv, Value bv, Value av);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Color4(const Color4 & o);
Color4(const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Color4();
Color4(Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Color4 & operator = (const Color4 & o);
~Color4() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Color4 & operator = (const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Color4 & operator = (Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Named color assignment operator.
*/
Color4 & operator = (CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Opaque color assignment operator.
*/
Color4 & operator = (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Color4 & operator += (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Color4 & operator -= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Color4 & operator *= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Color4 & operator /= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Color4 & operator %= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Color4 & operator &= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Color4 & operator |= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Color4 & operator ^= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Color4 & operator <<= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Color4 & operator >>= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Color4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Color4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Color4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Color4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Color4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Color4 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Color4 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Color4 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Color4 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Color4 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Color4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Color4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Color4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Color4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Color4 operator + (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Color4 operator - (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Color4 operator * (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Color4 operator / (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Color4 operator % (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Color4 operator & (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Color4 operator | (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Color4 operator ^ (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Color4 operator << (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Color4 operator >> (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Color4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Color4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Color4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Color4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Color4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Color4 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Color4 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Color4 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Color4 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Color4 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Color4 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Color4 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Color4 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Implicit conversion to opaque color.
*/
operator Color3 () const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nr, Value ng, Value nb, Value na);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an opaque color.
*/
void Set(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr name, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values from the identified color.
*/
void SetCol(CSStr name);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -453,12 +468,12 @@ struct Color4
}
/* --------------------------------------------------------------------------------------------
* ...
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* ...
* Inverse the color.
*/
void Inverse();
};

View File

@ -16,6 +16,14 @@ const Quaternion Quaternion::MAX = Quaternion(NumLimit< Quaternion::Value >::Max
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Quaternion::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Quaternion");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion()
: x(0.0), y(0.0), z(0.0), w(0.0)
@ -44,29 +52,6 @@ Quaternion::Quaternion(Value xv, Value yv, Value zv, Value wv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(const Quaternion & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::~Quaternion()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (const Quaternion & o)
{
x = o.x;
y = o.y;
z = o.z;
w = o.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (Value s)
{
@ -338,7 +323,7 @@ Int32 Quaternion::Cmp(const Quaternion & o) const
// ------------------------------------------------------------------------------------------------
CSStr Quaternion::ToString() const
{
return ToStringF("%f,%f,%f,%f", x, y, z, w);
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -408,31 +393,23 @@ void Quaternion::Generate()
void Quaternion::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Quaternion::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(wmax, wmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
// ------------------------------------------------------------------------------------------------
@ -463,6 +440,7 @@ void Register_Quaternion(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Quaternion::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Quaternion::ToString)
.SquirrelFunc(_SC("_typename"), &Quaternion::Typename)
.Func(_SC("_cmp"), &Quaternion::Cmp)
/* Metamethods */
.Func<Quaternion (Quaternion::*)(const Quaternion &) const>(_SC("_add"), &Quaternion::operator +)

View File

@ -8,34 +8,34 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Quaternion class for representing rotations.
*/
struct Quaternion
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Quaternion NIL;
static const Quaternion MIN;
static const Quaternion MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Quaternion();
@ -55,257 +55,272 @@ struct Quaternion
Quaternion(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Quaternion(const Quaternion & o);
Quaternion(const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Quaternion();
Quaternion(Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Quaternion & operator = (const Quaternion & o);
~Quaternion() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Quaternion & operator = (const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Quaternion & operator = (Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Quaternion & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Euler assignment operator.
*/
Quaternion & operator = (const Vector3 & q);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment operator threated as a three-dimensional vector.
*/
Quaternion & operator = (const Vector4 & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Quaternion & operator += (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Quaternion & operator -= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Quaternion & operator *= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Quaternion & operator /= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Quaternion & operator %= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Quaternion & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Quaternion & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Quaternion & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Quaternion & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Quaternion & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Quaternion & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Quaternion & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Quaternion operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Quaternion operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Quaternion operator + (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Quaternion operator - (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Quaternion operator * (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Quaternion operator / (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Quaternion operator % (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Quaternion operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Quaternion operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Quaternion operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Quaternion operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Quaternion operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Quaternion operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Quaternion operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a three-dimensional vector as euler rotation.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a four-dimensional vector.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -313,7 +328,7 @@ struct Quaternion
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Quaternion Abs() const;
};

View File

@ -35,6 +35,11 @@ PluginFuncs* _Func = NULL;
PluginCallbacks* _Clbk = NULL;
PluginInfo* _Info = NULL;
/* ------------------------------------------------------------------------------------------------
* Common buffer to reduce memory allocations. To be immediately copied uppon return!
*/
static SQChar g_Buffer[4096];
// ------------------------------------------------------------------------------------------------
const char NumLimit< char >::Min = CHAR_MIN;
const signed char NumLimit< signed char >::Min = SCHAR_MIN;
@ -99,30 +104,52 @@ bool SToB(CSStr str)
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CCStr fmt, ...)
void SqThrowF(CCStr fmt, ...)
{
static char buf[128];
// Initialize the argument list
va_list args;
va_start (args, fmt);
int ret = vsnprintf(buf, sizeof(buf), fmt, args);
if (ret < 0)
{
SqThrow("Failed to run the specified string format");
buf[0] = 0;
}
// Write the requested contents
if (snprintf(g_Buffer, sizeof(g_Buffer), fmt, args) < 0)
strcpy(g_Buffer, "Unknown error has occurred");
// Release the argument list
va_end(args);
return buf;
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr ToStrF(CCStr fmt, ...)
{
// Prepare the arguments list
va_list args;
va_start (args, fmt);
// Attempt to run the specified format
int ret = vsnprintf(g_Buffer, sizeof(g_Buffer), fmt, args);
// See if the format function failed
if (ret < 0)
SqThrowF("Failed to run the specified string format");
// Finalized the arguments list
va_end(args);
// Return the resulted string
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
CSStr ToStringF(CCStr fmt, ...)
{
// Acquire a moderately sized buffer
Buffer b(128);
// Prepare the arguments list
va_list args;
va_start (args, fmt);
// Attempt to run the specified format
if (b.WriteF(0, fmt, args) == 0)
b.At< SQChar >(0) = 0;
// Make sure the string is null terminated
b.At(0) = 0;
// Finalized the arguments list
va_end(args);
// Return the resulted string
return b.Get< SQChar >();
}
@ -280,22 +307,17 @@ const Color3 & GetRandomColor()
// ------------------------------------------------------------------------------------------------
Color3 GetColor(CSStr name)
{
Uint32 len = 0;
// See if we actually have something to search for
if(!name || (len = (Uint32)strlen(name)) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color3::NIL;
}
if(!name || *name == 0)
SqThrowF("Cannot extract values from an empty string");
// Clone the string into an editable version
CCStr str = StrJustAlphaNum(name);
str = StrToLowercase(str);
// See if we still have a valid name after the cleanup
if((len = (Uint32)strlen(name)) <= 0)
{
SqThrow("Cannot extract values from an invalid string: %s", name);
return Color3::NIL;
}
if(!str || *str == 0)
SqThrowF("Cannot extract values from an invalid string: %s", name);
// Calculate the name length
const Uint32 len = 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
@ -885,19 +907,17 @@ Color3 GetColor(CSStr name)
}
// ------------------------------------------------------------------------------------------------
AABB GetAABB(CSStr str, SQChar delim)
const AABB & GetAABB(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f , %f , %f ");
static AABB box;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
box.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return box;
}
else if (delim != AABB::Delim)
if (delim != AABB::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -920,19 +940,17 @@ AABB GetAABB(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Circle GetCircle(CSStr str, SQChar delim)
const Circle & GetCircle(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f ");
static Circle circle;
//circle.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return circle;
}
else if (delim != Circle::Delim)
circle.Clear();
if (delim != Circle::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -949,16 +967,15 @@ Circle GetCircle(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Color3 GetColor3(CSStr str, SQChar delim)
const Color3 & GetColor3(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %u , %u , %u ");
static Color3 col;
Uint32 r = 0, g = 0, b = 0;
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color3();
}
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
else if (delim != Color3::Delim)
{
fs[4] = delim;
@ -972,19 +989,23 @@ Color3 GetColor3(CSStr str, SQChar delim)
sscanf(str, &fs[0], &r, &g, &b);
return Color3(Color3::Value(r), Color3::Value(g), Color3::Value(b));
col.r = static_cast< Color4::Value >(r);
col.g = static_cast< Color4::Value >(g);
col.b = static_cast< Color4::Value >(b);
return col;
}
Color4 GetColor4(CSStr str, SQChar delim)
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %u , %u , %u , %u ");
static Color4 col;
Uint32 r = 0, g = 0, b = 0, a = 0;
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return Color4();
}
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
else if (delim != Color4::Delim)
{
fs[4] = delim;
@ -1000,23 +1021,26 @@ Color4 GetColor4(CSStr str, SQChar delim)
sscanf(str, &fs[0], &r, &g, &b, &a);
return Color4(Color4::Value(r), Color4::Value(g), Color4::Value(b), Color4::Value(a));
col.r = static_cast< Color4::Value >(r);
col.g = static_cast< Color4::Value >(g);
col.b = static_cast< Color4::Value >(b);
col.a = static_cast< Color4::Value >(a);
return col;
}
// ------------------------------------------------------------------------------------------------
Quaternion GetQuaternion(CSStr str, SQChar delim)
const Quaternion & GetQuaternion(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Quaternion quat;
//quat.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return quat;
}
else if (delim != Quaternion::Delim)
quat.Clear();
if (delim != Quaternion::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1034,19 +1058,18 @@ Quaternion GetQuaternion(CSStr str, SQChar delim)
return quat;
}
Sphere GetSphere(CSStr str, SQChar delim)
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Sphere sphere;
//sphere.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return sphere;
}
else if (delim != Sphere::Delim)
sphere.Clear();
if (delim != Sphere::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1065,52 +1088,40 @@ Sphere GetSphere(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Vector2 GetVector2(CSStr str, SQChar delim)
const Vector2 & GetVector2(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f ");
static Vector2 vec;
//vec.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector2::Delim)
{
vec.Clear();
if (delim != Vector2::Delim)
fs[4] = delim;
}
else
{
fs[4] = Vector2::Delim;
}
sscanf(str, &fs[0], &vec.x, &vec.y);
return vec;
}
Vector2i GetVector2i(CSStr str, SQChar delim)
const Vector2i & GetVector2i(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %d , %d ");
static Vector2i vec;
//vec.Clear();
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector2i::Delim)
{
vec.Clear();
if (delim != Vector2i::Delim)
fs[4] = delim;
}
else
{
fs[4] = Vector2i::Delim;
}
sscanf(str, &fs[0], &vec.x, &vec.y);
@ -1118,19 +1129,17 @@ Vector2i GetVector2i(CSStr str, SQChar delim)
}
// ------------------------------------------------------------------------------------------------
Vector3 GetVector3(CSStr str, SQChar delim)
const Vector3 & GetVector3(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f ");
static Vector3 vec;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
vec.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector3::Delim)
if (delim != Vector3::Delim)
{
fs[4] = delim;
fs[9] = delim;
@ -1146,19 +1155,17 @@ Vector3 GetVector3(CSStr str, SQChar delim)
return vec;
}
Vector4 GetVector4(CSStr str, SQChar delim)
const Vector4 & GetVector4(CSStr str, SQChar delim)
{
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Vector4 vec;
if (!str || *str == 0)
SqThrowF("Cannot extract values from an empty string");
vec.Clear();
if (strlen(str) <= 0)
{
SqThrow("Cannot extract values from an empty string");
return vec;
}
else if (delim != Vector4::Delim)
if (delim != Vector4::Delim)
{
fs[4] = delim;
fs[9] = delim;

View File

@ -5,8 +5,8 @@
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
#include <math.h>
#include <assert.h>
#include <cmath>
#include <cassert>
// ------------------------------------------------------------------------------------------------
#include <vcmp.h>
@ -19,7 +19,7 @@ namespace SqMod {
extern const SQChar * g_EmptyStr;
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the server.
* Proxies to communicate with the server.
*/
extern PluginFuncs* _Func;
extern PluginCallbacks* _Clbk;
@ -28,7 +28,9 @@ extern PluginInfo* _Info;
// ------------------------------------------------------------------------------------------------
template < typename T > struct NumLimit;
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Basic minimum and maximum values for primitive numeric types.
*/
template <> struct NumLimit< char > { static const char Min, Max; };
template <> struct NumLimit< signed char > { static const signed char Min, Max; };
template <> struct NumLimit< unsigned char > { static const unsigned char Min, Max; };
@ -44,7 +46,58 @@ template <> struct NumLimit< float > { static const float Min, Max; };
template <> struct NumLimit< double > { static const double Min, Max; };
template <> struct NumLimit< long double > { static const long double Min, Max; };
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Implements RAII to restore the VM stack to it's initial size on function exit.
*/
struct StackGuard
{
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackGuard(HSQUIRRELVM vm)
: m_Top(sq_gettop(vm)), m_VM(vm)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackGuard(const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
StackGuard(StackGuard &&) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackGuard()
{
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
StackGuard & operator = (const StackGuard &) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
StackGuard & operator = (StackGuard &&) = delete;
private:
// --------------------------------------------------------------------------------------------
Int32 m_Top; /* The top of the stack when this instance was created. */
HSQUIRRELVM m_VM; /* The VM where the stack should be restored. */
};
/* ------------------------------------------------------------------------------------------------
* Perform an equality comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsEq(const T a, const T b)
{
return abs(a - b) <= 0;
@ -60,7 +113,9 @@ template <> inline bool EpsEq(const Float64 a, const Float64 b)
return fabs(a - b) <= 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsLt(const T a, const T b)
{
return !EpsEq(a, b) && (a < b);
@ -76,7 +131,9 @@ template <> inline bool EpsLt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than comparison between two values taking into account floating point issues.
*/
template< typename T > inline bool EpsGt(const T a, const T b)
{
return !EpsEq(a, b) && (a > b);
@ -92,7 +149,10 @@ template <> inline bool EpsGt(const Float64 a, const Float64 b)
return !EpsEq(a, b) && (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a less than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsLtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a < b);
@ -108,7 +168,10 @@ template <> inline bool EpsLtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Perform a greater than or equal comparison between two values taking into account
* floating point issues.
*/
template< typename T > inline bool EpsGtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a > b);
@ -124,7 +187,9 @@ template <> inline bool EpsGtEq(const Float64 a, const Float64 b)
return !EpsEq(a, b) || (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Force a value to be within a certain range.
*/
template< typename T > inline T Clamp(T val, T min, T max)
{
return val < min ? min : (val > max ? max : val);
@ -144,6 +209,142 @@ inline Uint32 NextPow2(Uint32 num)
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script object.
*/
Object & NullObject();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null/empty script array.
*/
Array & NullArray();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script function.
*/
Function & NullFunction();
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the default VM.
*/
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Create a script object from the specified value on the specified VM.
*/
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string and throw it as a sqrat exception.
*/
void SqThrowF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Quickly generate a formatted string on a small static buffer without any memory allocations.
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string on a temporary buffer and return the string but not the buffer.
*/
CSStr ToStringF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Obtain a randomly chosen color from a list of known colors.
*/
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColor(CSStr name);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.
*/
const AABB & GetAABB(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.
*/
const Circle & GetCircle(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.
*/
const Color3 & GetColor3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.
*/
const Color4 & GetColor4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.
*/
const Quaternion & GetQuaternion(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.
*/
const Sphere & GetSphere(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.
*/
const Vector2 & GetVector2(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.
*/
const Vector2i & GetVector2i(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.
*/
const Vector3 & GetVector3(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.
*/
const Vector4 & GetVector4(CSStr str, SQChar delim);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
*/
// ------------------------------------------------------------------------------------------------
void LogDbg(CCStr fmt, ...);
void LogUsr(CCStr fmt, ...);
@ -180,84 +381,6 @@ bool cLogSWrn(bool cond, CCStr fmt, ...);
bool cLogSErr(bool cond, CCStr fmt, ...);
bool cLogSFtl(bool cond, CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
void SqThrow(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
// ------------------------------------------------------------------------------------------------
Object & NullObject();
// ------------------------------------------------------------------------------------------------
Array & NullArray();
// ------------------------------------------------------------------------------------------------
Function & NullFunction();
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(const T & v)
{
PushVar< T >(DefaultVM::Get(), v);
Var< Object > var(DefaultVM::Get(), -1);
sq_pop(DefaultVM::Get(), 1);
return var.value;
}
// ------------------------------------------------------------------------------------------------
template < typename T > Object MakeObject(HSQUIRRELVM vm, const T & v)
{
PushVar< T >(vm, v);
Var< Object > var(vm, -1);
sq_pop(vm, 1);
return var.value;
}
/* ------------------------------------------------------------------------------------------------
* Simple function to check whether the specified string can be considered as a boolean value
*/
bool SToB(CSStr str);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStrF(CCStr fmt, ...);
/* ------------------------------------------------------------------------------------------------
*
*/
CSStr ToStringF(CCStr fmt, ...);
// ------------------------------------------------------------------------------------------------
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Value extractors.
*/
Color3 GetColor(CSStr name);
AABB GetAABB(CSStr str, SQChar delim);
Circle GetCircle(CSStr str, SQChar delim);
Color3 GetColor3(CSStr str, SQChar delim);
Color4 GetColor4(CSStr str, SQChar delim);
Quaternion GetQuaternion(CSStr str, SQChar delim);
Sphere GetSphere(CSStr str, SQChar delim);
Vector2 GetVector2(CSStr str, SQChar delim);
Vector2i GetVector2i(CSStr str, SQChar delim);
Vector3 GetVector3(CSStr str, SQChar delim);
Vector4 GetVector4(CSStr str, SQChar delim);
} // Namespace:: SqMod
#endif // _BASE_SHARED_HPP_

View File

@ -14,6 +14,14 @@ const Sphere Sphere::MAX = Sphere(NumLimit< Sphere::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Sphere::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Sphere::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Sphere");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere()
: pos(0.0), rad(0.0)
@ -42,27 +50,6 @@ Sphere::Sphere(Value xv, Value yv, Value zv, Value rv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(const Sphere & o)
: pos(o.pos), rad(o.rad)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::~Sphere()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (const Sphere & o)
{
pos = o.pos;
rad = o.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (Value r)
{
@ -341,7 +328,7 @@ Int32 Sphere::Cmp(const Sphere & o) const
// ------------------------------------------------------------------------------------------------
CSStr Sphere::ToString() const
{
return ToStringF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
return ToStrF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
}
// ------------------------------------------------------------------------------------------------
@ -395,35 +382,28 @@ void Sphere::Generate()
void Sphere::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
SqThrowF("max value is lower than min value");
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
}
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax)
{
if (EpsLt(rmax, rmin))
{
SqThrow("max value is lower than min value");
}
else
{
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
rad = GetRandomFloat32(rmin, rmax);
}
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(rmax, rmin))
SqThrowF("max value is lower than min value");
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
@ -452,6 +432,7 @@ void Register_Sphere(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Sphere::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Sphere::ToString)
.SquirrelFunc(_SC("_typename"), &Sphere::Typename)
.Func(_SC("_cmp"), &Sphere::Cmp)
/* Metamethods */
.Func<Sphere (Sphere::*)(const Sphere &) const>(_SC("_add"), &Sphere::operator +)

View File

@ -9,355 +9,370 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a three-dimensional sphere.
*/
struct Sphere
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Sphere NIL;
static const Sphere MIN;
static const Sphere MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The position and radius components of this type.
*/
Vector3 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Sphere();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at position 0,0,0 using the specified radius.
*/
Sphere(Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(const Vector3 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(Value xv, Value yv, Value zv, Value rv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Sphere(const Sphere & o);
Sphere(const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Sphere();
Sphere(Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Sphere & operator = (const Sphere & o);
~Sphere() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Sphere & operator = (const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Sphere & operator = (Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Sphere & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position assignment operator.
*/
Sphere & operator = (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Sphere & operator += (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Sphere & operator -= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Sphere & operator *= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Sphere & operator /= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Sphere & operator %= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition assignment operator.
*/
Sphere & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction assignment operator.
*/
Sphere & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication assignment operator.
*/
Sphere & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius division assignment operator.
*/
Sphere & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo assignment operator.
*/
Sphere & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* ...
* Position addition assignment operator.
*/
Sphere & operator += (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction assignment operator.
*/
Sphere & operator -= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication assignment operator.
*/
Sphere & operator *= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position division assignment operator.
*/
Sphere & operator /= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo assignment operator.
*/
Sphere & operator %= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Sphere & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Sphere & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Sphere operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Sphere operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Sphere operator + (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Sphere operator - (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Sphere operator * (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Sphere operator / (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Sphere operator % (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius addition operator.
*/
Sphere operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius subtraction operator.
*/
Sphere operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius multiplication operator.
*/
Sphere operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius division operator.
*/
Sphere operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Radius modulo operator.
*/
Sphere operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position addition operator.
*/
Sphere operator + (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position subtraction operator.
*/
Sphere operator - (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position multiplication operator.
*/
Sphere operator * (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position division operator.
*/
Sphere operator / (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Position modulo operator.
*/
Sphere operator % (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Sphere operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Sphere operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void Set(Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the sphere from another instance of this type.
*/
void Set(const Sphere & ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set the position from the specified position.
*/
void Set(const Vector3 & np);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(const Vector3 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set the specified position and radius.
*/
void Set(Value nx, Value ny, Value nz, Value nr);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned sphere.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized or positioned sphere within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Generate a randomly sized and positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -365,7 +380,7 @@ struct Sphere
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Sphere Abs() const;
};

View File

@ -15,6 +15,14 @@ const Vector2 Vector2::MAX = Vector2(NumLimit< Vector2::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector2::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector2::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector2");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2()
: x(0.0), y(0.0)
@ -36,27 +44,6 @@ Vector2::Vector2(Value xv, Value yv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2(const Vector2 & o)
: x(o.x), y(o.y)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::~Vector2()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (const Vector2 & o)
{
x = o.x;
y = o.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (Value s)
{
@ -290,7 +277,7 @@ Int32 Vector2::Cmp(const Vector2 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector2::ToString() const
{
return ToStringF("%f,%f", x, y);
return ToStrF("%f,%f", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -335,27 +322,19 @@ void Vector2::Generate()
void Vector2::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Vector2::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(ymin, ymax);
y = GetRandomFloat32(xmin, xmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(ymin, ymax);
y = GetRandomFloat32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
@ -383,6 +362,7 @@ void Register_Vector2(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector2::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector2::ToString)
.SquirrelFunc(_SC("_typename"), &Vector2::Typename)
.Func(_SC("_cmp"), &Vector2::Cmp)
/* Metamethods */
.Func<Vector2 (Vector2::*)(const Vector2 &) const>(_SC("_add"), &Vector2::operator +)

View File

@ -8,289 +8,304 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional vector.
*/
struct Vector2
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2 NIL;
static const Vector2 MIN;
static const Vector2 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector2();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector2(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector2(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector2(const Vector2 & o);
Vector2(const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Vector2();
Vector2(Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector2 & operator = (const Vector2 & o);
~Vector2() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector2 & operator = (const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2 & operator = (Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* String assignment operator.
*/
Vector2 & operator = (CSStr values);
/* --------------------------------------------------------------------------------------------
* ...
* Integral two-dimensional vector assignment.
*/
Vector2 & operator = (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector2 & operator += (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector2 & operator -= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector2 & operator *= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector2 & operator /= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector2 & operator %= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector2 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector2 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector2 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector2 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector2 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector2 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector2 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector2 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector2 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector2 operator + (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector2 operator - (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector2 operator * (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector2 operator / (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector2 operator % (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector2 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector2 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector2 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector2 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector2 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector2 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector2 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from an integral two-dimensional vector.
*/
void Set(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -298,7 +313,7 @@ struct Vector2
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector2 Abs() const;
};

View File

@ -15,6 +15,14 @@ const Vector2i Vector2i::MAX = Vector2i(NumLimit< Vector2i::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector2i::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector2i::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector2i");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i()
: x(0), y(0)
@ -36,27 +44,6 @@ Vector2i::Vector2i(Value xv, Value yv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i(const Vector2i & o)
: x(o.x), y(o.y)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::~Vector2i()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (const Vector2i & o)
{
x = o.x;
y = o.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (Value s)
{
@ -416,7 +403,7 @@ Int32 Vector2i::Cmp(const Vector2i & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector2i::ToString() const
{
return ToStringF("%d,%d", x, y);
return ToStrF("%d,%d", x, y);
}
// ------------------------------------------------------------------------------------------------
@ -461,27 +448,19 @@ void Vector2i::Generate()
void Vector2i::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomInt32(min, max);
y = GetRandomInt32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomInt32(min, max);
y = GetRandomInt32(min, max);
}
void Vector2i::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (xmax < xmin || ymax < ymin)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomInt32(ymin, ymax);
y = GetRandomInt32(xmin, xmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomInt32(ymin, ymax);
y = GetRandomInt32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
@ -509,6 +488,7 @@ void Register_Vector2i(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector2i::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector2i::ToString)
.SquirrelFunc(_SC("_typename"), &Vector2i::Typename)
.Func(_SC("_cmp"), &Vector2i::Cmp)
/* Metamethods */
.Func<Vector2i (Vector2i::*)(const Vector2i &) const>(_SC("_add"), &Vector2i::operator +)

View File

@ -8,394 +8,409 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a two-dimensional vector using integral values.
*/
struct Vector2i
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef int Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2i NIL;
static const Vector2i MIN;
static const Vector2i MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector2i();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector2i(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector2i(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector2i(const Vector2i & o);
Vector2i(const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
~Vector2i();
Vector2i(Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector2i & operator = (const Vector2i & o);
~Vector2i() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector2i & operator = (const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2i & operator = (Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2i & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* String assignment operator.
*/
Vector2i & operator = (CSStr values);
/* --------------------------------------------------------------------------------------------
* ...
* Real two-dimensional vector assignment.
*/
Vector2i & operator = (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector2i & operator += (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector2i & operator -= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector2i & operator *= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector2i & operator /= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector2i & operator %= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND assignment operator.
*/
Vector2i & operator &= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR assignment operator.
*/
Vector2i & operator |= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR assignment operator.
*/
Vector2i & operator ^= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise left shift assignment operator.
*/
Vector2i & operator <<= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise right shift assignment operator.
*/
Vector2i & operator >>= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector2i & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector2i & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector2i & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector2i & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector2i & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND assignment operator.
*/
Vector2i & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR assignment operator.
*/
Vector2i & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR assignment operator.
*/
Vector2i & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise left shift assignment operator.
*/
Vector2i & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise right shift assignment operator.
*/
Vector2i & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector2i & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector2i & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector2i operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector2i operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector2i operator + (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector2i operator - (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector2i operator * (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector2i operator / (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector2i operator % (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise AND operator.
*/
Vector2i operator & (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise OR operator.
*/
Vector2i operator | (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise XOR operator.
*/
Vector2i operator ^ (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift left operator.
*/
Vector2i operator << (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise shift right operator.
*/
Vector2i operator >> (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector2i operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector2i operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector2i operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector2i operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector2i operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise AND operator.
*/
Vector2i operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise OR operator.
*/
Vector2i operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise XOR operator.
*/
Vector2i operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift left operator.
*/
Vector2i operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value bitwise shift right operator.
*/
Vector2i operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector2i operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector2i operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Bitwise NOT operator.
*/
Vector2i operator ~ () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a real two-dimensional vector.
*/
void Set(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -403,7 +418,7 @@ struct Vector2i
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector2i Abs() const;
};

View File

@ -16,6 +16,14 @@ const Vector3 Vector3::MAX = Vector3(NumLimit< Vector3::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector3::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector3::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector3");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3()
: x(0.0), y(0.0), z(0.0)
@ -37,28 +45,6 @@ Vector3::Vector3(Value xv, Value yv, Value zv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3(const Vector3 & o)
: x(o.x), y(o.y), z(o.z)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::~Vector3()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (const Vector3 & o)
{
x = o.x;
y = o.y;
z = o.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (Value s)
{
@ -310,7 +296,7 @@ Int32 Vector3::Cmp(const Vector3 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector3::ToString() const
{
return ToStringF("%f,%f,%f", x, y, z);
return ToStrF("%f,%f,%f", x, y, z);
}
// ------------------------------------------------------------------------------------------------
@ -367,29 +353,21 @@ void Vector3::Generate()
void Vector3::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
}
void Vector3::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
}
// ------------------------------------------------------------------------------------------------
@ -418,6 +396,7 @@ void Register_Vector3(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector3::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector3::ToString)
.SquirrelFunc(_SC("_typename"), &Vector3::Typename)
.Func(_SC("_cmp"), &Vector3::Cmp)
/* Metamethods */
.Func<Vector3 (Vector3::*)(const Vector3 &) const>(_SC("_add"), &Vector3::operator +)

View File

@ -8,294 +8,309 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a three-dimensional vector.
*/
struct Vector3
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector3 NIL;
static const Vector3 MIN;
static const Vector3 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y and z components of this type.
*/
Value x, y, z;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector3();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector3(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector3(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector3(const Vector3 & o);
Vector3(const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
~Vector3();
Vector3(Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector3 & operator = (const Vector3 & o);
~Vector3() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector3 & operator = (const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector3 & operator = (Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Four-dimensional vector assignment.
*/
Vector3 & operator = (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Quaternion rotation assignment.
*/
Vector3 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector3 & operator += (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector3 & operator -= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector3 & operator *= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector3 & operator /= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector3 & operator %= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector3 operator + (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector3 operator - (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector3 operator * (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector3 operator / (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector3 operator % (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector3 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector3 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a four-dimensional vector.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a quaternion rotation.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -303,7 +318,7 @@ struct Vector3
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector3 Abs() const;
};

View File

@ -16,6 +16,14 @@ const Vector4 Vector4::MAX = Vector4(NumLimit< Vector4::Value >::Max);
// ------------------------------------------------------------------------------------------------
SQChar Vector4::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Vector4::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("Vector4");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4()
: x(0.0), y(0.0), z(0.0), w(0.0)
@ -44,29 +52,6 @@ Vector4::Vector4(Value xv, Value yv, Value zv, Value wv)
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(const Vector4 & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::~Vector4()
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (const Vector4 & o)
{
x = o.x;
y = o.y;
z = o.z;
w = o.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (Value s)
{
@ -335,7 +320,7 @@ Int32 Vector4::Cmp(const Vector4 & o) const
// ------------------------------------------------------------------------------------------------
CSStr Vector4::ToString() const
{
return ToStringF("%f,%f,%f,%f", x, y, z, w);
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
@ -405,31 +390,23 @@ void Vector4::Generate()
void Vector4::Generate(Value min, Value max)
{
if (max < min)
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
void Vector4::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(wmax, wmin))
{
SqThrow("max value is lower than min value");
}
else
{
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
SqThrowF("max value is lower than min value");
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
}
// ------------------------------------------------------------------------------------------------
@ -460,6 +437,7 @@ void Register_Vector4(HSQUIRRELVM vm)
.Prop(_SC("abs"), &Vector4::Abs)
/* Core Metamethods */
.Func(_SC("_tostring"), &Vector4::ToString)
.SquirrelFunc(_SC("_typename"), &Vector4::Typename)
.Func(_SC("_cmp"), &Vector4::Cmp)
/* Metamethods */
.Func<Vector4 (Vector4::*)(const Vector4 &) const>(_SC("_add"), &Vector4::operator +)

View File

@ -8,304 +8,319 @@
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
* Class used to represent a four-dimensional vector.
*/
struct Vector4
{
/* --------------------------------------------------------------------------------------------
* ...
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* ...
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector4 NIL;
static const Vector4 MIN;
static const Vector4 MAX;
/* --------------------------------------------------------------------------------------------
* ...
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* ...
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
*
* Default constructor.
*/
Vector4();
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the same scalar value for all components.
*/
Vector4(Value sv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* ...
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
*
* Copy constructor.
*/
Vector4(const Vector4 & o);
Vector4(const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
*
* Move constructor.
*/
~Vector4();
Vector4(Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
*
* Destructor.
*/
Vector4 & operator = (const Vector4 & o);
~Vector4() = default;
/* --------------------------------------------------------------------------------------------
* ...
* Copy assignment operator.
*/
Vector4 & operator = (const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector4 & operator = (Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Three-dimensional vector assignment operator.
*/
Vector4 & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Quaternion rotation assignment operator.
*/
Vector4 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Addition assignment operator.
*/
Vector4 & operator += (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction assignment operator.
*/
Vector4 & operator -= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication assignment operator.
*/
Vector4 & operator *= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Division assignment operator.
*/
Vector4 & operator /= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Modulo assignment operator.
*/
Vector4 & operator %= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition assignment operator.
*/
Vector4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction assignment operator.
*/
Vector4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication assignment operator.
*/
Vector4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division assignment operator.
*/
Vector4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo assignment operator.
*/
Vector4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* ...
* Pre-increment operator.
*/
Vector4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* ...
* Pre-decrement operator.
*/
Vector4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* ...
* Post-increment operator.
*/
Vector4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* ...
* Post-decrement operator.
*/
Vector4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* ...
* Addition operator.
*/
Vector4 operator + (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Subtraction operator.
*/
Vector4 operator - (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Multiplication operator.
*/
Vector4 operator * (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Division operator.
*/
Vector4 operator / (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Modulo operator.
*/
Vector4 operator % (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value addition operator.
*/
Vector4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value subtraction operator.
*/
Vector4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value multiplication operator.
*/
Vector4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value division operator.
*/
Vector4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Scalar value modulo operator.
*/
Vector4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary plus operator.
*/
Vector4 operator + () const;
/* --------------------------------------------------------------------------------------------
* ...
* Unary minus operator.
*/
Vector4 operator - () const;
/* --------------------------------------------------------------------------------------------
* ...
* Equality comparison operator.
*/
bool operator == (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Inequality comparison operator.
*/
bool operator != (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than comparison operator.
*/
bool operator < (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than comparison operator.
*/
bool operator > (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Less than or equal comparison operator.
*/
bool operator <= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* ...
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void Set(Value ns);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* ...
* Set all components to the specified values.
*/
void Set(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from another instance of this type.
*/
void Set(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a three-dimensional vector.
*/
void Set(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* ...
* Copy the values from a quaternion rotation.
*/
void Set(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* ...
* Set the values extracted from the specified string using the specified delimiter.
*/
void Set(CSStr values, SQChar delim);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* ...
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* ...
* Clear the component values to default.
*/
void Clear()
{
@ -313,7 +328,7 @@ struct Vector4
}
/* --------------------------------------------------------------------------------------------
* ...
* Retrieve a new instance of this type with absolute component values.
*/
Vector4 Abs() const;
};