metaforce/Runtime/RetroTypes.hpp

174 lines
4.3 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
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>
#include <functional>
2015-08-17 20:33:58 +00:00
#include "GCNTypes.hpp"
2015-08-22 01:58:41 +00:00
#include "rstl.hpp"
2016-04-11 07:10:28 +00:00
#include "IOStreams.hpp"
#include "hecl/hecl.hpp"
2015-08-17 20:33:58 +00:00
2017-12-30 01:09:45 +00:00
#undef min
#undef max
2017-11-24 08:23:28 +00:00
using namespace std::literals;
2018-12-08 05:30:43 +00:00
namespace urde {
2016-03-04 23:04:53 +00:00
using FourCC = hecl::FourCC;
2017-08-13 05:26:14 +00:00
2018-12-08 05:30:43 +00:00
class CAssetId {
u64 id = UINT64_MAX;
2017-08-13 05:26:14 +00:00
public:
2018-12-08 05:30:43 +00:00
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; }
2017-08-13 05:26:14 +00:00
};
//#define kInvalidAssetId CAssetId()
2015-08-23 06:42:29 +00:00
2018-12-08 05:30:43 +00:00
struct SObjectTag {
FourCC type;
CAssetId id;
operator bool() const { return id.IsValid(); }
bool operator!=(const SObjectTag& other) const { return id != other.id; }
bool operator==(const SObjectTag& other) const { return id == other.id; }
bool operator<(const SObjectTag& other) const { return id < other.id; }
SObjectTag() = default;
SObjectTag(FourCC tp, CAssetId rid) : type(tp), id(rid) {}
SObjectTag(CInputStream& in) {
in.readBytesToBuf(&type, 4);
id = CAssetId(in);
}
void readMLVL(CInputStream& in) {
id = CAssetId(in);
in.readBytesToBuf(&type, 4);
}
2015-08-22 01:58:41 +00:00
};
2018-12-08 05:30:43 +00:00
struct TEditorId {
TEditorId() = default;
TEditorId(u32 idin) : id(idin) {}
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-13 05:26:14 +00:00
#define kInvalidEditorId TEditorId()
2018-12-08 05:30:43 +00:00
struct TUniqueId {
TUniqueId() = default;
TUniqueId(u16 value, u16 version) : id(value | (version << 10)) {}
u16 id = u16(-1);
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-13 05:26:14 +00:00
#define kInvalidUniqueId TUniqueId()
2017-01-15 03:59:37 +00:00
using TAreaId = s32;
2015-08-20 02:52:07 +00:00
#define kInvalidAreaId TAreaId(-1)
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
2018-12-08 05:30:43 +00:00
template <class T>
T GetAverage(const T* v, s32 count) {
T r = v[0];
for (s32 i = 1; i < count; ++i)
r += v[i];
2018-12-08 05:30:43 +00:00
return r / count;
}
2018-12-08 05:30:43 +00:00
template <class T, size_t N>
class TReservedAverage : rstl::reserved_vector<T, N> {
2016-09-16 22:21:19 +00:00
public:
2018-12-08 05:30:43 +00:00
TReservedAverage() = default;
2016-09-16 22:21:19 +00:00
2018-12-08 05:30:43 +00:00
TReservedAverage(const T& t) { rstl::reserved_vector<T, N>::resize(N, t); }
2018-12-08 05:30:43 +00:00
void AddValue(const T& t) {
if (this->size() < N) {
this->insert(this->begin(), t);
} else {
this->pop_back();
this->insert(this->begin(), t);
2016-09-16 22:21:19 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-09-16 22:21:19 +00:00
2019-01-29 08:28:35 +00:00
rstl::optional<T> GetAverage() const {
2018-12-08 05:30:43 +00:00
if (this->empty())
return {};
return {urde::GetAverage<T>(this->data(), this->size())};
}
2017-07-31 05:19:05 +00:00
2019-01-29 08:28:35 +00:00
rstl::optional<T> GetEntry(int i) const {
2018-12-08 05:30:43 +00:00
if (i >= this->size())
return {};
return this->operator[](i);
}
2017-09-30 03:45:57 +00:00
2018-12-08 05:30:43 +00:00
void Clear() { this->clear(); }
size_t Size() const { return this->size(); }
};
2016-09-16 22:21:19 +00:00
2018-12-08 05:30:43 +00:00
} // namespace urde
2018-12-08 05:30:43 +00:00
namespace std {
2016-09-10 04:50:00 +00:00
template <>
2018-12-08 05:30:43 +00:00
struct hash<urde::SObjectTag> {
size_t operator()(const urde::SObjectTag& tag) const noexcept { return tag.id.Value(); }
2017-08-13 05:26:14 +00:00
};
template <>
2018-12-08 05:30:43 +00:00
struct hash<urde::CAssetId> {
size_t operator()(const urde::CAssetId& id) const noexcept { return id.Value(); }
2015-08-23 06:42:29 +00:00
};
2018-12-08 05:30:43 +00:00
} // namespace std
2019-04-07 05:14:48 +00:00
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define URDE_MSAN 1
#endif
#endif