metaforce/Runtime/RetroTypes.hpp

182 lines
4.5 KiB
C++
Raw Normal View History

2016-04-12 23:07:23 -07:00
#ifndef __URDE_TYPES_HPP__
#define __URDE_TYPES_HPP__
2015-08-17 13:33:58 -07:00
2015-08-19 19:52:07 -07:00
#include <vector>
2015-08-17 22:54:43 -07:00
#include <utility>
2015-08-21 18:58:41 -07:00
#include <string>
2015-08-17 13:33:58 -07:00
#include "GCNTypes.hpp"
2015-08-21 18:58:41 -07:00
#include "rstl.hpp"
#include "DataSpec/DNACommon/DNACommon.hpp"
2016-04-11 00:10:28 -07:00
#include "IOStreams.hpp"
2015-08-17 13:33:58 -07:00
2016-03-04 15:04:53 -08:00
namespace urde
{
2016-03-04 15:04:53 -08:00
using FourCC = hecl::FourCC;
2017-08-12 22:26:14 -07:00
class CAssetId
{
private:
u64 id = UINT64_MAX;
public:
CAssetId() = default;
CAssetId(u64 v) { Assign(v); }
explicit CAssetId(CInputStream& in);
bool IsValid() const { return id != UINT64_MAX; }
u64 Value() const { return id; }
void Assign(u64 v) { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); }
void Reset() { id = UINT64_MAX; }
void PutTo(COutputStream& out);
bool operator==(const CAssetId& other) const { return id == other.id; }
bool operator!=(const CAssetId& other) const { return id != other.id; }
bool operator<(const CAssetId& other) const { return id < other.id; }
};
//#define kInvalidAssetId CAssetId()
2015-08-22 23:42:29 -07:00
2015-08-21 18:58:41 -07:00
struct SObjectTag
{
FourCC type;
2017-08-12 22:26:14 -07:00
CAssetId id;
operator bool() const { return id.IsValid(); }
2016-09-09 21:50:00 -07:00
bool operator!=(const SObjectTag& other) const { return id != other.id; }
bool operator==(const SObjectTag& other) const { return id == other.id; }
2017-02-07 22:48:43 -08:00
bool operator<(const SObjectTag& other) const { return id < other.id; }
2016-02-16 19:42:27 -08:00
SObjectTag() = default;
2017-08-12 22:26:14 -07:00
SObjectTag(FourCC tp, CAssetId rid) : type(tp), id(rid) {}
SObjectTag(CInputStream& in)
2016-04-11 00:10:28 -07:00
{
in.readBytesToBuf(&type, 4);
id = CAssetId(in);
2016-04-11 00:10:28 -07:00
}
void readMLVL(CInputStream& in)
2016-04-18 17:17:49 -07:00
{
id = CAssetId(in);
2016-04-18 17:17:49 -07:00
in.readBytesToBuf(&type, 4);
}
2015-08-21 18:58:41 -07:00
};
struct TEditorId
{
TEditorId() = default;
TEditorId(u32 idin) : id(idin) {}
2017-07-30 04:00:30 -07:00
u32 id = u32(-1);
u8 LayerNum() const { return u8((id >> 26) & 0x3f); }
u16 AreaNum() const { return u16((id >> 16) & 0x3ff); }
u16 Id() const { return u16(id & 0xffff); }
bool operator<(const TEditorId& other) const { return (id & 0x3ffffff) < (other.id & 0x3ffffff); }
bool operator!=(const TEditorId& other) const { return (id & 0x3ffffff) != (other.id & 0x3ffffff); }
bool operator==(const TEditorId& other) const { return (id & 0x3ffffff) == (other.id & 0x3ffffff); }
};
2017-08-12 22:26:14 -07:00
#define kInvalidEditorId TEditorId()
struct TUniqueId
{
TUniqueId() = default;
TUniqueId(u16 value, u16 version) : id(value | (version << 10)) {}
u16 id = u16(-1);
2017-08-24 23:18:09 -07:00
u16 Version() const { return u16((id >> 10) & 0x3f);}
u16 Value() const { return u16(id & 0x3ff);}
bool operator<(const TUniqueId& other) const { return (id < other.id); }
bool operator!=(const TUniqueId& other) const { return (id != other.id); }
bool operator==(const TUniqueId& other) const { return (id == other.id); }
};
2017-08-12 22:26:14 -07:00
#define kInvalidUniqueId TUniqueId()
2017-01-14 19:59:37 -08:00
using TAreaId = s32;
2015-08-19 19:52:07 -07:00
#define kInvalidAreaId TAreaId(-1)
}
2016-09-09 21:50:00 -07:00
#if 0
template <class T, size_t N>
class TRoundRobin
{
rstl::reserved_vector<T, N> vals;
public:
TRoundRobin(const T& val) : vals(N, val) {}
void PushBack(const T& val) { vals.push_back(val); }
size_t Size() const { return vals.size(); }
const T& GetLastValue() const { return vals.back(); }
void Clear() { vals.clear(); }
const T& GetValue(s32) const {}
};
#endif
template <class T>
T GetAverage(const T* v, s32 count)
{
T r = v[0];
for (s32 i = 1; i < count; ++i)
r += v[i];
return r / count;
}
2016-09-16 15:21:19 -07:00
template <class T, size_t N>
class TReservedAverage : rstl::reserved_vector<T, N>
2016-09-16 15:21:19 -07:00
{
public:
TReservedAverage() = default;
2017-09-11 22:51:17 -07:00
TReservedAverage(const T& t) { rstl::reserved_vector<T, N>::resize(N, t); }
2016-09-16 15:21:19 -07:00
void AddValue(const T& t)
2016-09-16 15:21:19 -07:00
{
if (this->size() < N)
2017-07-23 16:45:04 -07:00
{
2017-08-15 22:34:02 -07:00
this->insert(this->begin(), t);
2017-07-23 16:45:04 -07:00
}
else
{
this->pop_back();
this->insert(this->begin(), t);
}
}
2016-09-16 15:21:19 -07:00
rstl::optional_object<T> GetAverage() const
{
if (this->empty())
return {};
2017-01-14 19:59:37 -08:00
return {::GetAverage<T>(this->data(), this->size())};
2016-09-16 15:21:19 -07:00
}
2017-07-30 22:19:05 -07:00
rstl::optional_object<T> GetEntry(int i) const
{
if (i >= this->size())
return {};
return this->operator[](i);
}
void Clear() { this->clear(); }
2017-09-29 20:45:57 -07:00
size_t Size() const { return this->size(); }
};
2016-09-16 15:21:19 -07:00
2015-08-22 23:42:29 -07:00
namespace std
{
2016-09-09 21:50:00 -07:00
template <>
2016-03-04 15:04:53 -08:00
struct hash<urde::SObjectTag>
2015-08-22 23:42:29 -07:00
{
2017-08-12 22:26:14 -07:00
inline size_t operator()(const urde::SObjectTag& tag) const { return tag.id.Value(); }
};
template <>
struct hash<urde::CAssetId>
{
inline size_t operator()(const urde::CAssetId& id) const { return id.Value(); }
2015-08-22 23:42:29 -07:00
};
}
2016-04-12 23:07:23 -07:00
#endif // __URDE_TYPES_HPP__