From 5424a1d5434e3ddd911a504719918c2df027e2fd Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 17 Apr 2022 20:08:57 -0700 Subject: Changeset: 8 Initial work on sprites and texture system --- source/Enum.hpp | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 source/Enum.hpp (limited to 'source/Enum.hpp') diff --git a/source/Enum.hpp b/source/Enum.hpp new file mode 100644 index 0000000..5e106fe --- /dev/null +++ b/source/Enum.hpp @@ -0,0 +1,103 @@ +#pragma once + +#include +#include + +template +class EnumFlags { +public: + using Enum = TEnum; + using Underlying = std::underlying_type_t; + +private: + Underlying mValue; + +public: + EnumFlags() + : mValue{ 0 } { + } + + EnumFlags(TEnum e) + : mValue{ 1 << static_cast(e) } { + } + + bool IsSet(EnumFlags mask) const { + return (mValue & mask.mValue) == mask.mValue; + } + + bool IsSet(std::initializer_list enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast(e); + } + return IsSet(flags); + } + + bool IsSetExclusive(EnumFlags mask) const { + return mValue == mask.mValue; + } + + bool IsSetExclusive(std::initializer_list enums) { + EnumFlags flags; + for (auto& e : enums) { + flags.mValue |= static_cast(e); + } + return IsSetExclusive(flags); + } + + void SetOn(EnumFlags mask) { + mValue |= mask.mValue; + } + + void SetOff(EnumFlags mask) { + mValue &= ~mask.mValue; + } + + void Set(EnumFlags mask, bool enabled) { + if (enabled) { + SetOn(mask); + } else { + SetOff(mask); + } + } + + EnumFlags& operator|=(EnumFlags that) const { + mValue |= that.mValue; + return *this; + } + + EnumFlags& operator&=(EnumFlags that) const { + mValue &= that.mValue; + return *this; + } + + EnumFlags& operator^=(EnumFlags that) const { + mValue ^= that.mValue; + return *this; + } + + EnumFlags& operator|=(TEnum e) const { + mValue |= 1 << static_cast(e); + return *this; + } + + EnumFlags& operator&=(TEnum e) const { + mValue &= 1 << static_cast(e); + return *this; + } + + EnumFlags& operator^=(TEnum e) const { + mValue ^= 1 << static_cast(e); + return *this; + } + + EnumFlags operator|(EnumFlags that) const { return EnumFlags(mValue | that.mValue); } + EnumFlags operator&(EnumFlags that) const { return EnumFlags(mValue & that.mValue); } + EnumFlags operator^(EnumFlags that) const { return EnumFlags(mValue ^ that.mValue); } + + EnumFlags operator|(TEnum e) const { return EnumFlags(mValue | 1 << static_cast(e)); } + EnumFlags operator&(TEnum e) const { return EnumFlags(mValue & 1 << static_cast(e)); } + EnumFlags operator^(TEnum e) const { return EnumFlags(mValue ^ 1 << static_cast(e)); } + + EnumFlags operator~() const { return EnumFlags(~mValue); } +}; -- cgit v1.2.3-70-g09d2