metaforce/Runtime/RetroTypes.hpp

128 lines
2.9 KiB
C++
Raw Normal View History

2016-04-13 06:07:23 +00:00
#ifndef __URDE_TYPES_HPP__
#define __URDE_TYPES_HPP__
2015-08-17 20:33:58 +00:00
2015-08-20 02:52:07 +00:00
#include <vector>
2015-08-18 05:54:43 +00:00
#include <utility>
2015-08-22 01:58:41 +00:00
#include <string>
2015-08-17 20:33:58 +00:00
#include "GCNTypes.hpp"
2015-08-22 01:58:41 +00:00
#include "rstl.hpp"
#include "DataSpec/DNACommon/DNACommon.hpp"
2016-04-11 07:10:28 +00:00
#include "IOStreams.hpp"
2015-08-17 20:33:58 +00:00
2016-03-04 23:04:53 +00:00
namespace urde
{
2016-03-04 23:04:53 +00:00
using FourCC = hecl::FourCC;
2017-01-27 02:22:52 +00:00
using ResId = u64;
2015-08-23 06:42:29 +00:00
2015-08-22 01:58:41 +00:00
struct SObjectTag
{
FourCC type;
2016-04-14 21:42:47 +00:00
ResId id = -1;
operator bool() const { return (id & 0xffffffff) != 0xffffffff; }
2016-09-10 04:50:00 +00:00
bool operator!=(const SObjectTag& other) const { return id != other.id; }
bool operator==(const SObjectTag& other) const { return id == other.id; }
2017-02-08 06:48:43 +00:00
bool operator<(const SObjectTag& other) const { return id < other.id; }
2016-02-17 03:42:27 +00:00
SObjectTag() = default;
2016-04-14 21:42:47 +00:00
SObjectTag(FourCC tp, ResId rid) : type(tp), id(rid) {}
2016-04-11 07:10:28 +00:00
SObjectTag(CInputStream& in)
{
in.readBytesToBuf(&type, 4);
id = in.readUint32Big();
}
2016-04-19 00:17:49 +00:00
void readMLVL(CInputStream& in)
{
id = in.readUint32Big();
in.readBytesToBuf(&type, 4);
}
2015-08-22 01:58:41 +00:00
};
struct TEditorId
{
TEditorId() = default;
TEditorId(u32 idin) : id(idin) {}
u32 id = -1;
u8 LayerNum() const { return (id >> 26) & 0x3f; }
u16 AreaNum() const { return (id >> 16) & 0x3ff; }
2017-01-15 03:59:37 +00:00
u16 Id() const { return 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-01-15 03:59:37 +00:00
using TUniqueId = s16;
using TAreaId = s32;
#define kInvalidEditorId TEditorId()
2015-08-20 02:52:07 +00:00
#define kInvalidUniqueId TUniqueId(-1)
#define kInvalidAreaId TAreaId(-1)
2017-07-07 12:23:20 +00:00
#define kInvalidResId ResId(-1)
2015-08-20 02:52:07 +00:00
}
2016-09-10 04:50:00 +00: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 22:21:19 +00:00
template <class T, size_t N>
class TReservedAverage : rstl::reserved_vector<T, N>
2016-09-16 22:21:19 +00:00
{
public:
TReservedAverage() = default;
TReservedAverage(const T& t) { resize(N, t); }
2016-09-16 22:21:19 +00:00
void AddValue(const T& t)
2016-09-16 22:21:19 +00:00
{
if (this->size() < N)
this->push_back(t);
}
2016-09-16 22:21:19 +00:00
rstl::optional_object<T> GetAverage() const
{
if (this->empty())
return {};
2017-01-15 03:59:37 +00:00
return {::GetAverage<T>(this->data(), this->size())};
2016-09-16 22:21:19 +00:00
}
void Clear() { this->clear(); }
};
2016-09-16 22:21:19 +00:00
2015-08-23 06:42:29 +00:00
namespace std
{
2016-09-10 04:50:00 +00:00
template <>
2016-03-04 23:04:53 +00:00
struct hash<urde::SObjectTag>
2015-08-23 06:42:29 +00:00
{
2016-09-10 04:50:00 +00:00
inline size_t operator()(const urde::SObjectTag& tag) const { return tag.id; }
2015-08-23 06:42:29 +00:00
};
}
2016-04-13 06:07:23 +00:00
#endif // __URDE_TYPES_HPP__